Ray Milkey | 8c6d00e | 2015-03-13 14:14:34 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Ray Milkey | 8c6d00e | 2015-03-13 14:14:34 -0700 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package org.onosproject.net.intent.impl.phase; |
| 17 | |
| 18 | import org.onosproject.net.intent.IntentData; |
| 19 | import org.onosproject.net.intent.IntentState; |
| 20 | import org.slf4j.Logger; |
| 21 | |
Brian O'Connor | a27ada7 | 2015-04-29 18:25:12 -0700 | [diff] [blame] | 22 | import java.util.Optional; |
| 23 | |
Ray Milkey | 8c6d00e | 2015-03-13 14:14:34 -0700 | [diff] [blame] | 24 | import static com.google.common.base.Preconditions.checkNotNull; |
| 25 | import static org.slf4j.LoggerFactory.getLogger; |
| 26 | |
| 27 | /** |
| 28 | * Represents a phase of requesting a purge of an intent. |
| 29 | * <p> |
| 30 | * Note: The purge will only succeed if the intent is FAILED or WITHDRAWN. |
| 31 | * </p> |
| 32 | */ |
| 33 | final class PurgeRequest extends FinalIntentProcessPhase { |
| 34 | |
| 35 | private static final Logger log = getLogger(PurgeRequest.class); |
| 36 | |
Brian O'Connor | a27ada7 | 2015-04-29 18:25:12 -0700 | [diff] [blame] | 37 | private final IntentData data; |
| 38 | private final Optional<IntentData> stored; |
Ray Milkey | 8c6d00e | 2015-03-13 14:14:34 -0700 | [diff] [blame] | 39 | |
Brian O'Connor | a27ada7 | 2015-04-29 18:25:12 -0700 | [diff] [blame] | 40 | PurgeRequest(IntentData intentData, Optional<IntentData> stored) { |
| 41 | this.data = checkNotNull(intentData); |
| 42 | this.stored = checkNotNull(stored); |
Ray Milkey | 8c6d00e | 2015-03-13 14:14:34 -0700 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | private boolean shouldAcceptPurge() { |
Brian O'Connor | a27ada7 | 2015-04-29 18:25:12 -0700 | [diff] [blame] | 46 | if (!stored.isPresent()) { |
| 47 | log.info("Purge for intent {}, but intent is not present", |
| 48 | data.key()); |
Ray Milkey | 8c6d00e | 2015-03-13 14:14:34 -0700 | [diff] [blame] | 49 | return true; |
| 50 | } |
Brian O'Connor | a27ada7 | 2015-04-29 18:25:12 -0700 | [diff] [blame] | 51 | |
| 52 | IntentData storedData = stored.get(); |
| 53 | if (storedData.state() == IntentState.WITHDRAWN |
| 54 | || storedData.state() == IntentState.FAILED) { |
| 55 | return true; |
| 56 | } |
| 57 | log.info("Purge for intent {} is rejected because intent state is {}", |
| 58 | data.key(), storedData.state()); |
Ray Milkey | 8c6d00e | 2015-03-13 14:14:34 -0700 | [diff] [blame] | 59 | return false; |
| 60 | } |
| 61 | |
| 62 | @Override |
| 63 | public IntentData data() { |
| 64 | if (shouldAcceptPurge()) { |
Brian O'Connor | a27ada7 | 2015-04-29 18:25:12 -0700 | [diff] [blame] | 65 | return data; |
Ray Milkey | 8c6d00e | 2015-03-13 14:14:34 -0700 | [diff] [blame] | 66 | } else { |
Brian O'Connor | a27ada7 | 2015-04-29 18:25:12 -0700 | [diff] [blame] | 67 | return stored.get(); |
Ray Milkey | 8c6d00e | 2015-03-13 14:14:34 -0700 | [diff] [blame] | 68 | } |
| 69 | } |
| 70 | } |