blob: a6f29eaf83e049716ae550d0db3d322c153fc001 [file] [log] [blame]
Yoonseon Han096cea02017-05-15 15:10:41 -07001/*
2 * Copyright 2017-present Open Networking Foundation
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
17package org.onosproject.incubator.net.virtual.impl.intent.phase;
18
19import org.onosproject.net.intent.IntentData;
20import org.onosproject.net.intent.IntentState;
21import org.slf4j.Logger;
22
23import java.util.Optional;
24
25import static com.google.common.base.Preconditions.checkNotNull;
26import static org.slf4j.LoggerFactory.getLogger;
27
28/**
29 * Represents a phase of requesting a purge of an intent for a virtual network.
30 * Note: The purge will only succeed if the intent is FAILED or WITHDRAWN.
31 */
32final class VirtualIntentPurgeRequest extends VirtualFinalIntentProcessPhase {
33 private static final Logger log = getLogger(VirtualIntentPurgeRequest.class);
34
35 private final IntentData data;
36 protected final Optional<IntentData> stored;
37
38 VirtualIntentPurgeRequest(IntentData intentData, Optional<IntentData> stored) {
39 this.data = checkNotNull(intentData);
40 this.stored = checkNotNull(stored);
41 }
42
43 private boolean shouldAcceptPurge() {
44 if (!stored.isPresent()) {
45 log.info("Purge for intent {}, but intent is not present",
46 data.key());
47 return true;
48 }
49
50 IntentData storedData = stored.get();
51 if (storedData.state() == IntentState.WITHDRAWN
52 || storedData.state() == IntentState.FAILED) {
53 return true;
54 }
55 log.info("Purge for intent {} is rejected because intent state is {}",
56 data.key(), storedData.state());
57 return false;
58 }
59
60 @Override
61 public IntentData data() {
62 if (shouldAcceptPurge()) {
63 return data;
64 } else {
65 return stored.get();
66 }
67 }
68}