blob: fa8d824acda0db4eef16318a610bf61b1ec881be [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;
Yuta HIGUCHI9b9c7292017-03-07 22:50:04 -080060import org.onosproject.net.config.basics.BasicLinkConfig;
Naoki Shiota5a056062016-05-05 18:43:59 -070061import org.onosproject.net.resource.ContinuousResource;
62import org.onosproject.net.resource.Resource;
63import org.onosproject.net.resource.ResourceAllocation;
64import org.onosproject.net.resource.ResourceService;
65import org.onosproject.net.resource.Resources;
66import org.onosproject.net.topology.LinkWeight;
67import org.onosproject.net.topology.PathService;
68import org.onosproject.net.topology.TopologyEdge;
Naoki Shiota7c3111b2016-06-09 16:12:11 -070069import org.onosproject.store.serializers.KryoNamespaces;
Naoki Shiota5a056062016-05-05 18:43:59 -070070import org.onosproject.store.service.AtomicCounter;
Naoki Shiota7c3111b2016-06-09 16:12:11 -070071import org.onosproject.store.service.ConsistentMap;
72import org.onosproject.store.service.DistributedSet;
73import org.onosproject.store.service.MapEvent;
74import org.onosproject.store.service.MapEventListener;
75import org.onosproject.store.service.Serializer;
Naoki Shiota5a056062016-05-05 18:43:59 -070076import org.onosproject.store.service.StorageService;
Naoki Shiota7c3111b2016-06-09 16:12:11 -070077import org.onosproject.store.service.Versioned;
Naoki Shiota5a056062016-05-05 18:43:59 -070078import org.slf4j.Logger;
79import org.slf4j.LoggerFactory;
80
81import java.time.Duration;
Naoki Shiota7c3111b2016-06-09 16:12:11 -070082import java.util.ArrayList;
Naoki Shiota5a056062016-05-05 18:43:59 -070083import java.util.Collections;
Naoki Shiota7c3111b2016-06-09 16:12:11 -070084import java.util.HashMap;
85import java.util.HashSet;
Naoki Shiota5a056062016-05-05 18:43:59 -070086import java.util.Iterator;
87import java.util.LinkedList;
88import java.util.List;
89import java.util.Map;
90import java.util.Optional;
91import java.util.Set;
Naoki Shiota5a056062016-05-05 18:43:59 -070092import java.util.stream.Collectors;
93import java.util.stream.Stream;
94
95import static com.google.common.base.Preconditions.checkArgument;
96import static com.google.common.base.Preconditions.checkNotNull;
Yuta HIGUCHI9b9c7292017-03-07 22:50:04 -080097import static org.onosproject.net.LinkKey.linkKey;
Naoki Shiotae1366ad2016-05-11 19:24:11 -070098import static org.onosproject.net.optical.device.OpticalDeviceServiceView.opticalView;
Naoki Shiota5a056062016-05-05 18:43:59 -070099
100/**
101 * Main component to configure optical connectivity.
102 */
103@Beta
104@Service
105@Component(immediate = true)
106public class OpticalPathProvisioner
107 extends AbstractListenerManager<OpticalPathEvent, OpticalPathListener>
108 implements OpticalPathService {
Yuta HIGUCHI9b9c7292017-03-07 22:50:04 -0800109
Naoki Shiota5a056062016-05-05 18:43:59 -0700110 protected static final Logger log = LoggerFactory.getLogger(OpticalPathProvisioner.class);
111
Yuta HIGUCHI9b9c7292017-03-07 22:50:04 -0800112 /**
113 * Bandwidth representing no bandwidth requirement specified.
114 */
115 private static final Bandwidth NO_BW_REQUIREMENT = Bandwidth.bps(0);
116
Naoki Shiota5a056062016-05-05 18:43:59 -0700117 private static final String OPTICAL_CONNECTIVITY_ID_COUNTER = "optical-connectivity-id";
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700118 private static final String LINKPATH_MAP_NAME = "newoptical-linkpath";
119 private static final String CONNECTIVITY_MAP_NAME = "newoptical-connectivity";
120 private static final String CROSSCONNECTLINK_SET_NAME = "newoptical-crossconnectlink";
Naoki Shiota5a056062016-05-05 18:43:59 -0700121
122 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
123 protected IntentService intentService;
124
125 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
126 protected PathService pathService;
127
128 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
129 protected CoreService coreService;
130
131 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
132 protected LinkService linkService;
133
134 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
135 protected MastershipService mastershipService;
136
137 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
138 protected ClusterService clusterService;
139
140 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
141 protected DeviceService deviceService;
142
143 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
144 protected StorageService storageService;
145
146 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
147 protected NetworkConfigService networkConfigService;
148
149 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
150 protected ResourceService resourceService;
151
152
153 private ApplicationId appId;
154
155 private AtomicCounter idCounter;
156
Naoki Shiota0d2943e2016-05-13 18:53:21 -0700157 private ListenerTracker listeners;
Naoki Shiota5a056062016-05-05 18:43:59 -0700158
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700159 private InternalStoreListener storeListener = new InternalStoreListener();
Naoki Shiota5a056062016-05-05 18:43:59 -0700160
Yuta HIGUCHIe80ec2c2017-03-06 14:16:13 -0800161 /**
162 * Map from packet-layer link expected to be realized by some optical Intent to
163 * OpticalConnectivity (~=top level intent over multi-layer topology).
164 */
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700165 private ConsistentMap<PacketLinkRealizedByOptical, OpticalConnectivity> linkPathMap;
Naoki Shiota5a056062016-05-05 18:43:59 -0700166
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700167 private ConsistentMap<OpticalConnectivityId, OpticalConnectivity> connectivityMap;
168
Yuta HIGUCHIe80ec2c2017-03-06 14:16:13 -0800169 // FIXME in the long run. This is effectively app's own resource subsystem
170 /**
171 * Set of cross connect link currently used.
172 */
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700173 private DistributedSet<Link> usedCrossConnectLinkSet;
174
175 private static final KryoNamespace.Builder LINKPATH_SERIALIZER = KryoNamespace.newBuilder()
176 .register(KryoNamespaces.API)
177 .register(PacketLinkRealizedByOptical.class)
178 .register(OpticalConnectivityId.class)
179 .register(OpticalConnectivity.class);
180
181 private static final KryoNamespace.Builder CONNECTIVITY_SERIALIZER = KryoNamespace.newBuilder()
182 .register(KryoNamespaces.API)
Yuta HIGUCHI0086cf82016-07-18 22:49:45 -0700183 .register(PacketLinkRealizedByOptical.class)
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700184 .register(OpticalConnectivityId.class)
185 .register(OpticalConnectivity.class);
186
187 private static final KryoNamespace.Builder CROSSCONNECTLINKS_SERIALIZER = KryoNamespace.newBuilder()
188 .register(KryoNamespaces.API);
Naoki Shiota5a056062016-05-05 18:43:59 -0700189
190 @Activate
191 protected void activate() {
Naoki Shiotae1366ad2016-05-11 19:24:11 -0700192 deviceService = opticalView(deviceService);
Naoki Shiota5a056062016-05-05 18:43:59 -0700193 appId = coreService.registerApplication("org.onosproject.newoptical");
194
Naoki Shiota03c29e12016-05-16 16:58:07 -0700195 idCounter = storageService.getAtomicCounter(OPTICAL_CONNECTIVITY_ID_COUNTER);
Naoki Shiota5a056062016-05-05 18:43:59 -0700196
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700197 linkPathMap = storageService.<PacketLinkRealizedByOptical, OpticalConnectivity>consistentMapBuilder()
198 .withSerializer(Serializer.using(LINKPATH_SERIALIZER.build()))
199 .withName(LINKPATH_MAP_NAME)
200 .withApplicationId(appId)
201 .build();
202
203 connectivityMap = storageService.<OpticalConnectivityId, OpticalConnectivity>consistentMapBuilder()
204 .withSerializer(Serializer.using(CONNECTIVITY_SERIALIZER.build()))
205 .withName(CONNECTIVITY_MAP_NAME)
206 .withApplicationId(appId)
207 .build();
208
209 usedCrossConnectLinkSet = storageService.<Link>setBuilder()
210 .withSerializer(Serializer.using(CROSSCONNECTLINKS_SERIALIZER.build()))
211 .withName(CROSSCONNECTLINK_SET_NAME)
212 .withApplicationId(appId)
213 .build()
214 .asDistributedSet();
215
Naoki Shiota5a056062016-05-05 18:43:59 -0700216 eventDispatcher.addSink(OpticalPathEvent.class, listenerRegistry);
Naoki Shiota0d2943e2016-05-13 18:53:21 -0700217
218 listeners = new ListenerTracker();
219 listeners.addListener(linkService, new InternalLinkListener())
220 .addListener(intentService, new InternalIntentListener());
Naoki Shiota5a056062016-05-05 18:43:59 -0700221
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700222 linkPathMap.addListener(storeListener);
223
Naoki Shiota5a056062016-05-05 18:43:59 -0700224 log.info("Started");
225 }
226
227 @Deactivate
228 protected void deactivate() {
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700229 linkPathMap.removeListener(storeListener);
Naoki Shiota0d2943e2016-05-13 18:53:21 -0700230 listeners.removeListeners();
231 eventDispatcher.removeSink(OpticalPathEvent.class);
Naoki Shiota5a056062016-05-05 18:43:59 -0700232
233 log.info("Stopped");
234 }
235
Yuta HIGUCHIe80ec2c2017-03-06 14:16:13 -0800236 /*
237 * Request packet-layer connectivity between specified ports,
238 * over packet-optical multi-layer infrastructure.
239 *
240 * Functionality-wise this is effectively submitting Packet-Optical
241 * multi-layer P2P Intent.
242 *
243 * It computes multi-layer path meeting specified constraint,
244 * and calls setupPath.
245 */
Naoki Shiota5a056062016-05-05 18:43:59 -0700246 @Override
247 public OpticalConnectivityId setupConnectivity(ConnectPoint ingress, ConnectPoint egress,
248 Bandwidth bandwidth, Duration latency) {
249 checkNotNull(ingress);
250 checkNotNull(egress);
251 log.info("setupConnectivity({}, {}, {}, {})", ingress, egress, bandwidth, latency);
252
Yuta HIGUCHI9b9c7292017-03-07 22:50:04 -0800253 bandwidth = (bandwidth == null) ? NO_BW_REQUIREMENT : bandwidth;
Naoki Shiota5a056062016-05-05 18:43:59 -0700254
255 Set<Path> paths = pathService.getPaths(ingress.deviceId(), egress.deviceId(),
256 new BandwidthLinkWeight(bandwidth));
257 if (paths.isEmpty()) {
258 log.warn("Unable to find multi-layer path.");
259 return null;
260 }
261
262 // Search path with available cross connect points
263 for (Path path : paths) {
264 OpticalConnectivityId id = setupPath(path, bandwidth, latency);
265 if (id != null) {
266 log.info("Assigned OpticalConnectivityId: {}", id);
267 return id;
268 }
269 }
270
Yuta HIGUCHIe80ec2c2017-03-06 14:16:13 -0800271 log.error("setupConnectivity({}, {}, {}, {}) failed.", ingress, egress, bandwidth, latency);
Naoki Shiota0d2943e2016-05-13 18:53:21 -0700272
Naoki Shiota5a056062016-05-05 18:43:59 -0700273 return null;
274 }
275
Yuta HIGUCHIe80ec2c2017-03-06 14:16:13 -0800276 /*
277 * Given a multi-layer path,
278 * compute a set of segments which requires
279 * OpticalConnectivity(~=OpticalConnectivityIntent or OpticalCircuitPath)
280 * to provide packet-layer connectivity.
281 */
Naoki Shiota5a056062016-05-05 18:43:59 -0700282 @Override
283 public OpticalConnectivityId setupPath(Path path, Bandwidth bandwidth, Duration latency) {
284 checkNotNull(path);
285 log.info("setupPath({}, {}, {})", path, bandwidth, latency);
286
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700287 // map of cross connect points (optical port -> packet port)
288 Map<ConnectPoint, ConnectPoint> crossConnectPointMap = new HashMap<>();
289
290 // list of (src, dst) pair of optical ports between which optical path should be installed
291 List<Pair<ConnectPoint, ConnectPoint>> crossConnectPoints = new ArrayList<>();
292
293 // Scan path to find pairs of connect points between which optical intent is installed
294 // opticalSrcPort works as a flag parameter to show scanning status
295 ConnectPoint opticalSrcPort = null;
296 for (Link link : path.links()) {
297 if (!isCrossConnectLink(link)) {
298 continue;
299 }
300
301 if (opticalSrcPort != null) {
302 // opticalSrcPort!=null means src port was already found
303 // in this case link.src() is optical layer, and link.dst() is packet layer
304
305 // Check if types of src port and dst port matches
306 Device srcDevice = checkNotNull(deviceService.getDevice(opticalSrcPort.deviceId()),
307 "Unknown device ID");
308 Device dstDevice = checkNotNull(deviceService.getDevice(link.src().deviceId()),
309 "Unknown device ID");
310 if (srcDevice.type() != dstDevice.type()) {
311 log.error("Unsupported mix of cross connect points : {}, {}",
312 srcDevice.type(), dstDevice.type());
313 return null;
314 }
315
316 // Update cross connect points map
317 crossConnectPointMap.put(link.src(), link.dst());
318
319 // Add optical ports pair to list
320 crossConnectPoints.add(Pair.of(opticalSrcPort, link.src()));
321
322 // Reset flag parameter
323 opticalSrcPort = null;
324 } else {
325 // opticalSrcPort==null means src port was not found yet
326 // in this case link.src() is packet layer, and link.dst() is optical layer
327
328 // Update cross connect points map
329 crossConnectPointMap.put(link.dst(), link.src());
330 // Set opticalSrcPort to src of link (optical port)
331 opticalSrcPort = link.dst();
332 }
Naoki Shiota5a056062016-05-05 18:43:59 -0700333 }
334
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700335 // create intents from cross connect points
336 List<Intent> intents = createIntents(crossConnectPoints);
Naoki Shiota5a056062016-05-05 18:43:59 -0700337
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700338 // create set of PacketLinkRealizedByOptical
339 Set<PacketLinkRealizedByOptical> packetLinks = createPacketLinkSet(crossConnectPoints,
340 intents, crossConnectPointMap);
341
342 // create OpticalConnectivity object and store information to distributed store
343 OpticalConnectivity connectivity = createConnectivity(path, bandwidth, latency, packetLinks);
Naoki Shiota5a056062016-05-05 18:43:59 -0700344
345 // store cross connect port usage
346 path.links().stream().filter(this::isCrossConnectLink)
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700347 .forEach(usedCrossConnectLinkSet::add);
Naoki Shiota5a056062016-05-05 18:43:59 -0700348
349 // Submit the intents
350 for (Intent i : intents) {
351 intentService.submit(i);
352 log.debug("Submitted an intent: {}", i);
353 }
354
355 return connectivity.id();
356 }
357
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700358 private OpticalConnectivity createConnectivity(Path path, Bandwidth bandwidth, Duration latency,
359 Set<PacketLinkRealizedByOptical> links) {
Naoki Shiota5a056062016-05-05 18:43:59 -0700360 OpticalConnectivityId id = OpticalConnectivityId.of(idCounter.getAndIncrement());
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700361 OpticalConnectivity connectivity = new OpticalConnectivity(id, path.links(), bandwidth, latency,
362 links, Collections.emptySet());
363
364 links.forEach(l -> linkPathMap.put(l, connectivity));
Naoki Shiota5a056062016-05-05 18:43:59 -0700365
Naoki Shiota5a056062016-05-05 18:43:59 -0700366 // store connectivity information
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700367 connectivityMap.put(connectivity.id(), connectivity);
Naoki Shiota5a056062016-05-05 18:43:59 -0700368
369 return connectivity;
370 }
371
372 @Override
373 public boolean removeConnectivity(OpticalConnectivityId id) {
374 log.info("removeConnectivity({})", id);
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700375 Versioned<OpticalConnectivity> connectivity = connectivityMap.remove(id);
Naoki Shiota5a056062016-05-05 18:43:59 -0700376
377 if (connectivity == null) {
Naoki Shiota0d2943e2016-05-13 18:53:21 -0700378 log.info("OpticalConnectivity with id {} not found.", id);
Naoki Shiota5a056062016-05-05 18:43:59 -0700379 return false;
380 }
381
382 // TODO withdraw intent only if all of connectivities that use the optical path are withdrawn
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700383 connectivity.value().getRealizingLinks().forEach(l -> {
Naoki Shiota5a056062016-05-05 18:43:59 -0700384 Intent intent = intentService.getIntent(l.realizingIntentKey());
385 intentService.withdraw(intent);
386 });
387
388 return true;
389 }
390
391 @Override
Naoki Shiota0d2943e2016-05-13 18:53:21 -0700392 public Optional<List<Link>> getPath(OpticalConnectivityId id) {
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700393 Versioned<OpticalConnectivity> connectivity = connectivityMap.get(id);
Naoki Shiota5a056062016-05-05 18:43:59 -0700394 if (connectivity == null) {
Naoki Shiota0d2943e2016-05-13 18:53:21 -0700395 log.info("OpticalConnectivity with id {} not found.", id);
396 return Optional.empty();
Naoki Shiota5a056062016-05-05 18:43:59 -0700397 }
398
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700399 return Optional.of(ImmutableList.copyOf(connectivity.value().links()));
Naoki Shiota5a056062016-05-05 18:43:59 -0700400 }
401
402 /**
403 * Scans the list of cross connection points and returns a list of optical connectivity intents.
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700404 * During the process, save information about packet links to given set.
Naoki Shiota5a056062016-05-05 18:43:59 -0700405 *
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700406 * @param crossConnectPoints list of (src, dst) pair between which optical path will be set up
Naoki Shiota5a056062016-05-05 18:43:59 -0700407 * @return list of optical connectivity intents
408 */
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700409 private List<Intent> createIntents(List<Pair<ConnectPoint, ConnectPoint>> crossConnectPoints) {
Naoki Shiota5a056062016-05-05 18:43:59 -0700410 List<Intent> intents = new LinkedList<>();
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700411 Iterator<Pair<ConnectPoint, ConnectPoint>> itr = crossConnectPoints.iterator();
Naoki Shiota5a056062016-05-05 18:43:59 -0700412
413 while (itr.hasNext()) {
414 // checkArgument at start ensures we'll always have pairs of connect points
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700415 Pair<ConnectPoint, ConnectPoint> next = itr.next();
416 ConnectPoint src = next.getLeft();
417 ConnectPoint dst = next.getRight();
Naoki Shiota5a056062016-05-05 18:43:59 -0700418
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700419 Port srcPort = deviceService.getPort(src.deviceId(), src.port());
420 Port dstPort = deviceService.getPort(dst.deviceId(), dst.port());
Naoki Shiota5a056062016-05-05 18:43:59 -0700421
422 if (srcPort instanceof OduCltPort && dstPort instanceof OduCltPort) {
Naoki Shiotae1366ad2016-05-11 19:24:11 -0700423 OduCltPort srcOCPort = (OduCltPort) srcPort;
424 OduCltPort dstOCPort = (OduCltPort) dstPort;
425 if (!srcOCPort.signalType().equals(dstOCPort.signalType())) {
426 continue;
427 }
428
Naoki Shiota5a056062016-05-05 18:43:59 -0700429 // Create OTN circuit
430 OpticalCircuitIntent circuitIntent = OpticalCircuitIntent.builder()
431 .appId(appId)
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700432 .src(src)
433 .dst(dst)
Naoki Shiotae1366ad2016-05-11 19:24:11 -0700434 .signalType(srcOCPort.signalType())
Naoki Shiota5a056062016-05-05 18:43:59 -0700435 .bidirectional(true)
436 .build();
437 intents.add(circuitIntent);
Naoki Shiota5a056062016-05-05 18:43:59 -0700438 } else if (srcPort instanceof OchPort && dstPort instanceof OchPort) {
Naoki Shiotae1366ad2016-05-11 19:24:11 -0700439 OchPort srcOchPort = (OchPort) srcPort;
440 OchPort dstOchPort = (OchPort) dstPort;
441 if (!srcOchPort.signalType().equals(dstOchPort.signalType())) {
442 continue;
443 }
444
Naoki Shiota5a056062016-05-05 18:43:59 -0700445 // Create lightpath
Naoki Shiota5a056062016-05-05 18:43:59 -0700446 OpticalConnectivityIntent opticalIntent = OpticalConnectivityIntent.builder()
447 .appId(appId)
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700448 .src(src)
449 .dst(dst)
Naoki Shiotae1366ad2016-05-11 19:24:11 -0700450 .signalType(srcOchPort.signalType())
Naoki Shiota5a056062016-05-05 18:43:59 -0700451 .bidirectional(true)
452 .build();
453 intents.add(opticalIntent);
Naoki Shiota5a056062016-05-05 18:43:59 -0700454 } else {
455 log.warn("Unsupported cross connect point types {} {}", srcPort.type(), dstPort.type());
456 return Collections.emptyList();
457 }
458 }
459
460 return intents;
461 }
462
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700463 private Set<PacketLinkRealizedByOptical> createPacketLinkSet(List<Pair<ConnectPoint, ConnectPoint>> connectPoints,
464 List<Intent> intents,
465 Map<ConnectPoint, ConnectPoint> crossConnectPoints) {
466 checkArgument(connectPoints.size() == intents.size());
467
468 Set<PacketLinkRealizedByOptical> pLinks = new HashSet<>();
469
470 Iterator<Pair<ConnectPoint, ConnectPoint>> xcPointsItr = connectPoints.iterator();
471 Iterator<Intent> intentItr = intents.iterator();
472 while (xcPointsItr.hasNext()) {
473 Pair<ConnectPoint, ConnectPoint> xcPoints = xcPointsItr.next();
474 Intent intent = intentItr.next();
475
476 ConnectPoint packetSrc = checkNotNull(crossConnectPoints.get(xcPoints.getLeft()));
477 ConnectPoint packetDst = checkNotNull(crossConnectPoints.get(xcPoints.getRight()));
478
479 if (intent instanceof OpticalConnectivityIntent) {
480 pLinks.add(PacketLinkRealizedByOptical.create(packetSrc, packetDst,
481 (OpticalConnectivityIntent) intent));
482 } else if (intent instanceof OpticalCircuitIntent) {
483 pLinks.add(PacketLinkRealizedByOptical.create(packetSrc, packetDst,
484 (OpticalCircuitIntent) intent));
485 } else {
486 log.warn("Unexpected intent type: {}", intent.getClass());
487 }
488 }
489
490 return pLinks;
491 }
492
Naoki Shiota5a056062016-05-05 18:43:59 -0700493 /**
494 * Verifies if given device type is in packet layer, i.e., ROADM, OTN or ROADM_OTN device.
495 *
496 * @param type device type
497 * @return true if in packet layer, false otherwise
498 */
499 private boolean isPacketLayer(Device.Type type) {
500 return type == Device.Type.SWITCH || type == Device.Type.ROUTER || type == Device.Type.VIRTUAL;
501 }
502
503 /**
504 * Verifies if given device type is in packet layer, i.e., switch or router device.
505 *
506 * @param type device type
507 * @return true if in packet layer, false otherwise
508 */
509 private boolean isTransportLayer(Device.Type type) {
510 return type == Device.Type.ROADM || type == Device.Type.OTN || type == Device.Type.ROADM_OTN;
511 }
512
513 /**
514 * Verifies if given link forms a cross-connection between packet and optical layer.
515 *
516 * @param link the link
517 * @return true if the link is a cross-connect link, false otherwise
518 */
519 private boolean isCrossConnectLink(Link link) {
520 if (link.type() != Link.Type.OPTICAL) {
521 return false;
522 }
523
524 Device.Type src = deviceService.getDevice(link.src().deviceId()).type();
525 Device.Type dst = deviceService.getDevice(link.dst().deviceId()).type();
526
527 return src != dst &&
528 ((isPacketLayer(src) && isTransportLayer(dst)) || (isPacketLayer(dst) && isTransportLayer(src)));
529 }
530
531 /**
Yuta HIGUCHI9b9c7292017-03-07 22:50:04 -0800532 * Updates bandwidth resource of given connect point to specified value.
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700533 *
Naoki Shiota5a056062016-05-05 18:43:59 -0700534 * @param cp Connect point
535 * @param bandwidth New bandwidth
536 */
Yuta HIGUCHI9b9c7292017-03-07 22:50:04 -0800537 private void setPortBandwidth(ConnectPoint cp, Bandwidth bandwidth) {
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700538 log.debug("update Port {} Bandwidth {}", cp, bandwidth);
539 BandwidthCapacity bwCapacity = networkConfigService.addConfig(cp, BandwidthCapacity.class);
540 bwCapacity.capacity(bandwidth).apply();
Naoki Shiota5a056062016-05-05 18:43:59 -0700541 }
542
543 /**
544 * Updates usage information of bandwidth based on connectivity which is established.
545 * @param connectivity Optical connectivity
546 */
547 private void updateBandwidthUsage(OpticalConnectivity connectivity) {
Yuta HIGUCHI9b9c7292017-03-07 22:50:04 -0800548 if (NO_BW_REQUIREMENT.equals(connectivity.bandwidth())) {
549 // no bandwidth requirement, nothing to allocate.
550 return;
551 }
552
Naoki Shiotacb744db2016-05-13 19:32:35 -0700553 OpticalConnectivityId connectivityId = connectivity.id();
Naoki Shiota5a056062016-05-05 18:43:59 -0700554
555 List<Link> links = connectivity.links();
556
557 List<Resource> resources = links.stream().flatMap(l -> Stream.of(l.src(), l.dst()))
558 .filter(cp -> !isTransportLayer(deviceService.getDevice(cp.deviceId()).type()))
559 .map(cp -> Resources.continuous(cp.deviceId(), cp.port(),
560 Bandwidth.class).resource(connectivity.bandwidth().bps()))
561 .collect(Collectors.toList());
562
Naoki Shiotacb744db2016-05-13 19:32:35 -0700563 log.debug("allocating bandwidth for {} : {}", connectivityId, resources);
564 List<ResourceAllocation> allocations = resourceService.allocate(connectivityId, resources);
Naoki Shiota5a056062016-05-05 18:43:59 -0700565 if (allocations.isEmpty()) {
566 log.warn("Failed to allocate bandwidth {} to {}",
567 connectivity.bandwidth().bps(), resources);
568 // TODO any recovery?
569 }
Naoki Shiotacb744db2016-05-13 19:32:35 -0700570 log.debug("Done allocating bandwidth for {}", connectivityId);
Naoki Shiota5a056062016-05-05 18:43:59 -0700571 }
572
573 /**
574 * Release bandwidth allocated by given connectivity.
575 * @param connectivity Optical connectivity
576 */
577 private void releaseBandwidthUsage(OpticalConnectivity connectivity) {
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700578 if (connectivity.links().isEmpty()) {
579 return;
Naoki Shiota5a056062016-05-05 18:43:59 -0700580 }
Yuta HIGUCHI9b9c7292017-03-07 22:50:04 -0800581 if (NO_BW_REQUIREMENT.equals(connectivity.bandwidth())) {
582 // no bandwidth requirement, nothing to release.
583 return;
584 }
585
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700586
587 // release resource only if this node is the master for link head device
588 if (mastershipService.isLocalMaster(connectivity.links().get(0).src().deviceId())) {
589 OpticalConnectivityId connectivityId = connectivity.id();
590
591 log.debug("releasing bandwidth allocated to {}", connectivityId);
592 if (!resourceService.release(connectivityId)) {
593 log.warn("Failed to release bandwidth allocated to {}",
594 connectivityId);
595 // TODO any recovery?
596 }
597 log.debug("DONE releasing bandwidth for {}", connectivityId);
598 }
Naoki Shiota5a056062016-05-05 18:43:59 -0700599 }
600
Yuta HIGUCHI9b9c7292017-03-07 22:50:04 -0800601 private boolean linkDiscoveryEnabled(ConnectPoint cp) {
602 // FIXME should check Device feature and configuration state.
603
604 // short-term hack for ONS'17 time-frame,
605 // only expect OF device to have link discovery.
606 return cp.deviceId().uri().getScheme().equals("of");
607 }
608
609 /**
610 * Returns true if both connect point support for link discovery & enabled.
611 *
612 * @param cp1 port 1
613 * @param cp2 port 2
614 * @return true if both connect point support for link discovery & enabled.
615 */
616 private boolean linkDiscoveryEnabled(ConnectPoint cp1, ConnectPoint cp2) {
617 return linkDiscoveryEnabled(cp1) && linkDiscoveryEnabled(cp2);
618 }
619
Naoki Shiota5a056062016-05-05 18:43:59 -0700620 private class BandwidthLinkWeight implements LinkWeight {
621 private Bandwidth bandwidth = null;
622
623 public BandwidthLinkWeight(Bandwidth bandwidth) {
624 this.bandwidth = bandwidth;
625 }
626
627 @Override
628 public double weight(TopologyEdge edge) {
629 Link l = edge.link();
630
Naoki Shiota0d2943e2016-05-13 18:53:21 -0700631 // Avoid inactive links
Naoki Shiota5a056062016-05-05 18:43:59 -0700632 if (l.state() == Link.State.INACTIVE) {
Yuta HIGUCHI806113f2017-03-07 22:46:20 -0800633 log.trace("{} is not active", l);
Naoki Shiota5a056062016-05-05 18:43:59 -0700634 return -1.0;
635 }
636
Naoki Shiota0d2943e2016-05-13 18:53:21 -0700637 // Avoid cross connect links with used ports
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700638 if (isCrossConnectLink(l) && usedCrossConnectLinkSet.contains(l)) {
Yuta HIGUCHI806113f2017-03-07 22:46:20 -0800639 log.trace("Cross connect {} in use", l);
Naoki Shiota5a056062016-05-05 18:43:59 -0700640 return -1.0;
641 }
642
643 // Check availability of bandwidth
Yuta HIGUCHI9b9c7292017-03-07 22:50:04 -0800644 if (bandwidth != null && !NO_BW_REQUIREMENT.equals(bandwidth)) {
Naoki Shiota5a056062016-05-05 18:43:59 -0700645 if (hasEnoughBandwidth(l.src()) && hasEnoughBandwidth(l.dst())) {
646 return 1.0;
647 } else {
Yuta HIGUCHI806113f2017-03-07 22:46:20 -0800648 log.trace("Not enought bandwidth on {}", l);
Naoki Shiota5a056062016-05-05 18:43:59 -0700649 return -1.0;
650 }
651 } else {
652 // TODO needs to differentiate optical and packet?
653 if (l.type() == Link.Type.OPTICAL) {
654 // Transport links
655 return 1.0;
656 } else {
657 // Packet links
658 return 1.0;
659 }
660 }
661 }
662
663 private boolean hasEnoughBandwidth(ConnectPoint cp) {
664 if (cp.elementId() instanceof DeviceId) {
Naoki Shiotae1366ad2016-05-11 19:24:11 -0700665 Device device = deviceService.getDevice(cp.deviceId());
666 Device.Type type = device.type();
667
Naoki Shiota5a056062016-05-05 18:43:59 -0700668 if (isTransportLayer(type)) {
Naoki Shiotae1366ad2016-05-11 19:24:11 -0700669 // Check if the port has enough capacity
670 Port port = deviceService.getPort(cp.deviceId(), cp.port());
671 if (port instanceof OduCltPort || port instanceof OchPort) {
672 // Port with capacity
673 return bandwidth.bps() < port.portSpeed() * 1000000.0;
674 } else {
675 // Port without valid capacity (OMS port, etc.)
676 return true;
677 }
678 } else {
679 // Check if enough amount of bandwidth resource remains
680 ContinuousResource resource = Resources.continuous(cp.deviceId(), cp.port(), Bandwidth.class)
681 .resource(bandwidth.bps());
Yuta HIGUCHI806113f2017-03-07 22:46:20 -0800682 try {
683 return resourceService.isAvailable(resource);
684 } catch (Exception e) {
685 log.error("Resource service failed checking availability of {}",
686 resource, e);
687 throw e;
688 }
Naoki Shiota5a056062016-05-05 18:43:59 -0700689 }
Naoki Shiota5a056062016-05-05 18:43:59 -0700690 }
691 return false;
692 }
693 }
694
695
696 public class InternalIntentListener implements IntentListener {
697 @Override
698 public void event(IntentEvent event) {
699 switch (event.type()) {
700 case INSTALLED:
701 log.info("Intent {} installed.", event.subject());
702 updateCrossConnectLink(event.subject());
703 break;
704 case WITHDRAWN:
705 log.info("Intent {} withdrawn.", event.subject());
706 removeCrossConnectLinks(event.subject());
707 break;
708 case FAILED:
709 log.info("Intent {} failed.", event.subject());
Yuta HIGUCHIe80ec2c2017-03-06 14:16:13 -0800710 // TODO If it was one of it's own optical Intent,
711 // update link state
712 // TODO If it was packet P2P Intent, call setupConnectivity
Naoki Shiota5a056062016-05-05 18:43:59 -0700713 break;
714 default:
715 break;
716 }
717 }
718
Yuta HIGUCHIe80ec2c2017-03-06 14:16:13 -0800719 // TODO rename "CrossConnectLink"?
720 /**
721 * Update packet-layer link/port state once Intent is installed.
722 *
723 * @param intent which reached installed state
724 */
Naoki Shiota5a056062016-05-05 18:43:59 -0700725 private void updateCrossConnectLink(Intent intent) {
726 linkPathMap.entrySet().stream()
727 .filter(e -> e.getKey().realizingIntentKey().equals(intent.key()))
728 .forEach(e -> {
729 ConnectPoint packetSrc = e.getKey().src();
730 ConnectPoint packetDst = e.getKey().dst();
731 Bandwidth bw = e.getKey().bandwidth();
Naoki Shiota5a056062016-05-05 18:43:59 -0700732
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700733 // reflect modification only if packetSrc is local_
734 if (mastershipService.isLocalMaster(packetSrc.deviceId())) {
735 // Updates bandwidth of packet ports
Yuta HIGUCHI9b9c7292017-03-07 22:50:04 -0800736 setPortBandwidth(packetSrc, bw);
737 setPortBandwidth(packetDst, bw);
Naoki Shiota5a056062016-05-05 18:43:59 -0700738
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700739 // Updates link status in distributed map
740 linkPathMap.computeIfPresent(e.getKey(), (link, connectivity) ->
741 e.getValue().value().setLinkEstablished(packetSrc, packetDst, true));
Yuta HIGUCHIe80ec2c2017-03-06 14:16:13 -0800742
Yuta HIGUCHI9b9c7292017-03-07 22:50:04 -0800743
744 if (!linkDiscoveryEnabled(packetSrc, packetDst)) {
745 injectLink(packetSrc, packetDst);
746 }
Naoki Shiota5a056062016-05-05 18:43:59 -0700747 }
748 });
749 }
750
751 private void removeCrossConnectLinks(Intent intent) {
752 ConnectPoint src, dst;
753
754 if (intent instanceof OpticalCircuitIntent) {
755 OpticalCircuitIntent circuit = (OpticalCircuitIntent) intent;
756 src = circuit.getSrc();
757 dst = circuit.getDst();
758 } else if (intent instanceof OpticalConnectivityIntent) {
759 OpticalConnectivityIntent conn = (OpticalConnectivityIntent) intent;
760 src = conn.getSrc();
761 dst = conn.getDst();
762 } else {
763 return;
764 }
765
766 removeXcLinkUsage(src);
767 removeXcLinkUsage(dst);
768
769 // Set bandwidth of 0 to cross connect ports
770 Bandwidth bw = Bandwidth.bps(0);
771 linkPathMap.entrySet().stream()
772 .filter(e -> e.getKey().realizingIntentKey().equals(intent.key()))
773 .forEach(e -> {
774 ConnectPoint packetSrc = e.getKey().src();
775 ConnectPoint packetDst = e.getKey().dst();
Naoki Shiota5a056062016-05-05 18:43:59 -0700776
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700777 // reflect modification only if packetSrc is local_
778 if (mastershipService.isLocalMaster(packetSrc.deviceId())) {
779 // Updates bandwidth of packet ports
Yuta HIGUCHI9b9c7292017-03-07 22:50:04 -0800780 setPortBandwidth(packetSrc, bw);
781 setPortBandwidth(packetDst, bw);
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700782
783 // Updates link status in distributed map
784 linkPathMap.computeIfPresent(e.getKey(), (link, connectivity) ->
785 e.getValue().value().setLinkEstablished(packetSrc, packetDst, false));
Yuta HIGUCHIe80ec2c2017-03-06 14:16:13 -0800786
Yuta HIGUCHI9b9c7292017-03-07 22:50:04 -0800787
788 if (!linkDiscoveryEnabled(packetSrc, packetDst)) {
789 removeInjectedLink(packetSrc, packetDst);
790 }
Naoki Shiota5a056062016-05-05 18:43:59 -0700791 }
792 });
793 }
794
795 private void removeXcLinkUsage(ConnectPoint cp) {
796 Optional<Link> link = linkService.getLinks(cp).stream()
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700797 .filter(usedCrossConnectLinkSet::contains)
Naoki Shiota5a056062016-05-05 18:43:59 -0700798 .findAny();
799
800 if (!link.isPresent()) {
801 log.warn("Cross connect point {} has no cross connect link.", cp);
802 return;
803 }
804
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700805 usedCrossConnectLinkSet.remove(link.get());
Naoki Shiota5a056062016-05-05 18:43:59 -0700806 }
Yuta HIGUCHI9b9c7292017-03-07 22:50:04 -0800807
808 /**
809 * Injects link between specified packet port.
810 *
811 * @param packetSrc port 1
812 * @param packetDst port 2
813 */
814 private void injectLink(ConnectPoint packetSrc,
815 ConnectPoint packetDst) {
816 // inject expected link or durable link
817 // if packet device cannot advertise packet link
818 try {
819 BasicLinkConfig lnkCfg = networkConfigService
820 .addConfig(linkKey(packetSrc, packetDst),
821 BasicLinkConfig.class);
822 lnkCfg.isAllowed(true);
823 lnkCfg.isDurable(true);
824 lnkCfg.type(Link.Type.DIRECT);
825 lnkCfg.apply();
826 } catch (Exception ex) {
827 log.error("Applying BasicLinkConfig failed", ex);
828 }
829 }
830
831 /**
832 * Removes link injected between specified packet port.
833 *
834 * @param packetSrc port 1
835 * @param packetDst port 2
836 */
837 private void removeInjectedLink(ConnectPoint packetSrc,
838 ConnectPoint packetDst) {
839 // remove expected link or durable link
840 // if packet device cannot monitor packet link
841
842 try {
843 // hack to mark link off-line
844 BasicLinkConfig lnkCfg = networkConfigService
845 .getConfig(linkKey(packetSrc, packetDst),
846 BasicLinkConfig.class);
847 lnkCfg.isAllowed(false);
848 lnkCfg.apply();
849 } catch (Exception ex) {
850 log.error("Applying BasicLinkConfig failed", ex);
851 }
852
853 networkConfigService
854 .removeConfig(linkKey(packetSrc, packetDst),
855 BasicLinkConfig.class);
856 }
Naoki Shiota5a056062016-05-05 18:43:59 -0700857 }
858
859
860 private class InternalLinkListener implements LinkListener {
861
862 @Override
863 public void event(LinkEvent event) {
864 switch (event.type()) {
865 case LINK_REMOVED:
866 Link link = event.subject();
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700867 // updates linkPathMap only if src device of link is local
868 if (!mastershipService.isLocalMaster(link.src().deviceId())) {
869 return;
870 }
871
872 // find all packet links that correspond to removed link
Naoki Shiota5a056062016-05-05 18:43:59 -0700873 Set<PacketLinkRealizedByOptical> pLinks = linkPathMap.keySet().stream()
874 .filter(l -> l.isBetween(link.src(), link.dst()) || l.isBetween(link.dst(), link.src()))
875 .collect(Collectors.toSet());
876
877 pLinks.forEach(l -> {
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700878 // remove found packet links from distributed store
879 linkPathMap.computeIfPresent(l, (plink, conn) -> {
880 // Notifies listeners if all packet links are gone
881 if (conn.isAllRealizingLinkNotEstablished()) {
882 post(new OpticalPathEvent(OpticalPathEvent.Type.PATH_REMOVED, conn.id()));
883 }
884 return null;
885 });
Naoki Shiota5a056062016-05-05 18:43:59 -0700886 });
887 default:
888 break;
889 }
890 }
891 }
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700892
893 private class InternalStoreListener
894 implements MapEventListener<PacketLinkRealizedByOptical, OpticalConnectivity> {
895
896 @Override
897 public void event(MapEvent<PacketLinkRealizedByOptical, OpticalConnectivity> event) {
898 switch (event.type()) {
899 case UPDATE:
900 OpticalConnectivity oldConnectivity = event.oldValue().value();
901 OpticalConnectivity newConnectivity = event.newValue().value();
902
903 if (!oldConnectivity.isAllRealizingLinkEstablished() &&
904 newConnectivity.isAllRealizingLinkEstablished()) {
905 // Notifies listeners if all links are established
906 updateBandwidthUsage(newConnectivity);
907 post(new OpticalPathEvent(OpticalPathEvent.Type.PATH_INSTALLED, newConnectivity.id()));
908 } else if (!oldConnectivity.isAllRealizingLinkNotEstablished() &&
909 newConnectivity.isAllRealizingLinkNotEstablished()) {
910 // Notifies listeners if all links are gone
911 releaseBandwidthUsage(newConnectivity);
912 post(new OpticalPathEvent(OpticalPathEvent.Type.PATH_REMOVED, newConnectivity.id()));
913 }
914
915 break;
916 default:
917 break;
918 }
919 }
920
921 }
Naoki Shiota5a056062016-05-05 18:43:59 -0700922}
923