Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 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 | package org.onosproject.newoptical; |
| 17 | |
| 18 | import com.google.common.annotations.Beta; |
| 19 | import com.google.common.collect.ImmutableList; |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 20 | import org.apache.commons.lang3.tuple.Pair; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 21 | import org.osgi.service.component.annotations.Activate; |
| 22 | import org.osgi.service.component.annotations.Component; |
| 23 | import org.osgi.service.component.annotations.Deactivate; |
| 24 | import org.osgi.service.component.annotations.Modified; |
| 25 | import org.osgi.service.component.annotations.Reference; |
| 26 | import org.osgi.service.component.annotations.ReferenceCardinality; |
Marc De Leenheer | 552eeb6 | 2017-05-15 17:43:27 -0700 | [diff] [blame] | 27 | import org.onlab.graph.DefaultEdgeWeigher; |
| 28 | import org.onlab.graph.ScalarWeight; |
| 29 | import org.onlab.graph.Weight; |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 30 | import org.onlab.util.Bandwidth; |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 31 | import org.onlab.util.KryoNamespace; |
Marc De Leenheer | 552eeb6 | 2017-05-15 17:43:27 -0700 | [diff] [blame] | 32 | import org.onlab.util.Tools; |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 33 | import org.onosproject.cluster.ClusterService; |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 34 | import org.onosproject.core.ApplicationId; |
| 35 | import org.onosproject.core.CoreService; |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 36 | import org.onosproject.event.AbstractListenerManager; |
Marc De Leenheer | 552eeb6 | 2017-05-15 17:43:27 -0700 | [diff] [blame] | 37 | import org.onosproject.event.ListenerTracker; |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 38 | import org.onosproject.mastership.MastershipService; |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 39 | import org.onosproject.net.ConnectPoint; |
| 40 | import org.onosproject.net.Device; |
| 41 | import org.onosproject.net.DeviceId; |
| 42 | import org.onosproject.net.Link; |
Yuta HIGUCHI | 2103304 | 2017-05-04 17:58:24 -0700 | [diff] [blame] | 43 | import org.onosproject.net.LinkKey; |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 44 | import org.onosproject.net.Path; |
| 45 | import org.onosproject.net.Port; |
| 46 | import org.onosproject.net.config.NetworkConfigService; |
Marc De Leenheer | 552eeb6 | 2017-05-15 17:43:27 -0700 | [diff] [blame] | 47 | import org.onosproject.net.config.basics.BandwidthCapacity; |
| 48 | import org.onosproject.net.config.basics.BasicLinkConfig; |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 49 | import org.onosproject.net.device.DeviceService; |
| 50 | import org.onosproject.net.intent.Intent; |
| 51 | import org.onosproject.net.intent.IntentEvent; |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 52 | import org.onosproject.net.intent.IntentListener; |
| 53 | import org.onosproject.net.intent.IntentService; |
Yuta HIGUCHI | 8ac0509 | 2017-03-15 00:41:22 -0700 | [diff] [blame] | 54 | import org.onosproject.net.intent.Key; |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 55 | import org.onosproject.net.intent.OpticalCircuitIntent; |
| 56 | import org.onosproject.net.intent.OpticalConnectivityIntent; |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 57 | import org.onosproject.net.link.LinkEvent; |
| 58 | import org.onosproject.net.link.LinkListener; |
| 59 | import org.onosproject.net.link.LinkService; |
Marc De Leenheer | 552eeb6 | 2017-05-15 17:43:27 -0700 | [diff] [blame] | 60 | import org.onosproject.net.optical.OchPort; |
| 61 | import org.onosproject.net.optical.OduCltPort; |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 62 | import org.onosproject.net.resource.ContinuousResource; |
| 63 | import org.onosproject.net.resource.Resource; |
| 64 | import org.onosproject.net.resource.ResourceAllocation; |
| 65 | import org.onosproject.net.resource.ResourceService; |
| 66 | import org.onosproject.net.resource.Resources; |
Marc De Leenheer | 552eeb6 | 2017-05-15 17:43:27 -0700 | [diff] [blame] | 67 | import org.onosproject.net.topology.LinkWeigher; |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 68 | import org.onosproject.net.topology.TopologyEdge; |
Marc De Leenheer | 552eeb6 | 2017-05-15 17:43:27 -0700 | [diff] [blame] | 69 | import org.onosproject.net.topology.TopologyService; |
| 70 | import org.onosproject.net.topology.TopologyVertex; |
| 71 | import org.onosproject.newoptical.api.OpticalConnectivityId; |
| 72 | import org.onosproject.newoptical.api.OpticalPathEvent; |
| 73 | import org.onosproject.newoptical.api.OpticalPathListener; |
| 74 | import org.onosproject.newoptical.api.OpticalPathService; |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 75 | import org.onosproject.store.serializers.KryoNamespaces; |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 76 | import org.onosproject.store.service.AtomicCounter; |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 77 | import org.onosproject.store.service.ConsistentMap; |
| 78 | import org.onosproject.store.service.DistributedSet; |
| 79 | import org.onosproject.store.service.MapEvent; |
| 80 | import org.onosproject.store.service.MapEventListener; |
| 81 | import org.onosproject.store.service.Serializer; |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 82 | import org.onosproject.store.service.StorageService; |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 83 | import org.onosproject.store.service.Versioned; |
Marc De Leenheer | 552eeb6 | 2017-05-15 17:43:27 -0700 | [diff] [blame] | 84 | import org.osgi.service.component.ComponentContext; |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 85 | import org.slf4j.Logger; |
| 86 | import org.slf4j.LoggerFactory; |
| 87 | |
| 88 | import java.time.Duration; |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 89 | import java.util.ArrayList; |
Yuta HIGUCHI | f167bf9 | 2017-03-08 13:19:04 -0800 | [diff] [blame] | 90 | import java.util.Collection; |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 91 | import java.util.Collections; |
Marc De Leenheer | 552eeb6 | 2017-05-15 17:43:27 -0700 | [diff] [blame] | 92 | import java.util.Dictionary; |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 93 | import java.util.HashMap; |
| 94 | import java.util.HashSet; |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 95 | import java.util.Iterator; |
| 96 | import java.util.LinkedList; |
| 97 | import java.util.List; |
| 98 | import java.util.Map; |
Yuta HIGUCHI | 8ac0509 | 2017-03-15 00:41:22 -0700 | [diff] [blame] | 99 | import java.util.Map.Entry; |
Marc De Leenheer | 552eeb6 | 2017-05-15 17:43:27 -0700 | [diff] [blame] | 100 | import java.util.Objects; |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 101 | import java.util.Optional; |
| 102 | import java.util.Set; |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 103 | import java.util.stream.Collectors; |
| 104 | import java.util.stream.Stream; |
| 105 | |
| 106 | import static com.google.common.base.Preconditions.checkArgument; |
| 107 | import static com.google.common.base.Preconditions.checkNotNull; |
Yuta HIGUCHI | 9b9c729 | 2017-03-07 22:50:04 -0800 | [diff] [blame] | 108 | import static org.onosproject.net.LinkKey.linkKey; |
Naoki Shiota | e1366ad | 2016-05-11 19:24:11 -0700 | [diff] [blame] | 109 | import static org.onosproject.net.optical.device.OpticalDeviceServiceView.opticalView; |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 110 | |
| 111 | /** |
| 112 | * Main component to configure optical connectivity. |
| 113 | */ |
| 114 | @Beta |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 115 | @Component(immediate = true, service = OpticalPathService.class) |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 116 | public class OpticalPathProvisioner |
| 117 | extends AbstractListenerManager<OpticalPathEvent, OpticalPathListener> |
| 118 | implements OpticalPathService { |
Yuta HIGUCHI | 9b9c729 | 2017-03-07 22:50:04 -0800 | [diff] [blame] | 119 | |
Ray Milkey | 9c9cde4 | 2018-01-12 14:22:06 -0800 | [diff] [blame] | 120 | private static final Logger log = LoggerFactory.getLogger(OpticalPathProvisioner.class); |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 121 | |
Yuta HIGUCHI | 9b9c729 | 2017-03-07 22:50:04 -0800 | [diff] [blame] | 122 | /** |
| 123 | * Bandwidth representing no bandwidth requirement specified. |
| 124 | */ |
| 125 | private static final Bandwidth NO_BW_REQUIREMENT = Bandwidth.bps(0); |
| 126 | |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 127 | private static final String OPTICAL_CONNECTIVITY_ID_COUNTER = "optical-connectivity-id"; |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 128 | private static final String LINKPATH_MAP_NAME = "newoptical-linkpath"; |
| 129 | private static final String CONNECTIVITY_MAP_NAME = "newoptical-connectivity"; |
| 130 | private static final String CROSSCONNECTLINK_SET_NAME = "newoptical-crossconnectlink"; |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 131 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 132 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 133 | protected IntentService intentService; |
| 134 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 135 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Marc De Leenheer | 552eeb6 | 2017-05-15 17:43:27 -0700 | [diff] [blame] | 136 | protected TopologyService topologyService; |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 137 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 138 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 139 | protected CoreService coreService; |
| 140 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 141 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 142 | protected LinkService linkService; |
| 143 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 144 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 145 | protected MastershipService mastershipService; |
| 146 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 147 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 148 | protected ClusterService clusterService; |
| 149 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 150 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 151 | protected DeviceService deviceService; |
| 152 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 153 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 154 | protected StorageService storageService; |
| 155 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 156 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 157 | protected NetworkConfigService networkConfigService; |
| 158 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 159 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 160 | protected ResourceService resourceService; |
| 161 | |
Marc De Leenheer | 552eeb6 | 2017-05-15 17:43:27 -0700 | [diff] [blame] | 162 | private static final String MAX_PATHS = "maxPaths"; |
| 163 | private static final int DEFAULT_MAX_PATHS = 10; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 164 | //@Property(name = MAX_PATHS, intValue = DEFAULT_MAX_PATHS, |
| 165 | // label = "Maximum number of paths to consider for path provisioning") |
Marc De Leenheer | 552eeb6 | 2017-05-15 17:43:27 -0700 | [diff] [blame] | 166 | private int maxPaths = DEFAULT_MAX_PATHS; |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 167 | |
| 168 | private ApplicationId appId; |
| 169 | |
| 170 | private AtomicCounter idCounter; |
| 171 | |
Naoki Shiota | 0d2943e | 2016-05-13 18:53:21 -0700 | [diff] [blame] | 172 | private ListenerTracker listeners; |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 173 | |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 174 | private InternalStoreListener storeListener = new InternalStoreListener(); |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 175 | |
Yuta HIGUCHI | e80ec2c | 2017-03-06 14:16:13 -0800 | [diff] [blame] | 176 | /** |
| 177 | * Map from packet-layer link expected to be realized by some optical Intent to |
| 178 | * OpticalConnectivity (~=top level intent over multi-layer topology). |
| 179 | */ |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 180 | private ConsistentMap<PacketLinkRealizedByOptical, OpticalConnectivity> linkPathMap; |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 181 | |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 182 | private ConsistentMap<OpticalConnectivityId, OpticalConnectivity> connectivityMap; |
| 183 | |
Yuta HIGUCHI | e80ec2c | 2017-03-06 14:16:13 -0800 | [diff] [blame] | 184 | // FIXME in the long run. This is effectively app's own resource subsystem |
| 185 | /** |
| 186 | * Set of cross connect link currently used. |
| 187 | */ |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 188 | private DistributedSet<Link> usedCrossConnectLinkSet; |
| 189 | |
| 190 | private static final KryoNamespace.Builder LINKPATH_SERIALIZER = KryoNamespace.newBuilder() |
| 191 | .register(KryoNamespaces.API) |
| 192 | .register(PacketLinkRealizedByOptical.class) |
| 193 | .register(OpticalConnectivityId.class) |
| 194 | .register(OpticalConnectivity.class); |
| 195 | |
| 196 | private static final KryoNamespace.Builder CONNECTIVITY_SERIALIZER = KryoNamespace.newBuilder() |
| 197 | .register(KryoNamespaces.API) |
Yuta HIGUCHI | 0086cf8 | 2016-07-18 22:49:45 -0700 | [diff] [blame] | 198 | .register(PacketLinkRealizedByOptical.class) |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 199 | .register(OpticalConnectivityId.class) |
| 200 | .register(OpticalConnectivity.class); |
| 201 | |
| 202 | private static final KryoNamespace.Builder CROSSCONNECTLINKS_SERIALIZER = KryoNamespace.newBuilder() |
| 203 | .register(KryoNamespaces.API); |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 204 | |
| 205 | @Activate |
Marc De Leenheer | 552eeb6 | 2017-05-15 17:43:27 -0700 | [diff] [blame] | 206 | protected void activate(ComponentContext context) { |
Naoki Shiota | e1366ad | 2016-05-11 19:24:11 -0700 | [diff] [blame] | 207 | deviceService = opticalView(deviceService); |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 208 | appId = coreService.registerApplication("org.onosproject.newoptical"); |
| 209 | |
Naoki Shiota | 03c29e1 | 2016-05-16 16:58:07 -0700 | [diff] [blame] | 210 | idCounter = storageService.getAtomicCounter(OPTICAL_CONNECTIVITY_ID_COUNTER); |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 211 | |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 212 | linkPathMap = storageService.<PacketLinkRealizedByOptical, OpticalConnectivity>consistentMapBuilder() |
| 213 | .withSerializer(Serializer.using(LINKPATH_SERIALIZER.build())) |
| 214 | .withName(LINKPATH_MAP_NAME) |
| 215 | .withApplicationId(appId) |
| 216 | .build(); |
| 217 | |
| 218 | connectivityMap = storageService.<OpticalConnectivityId, OpticalConnectivity>consistentMapBuilder() |
| 219 | .withSerializer(Serializer.using(CONNECTIVITY_SERIALIZER.build())) |
| 220 | .withName(CONNECTIVITY_MAP_NAME) |
| 221 | .withApplicationId(appId) |
| 222 | .build(); |
| 223 | |
| 224 | usedCrossConnectLinkSet = storageService.<Link>setBuilder() |
| 225 | .withSerializer(Serializer.using(CROSSCONNECTLINKS_SERIALIZER.build())) |
| 226 | .withName(CROSSCONNECTLINK_SET_NAME) |
| 227 | .withApplicationId(appId) |
| 228 | .build() |
| 229 | .asDistributedSet(); |
| 230 | |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 231 | eventDispatcher.addSink(OpticalPathEvent.class, listenerRegistry); |
Naoki Shiota | 0d2943e | 2016-05-13 18:53:21 -0700 | [diff] [blame] | 232 | |
| 233 | listeners = new ListenerTracker(); |
| 234 | listeners.addListener(linkService, new InternalLinkListener()) |
| 235 | .addListener(intentService, new InternalIntentListener()); |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 236 | |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 237 | linkPathMap.addListener(storeListener); |
| 238 | |
Marc De Leenheer | 552eeb6 | 2017-05-15 17:43:27 -0700 | [diff] [blame] | 239 | readComponentConfiguration(context); |
| 240 | |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 241 | log.info("Started"); |
| 242 | } |
| 243 | |
| 244 | @Deactivate |
| 245 | protected void deactivate() { |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 246 | linkPathMap.removeListener(storeListener); |
Naoki Shiota | 0d2943e | 2016-05-13 18:53:21 -0700 | [diff] [blame] | 247 | listeners.removeListeners(); |
| 248 | eventDispatcher.removeSink(OpticalPathEvent.class); |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 249 | |
| 250 | log.info("Stopped"); |
| 251 | } |
| 252 | |
Marc De Leenheer | 552eeb6 | 2017-05-15 17:43:27 -0700 | [diff] [blame] | 253 | @Modified |
| 254 | public void modified(ComponentContext context) { |
| 255 | readComponentConfiguration(context); |
| 256 | } |
| 257 | |
| 258 | /** |
| 259 | * Extracts properties from the component configuration context. |
| 260 | * |
| 261 | * @param context the component context |
| 262 | */ |
| 263 | private void readComponentConfiguration(ComponentContext context) { |
| 264 | Dictionary<?, ?> properties = context.getProperties(); |
| 265 | maxPaths = Tools.getIntegerProperty(properties, MAX_PATHS, DEFAULT_MAX_PATHS); |
| 266 | log.info("Configured. Maximum paths to consider is configured to {}", maxPaths); |
| 267 | } |
| 268 | |
Yuta HIGUCHI | f167bf9 | 2017-03-08 13:19:04 -0800 | [diff] [blame] | 269 | @Override |
| 270 | public Collection<OpticalConnectivity> listConnectivity() { |
| 271 | return connectivityMap.values().stream() |
| 272 | .map(Versioned::value) |
Yuta HIGUCHI | 498fa1d | 2017-05-17 16:08:40 -0700 | [diff] [blame] | 273 | .collect(ImmutableList.toImmutableList()); |
Yuta HIGUCHI | f167bf9 | 2017-03-08 13:19:04 -0800 | [diff] [blame] | 274 | } |
Yuta HIGUCHI | 8ac0509 | 2017-03-15 00:41:22 -0700 | [diff] [blame] | 275 | |
Yuta HIGUCHI | 2a77273 | 2017-05-01 20:14:33 -0700 | [diff] [blame] | 276 | @Override |
Yuta HIGUCHI | 8ac0509 | 2017-03-15 00:41:22 -0700 | [diff] [blame] | 277 | public Set<Key> listIntents(OpticalConnectivityId id) { |
| 278 | return linkPathMap.entrySet().stream() |
| 279 | .filter(ent -> id.equals(ent.getValue().value().id())) |
| 280 | .map(Entry::getKey) |
| 281 | .map(PacketLinkRealizedByOptical::realizingIntentKey) |
| 282 | .collect(Collectors.toSet()); |
| 283 | } |
| 284 | |
Yuta HIGUCHI | e80ec2c | 2017-03-06 14:16:13 -0800 | [diff] [blame] | 285 | /* |
| 286 | * Request packet-layer connectivity between specified ports, |
| 287 | * over packet-optical multi-layer infrastructure. |
| 288 | * |
| 289 | * Functionality-wise this is effectively submitting Packet-Optical |
| 290 | * multi-layer P2P Intent. |
| 291 | * |
| 292 | * It computes multi-layer path meeting specified constraint, |
| 293 | * and calls setupPath. |
| 294 | */ |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 295 | @Override |
| 296 | public OpticalConnectivityId setupConnectivity(ConnectPoint ingress, ConnectPoint egress, |
| 297 | Bandwidth bandwidth, Duration latency) { |
| 298 | checkNotNull(ingress); |
| 299 | checkNotNull(egress); |
| 300 | log.info("setupConnectivity({}, {}, {}, {})", ingress, egress, bandwidth, latency); |
| 301 | |
Marc De Leenheer | 552eeb6 | 2017-05-15 17:43:27 -0700 | [diff] [blame] | 302 | Bandwidth bw = (bandwidth == null) ? NO_BW_REQUIREMENT : bandwidth; |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 303 | |
Marc De Leenheer | 552eeb6 | 2017-05-15 17:43:27 -0700 | [diff] [blame] | 304 | Stream<Path> paths = topologyService.getKShortestPaths( |
| 305 | topologyService.currentTopology(), |
| 306 | ingress.deviceId(), egress.deviceId(), |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 307 | new BandwidthLinkWeight(bandwidth)); |
Marc De Leenheer | 552eeb6 | 2017-05-15 17:43:27 -0700 | [diff] [blame] | 308 | |
| 309 | // Path service calculates from node to node, we're only interested in port to port |
| 310 | Optional<OpticalConnectivityId> id = |
| 311 | paths.filter(p -> p.src().equals(ingress) && p.dst().equals(egress)) |
| 312 | .limit(maxPaths) |
| 313 | .map(p -> setupPath(p, bw, latency)) |
| 314 | .filter(Objects::nonNull) |
| 315 | .findFirst(); |
| 316 | |
| 317 | if (id.isPresent()) { |
| 318 | log.info("Assigned OpticalConnectivityId: {}", id); |
| 319 | } else { |
| 320 | log.error("setupConnectivity({}, {}, {}, {}) failed.", ingress, egress, bandwidth, latency); |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 321 | } |
| 322 | |
Marc De Leenheer | 552eeb6 | 2017-05-15 17:43:27 -0700 | [diff] [blame] | 323 | return id.orElse(null); |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 324 | } |
| 325 | |
Yuta HIGUCHI | e80ec2c | 2017-03-06 14:16:13 -0800 | [diff] [blame] | 326 | /* |
| 327 | * Given a multi-layer path, |
| 328 | * compute a set of segments which requires |
| 329 | * OpticalConnectivity(~=OpticalConnectivityIntent or OpticalCircuitPath) |
| 330 | * to provide packet-layer connectivity. |
| 331 | */ |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 332 | @Override |
| 333 | public OpticalConnectivityId setupPath(Path path, Bandwidth bandwidth, Duration latency) { |
| 334 | checkNotNull(path); |
Yuta HIGUCHI | 8fc32f8 | 2017-03-14 22:12:42 -0700 | [diff] [blame] | 335 | log.debug("setupPath({}, {}, {})", path, bandwidth, latency); |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 336 | |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 337 | // map of cross connect points (optical port -> packet port) |
| 338 | Map<ConnectPoint, ConnectPoint> crossConnectPointMap = new HashMap<>(); |
| 339 | |
| 340 | // list of (src, dst) pair of optical ports between which optical path should be installed |
| 341 | List<Pair<ConnectPoint, ConnectPoint>> crossConnectPoints = new ArrayList<>(); |
| 342 | |
| 343 | // Scan path to find pairs of connect points between which optical intent is installed |
| 344 | // opticalSrcPort works as a flag parameter to show scanning status |
| 345 | ConnectPoint opticalSrcPort = null; |
| 346 | for (Link link : path.links()) { |
| 347 | if (!isCrossConnectLink(link)) { |
| 348 | continue; |
| 349 | } |
| 350 | |
| 351 | if (opticalSrcPort != null) { |
| 352 | // opticalSrcPort!=null means src port was already found |
| 353 | // in this case link.src() is optical layer, and link.dst() is packet layer |
| 354 | |
| 355 | // Check if types of src port and dst port matches |
| 356 | Device srcDevice = checkNotNull(deviceService.getDevice(opticalSrcPort.deviceId()), |
| 357 | "Unknown device ID"); |
| 358 | Device dstDevice = checkNotNull(deviceService.getDevice(link.src().deviceId()), |
| 359 | "Unknown device ID"); |
| 360 | if (srcDevice.type() != dstDevice.type()) { |
| 361 | log.error("Unsupported mix of cross connect points : {}, {}", |
| 362 | srcDevice.type(), dstDevice.type()); |
| 363 | return null; |
| 364 | } |
| 365 | |
| 366 | // Update cross connect points map |
| 367 | crossConnectPointMap.put(link.src(), link.dst()); |
| 368 | |
| 369 | // Add optical ports pair to list |
| 370 | crossConnectPoints.add(Pair.of(opticalSrcPort, link.src())); |
| 371 | |
| 372 | // Reset flag parameter |
| 373 | opticalSrcPort = null; |
| 374 | } else { |
| 375 | // opticalSrcPort==null means src port was not found yet |
| 376 | // in this case link.src() is packet layer, and link.dst() is optical layer |
| 377 | |
| 378 | // Update cross connect points map |
| 379 | crossConnectPointMap.put(link.dst(), link.src()); |
| 380 | // Set opticalSrcPort to src of link (optical port) |
| 381 | opticalSrcPort = link.dst(); |
| 382 | } |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 383 | } |
| 384 | |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 385 | // create intents from cross connect points |
| 386 | List<Intent> intents = createIntents(crossConnectPoints); |
Yuta HIGUCHI | d2f041e | 2017-03-10 12:45:15 -0800 | [diff] [blame] | 387 | if (intents.isEmpty()) { |
| 388 | log.error("No intents produced from {}", crossConnectPoints); |
| 389 | return null; |
| 390 | } |
| 391 | |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 392 | // create set of PacketLinkRealizedByOptical |
| 393 | Set<PacketLinkRealizedByOptical> packetLinks = createPacketLinkSet(crossConnectPoints, |
| 394 | intents, crossConnectPointMap); |
| 395 | |
| 396 | // create OpticalConnectivity object and store information to distributed store |
| 397 | OpticalConnectivity connectivity = createConnectivity(path, bandwidth, latency, packetLinks); |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 398 | |
| 399 | // store cross connect port usage |
| 400 | path.links().stream().filter(this::isCrossConnectLink) |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 401 | .forEach(usedCrossConnectLinkSet::add); |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 402 | |
| 403 | // Submit the intents |
| 404 | for (Intent i : intents) { |
| 405 | intentService.submit(i); |
| 406 | log.debug("Submitted an intent: {}", i); |
| 407 | } |
| 408 | |
| 409 | return connectivity.id(); |
| 410 | } |
| 411 | |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 412 | private OpticalConnectivity createConnectivity(Path path, Bandwidth bandwidth, Duration latency, |
| 413 | Set<PacketLinkRealizedByOptical> links) { |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 414 | OpticalConnectivityId id = OpticalConnectivityId.of(idCounter.getAndIncrement()); |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 415 | OpticalConnectivity connectivity = new OpticalConnectivity(id, path.links(), bandwidth, latency, |
| 416 | links, Collections.emptySet()); |
| 417 | |
| 418 | links.forEach(l -> linkPathMap.put(l, connectivity)); |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 419 | |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 420 | // store connectivity information |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 421 | connectivityMap.put(connectivity.id(), connectivity); |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 422 | |
| 423 | return connectivity; |
| 424 | } |
| 425 | |
| 426 | @Override |
| 427 | public boolean removeConnectivity(OpticalConnectivityId id) { |
| 428 | log.info("removeConnectivity({})", id); |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 429 | Versioned<OpticalConnectivity> connectivity = connectivityMap.remove(id); |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 430 | |
| 431 | if (connectivity == null) { |
Naoki Shiota | 0d2943e | 2016-05-13 18:53:21 -0700 | [diff] [blame] | 432 | log.info("OpticalConnectivity with id {} not found.", id); |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 433 | return false; |
| 434 | } |
| 435 | |
| 436 | // TODO withdraw intent only if all of connectivities that use the optical path are withdrawn |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 437 | connectivity.value().getRealizingLinks().forEach(l -> { |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 438 | Intent intent = intentService.getIntent(l.realizingIntentKey()); |
| 439 | intentService.withdraw(intent); |
| 440 | }); |
| 441 | |
| 442 | return true; |
| 443 | } |
| 444 | |
| 445 | @Override |
Naoki Shiota | 0d2943e | 2016-05-13 18:53:21 -0700 | [diff] [blame] | 446 | public Optional<List<Link>> getPath(OpticalConnectivityId id) { |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 447 | Versioned<OpticalConnectivity> connectivity = connectivityMap.get(id); |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 448 | if (connectivity == null) { |
Naoki Shiota | 0d2943e | 2016-05-13 18:53:21 -0700 | [diff] [blame] | 449 | log.info("OpticalConnectivity with id {} not found.", id); |
| 450 | return Optional.empty(); |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 451 | } |
| 452 | |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 453 | return Optional.of(ImmutableList.copyOf(connectivity.value().links())); |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 454 | } |
| 455 | |
| 456 | /** |
| 457 | * Scans the list of cross connection points and returns a list of optical connectivity intents. |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 458 | * During the process, save information about packet links to given set. |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 459 | * |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 460 | * @param crossConnectPoints list of (src, dst) pair between which optical path will be set up |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 461 | * @return list of optical connectivity intents |
| 462 | */ |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 463 | private List<Intent> createIntents(List<Pair<ConnectPoint, ConnectPoint>> crossConnectPoints) { |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 464 | List<Intent> intents = new LinkedList<>(); |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 465 | Iterator<Pair<ConnectPoint, ConnectPoint>> itr = crossConnectPoints.iterator(); |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 466 | |
| 467 | while (itr.hasNext()) { |
| 468 | // checkArgument at start ensures we'll always have pairs of connect points |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 469 | Pair<ConnectPoint, ConnectPoint> next = itr.next(); |
| 470 | ConnectPoint src = next.getLeft(); |
| 471 | ConnectPoint dst = next.getRight(); |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 472 | |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 473 | Port srcPort = deviceService.getPort(src.deviceId(), src.port()); |
| 474 | Port dstPort = deviceService.getPort(dst.deviceId(), dst.port()); |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 475 | |
| 476 | if (srcPort instanceof OduCltPort && dstPort instanceof OduCltPort) { |
Naoki Shiota | e1366ad | 2016-05-11 19:24:11 -0700 | [diff] [blame] | 477 | OduCltPort srcOCPort = (OduCltPort) srcPort; |
| 478 | OduCltPort dstOCPort = (OduCltPort) dstPort; |
| 479 | if (!srcOCPort.signalType().equals(dstOCPort.signalType())) { |
| 480 | continue; |
| 481 | } |
| 482 | |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 483 | // Create OTN circuit |
| 484 | OpticalCircuitIntent circuitIntent = OpticalCircuitIntent.builder() |
| 485 | .appId(appId) |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 486 | .src(src) |
| 487 | .dst(dst) |
Naoki Shiota | e1366ad | 2016-05-11 19:24:11 -0700 | [diff] [blame] | 488 | .signalType(srcOCPort.signalType()) |
Marc De Leenheer | 43b91ad | 2017-03-17 14:45:28 -0700 | [diff] [blame] | 489 | .bidirectional(false) |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 490 | .build(); |
| 491 | intents.add(circuitIntent); |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 492 | } else if (srcPort instanceof OchPort && dstPort instanceof OchPort) { |
Naoki Shiota | e1366ad | 2016-05-11 19:24:11 -0700 | [diff] [blame] | 493 | OchPort srcOchPort = (OchPort) srcPort; |
| 494 | OchPort dstOchPort = (OchPort) dstPort; |
| 495 | if (!srcOchPort.signalType().equals(dstOchPort.signalType())) { |
| 496 | continue; |
| 497 | } |
| 498 | |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 499 | // Create lightpath |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 500 | OpticalConnectivityIntent opticalIntent = OpticalConnectivityIntent.builder() |
| 501 | .appId(appId) |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 502 | .src(src) |
| 503 | .dst(dst) |
Naoki Shiota | e1366ad | 2016-05-11 19:24:11 -0700 | [diff] [blame] | 504 | .signalType(srcOchPort.signalType()) |
Marc De Leenheer | 43b91ad | 2017-03-17 14:45:28 -0700 | [diff] [blame] | 505 | .bidirectional(false) |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 506 | .build(); |
| 507 | intents.add(opticalIntent); |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 508 | } else { |
| 509 | log.warn("Unsupported cross connect point types {} {}", srcPort.type(), dstPort.type()); |
| 510 | return Collections.emptyList(); |
| 511 | } |
| 512 | } |
| 513 | |
| 514 | return intents; |
| 515 | } |
| 516 | |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 517 | private Set<PacketLinkRealizedByOptical> createPacketLinkSet(List<Pair<ConnectPoint, ConnectPoint>> connectPoints, |
| 518 | List<Intent> intents, |
| 519 | Map<ConnectPoint, ConnectPoint> crossConnectPoints) { |
| 520 | checkArgument(connectPoints.size() == intents.size()); |
| 521 | |
| 522 | Set<PacketLinkRealizedByOptical> pLinks = new HashSet<>(); |
| 523 | |
| 524 | Iterator<Pair<ConnectPoint, ConnectPoint>> xcPointsItr = connectPoints.iterator(); |
| 525 | Iterator<Intent> intentItr = intents.iterator(); |
| 526 | while (xcPointsItr.hasNext()) { |
| 527 | Pair<ConnectPoint, ConnectPoint> xcPoints = xcPointsItr.next(); |
| 528 | Intent intent = intentItr.next(); |
| 529 | |
| 530 | ConnectPoint packetSrc = checkNotNull(crossConnectPoints.get(xcPoints.getLeft())); |
| 531 | ConnectPoint packetDst = checkNotNull(crossConnectPoints.get(xcPoints.getRight())); |
| 532 | |
| 533 | if (intent instanceof OpticalConnectivityIntent) { |
| 534 | pLinks.add(PacketLinkRealizedByOptical.create(packetSrc, packetDst, |
| 535 | (OpticalConnectivityIntent) intent)); |
| 536 | } else if (intent instanceof OpticalCircuitIntent) { |
| 537 | pLinks.add(PacketLinkRealizedByOptical.create(packetSrc, packetDst, |
| 538 | (OpticalCircuitIntent) intent)); |
| 539 | } else { |
| 540 | log.warn("Unexpected intent type: {}", intent.getClass()); |
| 541 | } |
| 542 | } |
| 543 | |
| 544 | return pLinks; |
| 545 | } |
| 546 | |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 547 | /** |
| 548 | * Verifies if given device type is in packet layer, i.e., ROADM, OTN or ROADM_OTN device. |
| 549 | * |
| 550 | * @param type device type |
| 551 | * @return true if in packet layer, false otherwise |
| 552 | */ |
| 553 | private boolean isPacketLayer(Device.Type type) { |
| 554 | return type == Device.Type.SWITCH || type == Device.Type.ROUTER || type == Device.Type.VIRTUAL; |
| 555 | } |
| 556 | |
| 557 | /** |
Yuta HIGUCHI | d2f041e | 2017-03-10 12:45:15 -0800 | [diff] [blame] | 558 | * Verifies if given device type is NOT in packet layer, i.e., switch or router device. |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 559 | * |
| 560 | * @param type device type |
| 561 | * @return true if in packet layer, false otherwise |
| 562 | */ |
| 563 | private boolean isTransportLayer(Device.Type type) { |
| 564 | return type == Device.Type.ROADM || type == Device.Type.OTN || type == Device.Type.ROADM_OTN; |
| 565 | } |
| 566 | |
| 567 | /** |
| 568 | * Verifies if given link forms a cross-connection between packet and optical layer. |
| 569 | * |
| 570 | * @param link the link |
| 571 | * @return true if the link is a cross-connect link, false otherwise |
| 572 | */ |
| 573 | private boolean isCrossConnectLink(Link link) { |
| 574 | if (link.type() != Link.Type.OPTICAL) { |
| 575 | return false; |
| 576 | } |
| 577 | |
| 578 | Device.Type src = deviceService.getDevice(link.src().deviceId()).type(); |
| 579 | Device.Type dst = deviceService.getDevice(link.dst().deviceId()).type(); |
| 580 | |
| 581 | return src != dst && |
| 582 | ((isPacketLayer(src) && isTransportLayer(dst)) || (isPacketLayer(dst) && isTransportLayer(src))); |
| 583 | } |
| 584 | |
| 585 | /** |
Yuta HIGUCHI | 9b9c729 | 2017-03-07 22:50:04 -0800 | [diff] [blame] | 586 | * Updates bandwidth resource of given connect point to specified value. |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 587 | * |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 588 | * @param cp Connect point |
| 589 | * @param bandwidth New bandwidth |
| 590 | */ |
Yuta HIGUCHI | 9b9c729 | 2017-03-07 22:50:04 -0800 | [diff] [blame] | 591 | private void setPortBandwidth(ConnectPoint cp, Bandwidth bandwidth) { |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 592 | log.debug("update Port {} Bandwidth {}", cp, bandwidth); |
| 593 | BandwidthCapacity bwCapacity = networkConfigService.addConfig(cp, BandwidthCapacity.class); |
| 594 | bwCapacity.capacity(bandwidth).apply(); |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 595 | } |
| 596 | |
| 597 | /** |
| 598 | * Updates usage information of bandwidth based on connectivity which is established. |
| 599 | * @param connectivity Optical connectivity |
| 600 | */ |
| 601 | private void updateBandwidthUsage(OpticalConnectivity connectivity) { |
Yuta HIGUCHI | 9b9c729 | 2017-03-07 22:50:04 -0800 | [diff] [blame] | 602 | if (NO_BW_REQUIREMENT.equals(connectivity.bandwidth())) { |
| 603 | // no bandwidth requirement, nothing to allocate. |
| 604 | return; |
| 605 | } |
| 606 | |
Naoki Shiota | cb744db | 2016-05-13 19:32:35 -0700 | [diff] [blame] | 607 | OpticalConnectivityId connectivityId = connectivity.id(); |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 608 | |
| 609 | List<Link> links = connectivity.links(); |
| 610 | |
| 611 | List<Resource> resources = links.stream().flatMap(l -> Stream.of(l.src(), l.dst())) |
| 612 | .filter(cp -> !isTransportLayer(deviceService.getDevice(cp.deviceId()).type())) |
| 613 | .map(cp -> Resources.continuous(cp.deviceId(), cp.port(), |
| 614 | Bandwidth.class).resource(connectivity.bandwidth().bps())) |
| 615 | .collect(Collectors.toList()); |
| 616 | |
Naoki Shiota | cb744db | 2016-05-13 19:32:35 -0700 | [diff] [blame] | 617 | log.debug("allocating bandwidth for {} : {}", connectivityId, resources); |
| 618 | List<ResourceAllocation> allocations = resourceService.allocate(connectivityId, resources); |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 619 | if (allocations.isEmpty()) { |
| 620 | log.warn("Failed to allocate bandwidth {} to {}", |
| 621 | connectivity.bandwidth().bps(), resources); |
| 622 | // TODO any recovery? |
| 623 | } |
Naoki Shiota | cb744db | 2016-05-13 19:32:35 -0700 | [diff] [blame] | 624 | log.debug("Done allocating bandwidth for {}", connectivityId); |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 625 | } |
| 626 | |
| 627 | /** |
| 628 | * Release bandwidth allocated by given connectivity. |
| 629 | * @param connectivity Optical connectivity |
| 630 | */ |
| 631 | private void releaseBandwidthUsage(OpticalConnectivity connectivity) { |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 632 | if (connectivity.links().isEmpty()) { |
| 633 | return; |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 634 | } |
Yuta HIGUCHI | 9b9c729 | 2017-03-07 22:50:04 -0800 | [diff] [blame] | 635 | if (NO_BW_REQUIREMENT.equals(connectivity.bandwidth())) { |
| 636 | // no bandwidth requirement, nothing to release. |
| 637 | return; |
| 638 | } |
| 639 | |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 640 | |
| 641 | // release resource only if this node is the master for link head device |
| 642 | if (mastershipService.isLocalMaster(connectivity.links().get(0).src().deviceId())) { |
| 643 | OpticalConnectivityId connectivityId = connectivity.id(); |
| 644 | |
| 645 | log.debug("releasing bandwidth allocated to {}", connectivityId); |
| 646 | if (!resourceService.release(connectivityId)) { |
| 647 | log.warn("Failed to release bandwidth allocated to {}", |
| 648 | connectivityId); |
| 649 | // TODO any recovery? |
| 650 | } |
| 651 | log.debug("DONE releasing bandwidth for {}", connectivityId); |
| 652 | } |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 653 | } |
| 654 | |
Yuta HIGUCHI | 9b9c729 | 2017-03-07 22:50:04 -0800 | [diff] [blame] | 655 | private boolean linkDiscoveryEnabled(ConnectPoint cp) { |
| 656 | // FIXME should check Device feature and configuration state. |
| 657 | |
| 658 | // short-term hack for ONS'17 time-frame, |
| 659 | // only expect OF device to have link discovery. |
Jon Hall | a3fcf67 | 2017-03-28 16:53:22 -0700 | [diff] [blame] | 660 | return "of".equals(cp.deviceId().uri().getScheme()); |
Yuta HIGUCHI | 9b9c729 | 2017-03-07 22:50:04 -0800 | [diff] [blame] | 661 | } |
| 662 | |
| 663 | /** |
| 664 | * Returns true if both connect point support for link discovery & enabled. |
| 665 | * |
| 666 | * @param cp1 port 1 |
| 667 | * @param cp2 port 2 |
| 668 | * @return true if both connect point support for link discovery & enabled. |
| 669 | */ |
| 670 | private boolean linkDiscoveryEnabled(ConnectPoint cp1, ConnectPoint cp2) { |
| 671 | return linkDiscoveryEnabled(cp1) && linkDiscoveryEnabled(cp2); |
| 672 | } |
| 673 | |
Marc De Leenheer | 552eeb6 | 2017-05-15 17:43:27 -0700 | [diff] [blame] | 674 | private class BandwidthLinkWeight extends DefaultEdgeWeigher<TopologyVertex, TopologyEdge> implements LinkWeigher { |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 675 | private Bandwidth bandwidth = null; |
| 676 | |
| 677 | public BandwidthLinkWeight(Bandwidth bandwidth) { |
| 678 | this.bandwidth = bandwidth; |
| 679 | } |
| 680 | |
| 681 | @Override |
Marc De Leenheer | 552eeb6 | 2017-05-15 17:43:27 -0700 | [diff] [blame] | 682 | public Weight weight(TopologyEdge edge) { |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 683 | Link l = edge.link(); |
| 684 | |
Naoki Shiota | 0d2943e | 2016-05-13 18:53:21 -0700 | [diff] [blame] | 685 | // Avoid inactive links |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 686 | if (l.state() == Link.State.INACTIVE) { |
Yuta HIGUCHI | 806113f | 2017-03-07 22:46:20 -0800 | [diff] [blame] | 687 | log.trace("{} is not active", l); |
Marc De Leenheer | 552eeb6 | 2017-05-15 17:43:27 -0700 | [diff] [blame] | 688 | return ScalarWeight.NON_VIABLE_WEIGHT; |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 689 | } |
| 690 | |
Naoki Shiota | 0d2943e | 2016-05-13 18:53:21 -0700 | [diff] [blame] | 691 | // Avoid cross connect links with used ports |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 692 | if (isCrossConnectLink(l) && usedCrossConnectLinkSet.contains(l)) { |
Yuta HIGUCHI | 806113f | 2017-03-07 22:46:20 -0800 | [diff] [blame] | 693 | log.trace("Cross connect {} in use", l); |
Marc De Leenheer | 552eeb6 | 2017-05-15 17:43:27 -0700 | [diff] [blame] | 694 | return ScalarWeight.NON_VIABLE_WEIGHT; |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 695 | } |
| 696 | |
| 697 | // Check availability of bandwidth |
Yuta HIGUCHI | 9b9c729 | 2017-03-07 22:50:04 -0800 | [diff] [blame] | 698 | if (bandwidth != null && !NO_BW_REQUIREMENT.equals(bandwidth)) { |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 699 | if (hasEnoughBandwidth(l.src()) && hasEnoughBandwidth(l.dst())) { |
Marc De Leenheer | 552eeb6 | 2017-05-15 17:43:27 -0700 | [diff] [blame] | 700 | return new ScalarWeight(1.0); |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 701 | } else { |
Marc De Leenheer | 43b91ad | 2017-03-17 14:45:28 -0700 | [diff] [blame] | 702 | log.trace("Not enough bandwidth on {}", l); |
Marc De Leenheer | 552eeb6 | 2017-05-15 17:43:27 -0700 | [diff] [blame] | 703 | return ScalarWeight.NON_VIABLE_WEIGHT; |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 704 | } |
Marc De Leenheer | 552eeb6 | 2017-05-15 17:43:27 -0700 | [diff] [blame] | 705 | // Allow everything else |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 706 | } else { |
Marc De Leenheer | 552eeb6 | 2017-05-15 17:43:27 -0700 | [diff] [blame] | 707 | return new ScalarWeight(1.0); |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 708 | } |
| 709 | } |
| 710 | |
| 711 | private boolean hasEnoughBandwidth(ConnectPoint cp) { |
| 712 | if (cp.elementId() instanceof DeviceId) { |
Marc De Leenheer | 552eeb6 | 2017-05-15 17:43:27 -0700 | [diff] [blame] | 713 | Device device = deviceService.getDevice(cp.deviceId()); |
Naoki Shiota | e1366ad | 2016-05-11 19:24:11 -0700 | [diff] [blame] | 714 | Device.Type type = device.type(); |
| 715 | |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 716 | if (isTransportLayer(type)) { |
Naoki Shiota | e1366ad | 2016-05-11 19:24:11 -0700 | [diff] [blame] | 717 | // Check if the port has enough capacity |
| 718 | Port port = deviceService.getPort(cp.deviceId(), cp.port()); |
| 719 | if (port instanceof OduCltPort || port instanceof OchPort) { |
| 720 | // Port with capacity |
| 721 | return bandwidth.bps() < port.portSpeed() * 1000000.0; |
| 722 | } else { |
| 723 | // Port without valid capacity (OMS port, etc.) |
| 724 | return true; |
| 725 | } |
| 726 | } else { |
| 727 | // Check if enough amount of bandwidth resource remains |
| 728 | ContinuousResource resource = Resources.continuous(cp.deviceId(), cp.port(), Bandwidth.class) |
| 729 | .resource(bandwidth.bps()); |
Yuta HIGUCHI | 806113f | 2017-03-07 22:46:20 -0800 | [diff] [blame] | 730 | try { |
| 731 | return resourceService.isAvailable(resource); |
| 732 | } catch (Exception e) { |
| 733 | log.error("Resource service failed checking availability of {}", |
Marc De Leenheer | 552eeb6 | 2017-05-15 17:43:27 -0700 | [diff] [blame] | 734 | resource, e); |
Yuta HIGUCHI | 806113f | 2017-03-07 22:46:20 -0800 | [diff] [blame] | 735 | throw e; |
| 736 | } |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 737 | } |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 738 | } |
| 739 | return false; |
| 740 | } |
| 741 | } |
| 742 | |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 743 | public class InternalIntentListener implements IntentListener { |
| 744 | @Override |
| 745 | public void event(IntentEvent event) { |
| 746 | switch (event.type()) { |
| 747 | case INSTALLED: |
Yuta HIGUCHI | 2a77273 | 2017-05-01 20:14:33 -0700 | [diff] [blame] | 748 | log.debug("Intent {} installed.", event.subject()); |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 749 | updateCrossConnectLink(event.subject()); |
| 750 | break; |
| 751 | case WITHDRAWN: |
Yuta HIGUCHI | 2a77273 | 2017-05-01 20:14:33 -0700 | [diff] [blame] | 752 | log.debug("Intent {} withdrawn.", event.subject()); |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 753 | removeCrossConnectLinks(event.subject()); |
| 754 | break; |
| 755 | case FAILED: |
Yuta HIGUCHI | 2a77273 | 2017-05-01 20:14:33 -0700 | [diff] [blame] | 756 | log.debug("Intent {} failed.", event.subject()); |
Yuta HIGUCHI | e80ec2c | 2017-03-06 14:16:13 -0800 | [diff] [blame] | 757 | // TODO If it was one of it's own optical Intent, |
| 758 | // update link state |
| 759 | // TODO If it was packet P2P Intent, call setupConnectivity |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 760 | break; |
| 761 | default: |
| 762 | break; |
| 763 | } |
| 764 | } |
| 765 | |
Yuta HIGUCHI | e80ec2c | 2017-03-06 14:16:13 -0800 | [diff] [blame] | 766 | // TODO rename "CrossConnectLink"? |
| 767 | /** |
| 768 | * Update packet-layer link/port state once Intent is installed. |
| 769 | * |
| 770 | * @param intent which reached installed state |
| 771 | */ |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 772 | private void updateCrossConnectLink(Intent intent) { |
| 773 | linkPathMap.entrySet().stream() |
| 774 | .filter(e -> e.getKey().realizingIntentKey().equals(intent.key())) |
| 775 | .forEach(e -> { |
| 776 | ConnectPoint packetSrc = e.getKey().src(); |
| 777 | ConnectPoint packetDst = e.getKey().dst(); |
| 778 | Bandwidth bw = e.getKey().bandwidth(); |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 779 | |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 780 | // reflect modification only if packetSrc is local_ |
| 781 | if (mastershipService.isLocalMaster(packetSrc.deviceId())) { |
| 782 | // Updates bandwidth of packet ports |
Yuta HIGUCHI | 9b9c729 | 2017-03-07 22:50:04 -0800 | [diff] [blame] | 783 | setPortBandwidth(packetSrc, bw); |
| 784 | setPortBandwidth(packetDst, bw); |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 785 | |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 786 | // Updates link status in distributed map |
| 787 | linkPathMap.computeIfPresent(e.getKey(), (link, connectivity) -> |
| 788 | e.getValue().value().setLinkEstablished(packetSrc, packetDst, true)); |
Yuta HIGUCHI | e80ec2c | 2017-03-06 14:16:13 -0800 | [diff] [blame] | 789 | |
Yuta HIGUCHI | 9b9c729 | 2017-03-07 22:50:04 -0800 | [diff] [blame] | 790 | |
| 791 | if (!linkDiscoveryEnabled(packetSrc, packetDst)) { |
| 792 | injectLink(packetSrc, packetDst); |
| 793 | } |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 794 | } |
| 795 | }); |
| 796 | } |
| 797 | |
| 798 | private void removeCrossConnectLinks(Intent intent) { |
| 799 | ConnectPoint src, dst; |
| 800 | |
| 801 | if (intent instanceof OpticalCircuitIntent) { |
| 802 | OpticalCircuitIntent circuit = (OpticalCircuitIntent) intent; |
| 803 | src = circuit.getSrc(); |
| 804 | dst = circuit.getDst(); |
| 805 | } else if (intent instanceof OpticalConnectivityIntent) { |
| 806 | OpticalConnectivityIntent conn = (OpticalConnectivityIntent) intent; |
| 807 | src = conn.getSrc(); |
| 808 | dst = conn.getDst(); |
| 809 | } else { |
| 810 | return; |
| 811 | } |
| 812 | |
| 813 | removeXcLinkUsage(src); |
| 814 | removeXcLinkUsage(dst); |
| 815 | |
| 816 | // Set bandwidth of 0 to cross connect ports |
| 817 | Bandwidth bw = Bandwidth.bps(0); |
| 818 | linkPathMap.entrySet().stream() |
| 819 | .filter(e -> e.getKey().realizingIntentKey().equals(intent.key())) |
| 820 | .forEach(e -> { |
| 821 | ConnectPoint packetSrc = e.getKey().src(); |
| 822 | ConnectPoint packetDst = e.getKey().dst(); |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 823 | |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 824 | // reflect modification only if packetSrc is local_ |
| 825 | if (mastershipService.isLocalMaster(packetSrc.deviceId())) { |
| 826 | // Updates bandwidth of packet ports |
Yuta HIGUCHI | 9b9c729 | 2017-03-07 22:50:04 -0800 | [diff] [blame] | 827 | setPortBandwidth(packetSrc, bw); |
| 828 | setPortBandwidth(packetDst, bw); |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 829 | |
| 830 | // Updates link status in distributed map |
| 831 | linkPathMap.computeIfPresent(e.getKey(), (link, connectivity) -> |
| 832 | e.getValue().value().setLinkEstablished(packetSrc, packetDst, false)); |
Yuta HIGUCHI | e80ec2c | 2017-03-06 14:16:13 -0800 | [diff] [blame] | 833 | |
Yuta HIGUCHI | 9b9c729 | 2017-03-07 22:50:04 -0800 | [diff] [blame] | 834 | |
| 835 | if (!linkDiscoveryEnabled(packetSrc, packetDst)) { |
| 836 | removeInjectedLink(packetSrc, packetDst); |
| 837 | } |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 838 | } |
| 839 | }); |
| 840 | } |
| 841 | |
| 842 | private void removeXcLinkUsage(ConnectPoint cp) { |
| 843 | Optional<Link> link = linkService.getLinks(cp).stream() |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 844 | .filter(usedCrossConnectLinkSet::contains) |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 845 | .findAny(); |
| 846 | |
| 847 | if (!link.isPresent()) { |
Yuta HIGUCHI | e2689ee | 2017-05-04 16:53:18 -0700 | [diff] [blame] | 848 | log.warn("Cross connect point {} has no cross connect link to release.", cp); |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 849 | return; |
| 850 | } |
| 851 | |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 852 | usedCrossConnectLinkSet.remove(link.get()); |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 853 | } |
Yuta HIGUCHI | 9b9c729 | 2017-03-07 22:50:04 -0800 | [diff] [blame] | 854 | |
| 855 | /** |
| 856 | * Injects link between specified packet port. |
| 857 | * |
| 858 | * @param packetSrc port 1 |
| 859 | * @param packetDst port 2 |
| 860 | */ |
| 861 | private void injectLink(ConnectPoint packetSrc, |
| 862 | ConnectPoint packetDst) { |
| 863 | // inject expected link or durable link |
| 864 | // if packet device cannot advertise packet link |
| 865 | try { |
Yuta HIGUCHI | 2103304 | 2017-05-04 17:58:24 -0700 | [diff] [blame] | 866 | // cannot call addConfig. |
| 867 | // it will create default BasicLinkConfig, |
| 868 | // which will end up advertising DIRECT links and |
| 869 | // DIRECT Link type cannot transition from DIRECT to INDIRECT |
| 870 | LinkKey lnkKey = linkKey(packetSrc, packetDst); |
Yuta HIGUCHI | 9b9c729 | 2017-03-07 22:50:04 -0800 | [diff] [blame] | 871 | BasicLinkConfig lnkCfg = networkConfigService |
Yuta HIGUCHI | 2103304 | 2017-05-04 17:58:24 -0700 | [diff] [blame] | 872 | .getConfig(lnkKey, BasicLinkConfig.class); |
| 873 | if (lnkCfg == null) { |
| 874 | lnkCfg = new BasicLinkConfig(lnkKey); |
| 875 | } |
Yuta HIGUCHI | 9b9c729 | 2017-03-07 22:50:04 -0800 | [diff] [blame] | 876 | lnkCfg.isAllowed(true); |
| 877 | lnkCfg.isDurable(true); |
Marc De Leenheer | 43b91ad | 2017-03-17 14:45:28 -0700 | [diff] [blame] | 878 | lnkCfg.type(Link.Type.INDIRECT); |
| 879 | lnkCfg.isBidirectional(false); |
Yuta HIGUCHI | 2103304 | 2017-05-04 17:58:24 -0700 | [diff] [blame] | 880 | // cannot call apply against manually created instance |
| 881 | //lnkCfg.apply(); |
| 882 | networkConfigService.applyConfig(lnkKey, |
| 883 | BasicLinkConfig.class, |
| 884 | lnkCfg.node()); |
| 885 | |
Yuta HIGUCHI | 9b9c729 | 2017-03-07 22:50:04 -0800 | [diff] [blame] | 886 | } catch (Exception ex) { |
| 887 | log.error("Applying BasicLinkConfig failed", ex); |
| 888 | } |
| 889 | } |
| 890 | |
| 891 | /** |
| 892 | * Removes link injected between specified packet port. |
| 893 | * |
| 894 | * @param packetSrc port 1 |
| 895 | * @param packetDst port 2 |
| 896 | */ |
| 897 | private void removeInjectedLink(ConnectPoint packetSrc, |
| 898 | ConnectPoint packetDst) { |
| 899 | // remove expected link or durable link |
| 900 | // if packet device cannot monitor packet link |
| 901 | |
| 902 | try { |
| 903 | // hack to mark link off-line |
| 904 | BasicLinkConfig lnkCfg = networkConfigService |
| 905 | .getConfig(linkKey(packetSrc, packetDst), |
| 906 | BasicLinkConfig.class); |
| 907 | lnkCfg.isAllowed(false); |
| 908 | lnkCfg.apply(); |
| 909 | } catch (Exception ex) { |
| 910 | log.error("Applying BasicLinkConfig failed", ex); |
| 911 | } |
| 912 | |
| 913 | networkConfigService |
| 914 | .removeConfig(linkKey(packetSrc, packetDst), |
| 915 | BasicLinkConfig.class); |
| 916 | } |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 917 | } |
| 918 | |
| 919 | |
| 920 | private class InternalLinkListener implements LinkListener { |
| 921 | |
| 922 | @Override |
| 923 | public void event(LinkEvent event) { |
| 924 | switch (event.type()) { |
| 925 | case LINK_REMOVED: |
| 926 | Link link = event.subject(); |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 927 | // updates linkPathMap only if src device of link is local |
| 928 | if (!mastershipService.isLocalMaster(link.src().deviceId())) { |
| 929 | return; |
| 930 | } |
| 931 | |
| 932 | // find all packet links that correspond to removed link |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 933 | Set<PacketLinkRealizedByOptical> pLinks = linkPathMap.keySet().stream() |
| 934 | .filter(l -> l.isBetween(link.src(), link.dst()) || l.isBetween(link.dst(), link.src())) |
| 935 | .collect(Collectors.toSet()); |
| 936 | |
| 937 | pLinks.forEach(l -> { |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 938 | // remove found packet links from distributed store |
| 939 | linkPathMap.computeIfPresent(l, (plink, conn) -> { |
| 940 | // Notifies listeners if all packet links are gone |
| 941 | if (conn.isAllRealizingLinkNotEstablished()) { |
| 942 | post(new OpticalPathEvent(OpticalPathEvent.Type.PATH_REMOVED, conn.id())); |
| 943 | } |
| 944 | return null; |
| 945 | }); |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 946 | }); |
| 947 | default: |
| 948 | break; |
| 949 | } |
| 950 | } |
| 951 | } |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 952 | |
| 953 | private class InternalStoreListener |
| 954 | implements MapEventListener<PacketLinkRealizedByOptical, OpticalConnectivity> { |
| 955 | |
| 956 | @Override |
| 957 | public void event(MapEvent<PacketLinkRealizedByOptical, OpticalConnectivity> event) { |
| 958 | switch (event.type()) { |
| 959 | case UPDATE: |
| 960 | OpticalConnectivity oldConnectivity = event.oldValue().value(); |
| 961 | OpticalConnectivity newConnectivity = event.newValue().value(); |
| 962 | |
| 963 | if (!oldConnectivity.isAllRealizingLinkEstablished() && |
| 964 | newConnectivity.isAllRealizingLinkEstablished()) { |
| 965 | // Notifies listeners if all links are established |
| 966 | updateBandwidthUsage(newConnectivity); |
| 967 | post(new OpticalPathEvent(OpticalPathEvent.Type.PATH_INSTALLED, newConnectivity.id())); |
| 968 | } else if (!oldConnectivity.isAllRealizingLinkNotEstablished() && |
| 969 | newConnectivity.isAllRealizingLinkNotEstablished()) { |
| 970 | // Notifies listeners if all links are gone |
| 971 | releaseBandwidthUsage(newConnectivity); |
| 972 | post(new OpticalPathEvent(OpticalPathEvent.Type.PATH_REMOVED, newConnectivity.id())); |
| 973 | } |
| 974 | |
| 975 | break; |
| 976 | default: |
| 977 | break; |
| 978 | } |
| 979 | } |
| 980 | |
| 981 | } |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 982 | } |
| 983 | |