blob: e557d28568bd5788e4420358b762f109c52bd426 [file] [log] [blame]
Ray Milkey8c6d00e2015-03-13 14:14:34 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Ray Milkey8c6d00e2015-03-13 14:14:34 -07003 *
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 */
16package org.onosproject.net.intent.impl.phase;
17
18import org.onosproject.net.intent.IntentData;
19import org.onosproject.net.intent.IntentState;
20import org.slf4j.Logger;
21
Brian O'Connora27ada72015-04-29 18:25:12 -070022import java.util.Optional;
23
Ray Milkey8c6d00e2015-03-13 14:14:34 -070024import static com.google.common.base.Preconditions.checkNotNull;
25import 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 */
33final class PurgeRequest extends FinalIntentProcessPhase {
34
35 private static final Logger log = getLogger(PurgeRequest.class);
36
Brian O'Connora27ada72015-04-29 18:25:12 -070037 private final IntentData data;
38 private final Optional<IntentData> stored;
Ray Milkey8c6d00e2015-03-13 14:14:34 -070039
Brian O'Connora27ada72015-04-29 18:25:12 -070040 PurgeRequest(IntentData intentData, Optional<IntentData> stored) {
41 this.data = checkNotNull(intentData);
42 this.stored = checkNotNull(stored);
Ray Milkey8c6d00e2015-03-13 14:14:34 -070043 }
44
45 private boolean shouldAcceptPurge() {
Brian O'Connora27ada72015-04-29 18:25:12 -070046 if (!stored.isPresent()) {
47 log.info("Purge for intent {}, but intent is not present",
48 data.key());
Ray Milkey8c6d00e2015-03-13 14:14:34 -070049 return true;
50 }
Brian O'Connora27ada72015-04-29 18:25:12 -070051
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 Milkey8c6d00e2015-03-13 14:14:34 -070059 return false;
60 }
61
62 @Override
63 public IntentData data() {
64 if (shouldAcceptPurge()) {
Brian O'Connora27ada72015-04-29 18:25:12 -070065 return data;
Ray Milkey8c6d00e2015-03-13 14:14:34 -070066 } else {
Brian O'Connora27ada72015-04-29 18:25:12 -070067 return stored.get();
Ray Milkey8c6d00e2015-03-13 14:14:34 -070068 }
69 }
70}