blob: ce8dfc9325429425c877728414b77f6868a04163 [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.incubator.net.virtual.NetworkId;
20import org.onosproject.incubator.net.virtual.impl.intent.VirtualIntentProcessor;
21import org.onosproject.net.intent.IntentData;
22import org.slf4j.Logger;
23import org.slf4j.LoggerFactory;
24
25import java.util.Optional;
26
27import static com.google.common.base.Preconditions.checkNotNull;
28import static org.onosproject.incubator.net.virtual.impl.intent.phase.VirtualIntentProcessPhase.transferErrorCount;
29
30/**
31 * Represents a phase of requesting a withdraw of an intent for a virtual network.
32 */
33final class VirtualIntentWithdrawRequest implements VirtualIntentProcessPhase {
34 private static final Logger log = LoggerFactory.getLogger(VirtualIntentWithdrawRequest.class);
35
36 private final NetworkId networkId;
37 private final VirtualIntentProcessor processor;
38 private final IntentData data;
39 private final Optional<IntentData> stored;
40
41 /**
42 * Creates a withdraw request phase.
43 *
44 * @param networkId virtual network identifier
45 * @param processor intent processor to be passed to intent process phases
46 * generated after this phase
47 * @param intentData intent data to be processed
48 * @param stored intent data stored in the store
49 */
50 VirtualIntentWithdrawRequest(NetworkId networkId, VirtualIntentProcessor processor,
51 IntentData intentData, Optional<IntentData> stored) {
52 this.networkId = checkNotNull(networkId);
53 this.processor = checkNotNull(processor);
54 this.data = checkNotNull(intentData);
55 this.stored = checkNotNull(stored);
56 }
57
58 @Override
59 public Optional<VirtualIntentProcessPhase> execute() {
60 //TODO perhaps we want to validate that the pending and current are the
61 // same version i.e. they are the same
62 // Note: this call is not just the symmetric version of submit
63
64 transferErrorCount(data, stored);
65
66 if (!stored.isPresent() || stored.get().installables().isEmpty()) {
67 switch (data.request()) {
68 case INSTALL_REQ:
69 // illegal state?
70 log.warn("{} was requested to withdraw during installation?", data.intent());
71 return Optional.of(new VirtualIntentFailed(data));
72 case WITHDRAW_REQ:
73 default: //TODO "default" case should not happen
74 return Optional.of(new VirtualIntentWithdrawn(data));
75 }
76 }
77
78 return Optional.of(new VirtualIntentWithdrawing(networkId, processor,
79 new IntentData(data, stored.get().installables())));
80 }
81}