Committed by
Gerrit Code Review
NPE protection for purge requests (ONOS-1463)
Change-Id: I02732ed83aaaf5c760fdb96899696177415ebd23
Showing
2 changed files
with
21 additions
and
11 deletions
| ... | @@ -50,7 +50,7 @@ public interface IntentProcessPhase { | ... | @@ -50,7 +50,7 @@ public interface IntentProcessPhase { |
| 50 | case WITHDRAW_REQ: | 50 | case WITHDRAW_REQ: |
| 51 | return new WithdrawRequest(processor, data, Optional.ofNullable(current)); | 51 | return new WithdrawRequest(processor, data, Optional.ofNullable(current)); |
| 52 | case PURGE_REQ: | 52 | case PURGE_REQ: |
| 53 | - return new PurgeRequest(data, current); | 53 | + return new PurgeRequest(data, Optional.ofNullable(current)); |
| 54 | default: | 54 | default: |
| 55 | // illegal state | 55 | // illegal state |
| 56 | return new Failed(data); | 56 | return new Failed(data); | ... | ... |
| ... | @@ -19,6 +19,8 @@ import org.onosproject.net.intent.IntentData; | ... | @@ -19,6 +19,8 @@ import org.onosproject.net.intent.IntentData; |
| 19 | import org.onosproject.net.intent.IntentState; | 19 | import org.onosproject.net.intent.IntentState; |
| 20 | import org.slf4j.Logger; | 20 | import org.slf4j.Logger; |
| 21 | 21 | ||
| 22 | +import java.util.Optional; | ||
| 23 | + | ||
| 22 | import static com.google.common.base.Preconditions.checkNotNull; | 24 | import static com.google.common.base.Preconditions.checkNotNull; |
| 23 | import static org.slf4j.LoggerFactory.getLogger; | 25 | import static org.slf4j.LoggerFactory.getLogger; |
| 24 | 26 | ||
| ... | @@ -32,29 +34,37 @@ final class PurgeRequest extends FinalIntentProcessPhase { | ... | @@ -32,29 +34,37 @@ final class PurgeRequest extends FinalIntentProcessPhase { |
| 32 | 34 | ||
| 33 | private static final Logger log = getLogger(PurgeRequest.class); | 35 | private static final Logger log = getLogger(PurgeRequest.class); |
| 34 | 36 | ||
| 35 | - private final IntentData pending; | 37 | + private final IntentData data; |
| 36 | - private final IntentData current; | 38 | + private final Optional<IntentData> stored; |
| 37 | 39 | ||
| 38 | - PurgeRequest(IntentData intentData, IntentData current) { | 40 | + PurgeRequest(IntentData intentData, Optional<IntentData> stored) { |
| 39 | - this.pending = checkNotNull(intentData); | 41 | + this.data = checkNotNull(intentData); |
| 40 | - this.current = checkNotNull(current); | 42 | + this.stored = checkNotNull(stored); |
| 41 | } | 43 | } |
| 42 | 44 | ||
| 43 | private boolean shouldAcceptPurge() { | 45 | private boolean shouldAcceptPurge() { |
| 44 | - if (current.state() == IntentState.WITHDRAWN | 46 | + if (!stored.isPresent()) { |
| 45 | - || current.state() == IntentState.FAILED) { | 47 | + log.info("Purge for intent {}, but intent is not present", |
| 48 | + data.key()); | ||
| 49 | + return true; | ||
| 50 | + } | ||
| 51 | + | ||
| 52 | + IntentData storedData = stored.get(); | ||
| 53 | + if (storedData.state() == IntentState.WITHDRAWN | ||
| 54 | + || storedData.state() == IntentState.FAILED) { | ||
| 46 | return true; | 55 | return true; |
| 47 | } | 56 | } |
| 48 | - log.info("Purge for intent {} is rejected", pending.key()); | 57 | + log.info("Purge for intent {} is rejected because intent state is {}", |
| 58 | + data.key(), storedData.state()); | ||
| 49 | return false; | 59 | return false; |
| 50 | } | 60 | } |
| 51 | 61 | ||
| 52 | @Override | 62 | @Override |
| 53 | public IntentData data() { | 63 | public IntentData data() { |
| 54 | if (shouldAcceptPurge()) { | 64 | if (shouldAcceptPurge()) { |
| 55 | - return pending; | 65 | + return data; |
| 56 | } else { | 66 | } else { |
| 57 | - return current; | 67 | + return stored.get(); |
| 58 | } | 68 | } |
| 59 | } | 69 | } |
| 60 | } | 70 | } | ... | ... |
-
Please register or login to post a comment