blob: 41cd9525a68c5f4d819a6e82afe645b0232e6fe9 [file] [log] [blame]
Brian Stanke11f6d532016-07-05 16:17:59 -04001/*
2 * Copyright 2016-present Open Networking Laboratory
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;
18
19import com.google.common.collect.Iterators;
yoonseonc6a69272017-01-12 18:22:20 -080020import org.onosproject.incubator.net.virtual.NetworkId;
Brian Stanke11f6d532016-07-05 16:17:59 -040021import org.onosproject.incubator.net.virtual.VirtualNetworkIntent;
22import org.onosproject.incubator.net.virtual.VirtualNetworkService;
23import org.onosproject.incubator.net.virtual.VirtualNetworkStore;
24import org.onosproject.incubator.net.virtual.VirtualPort;
yoonseon214963b2016-11-21 15:41:07 -080025import org.onosproject.incubator.net.virtual.VnetService;
yoonseonc6a69272017-01-12 18:22:20 -080026import org.onosproject.incubator.net.virtual.event.AbstractVirtualListenerManager;
Brian Stanke11f6d532016-07-05 16:17:59 -040027import org.onosproject.net.ConnectPoint;
28import org.onosproject.net.DeviceId;
29import org.onosproject.net.Port;
30import org.onosproject.net.PortNumber;
31import org.onosproject.net.intent.Intent;
32import org.onosproject.net.intent.IntentData;
33import org.onosproject.net.intent.IntentEvent;
34import org.onosproject.net.intent.IntentListener;
Madan Jampani3b8101a2016-09-15 13:22:01 -070035import org.onosproject.net.intent.WorkPartitionService;
Brian Stanke11f6d532016-07-05 16:17:59 -040036import org.onosproject.net.intent.IntentService;
37import org.onosproject.net.intent.IntentState;
38import org.onosproject.net.intent.Key;
39import org.slf4j.Logger;
40import org.slf4j.LoggerFactory;
41
42import java.util.ArrayList;
43import java.util.List;
44import java.util.Optional;
45
46import static com.google.common.base.Preconditions.*;
47
48/**
Brian Stankefb61df42016-07-25 11:47:51 -040049 * Intent service implementation built on the virtual network service.
Brian Stanke11f6d532016-07-05 16:17:59 -040050 */
yoonseon214963b2016-11-21 15:41:07 -080051public class VirtualNetworkIntentManager
yoonseonc6a69272017-01-12 18:22:20 -080052 extends AbstractVirtualListenerManager<IntentEvent, IntentListener>
Brian Stanke11f6d532016-07-05 16:17:59 -040053 implements IntentService, VnetService {
54
55 private final Logger log = LoggerFactory.getLogger(getClass());
56
Brian Stanke11f6d532016-07-05 16:17:59 -040057 private static final String NETWORK_ID_NULL = "Network ID cannot be null";
58 private static final String DEVICE_NULL = "Device cannot be null";
59 private static final String INTENT_NULL = "Intent cannot be null";
60 private static final String KEY_NULL = "Key cannot be null";
61 private static final String APP_ID_NULL = "Intent app identifier cannot be null";
62 private static final String INTENT_KEY_NULL = "Intent key cannot be null";
63 private static final String CP_NULL = "Connect Point cannot be null";
64
65 protected IntentService intentService;
66 protected VirtualNetworkStore store;
Madan Jampani3b8101a2016-09-15 13:22:01 -070067 protected WorkPartitionService partitionService;
Brian Stanke11f6d532016-07-05 16:17:59 -040068
Brian Stanke11f6d532016-07-05 16:17:59 -040069 /**
70 * Creates a new VirtualNetworkIntentService object.
71 *
72 * @param virtualNetworkManager virtual network manager service
yoonseonc6a69272017-01-12 18:22:20 -080073 * @param networkId a virtual network identifier
Brian Stanke11f6d532016-07-05 16:17:59 -040074 */
yoonseon214963b2016-11-21 15:41:07 -080075 public VirtualNetworkIntentManager(VirtualNetworkService virtualNetworkManager,
yoonseonc6a69272017-01-12 18:22:20 -080076 NetworkId networkId) {
77
Yoonseon Hanb14461b2017-03-07 14:08:01 +090078 super(virtualNetworkManager, networkId, IntentEvent.class);
yoonseonc6a69272017-01-12 18:22:20 -080079
Brian Stanke11f6d532016-07-05 16:17:59 -040080 this.store = serviceDirectory.get(VirtualNetworkStore.class);
81 this.intentService = serviceDirectory.get(IntentService.class);
Madan Jampani3b8101a2016-09-15 13:22:01 -070082 this.partitionService = serviceDirectory.get(WorkPartitionService.class);
Brian Stanke11f6d532016-07-05 16:17:59 -040083 }
84
85 @Override
86 public void submit(Intent intent) {
87 checkNotNull(intent, INTENT_NULL);
88 checkState(intent instanceof VirtualNetworkIntent, "Only VirtualNetworkIntent is supported.");
89 checkArgument(validateIntent((VirtualNetworkIntent) intent), "Invalid Intent");
90
91 intentService.submit(intent);
92 }
93
94 /**
95 * Returns true if the virtual network intent is valid.
96 *
97 * @param intent virtual network intent
98 * @return true if intent is valid
99 */
100 private boolean validateIntent(VirtualNetworkIntent intent) {
101 checkNotNull(intent, INTENT_NULL);
102 checkNotNull(intent.networkId(), NETWORK_ID_NULL);
103 checkNotNull(intent.appId(), APP_ID_NULL);
104 checkNotNull(intent.key(), INTENT_KEY_NULL);
105 ConnectPoint ingressPoint = intent.ingressPoint();
106 ConnectPoint egressPoint = intent.egressPoint();
107
108 return (validateConnectPoint(ingressPoint) && validateConnectPoint(egressPoint));
109 }
110
111 /**
112 * Returns true if the connect point is valid.
113 *
114 * @param connectPoint connect point
115 * @return true if connect point is valid
116 */
117 private boolean validateConnectPoint(ConnectPoint connectPoint) {
118 checkNotNull(connectPoint, CP_NULL);
119 Port port = getPort(connectPoint.deviceId(), connectPoint.port());
120 return port == null ? false : true;
121 }
122
123 /**
124 * Returns the virtual port for the given device identifier and port number.
125 *
126 * @param deviceId virtual device identifier
127 * @param portNumber virtual port number
128 * @return virtual port
129 */
130 private Port getPort(DeviceId deviceId, PortNumber portNumber) {
131 checkNotNull(deviceId, DEVICE_NULL);
132
yoonseonc6a69272017-01-12 18:22:20 -0800133 Optional<VirtualPort> foundPort = manager.getVirtualPorts(this.networkId(), deviceId)
Brian Stanke11f6d532016-07-05 16:17:59 -0400134 .stream()
135 .filter(port -> port.number().equals(portNumber))
136 .findFirst();
137 if (foundPort.isPresent()) {
138 return foundPort.get();
139 }
140 return null;
141 }
142
143 @Override
144 public void withdraw(Intent intent) {
145 checkNotNull(intent, INTENT_NULL);
146 // Withdraws the physical intents created due to the virtual intents.
Brian Stankefb61df42016-07-25 11:47:51 -0400147 store.getTunnelIds(intent).forEach(tunnelId -> {
148 Key intentKey = Key.of(tunnelId.id(), intent.appId());
149 Intent physicalIntent = intentService.getIntent(intentKey);
150 checkNotNull(physicalIntent, INTENT_NULL);
Brian Stanke11f6d532016-07-05 16:17:59 -0400151
Brian Stankefb61df42016-07-25 11:47:51 -0400152 // Withdraw the physical intent(s)
153 log.debug("Withdrawing pt-pt intent: " + physicalIntent);
154 intentService.withdraw(physicalIntent);
155 });
Brian Stanke11f6d532016-07-05 16:17:59 -0400156 // Now withdraw the virtual intent
Brian Stankefb61df42016-07-25 11:47:51 -0400157 log.debug("Withdrawing virtual intent: " + intent);
Brian Stanke11f6d532016-07-05 16:17:59 -0400158 intentService.withdraw(intent);
159 }
160
161 @Override
162 public void purge(Intent intent) {
163 checkNotNull(intent, INTENT_NULL);
164 // Purges the physical intents created for each tunnelId.
165 store.getTunnelIds(intent)
166 .forEach(tunnelId -> {
167 Key intentKey = Key.of(tunnelId.id(), intent.appId());
168 Intent physicalIntent = intentService.getIntent(intentKey);
169 checkNotNull(physicalIntent, INTENT_NULL);
170
171 // Purge the physical intent(s)
172 intentService.purge(physicalIntent);
173 store.removeTunnelId(intent, tunnelId);
174 });
175 // Now purge the virtual intent
176 intentService.purge(intent);
177 }
178
179 @Override
180 public Intent getIntent(Key key) {
181 checkNotNull(key, KEY_NULL);
182 return store.getIntent(key);
183 }
184
185 @Override
186 public Iterable<Intent> getIntents() {
187 return store.getIntents();
188 }
189
190 @Override
Brian O'Connor38224302016-08-02 22:03:01 -0700191 public void addPending(IntentData intentData) {
192 intentService.addPending(intentData);
193 }
194
195 @Override
Brian Stanke11f6d532016-07-05 16:17:59 -0400196 public Iterable<IntentData> getIntentData() {
197 return store.getIntentData();
198 }
199
200 @Override
201 public long getIntentCount() {
202 return Iterators.size(getIntents().iterator());
203 }
204
205 @Override
206 public IntentState getIntentState(Key intentKey) {
207 checkNotNull(intentKey, KEY_NULL);
Yuta HIGUCHIe2eeae82016-07-28 13:56:31 -0700208 return Optional.ofNullable(store.getIntentData(intentKey))
209 .map(IntentData::state)
210 .orElse(null);
Brian Stanke11f6d532016-07-05 16:17:59 -0400211 }
212
213 @Override
214 public List<Intent> getInstallableIntents(Key intentKey) {
215 List<Intent> intents = new ArrayList<>();
216 getIntentData().forEach(intentData -> {
217 if (intentData.intent().key().equals(intentKey)) {
218 intents.addAll(intentData.installables());
219 }
220 });
221 return intents;
222 }
223
224 @Override
225 public boolean isLocal(Key intentKey) {
226 checkNotNull(intentKey, INTENT_KEY_NULL);
227 Intent intent = getIntent(intentKey);
228 checkNotNull(intent, INTENT_NULL);
Madan Jampani3b8101a2016-09-15 13:22:01 -0700229 return partitionService.isMine(intentKey, Key::hash);
Brian Stanke11f6d532016-07-05 16:17:59 -0400230 }
231
232 @Override
233 public Iterable<Intent> getPending() {
234 return null;
235 }
Brian Stanke11f6d532016-07-05 16:17:59 -0400236}