blob: 75f962e4ca283187f5364340c1aa61268749a10d [file] [log] [blame]
Naoki Shiota5a056062016-05-05 18:43:59 -07001/*
Brian O'Connor0a4e6742016-09-15 23:03:10 -07002 * Copyright 2016-present Open Networking Laboratory
Naoki Shiota5a056062016-05-05 18:43:59 -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.newoptical;
17
18import com.google.common.annotations.Beta;
19import com.google.common.collect.ImmutableList;
Naoki Shiota5a056062016-05-05 18:43:59 -070020
21import org.apache.commons.lang3.tuple.Pair;
22import org.apache.felix.scr.annotations.Activate;
23import org.apache.felix.scr.annotations.Component;
24import org.apache.felix.scr.annotations.Deactivate;
25import org.apache.felix.scr.annotations.Reference;
26import org.apache.felix.scr.annotations.ReferenceCardinality;
27import org.apache.felix.scr.annotations.Service;
28import org.onlab.util.Bandwidth;
Naoki Shiota7c3111b2016-06-09 16:12:11 -070029import org.onlab.util.KryoNamespace;
Naoki Shiota5a056062016-05-05 18:43:59 -070030import org.onosproject.cluster.ClusterService;
Naoki Shiota5a056062016-05-05 18:43:59 -070031import org.onosproject.core.ApplicationId;
32import org.onosproject.core.CoreService;
Naoki Shiota0d2943e2016-05-13 18:53:21 -070033import org.onosproject.event.ListenerTracker;
34import org.onosproject.net.optical.OchPort;
35import org.onosproject.net.optical.OduCltPort;
Naoki Shiota5a056062016-05-05 18:43:59 -070036import org.onosproject.newoptical.api.OpticalConnectivityId;
37import org.onosproject.newoptical.api.OpticalPathEvent;
38import org.onosproject.newoptical.api.OpticalPathListener;
39import org.onosproject.newoptical.api.OpticalPathService;
40import org.onosproject.event.AbstractListenerManager;
41import org.onosproject.mastership.MastershipService;
Naoki Shiota5a056062016-05-05 18:43:59 -070042import org.onosproject.net.ConnectPoint;
43import org.onosproject.net.Device;
44import org.onosproject.net.DeviceId;
45import org.onosproject.net.Link;
Naoki Shiota5a056062016-05-05 18:43:59 -070046import org.onosproject.net.Path;
47import org.onosproject.net.Port;
48import org.onosproject.net.config.NetworkConfigService;
49import org.onosproject.net.device.DeviceService;
50import org.onosproject.net.intent.Intent;
51import org.onosproject.net.intent.IntentEvent;
Naoki Shiota5a056062016-05-05 18:43:59 -070052import org.onosproject.net.intent.IntentListener;
53import org.onosproject.net.intent.IntentService;
Naoki Shiota5a056062016-05-05 18:43:59 -070054import org.onosproject.net.intent.OpticalCircuitIntent;
55import org.onosproject.net.intent.OpticalConnectivityIntent;
Naoki Shiota5a056062016-05-05 18:43:59 -070056import org.onosproject.net.link.LinkEvent;
57import org.onosproject.net.link.LinkListener;
58import org.onosproject.net.link.LinkService;
Sho SHIMIZUb6c63a32016-05-26 12:07:19 -070059import org.onosproject.net.config.basics.BandwidthCapacity;
Naoki Shiota5a056062016-05-05 18:43:59 -070060import org.onosproject.net.resource.ContinuousResource;
61import org.onosproject.net.resource.Resource;
62import org.onosproject.net.resource.ResourceAllocation;
63import org.onosproject.net.resource.ResourceService;
64import org.onosproject.net.resource.Resources;
65import org.onosproject.net.topology.LinkWeight;
66import org.onosproject.net.topology.PathService;
67import org.onosproject.net.topology.TopologyEdge;
Naoki Shiota7c3111b2016-06-09 16:12:11 -070068import org.onosproject.store.serializers.KryoNamespaces;
Naoki Shiota5a056062016-05-05 18:43:59 -070069import org.onosproject.store.service.AtomicCounter;
Naoki Shiota7c3111b2016-06-09 16:12:11 -070070import org.onosproject.store.service.ConsistentMap;
71import org.onosproject.store.service.DistributedSet;
72import org.onosproject.store.service.MapEvent;
73import org.onosproject.store.service.MapEventListener;
74import org.onosproject.store.service.Serializer;
Naoki Shiota5a056062016-05-05 18:43:59 -070075import org.onosproject.store.service.StorageService;
Naoki Shiota7c3111b2016-06-09 16:12:11 -070076import org.onosproject.store.service.Versioned;
Naoki Shiota5a056062016-05-05 18:43:59 -070077import org.slf4j.Logger;
78import org.slf4j.LoggerFactory;
79
80import java.time.Duration;
Naoki Shiota7c3111b2016-06-09 16:12:11 -070081import java.util.ArrayList;
Naoki Shiota5a056062016-05-05 18:43:59 -070082import java.util.Collections;
Naoki Shiota7c3111b2016-06-09 16:12:11 -070083import java.util.HashMap;
84import java.util.HashSet;
Naoki Shiota5a056062016-05-05 18:43:59 -070085import java.util.Iterator;
86import java.util.LinkedList;
87import java.util.List;
88import java.util.Map;
89import java.util.Optional;
90import java.util.Set;
Naoki Shiota5a056062016-05-05 18:43:59 -070091import java.util.stream.Collectors;
92import java.util.stream.Stream;
93
94import static com.google.common.base.Preconditions.checkArgument;
95import static com.google.common.base.Preconditions.checkNotNull;
Naoki Shiotae1366ad2016-05-11 19:24:11 -070096import static org.onosproject.net.optical.device.OpticalDeviceServiceView.opticalView;
Naoki Shiota5a056062016-05-05 18:43:59 -070097
98/**
99 * Main component to configure optical connectivity.
100 */
101@Beta
102@Service
103@Component(immediate = true)
104public class OpticalPathProvisioner
105 extends AbstractListenerManager<OpticalPathEvent, OpticalPathListener>
106 implements OpticalPathService {
107 protected static final Logger log = LoggerFactory.getLogger(OpticalPathProvisioner.class);
108
109 private static final String OPTICAL_CONNECTIVITY_ID_COUNTER = "optical-connectivity-id";
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700110 private static final String LINKPATH_MAP_NAME = "newoptical-linkpath";
111 private static final String CONNECTIVITY_MAP_NAME = "newoptical-connectivity";
112 private static final String CROSSCONNECTLINK_SET_NAME = "newoptical-crossconnectlink";
Naoki Shiota5a056062016-05-05 18:43:59 -0700113
114 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
115 protected IntentService intentService;
116
117 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
118 protected PathService pathService;
119
120 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
121 protected CoreService coreService;
122
123 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
124 protected LinkService linkService;
125
126 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
127 protected MastershipService mastershipService;
128
129 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
130 protected ClusterService clusterService;
131
132 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
133 protected DeviceService deviceService;
134
135 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
136 protected StorageService storageService;
137
138 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
139 protected NetworkConfigService networkConfigService;
140
141 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
142 protected ResourceService resourceService;
143
144
145 private ApplicationId appId;
146
147 private AtomicCounter idCounter;
148
Naoki Shiota0d2943e2016-05-13 18:53:21 -0700149 private ListenerTracker listeners;
Naoki Shiota5a056062016-05-05 18:43:59 -0700150
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700151 private InternalStoreListener storeListener = new InternalStoreListener();
Naoki Shiota5a056062016-05-05 18:43:59 -0700152
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700153 private ConsistentMap<PacketLinkRealizedByOptical, OpticalConnectivity> linkPathMap;
Naoki Shiota5a056062016-05-05 18:43:59 -0700154
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700155 private ConsistentMap<OpticalConnectivityId, OpticalConnectivity> connectivityMap;
156
Naoki Shiota5a056062016-05-05 18:43:59 -0700157 // Map of cross connect link and installed path which uses the link
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700158 private DistributedSet<Link> usedCrossConnectLinkSet;
159
160 private static final KryoNamespace.Builder LINKPATH_SERIALIZER = KryoNamespace.newBuilder()
161 .register(KryoNamespaces.API)
162 .register(PacketLinkRealizedByOptical.class)
163 .register(OpticalConnectivityId.class)
164 .register(OpticalConnectivity.class);
165
166 private static final KryoNamespace.Builder CONNECTIVITY_SERIALIZER = KryoNamespace.newBuilder()
167 .register(KryoNamespaces.API)
Yuta HIGUCHI0086cf82016-07-18 22:49:45 -0700168 .register(PacketLinkRealizedByOptical.class)
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700169 .register(OpticalConnectivityId.class)
170 .register(OpticalConnectivity.class);
171
172 private static final KryoNamespace.Builder CROSSCONNECTLINKS_SERIALIZER = KryoNamespace.newBuilder()
173 .register(KryoNamespaces.API);
Naoki Shiota5a056062016-05-05 18:43:59 -0700174
175 @Activate
176 protected void activate() {
Naoki Shiotae1366ad2016-05-11 19:24:11 -0700177 deviceService = opticalView(deviceService);
Naoki Shiota5a056062016-05-05 18:43:59 -0700178 appId = coreService.registerApplication("org.onosproject.newoptical");
179
Naoki Shiota03c29e12016-05-16 16:58:07 -0700180 idCounter = storageService.getAtomicCounter(OPTICAL_CONNECTIVITY_ID_COUNTER);
Naoki Shiota5a056062016-05-05 18:43:59 -0700181
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700182 linkPathMap = storageService.<PacketLinkRealizedByOptical, OpticalConnectivity>consistentMapBuilder()
183 .withSerializer(Serializer.using(LINKPATH_SERIALIZER.build()))
184 .withName(LINKPATH_MAP_NAME)
185 .withApplicationId(appId)
186 .build();
187
188 connectivityMap = storageService.<OpticalConnectivityId, OpticalConnectivity>consistentMapBuilder()
189 .withSerializer(Serializer.using(CONNECTIVITY_SERIALIZER.build()))
190 .withName(CONNECTIVITY_MAP_NAME)
191 .withApplicationId(appId)
192 .build();
193
194 usedCrossConnectLinkSet = storageService.<Link>setBuilder()
195 .withSerializer(Serializer.using(CROSSCONNECTLINKS_SERIALIZER.build()))
196 .withName(CROSSCONNECTLINK_SET_NAME)
197 .withApplicationId(appId)
198 .build()
199 .asDistributedSet();
200
Naoki Shiota5a056062016-05-05 18:43:59 -0700201 eventDispatcher.addSink(OpticalPathEvent.class, listenerRegistry);
Naoki Shiota0d2943e2016-05-13 18:53:21 -0700202
203 listeners = new ListenerTracker();
204 listeners.addListener(linkService, new InternalLinkListener())
205 .addListener(intentService, new InternalIntentListener());
Naoki Shiota5a056062016-05-05 18:43:59 -0700206
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700207 linkPathMap.addListener(storeListener);
208
Naoki Shiota5a056062016-05-05 18:43:59 -0700209 log.info("Started");
210 }
211
212 @Deactivate
213 protected void deactivate() {
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700214 linkPathMap.removeListener(storeListener);
Naoki Shiota0d2943e2016-05-13 18:53:21 -0700215 listeners.removeListeners();
216 eventDispatcher.removeSink(OpticalPathEvent.class);
Naoki Shiota5a056062016-05-05 18:43:59 -0700217
218 log.info("Stopped");
219 }
220
221 @Override
222 public OpticalConnectivityId setupConnectivity(ConnectPoint ingress, ConnectPoint egress,
223 Bandwidth bandwidth, Duration latency) {
224 checkNotNull(ingress);
225 checkNotNull(egress);
226 log.info("setupConnectivity({}, {}, {}, {})", ingress, egress, bandwidth, latency);
227
228 bandwidth = (bandwidth == null) ? Bandwidth.bps(0) : bandwidth;
229
230 Set<Path> paths = pathService.getPaths(ingress.deviceId(), egress.deviceId(),
231 new BandwidthLinkWeight(bandwidth));
232 if (paths.isEmpty()) {
233 log.warn("Unable to find multi-layer path.");
234 return null;
235 }
236
237 // Search path with available cross connect points
238 for (Path path : paths) {
239 OpticalConnectivityId id = setupPath(path, bandwidth, latency);
240 if (id != null) {
241 log.info("Assigned OpticalConnectivityId: {}", id);
242 return id;
243 }
244 }
245
Naoki Shiota0d2943e2016-05-13 18:53:21 -0700246 log.info("setupConnectivity({}, {}, {}, {}) failed.", ingress, egress, bandwidth, latency);
247
Naoki Shiota5a056062016-05-05 18:43:59 -0700248 return null;
249 }
250
251 @Override
252 public OpticalConnectivityId setupPath(Path path, Bandwidth bandwidth, Duration latency) {
253 checkNotNull(path);
254 log.info("setupPath({}, {}, {})", path, bandwidth, latency);
255
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700256 // map of cross connect points (optical port -> packet port)
257 Map<ConnectPoint, ConnectPoint> crossConnectPointMap = new HashMap<>();
258
259 // list of (src, dst) pair of optical ports between which optical path should be installed
260 List<Pair<ConnectPoint, ConnectPoint>> crossConnectPoints = new ArrayList<>();
261
262 // Scan path to find pairs of connect points between which optical intent is installed
263 // opticalSrcPort works as a flag parameter to show scanning status
264 ConnectPoint opticalSrcPort = null;
265 for (Link link : path.links()) {
266 if (!isCrossConnectLink(link)) {
267 continue;
268 }
269
270 if (opticalSrcPort != null) {
271 // opticalSrcPort!=null means src port was already found
272 // in this case link.src() is optical layer, and link.dst() is packet layer
273
274 // Check if types of src port and dst port matches
275 Device srcDevice = checkNotNull(deviceService.getDevice(opticalSrcPort.deviceId()),
276 "Unknown device ID");
277 Device dstDevice = checkNotNull(deviceService.getDevice(link.src().deviceId()),
278 "Unknown device ID");
279 if (srcDevice.type() != dstDevice.type()) {
280 log.error("Unsupported mix of cross connect points : {}, {}",
281 srcDevice.type(), dstDevice.type());
282 return null;
283 }
284
285 // Update cross connect points map
286 crossConnectPointMap.put(link.src(), link.dst());
287
288 // Add optical ports pair to list
289 crossConnectPoints.add(Pair.of(opticalSrcPort, link.src()));
290
291 // Reset flag parameter
292 opticalSrcPort = null;
293 } else {
294 // opticalSrcPort==null means src port was not found yet
295 // in this case link.src() is packet layer, and link.dst() is optical layer
296
297 // Update cross connect points map
298 crossConnectPointMap.put(link.dst(), link.src());
299 // Set opticalSrcPort to src of link (optical port)
300 opticalSrcPort = link.dst();
301 }
Naoki Shiota5a056062016-05-05 18:43:59 -0700302 }
303
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700304 // create intents from cross connect points
305 List<Intent> intents = createIntents(crossConnectPoints);
Naoki Shiota5a056062016-05-05 18:43:59 -0700306
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700307 // create set of PacketLinkRealizedByOptical
308 Set<PacketLinkRealizedByOptical> packetLinks = createPacketLinkSet(crossConnectPoints,
309 intents, crossConnectPointMap);
310
311 // create OpticalConnectivity object and store information to distributed store
312 OpticalConnectivity connectivity = createConnectivity(path, bandwidth, latency, packetLinks);
Naoki Shiota5a056062016-05-05 18:43:59 -0700313
314 // store cross connect port usage
315 path.links().stream().filter(this::isCrossConnectLink)
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700316 .forEach(usedCrossConnectLinkSet::add);
Naoki Shiota5a056062016-05-05 18:43:59 -0700317
318 // Submit the intents
319 for (Intent i : intents) {
320 intentService.submit(i);
321 log.debug("Submitted an intent: {}", i);
322 }
323
324 return connectivity.id();
325 }
326
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700327 private OpticalConnectivity createConnectivity(Path path, Bandwidth bandwidth, Duration latency,
328 Set<PacketLinkRealizedByOptical> links) {
Naoki Shiota5a056062016-05-05 18:43:59 -0700329 OpticalConnectivityId id = OpticalConnectivityId.of(idCounter.getAndIncrement());
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700330 OpticalConnectivity connectivity = new OpticalConnectivity(id, path.links(), bandwidth, latency,
331 links, Collections.emptySet());
332
333 links.forEach(l -> linkPathMap.put(l, connectivity));
Naoki Shiota5a056062016-05-05 18:43:59 -0700334
Naoki Shiota5a056062016-05-05 18:43:59 -0700335 // store connectivity information
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700336 connectivityMap.put(connectivity.id(), connectivity);
Naoki Shiota5a056062016-05-05 18:43:59 -0700337
338 return connectivity;
339 }
340
341 @Override
342 public boolean removeConnectivity(OpticalConnectivityId id) {
343 log.info("removeConnectivity({})", id);
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700344 Versioned<OpticalConnectivity> connectivity = connectivityMap.remove(id);
Naoki Shiota5a056062016-05-05 18:43:59 -0700345
346 if (connectivity == null) {
Naoki Shiota0d2943e2016-05-13 18:53:21 -0700347 log.info("OpticalConnectivity with id {} not found.", id);
Naoki Shiota5a056062016-05-05 18:43:59 -0700348 return false;
349 }
350
351 // TODO withdraw intent only if all of connectivities that use the optical path are withdrawn
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700352 connectivity.value().getRealizingLinks().forEach(l -> {
Naoki Shiota5a056062016-05-05 18:43:59 -0700353 Intent intent = intentService.getIntent(l.realizingIntentKey());
354 intentService.withdraw(intent);
355 });
356
357 return true;
358 }
359
360 @Override
Naoki Shiota0d2943e2016-05-13 18:53:21 -0700361 public Optional<List<Link>> getPath(OpticalConnectivityId id) {
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700362 Versioned<OpticalConnectivity> connectivity = connectivityMap.get(id);
Naoki Shiota5a056062016-05-05 18:43:59 -0700363 if (connectivity == null) {
Naoki Shiota0d2943e2016-05-13 18:53:21 -0700364 log.info("OpticalConnectivity with id {} not found.", id);
365 return Optional.empty();
Naoki Shiota5a056062016-05-05 18:43:59 -0700366 }
367
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700368 return Optional.of(ImmutableList.copyOf(connectivity.value().links()));
Naoki Shiota5a056062016-05-05 18:43:59 -0700369 }
370
371 /**
372 * Scans the list of cross connection points and returns a list of optical connectivity intents.
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700373 * During the process, save information about packet links to given set.
Naoki Shiota5a056062016-05-05 18:43:59 -0700374 *
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700375 * @param crossConnectPoints list of (src, dst) pair between which optical path will be set up
Naoki Shiota5a056062016-05-05 18:43:59 -0700376 * @return list of optical connectivity intents
377 */
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700378 private List<Intent> createIntents(List<Pair<ConnectPoint, ConnectPoint>> crossConnectPoints) {
Naoki Shiota5a056062016-05-05 18:43:59 -0700379 List<Intent> intents = new LinkedList<>();
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700380 Iterator<Pair<ConnectPoint, ConnectPoint>> itr = crossConnectPoints.iterator();
Naoki Shiota5a056062016-05-05 18:43:59 -0700381
382 while (itr.hasNext()) {
383 // checkArgument at start ensures we'll always have pairs of connect points
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700384 Pair<ConnectPoint, ConnectPoint> next = itr.next();
385 ConnectPoint src = next.getLeft();
386 ConnectPoint dst = next.getRight();
Naoki Shiota5a056062016-05-05 18:43:59 -0700387
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700388 Port srcPort = deviceService.getPort(src.deviceId(), src.port());
389 Port dstPort = deviceService.getPort(dst.deviceId(), dst.port());
Naoki Shiota5a056062016-05-05 18:43:59 -0700390
391 if (srcPort instanceof OduCltPort && dstPort instanceof OduCltPort) {
Naoki Shiotae1366ad2016-05-11 19:24:11 -0700392 OduCltPort srcOCPort = (OduCltPort) srcPort;
393 OduCltPort dstOCPort = (OduCltPort) dstPort;
394 if (!srcOCPort.signalType().equals(dstOCPort.signalType())) {
395 continue;
396 }
397
Naoki Shiota5a056062016-05-05 18:43:59 -0700398 // Create OTN circuit
399 OpticalCircuitIntent circuitIntent = OpticalCircuitIntent.builder()
400 .appId(appId)
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700401 .src(src)
402 .dst(dst)
Naoki Shiotae1366ad2016-05-11 19:24:11 -0700403 .signalType(srcOCPort.signalType())
Naoki Shiota5a056062016-05-05 18:43:59 -0700404 .bidirectional(true)
405 .build();
406 intents.add(circuitIntent);
Naoki Shiota5a056062016-05-05 18:43:59 -0700407 } else if (srcPort instanceof OchPort && dstPort instanceof OchPort) {
Naoki Shiotae1366ad2016-05-11 19:24:11 -0700408 OchPort srcOchPort = (OchPort) srcPort;
409 OchPort dstOchPort = (OchPort) dstPort;
410 if (!srcOchPort.signalType().equals(dstOchPort.signalType())) {
411 continue;
412 }
413
Naoki Shiota5a056062016-05-05 18:43:59 -0700414 // Create lightpath
Naoki Shiota5a056062016-05-05 18:43:59 -0700415 OpticalConnectivityIntent opticalIntent = OpticalConnectivityIntent.builder()
416 .appId(appId)
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700417 .src(src)
418 .dst(dst)
Naoki Shiotae1366ad2016-05-11 19:24:11 -0700419 .signalType(srcOchPort.signalType())
Naoki Shiota5a056062016-05-05 18:43:59 -0700420 .bidirectional(true)
421 .build();
422 intents.add(opticalIntent);
Naoki Shiota5a056062016-05-05 18:43:59 -0700423 } else {
424 log.warn("Unsupported cross connect point types {} {}", srcPort.type(), dstPort.type());
425 return Collections.emptyList();
426 }
427 }
428
429 return intents;
430 }
431
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700432 private Set<PacketLinkRealizedByOptical> createPacketLinkSet(List<Pair<ConnectPoint, ConnectPoint>> connectPoints,
433 List<Intent> intents,
434 Map<ConnectPoint, ConnectPoint> crossConnectPoints) {
435 checkArgument(connectPoints.size() == intents.size());
436
437 Set<PacketLinkRealizedByOptical> pLinks = new HashSet<>();
438
439 Iterator<Pair<ConnectPoint, ConnectPoint>> xcPointsItr = connectPoints.iterator();
440 Iterator<Intent> intentItr = intents.iterator();
441 while (xcPointsItr.hasNext()) {
442 Pair<ConnectPoint, ConnectPoint> xcPoints = xcPointsItr.next();
443 Intent intent = intentItr.next();
444
445 ConnectPoint packetSrc = checkNotNull(crossConnectPoints.get(xcPoints.getLeft()));
446 ConnectPoint packetDst = checkNotNull(crossConnectPoints.get(xcPoints.getRight()));
447
448 if (intent instanceof OpticalConnectivityIntent) {
449 pLinks.add(PacketLinkRealizedByOptical.create(packetSrc, packetDst,
450 (OpticalConnectivityIntent) intent));
451 } else if (intent instanceof OpticalCircuitIntent) {
452 pLinks.add(PacketLinkRealizedByOptical.create(packetSrc, packetDst,
453 (OpticalCircuitIntent) intent));
454 } else {
455 log.warn("Unexpected intent type: {}", intent.getClass());
456 }
457 }
458
459 return pLinks;
460 }
461
Naoki Shiota5a056062016-05-05 18:43:59 -0700462 /**
463 * Verifies if given device type is in packet layer, i.e., ROADM, OTN or ROADM_OTN device.
464 *
465 * @param type device type
466 * @return true if in packet layer, false otherwise
467 */
468 private boolean isPacketLayer(Device.Type type) {
469 return type == Device.Type.SWITCH || type == Device.Type.ROUTER || type == Device.Type.VIRTUAL;
470 }
471
472 /**
473 * Verifies if given device type is in packet layer, i.e., switch or router device.
474 *
475 * @param type device type
476 * @return true if in packet layer, false otherwise
477 */
478 private boolean isTransportLayer(Device.Type type) {
479 return type == Device.Type.ROADM || type == Device.Type.OTN || type == Device.Type.ROADM_OTN;
480 }
481
482 /**
483 * Verifies if given link forms a cross-connection between packet and optical layer.
484 *
485 * @param link the link
486 * @return true if the link is a cross-connect link, false otherwise
487 */
488 private boolean isCrossConnectLink(Link link) {
489 if (link.type() != Link.Type.OPTICAL) {
490 return false;
491 }
492
493 Device.Type src = deviceService.getDevice(link.src().deviceId()).type();
494 Device.Type dst = deviceService.getDevice(link.dst().deviceId()).type();
495
496 return src != dst &&
497 ((isPacketLayer(src) && isTransportLayer(dst)) || (isPacketLayer(dst) && isTransportLayer(src)));
498 }
499
500 /**
501 * Updates bandwidth resource of given connect point.
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700502 *
Naoki Shiota5a056062016-05-05 18:43:59 -0700503 * @param cp Connect point
504 * @param bandwidth New bandwidth
505 */
506 private void updatePortBandwidth(ConnectPoint cp, Bandwidth bandwidth) {
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700507 log.debug("update Port {} Bandwidth {}", cp, bandwidth);
508 BandwidthCapacity bwCapacity = networkConfigService.addConfig(cp, BandwidthCapacity.class);
509 bwCapacity.capacity(bandwidth).apply();
Naoki Shiota5a056062016-05-05 18:43:59 -0700510 }
511
512 /**
513 * Updates usage information of bandwidth based on connectivity which is established.
514 * @param connectivity Optical connectivity
515 */
516 private void updateBandwidthUsage(OpticalConnectivity connectivity) {
Naoki Shiotacb744db2016-05-13 19:32:35 -0700517 OpticalConnectivityId connectivityId = connectivity.id();
Naoki Shiota5a056062016-05-05 18:43:59 -0700518
519 List<Link> links = connectivity.links();
520
521 List<Resource> resources = links.stream().flatMap(l -> Stream.of(l.src(), l.dst()))
522 .filter(cp -> !isTransportLayer(deviceService.getDevice(cp.deviceId()).type()))
523 .map(cp -> Resources.continuous(cp.deviceId(), cp.port(),
524 Bandwidth.class).resource(connectivity.bandwidth().bps()))
525 .collect(Collectors.toList());
526
Naoki Shiotacb744db2016-05-13 19:32:35 -0700527 log.debug("allocating bandwidth for {} : {}", connectivityId, resources);
528 List<ResourceAllocation> allocations = resourceService.allocate(connectivityId, resources);
Naoki Shiota5a056062016-05-05 18:43:59 -0700529 if (allocations.isEmpty()) {
530 log.warn("Failed to allocate bandwidth {} to {}",
531 connectivity.bandwidth().bps(), resources);
532 // TODO any recovery?
533 }
Naoki Shiotacb744db2016-05-13 19:32:35 -0700534 log.debug("Done allocating bandwidth for {}", connectivityId);
Naoki Shiota5a056062016-05-05 18:43:59 -0700535 }
536
537 /**
538 * Release bandwidth allocated by given connectivity.
539 * @param connectivity Optical connectivity
540 */
541 private void releaseBandwidthUsage(OpticalConnectivity connectivity) {
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700542 if (connectivity.links().isEmpty()) {
543 return;
Naoki Shiota5a056062016-05-05 18:43:59 -0700544 }
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700545
546 // release resource only if this node is the master for link head device
547 if (mastershipService.isLocalMaster(connectivity.links().get(0).src().deviceId())) {
548 OpticalConnectivityId connectivityId = connectivity.id();
549
550 log.debug("releasing bandwidth allocated to {}", connectivityId);
551 if (!resourceService.release(connectivityId)) {
552 log.warn("Failed to release bandwidth allocated to {}",
553 connectivityId);
554 // TODO any recovery?
555 }
556 log.debug("DONE releasing bandwidth for {}", connectivityId);
557 }
Naoki Shiota5a056062016-05-05 18:43:59 -0700558 }
559
560 private class BandwidthLinkWeight implements LinkWeight {
561 private Bandwidth bandwidth = null;
562
563 public BandwidthLinkWeight(Bandwidth bandwidth) {
564 this.bandwidth = bandwidth;
565 }
566
567 @Override
568 public double weight(TopologyEdge edge) {
569 Link l = edge.link();
570
Naoki Shiota0d2943e2016-05-13 18:53:21 -0700571 // Avoid inactive links
Naoki Shiota5a056062016-05-05 18:43:59 -0700572 if (l.state() == Link.State.INACTIVE) {
573 return -1.0;
574 }
575
Naoki Shiota0d2943e2016-05-13 18:53:21 -0700576 // Avoid cross connect links with used ports
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700577 if (isCrossConnectLink(l) && usedCrossConnectLinkSet.contains(l)) {
Naoki Shiota5a056062016-05-05 18:43:59 -0700578 return -1.0;
579 }
580
581 // Check availability of bandwidth
582 if (bandwidth != null) {
583 if (hasEnoughBandwidth(l.src()) && hasEnoughBandwidth(l.dst())) {
584 return 1.0;
585 } else {
586 return -1.0;
587 }
588 } else {
589 // TODO needs to differentiate optical and packet?
590 if (l.type() == Link.Type.OPTICAL) {
591 // Transport links
592 return 1.0;
593 } else {
594 // Packet links
595 return 1.0;
596 }
597 }
598 }
599
600 private boolean hasEnoughBandwidth(ConnectPoint cp) {
601 if (cp.elementId() instanceof DeviceId) {
Naoki Shiotae1366ad2016-05-11 19:24:11 -0700602 Device device = deviceService.getDevice(cp.deviceId());
603 Device.Type type = device.type();
604
Naoki Shiota5a056062016-05-05 18:43:59 -0700605 if (isTransportLayer(type)) {
Naoki Shiotae1366ad2016-05-11 19:24:11 -0700606 // Check if the port has enough capacity
607 Port port = deviceService.getPort(cp.deviceId(), cp.port());
608 if (port instanceof OduCltPort || port instanceof OchPort) {
609 // Port with capacity
610 return bandwidth.bps() < port.portSpeed() * 1000000.0;
611 } else {
612 // Port without valid capacity (OMS port, etc.)
613 return true;
614 }
615 } else {
616 // Check if enough amount of bandwidth resource remains
617 ContinuousResource resource = Resources.continuous(cp.deviceId(), cp.port(), Bandwidth.class)
618 .resource(bandwidth.bps());
619 return resourceService.isAvailable(resource);
Naoki Shiota5a056062016-05-05 18:43:59 -0700620 }
Naoki Shiota5a056062016-05-05 18:43:59 -0700621 }
622 return false;
623 }
624 }
625
626
627 public class InternalIntentListener implements IntentListener {
628 @Override
629 public void event(IntentEvent event) {
630 switch (event.type()) {
631 case INSTALLED:
632 log.info("Intent {} installed.", event.subject());
633 updateCrossConnectLink(event.subject());
634 break;
635 case WITHDRAWN:
636 log.info("Intent {} withdrawn.", event.subject());
637 removeCrossConnectLinks(event.subject());
638 break;
639 case FAILED:
640 log.info("Intent {} failed.", event.subject());
641 break;
642 default:
643 break;
644 }
645 }
646
647 private void updateCrossConnectLink(Intent intent) {
648 linkPathMap.entrySet().stream()
649 .filter(e -> e.getKey().realizingIntentKey().equals(intent.key()))
650 .forEach(e -> {
651 ConnectPoint packetSrc = e.getKey().src();
652 ConnectPoint packetDst = e.getKey().dst();
653 Bandwidth bw = e.getKey().bandwidth();
Naoki Shiota5a056062016-05-05 18:43:59 -0700654
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700655 // reflect modification only if packetSrc is local_
656 if (mastershipService.isLocalMaster(packetSrc.deviceId())) {
657 // Updates bandwidth of packet ports
658 updatePortBandwidth(packetSrc, bw);
659 updatePortBandwidth(packetDst, bw);
Naoki Shiota5a056062016-05-05 18:43:59 -0700660
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700661 // Updates link status in distributed map
662 linkPathMap.computeIfPresent(e.getKey(), (link, connectivity) ->
663 e.getValue().value().setLinkEstablished(packetSrc, packetDst, true));
Naoki Shiota5a056062016-05-05 18:43:59 -0700664 }
665 });
666 }
667
668 private void removeCrossConnectLinks(Intent intent) {
669 ConnectPoint src, dst;
670
671 if (intent instanceof OpticalCircuitIntent) {
672 OpticalCircuitIntent circuit = (OpticalCircuitIntent) intent;
673 src = circuit.getSrc();
674 dst = circuit.getDst();
675 } else if (intent instanceof OpticalConnectivityIntent) {
676 OpticalConnectivityIntent conn = (OpticalConnectivityIntent) intent;
677 src = conn.getSrc();
678 dst = conn.getDst();
679 } else {
680 return;
681 }
682
683 removeXcLinkUsage(src);
684 removeXcLinkUsage(dst);
685
686 // Set bandwidth of 0 to cross connect ports
687 Bandwidth bw = Bandwidth.bps(0);
688 linkPathMap.entrySet().stream()
689 .filter(e -> e.getKey().realizingIntentKey().equals(intent.key()))
690 .forEach(e -> {
691 ConnectPoint packetSrc = e.getKey().src();
692 ConnectPoint packetDst = e.getKey().dst();
Naoki Shiota5a056062016-05-05 18:43:59 -0700693
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700694 // reflect modification only if packetSrc is local_
695 if (mastershipService.isLocalMaster(packetSrc.deviceId())) {
696 // Updates bandwidth of packet ports
697 updatePortBandwidth(packetSrc, bw);
698 updatePortBandwidth(packetDst, bw);
699
700 // Updates link status in distributed map
701 linkPathMap.computeIfPresent(e.getKey(), (link, connectivity) ->
702 e.getValue().value().setLinkEstablished(packetSrc, packetDst, false));
Naoki Shiota5a056062016-05-05 18:43:59 -0700703 }
704 });
705 }
706
707 private void removeXcLinkUsage(ConnectPoint cp) {
708 Optional<Link> link = linkService.getLinks(cp).stream()
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700709 .filter(usedCrossConnectLinkSet::contains)
Naoki Shiota5a056062016-05-05 18:43:59 -0700710 .findAny();
711
712 if (!link.isPresent()) {
713 log.warn("Cross connect point {} has no cross connect link.", cp);
714 return;
715 }
716
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700717 usedCrossConnectLinkSet.remove(link.get());
Naoki Shiota5a056062016-05-05 18:43:59 -0700718 }
719 }
720
721
722 private class InternalLinkListener implements LinkListener {
723
724 @Override
725 public void event(LinkEvent event) {
726 switch (event.type()) {
727 case LINK_REMOVED:
728 Link link = event.subject();
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700729 // updates linkPathMap only if src device of link is local
730 if (!mastershipService.isLocalMaster(link.src().deviceId())) {
731 return;
732 }
733
734 // find all packet links that correspond to removed link
Naoki Shiota5a056062016-05-05 18:43:59 -0700735 Set<PacketLinkRealizedByOptical> pLinks = linkPathMap.keySet().stream()
736 .filter(l -> l.isBetween(link.src(), link.dst()) || l.isBetween(link.dst(), link.src()))
737 .collect(Collectors.toSet());
738
739 pLinks.forEach(l -> {
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700740 // remove found packet links from distributed store
741 linkPathMap.computeIfPresent(l, (plink, conn) -> {
742 // Notifies listeners if all packet links are gone
743 if (conn.isAllRealizingLinkNotEstablished()) {
744 post(new OpticalPathEvent(OpticalPathEvent.Type.PATH_REMOVED, conn.id()));
745 }
746 return null;
747 });
Naoki Shiota5a056062016-05-05 18:43:59 -0700748 });
749 default:
750 break;
751 }
752 }
753 }
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700754
755 private class InternalStoreListener
756 implements MapEventListener<PacketLinkRealizedByOptical, OpticalConnectivity> {
757
758 @Override
759 public void event(MapEvent<PacketLinkRealizedByOptical, OpticalConnectivity> event) {
760 switch (event.type()) {
761 case UPDATE:
762 OpticalConnectivity oldConnectivity = event.oldValue().value();
763 OpticalConnectivity newConnectivity = event.newValue().value();
764
765 if (!oldConnectivity.isAllRealizingLinkEstablished() &&
766 newConnectivity.isAllRealizingLinkEstablished()) {
767 // Notifies listeners if all links are established
768 updateBandwidthUsage(newConnectivity);
769 post(new OpticalPathEvent(OpticalPathEvent.Type.PATH_INSTALLED, newConnectivity.id()));
770 } else if (!oldConnectivity.isAllRealizingLinkNotEstablished() &&
771 newConnectivity.isAllRealizingLinkNotEstablished()) {
772 // Notifies listeners if all links are gone
773 releaseBandwidthUsage(newConnectivity);
774 post(new OpticalPathEvent(OpticalPathEvent.Type.PATH_REMOVED, newConnectivity.id()));
775 }
776
777 break;
778 default:
779 break;
780 }
781 }
782
783 }
Naoki Shiota5a056062016-05-05 18:43:59 -0700784}
785