Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-present Open Networking Foundation |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package org.onosproject.incubator.net.l2monitoring.cfm.impl; |
| 17 | |
Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 18 | import org.onosproject.core.CoreService; |
| 19 | import org.onosproject.core.IdGenerator; |
| 20 | import org.onosproject.event.AbstractListenerManager; |
Sean Condon | 96b896d | 2017-12-11 12:44:29 -0800 | [diff] [blame] | 21 | import org.onosproject.event.Event; |
| 22 | import org.onosproject.incubator.net.l2monitoring.cfm.MaintenanceDomain; |
Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 23 | import org.onosproject.incubator.net.l2monitoring.cfm.Mep; |
| 24 | import org.onosproject.incubator.net.l2monitoring.cfm.MepEntry; |
| 25 | import org.onosproject.incubator.net.l2monitoring.cfm.MepLbCreate; |
| 26 | import org.onosproject.incubator.net.l2monitoring.cfm.MepLtCreate; |
| 27 | import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MaIdShort; |
| 28 | import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MdId; |
| 29 | import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MepId; |
Sean Condon | 96b896d | 2017-12-11 12:44:29 -0800 | [diff] [blame] | 30 | import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MepKeyId; |
Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 31 | import org.onosproject.incubator.net.l2monitoring.cfm.service.CfmConfigException; |
| 32 | import org.onosproject.incubator.net.l2monitoring.cfm.service.CfmMdService; |
| 33 | import org.onosproject.incubator.net.l2monitoring.cfm.service.CfmMepEvent; |
| 34 | import org.onosproject.incubator.net.l2monitoring.cfm.service.CfmMepListener; |
| 35 | import org.onosproject.incubator.net.l2monitoring.cfm.service.CfmMepProgrammable; |
| 36 | import org.onosproject.incubator.net.l2monitoring.cfm.service.CfmMepService; |
Sean Condon | 96b896d | 2017-12-11 12:44:29 -0800 | [diff] [blame] | 37 | import org.onosproject.incubator.net.l2monitoring.cfm.service.MdEvent; |
| 38 | import org.onosproject.incubator.net.l2monitoring.cfm.service.MdListener; |
| 39 | import org.onosproject.incubator.net.l2monitoring.cfm.service.MepStore; |
| 40 | import org.onosproject.incubator.net.l2monitoring.cfm.service.MepStoreDelegate; |
Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 41 | import org.onosproject.mastership.MastershipService; |
| 42 | import org.onosproject.net.Device; |
| 43 | import org.onosproject.net.DeviceId; |
| 44 | import org.onosproject.net.device.DeviceEvent; |
| 45 | import org.onosproject.net.device.DeviceListener; |
| 46 | import org.onosproject.net.device.DeviceService; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 47 | import org.osgi.service.component.annotations.Activate; |
| 48 | import org.osgi.service.component.annotations.Component; |
| 49 | import org.osgi.service.component.annotations.Deactivate; |
| 50 | import org.osgi.service.component.annotations.Reference; |
| 51 | import org.osgi.service.component.annotations.ReferenceCardinality; |
Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 52 | import org.slf4j.Logger; |
| 53 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 54 | import java.util.ArrayList; |
| 55 | import java.util.Collection; |
| 56 | import java.util.HashSet; |
| 57 | import java.util.Iterator; |
| 58 | import java.util.List; |
| 59 | import java.util.Optional; |
| 60 | import java.util.Set; |
| 61 | import java.util.concurrent.ConcurrentLinkedQueue; |
| 62 | import java.util.concurrent.Executors; |
| 63 | import java.util.concurrent.ScheduledExecutorService; |
| 64 | import java.util.concurrent.ScheduledFuture; |
| 65 | import java.util.concurrent.TimeUnit; |
| 66 | |
| 67 | import static org.onlab.util.Tools.groupedThreads; |
| 68 | import static org.slf4j.LoggerFactory.getLogger; |
| 69 | |
Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 70 | /** |
| 71 | * Provides implementation of the CFM North and South Bound Interfaces. |
| 72 | */ |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 73 | @Component(immediate = true, service = CfmMepService.class) |
Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 74 | public class CfmMepManager |
| 75 | extends AbstractListenerManager<CfmMepEvent, CfmMepListener> |
| 76 | implements CfmMepService { |
| 77 | |
| 78 | private final Logger log = getLogger(getClass()); |
| 79 | |
Sean Condon | 96b896d | 2017-12-11 12:44:29 -0800 | [diff] [blame] | 80 | private InternalDeviceListener deviceListener = null; |
| 81 | private InternalMdListener mdListener = null; |
Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 82 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 83 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 84 | protected DeviceService deviceService; |
| 85 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 86 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 87 | protected CoreService coreService; |
| 88 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 89 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 90 | protected MastershipService mastershipService; |
| 91 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 92 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 93 | protected CfmMdService cfmMdService; |
| 94 | |
| 95 | private static final int DEFAULT_POLL_FREQUENCY = 30; |
| 96 | private int fallbackMepPollFrequency = DEFAULT_POLL_FREQUENCY; |
| 97 | |
Sean Condon | 96b896d | 2017-12-11 12:44:29 -0800 | [diff] [blame] | 98 | private InternalEventHandler eventHandler = new InternalEventHandler(); |
| 99 | private static final Object THREAD_SCHED_LOCK = new Object(); |
| 100 | private static int numOfEventsQueued = 0; |
| 101 | private static int numOfEventsExecuted = 0; |
| 102 | private static int numOfHandlerExecution = 0; |
| 103 | private static int numOfHandlerScheduled = 0; |
| 104 | |
| 105 | private ScheduledExecutorService executorService = Executors |
| 106 | .newScheduledThreadPool(1, |
| 107 | groupedThreads("CfmMepManager", "event-%d", log)); |
| 108 | |
| 109 | @SuppressWarnings("unused") |
| 110 | private static ScheduledFuture<?> eventHandlerFuture = null; |
| 111 | @SuppressWarnings("rawtypes") |
| 112 | private ConcurrentLinkedQueue<Event> eventQueue = new ConcurrentLinkedQueue<>(); |
| 113 | |
| 114 | |
Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 115 | private IdGenerator idGenerator; |
| 116 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 117 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Sean Condon | 96b896d | 2017-12-11 12:44:29 -0800 | [diff] [blame] | 118 | protected MepStore mepStore; |
Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 119 | |
Sean Condon | 96b896d | 2017-12-11 12:44:29 -0800 | [diff] [blame] | 120 | protected final MepStoreDelegate delegate = new InternalStoreDelegate(); |
Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 121 | |
| 122 | @Activate |
| 123 | public void activate() { |
Sean Condon | 96b896d | 2017-12-11 12:44:29 -0800 | [diff] [blame] | 124 | mepStore.setDelegate(delegate); |
Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 125 | |
Sean Condon | 96b896d | 2017-12-11 12:44:29 -0800 | [diff] [blame] | 126 | deviceListener = new InternalDeviceListener(); |
Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 127 | deviceService.addListener(deviceListener); |
Sean Condon | 96b896d | 2017-12-11 12:44:29 -0800 | [diff] [blame] | 128 | mdListener = new InternalMdListener(); |
| 129 | cfmMdService.addListener(mdListener); |
| 130 | eventDispatcher.addSink(CfmMepEvent.class, listenerRegistry); |
Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 131 | idGenerator = coreService.getIdGenerator("mep-ids"); |
| 132 | log.info("CFM MEP Manager Started"); |
| 133 | } |
| 134 | |
| 135 | @Deactivate |
| 136 | public void deactivate() { |
| 137 | deviceService.removeListener(deviceListener); |
Sean Condon | 96b896d | 2017-12-11 12:44:29 -0800 | [diff] [blame] | 138 | cfmMdService.removeListener(mdListener); |
Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 139 | eventDispatcher.removeSink(CfmMepEvent.class); |
| 140 | log.info("CFM MEP Manager Stopped"); |
Sean Condon | 96b896d | 2017-12-11 12:44:29 -0800 | [diff] [blame] | 141 | mepStore.unsetDelegate(delegate); |
| 142 | deviceListener = null; |
| 143 | mdListener = null; |
Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | @Override |
| 147 | public Collection<MepEntry> getAllMeps(MdId mdName, MaIdShort maName) |
| 148 | throws CfmConfigException { |
| 149 | //Will throw IllegalArgumentException if ma does not exist |
| 150 | cfmMdService.getMaintenanceAssociation(mdName, maName); |
| 151 | |
Sean Condon | 96b896d | 2017-12-11 12:44:29 -0800 | [diff] [blame] | 152 | Collection<Mep> mepStoreCollection = mepStore.getAllMeps(); |
Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 153 | Collection<MepEntry> mepEntryCollection = new ArrayList<>(); |
| 154 | |
Sean Condon | 96b896d | 2017-12-11 12:44:29 -0800 | [diff] [blame] | 155 | for (Mep mep : mepStoreCollection) { |
Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 156 | if (mep.mdId().equals(mdName) && mep.maId().equals(maName)) { |
| 157 | DeviceId mepDeviceId = mep.deviceId(); |
| 158 | if (deviceService.getDevice(mepDeviceId) == null) { |
| 159 | log.warn("Device not found/available " + mepDeviceId + |
Sean Condon | 96b896d | 2017-12-11 12:44:29 -0800 | [diff] [blame] | 160 | " for MEP: " + mdName + "/" + maName + "/" + mep.mepId()); |
Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 161 | continue; |
| 162 | } else if (!deviceService.getDevice(mepDeviceId) |
Sean Condon | 96b896d | 2017-12-11 12:44:29 -0800 | [diff] [blame] | 163 | .is(CfmMepProgrammable.class)) { |
Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 164 | throw new CfmConfigException("Device " + mepDeviceId + |
| 165 | " does not support CfmMepProgrammable behaviour."); |
| 166 | } |
| 167 | |
| 168 | log.debug("Retrieving MEP results for Mep {} in MD {}, MA {} " |
Sean Condon | 96b896d | 2017-12-11 12:44:29 -0800 | [diff] [blame] | 169 | + "on Device {}", mep.mepId(), mdName, maName, mepDeviceId); |
Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 170 | mepEntryCollection.add(deviceService |
Sean Condon | 96b896d | 2017-12-11 12:44:29 -0800 | [diff] [blame] | 171 | .getDevice(mepDeviceId) |
| 172 | .as(CfmMepProgrammable.class) |
| 173 | .getMep(mdName, maName, mep.mepId())); |
Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 174 | } |
| 175 | } |
| 176 | |
| 177 | return mepEntryCollection; |
| 178 | } |
| 179 | |
| 180 | @Override |
Sean Condon | 96b896d | 2017-12-11 12:44:29 -0800 | [diff] [blame] | 181 | public Collection<Mep> getAllMepsByDevice(DeviceId deviceId) throws CfmConfigException { |
| 182 | return mepStore.getMepsByDeviceId(deviceId); |
Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | @Override |
Sean Condon | 96b896d | 2017-12-11 12:44:29 -0800 | [diff] [blame] | 186 | public MepEntry getMep(MdId mdName, MaIdShort maName, MepId mepId) throws CfmConfigException { |
| 187 | MepKeyId key = new MepKeyId(mdName, maName, mepId); |
| 188 | |
Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 189 | //Will throw IllegalArgumentException if ma does not exist |
| 190 | cfmMdService.getMaintenanceAssociation(mdName, maName); |
| 191 | |
Sean Condon | 96b896d | 2017-12-11 12:44:29 -0800 | [diff] [blame] | 192 | Optional<Mep> mepOptional = mepStore.getMep(key); |
| 193 | if (mepOptional.isPresent()) { |
| 194 | Mep mep = mepOptional.get(); |
| 195 | DeviceId mepDeviceId = mep.deviceId(); |
| 196 | if (deviceService.getDevice(mepDeviceId) == null) { |
| 197 | throw new CfmConfigException("Device not found " + mepDeviceId); |
| 198 | } else if (!deviceService.getDevice(mepDeviceId).is(CfmMepProgrammable.class)) { |
| 199 | throw new CfmConfigException("Device " + mepDeviceId + |
| 200 | " does not support CfmMepProgrammable behaviour."); |
| 201 | } |
| 202 | |
| 203 | log.debug("Retrieving MEP reults for Mep {} in MD {}, MA {} on Device {}", |
| 204 | mep.mepId(), mdName, maName, mepDeviceId); |
| 205 | |
| 206 | return deviceService.getDevice(mepDeviceId) |
| 207 | .as(CfmMepProgrammable.class).getMep(mdName, maName, mepId); |
| 208 | } else { |
| 209 | return null; |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | @Override |
| 214 | public boolean deleteMep(MdId mdName, MaIdShort maName, MepId mepId, |
| 215 | Optional<MaintenanceDomain> oldMd) throws CfmConfigException { |
| 216 | MepKeyId key = new MepKeyId(mdName, maName, mepId); |
| 217 | |
| 218 | //Will throw IllegalArgumentException if ma does not exist |
| 219 | cfmMdService.getMaintenanceAssociation(mdName, maName); |
| 220 | |
| 221 | //Get the device ID from the MEP |
| 222 | Optional<Mep> deletedMep = mepStore.getMep(key); |
| 223 | if (!deletedMep.isPresent()) { |
| 224 | log.warn("MEP {} not found when deleting Mep", key); |
| 225 | return false; |
| 226 | } |
| 227 | |
| 228 | DeviceId mepDeviceId = deletedMep.get().deviceId(); |
| 229 | boolean deleted = mepStore.deleteMep(key); |
| 230 | |
| 231 | Device mepDevice = deviceService.getDevice(mepDeviceId); |
| 232 | if (mepDevice == null || !mepDevice.is(CfmMepProgrammable.class)) { |
| 233 | throw new CfmConfigException("Unexpeced fault on device driver for " |
| 234 | + mepDeviceId); |
| 235 | } |
| 236 | try { |
| 237 | deleted = mepDevice.as(CfmMepProgrammable.class) |
| 238 | .deleteMep(mdName, maName, mepId, oldMd); |
| 239 | } catch (CfmConfigException e) { |
| 240 | log.warn("MEP could not be deleted on device - perhaps it " |
| 241 | + "does not exist. Continuing"); |
| 242 | } |
| 243 | |
| 244 | //Iterate through all other devices and remove as a Remote Mep |
| 245 | int mepsOnMdCount = 0; |
| 246 | int mepsOnMaCount = 0; |
| 247 | for (Mep mep : mepStore.getAllMeps()) { |
| 248 | if (mep.deviceId().equals(mepDeviceId) && mdName.equals(mep.mdId())) { |
| 249 | mepsOnMdCount++; |
| 250 | if (maName.equals(mep.maId())) { |
| 251 | mepsOnMaCount++; |
Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 252 | } |
| 253 | } |
Sean Condon | 96b896d | 2017-12-11 12:44:29 -0800 | [diff] [blame] | 254 | List<DeviceId> alreadyHandledDevices = new ArrayList<>(); |
| 255 | if (mep.deviceId().equals(mepDeviceId) || !mep.mdId().equals(mdName) || |
| 256 | !mep.maId().equals(maName) || |
| 257 | alreadyHandledDevices.contains(mep.deviceId())) { |
| 258 | continue; |
| 259 | } |
| 260 | deviceService.getDevice(mep.deviceId()) |
| 261 | .as(CfmMepProgrammable.class) |
| 262 | .deleteMaRemoteMepOnDevice(mdName, maName, mepId); |
| 263 | alreadyHandledDevices.add(mep.deviceId()); |
| 264 | log.info("Deleted RMep entry on {} on device {}", |
| 265 | mdName.mdName() + "/" + maName.maName(), mep.deviceId()); |
Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 266 | } |
Sean Condon | 96b896d | 2017-12-11 12:44:29 -0800 | [diff] [blame] | 267 | |
| 268 | //Also if this is the last MEP in this MA then delete this MA from device |
| 269 | //If this is the last MA in this MD on device, then delete the MD from the device |
| 270 | if (mepsOnMdCount == 0) { |
| 271 | boolean deletedMd = deviceService.getDevice(mepDeviceId) |
| 272 | .as(CfmMepProgrammable.class).deleteMdOnDevice(mdName, oldMd); |
| 273 | log.info("Deleted MD {} from Device {}", mdName.mdName(), mepDeviceId); |
| 274 | } else if (mepsOnMaCount == 0) { |
| 275 | boolean deletedMa = deviceService.getDevice(mepDeviceId) |
| 276 | .as(CfmMepProgrammable.class).deleteMaOnDevice(mdName, maName, oldMd); |
| 277 | log.info("Deleted MA {} from Device {}", |
| 278 | mdName.mdName() + "/" + maName.maName(), mepDeviceId); |
| 279 | } |
| 280 | |
| 281 | return deleted; |
Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | @Override |
| 285 | public boolean createMep(MdId mdName, MaIdShort maName, Mep newMep) throws CfmConfigException { |
Sean Condon | 96b896d | 2017-12-11 12:44:29 -0800 | [diff] [blame] | 286 | MepKeyId key = new MepKeyId(mdName, maName, newMep.mepId()); |
Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 287 | log.debug("Creating MEP " + newMep.mepId() + " on MD {}, MA {} on Device {}", |
| 288 | mdName, maName, newMep.deviceId().toString()); |
Sean Condon | 96b896d | 2017-12-11 12:44:29 -0800 | [diff] [blame] | 289 | if (mepStore.getMep(key).isPresent()) { |
| 290 | return false; |
Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | //Will throw IllegalArgumentException if ma does not exist |
| 294 | cfmMdService.getMaintenanceAssociation(mdName, maName); |
| 295 | |
| 296 | DeviceId mepDeviceId = newMep.deviceId(); |
| 297 | if (deviceService.getDevice(mepDeviceId) == null) { |
| 298 | throw new CfmConfigException("Device not found " + mepDeviceId); |
| 299 | } else if (!deviceService.getDevice(mepDeviceId).is(CfmMepProgrammable.class)) { |
| 300 | throw new CfmConfigException("Device " + mepDeviceId + " does not support CfmMepProgrammable behaviour."); |
| 301 | } |
| 302 | |
| 303 | boolean deviceResult = |
| 304 | deviceService.getDevice(mepDeviceId).as(CfmMepProgrammable.class).createMep(mdName, maName, newMep); |
| 305 | log.debug("MEP created on {}", mepDeviceId); |
| 306 | if (deviceResult) { |
Sean Condon | 96b896d | 2017-12-11 12:44:29 -0800 | [diff] [blame] | 307 | boolean alreadyExisted = mepStore.createUpdateMep(key, newMep); |
| 308 | |
| 309 | //Add to other Remote Mep List on other devices |
| 310 | for (Mep mep:mepStore.getMepsByMdMa(mdName, maName)) { |
| 311 | List<DeviceId> alreadyHandledDevices = new ArrayList<>(); |
| 312 | if (mep.deviceId().equals(mepDeviceId) || |
| 313 | alreadyHandledDevices.contains(mep.deviceId())) { |
| 314 | continue; |
| 315 | } |
| 316 | boolean created = deviceService.getDevice(mep.deviceId()) |
| 317 | .as(CfmMepProgrammable.class) |
| 318 | .createMaRemoteMepOnDevice(mdName, maName, newMep.mepId()); |
| 319 | alreadyHandledDevices.add(mep.deviceId()); |
| 320 | log.info("Created RMep entry on {} on device {}", |
| 321 | mdName.mdName() + "/" + maName.maName(), mep.deviceId()); |
| 322 | } |
| 323 | |
| 324 | return !alreadyExisted; |
Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 325 | } else { |
| 326 | return deviceResult; |
| 327 | } |
| 328 | } |
| 329 | |
| 330 | @Override |
| 331 | public void transmitLoopback(MdId mdName, MaIdShort maName, |
Sean Condon | 96b896d | 2017-12-11 12:44:29 -0800 | [diff] [blame] | 332 | MepId mepId, MepLbCreate lbCreate) throws CfmConfigException { |
| 333 | MepKeyId key = new MepKeyId(mdName, maName, mepId); |
| 334 | Mep mep = mepStore.getMep(key) |
| 335 | .orElseThrow(() -> new CfmConfigException("Mep " + mdName + "/" + maName + "/" |
| 336 | + mepId + " not found when calling Transmit Loopback")); |
| 337 | |
| 338 | log.debug("Transmitting Loopback on MEP {} on Device {}", |
| 339 | key, mep.deviceId()); |
| 340 | deviceService.getDevice(mep.deviceId()) |
| 341 | .as(CfmMepProgrammable.class) |
| 342 | .transmitLoopback(mdName, maName, mepId, lbCreate); |
Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | @Override |
| 346 | public void abortLoopback(MdId mdName, MaIdShort maName, MepId mepId) |
| 347 | throws CfmConfigException { |
Sean Condon | 96b896d | 2017-12-11 12:44:29 -0800 | [diff] [blame] | 348 | |
| 349 | MepKeyId key = new MepKeyId(mdName, maName, mepId); |
| 350 | Mep mep = mepStore.getMep(key) |
| 351 | .orElseThrow(() -> new CfmConfigException("Mep " + mdName + "/" + maName + "/" |
| 352 | + mepId + " not found when calling Aborting Loopback")); |
| 353 | |
| 354 | log.debug("Aborting Loopback on MEP {} on Device {}", |
| 355 | key, mep.deviceId()); |
| 356 | deviceService.getDevice(mep.deviceId()) |
| 357 | .as(CfmMepProgrammable.class) |
| 358 | .abortLoopback(mdName, maName, mepId); |
Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | @Override |
| 362 | public void transmitLinktrace(MdId mdName, MaIdShort maName, MepId mepId, |
Sean Condon | 96b896d | 2017-12-11 12:44:29 -0800 | [diff] [blame] | 363 | MepLtCreate ltCreate) { |
Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 364 | throw new UnsupportedOperationException("Not yet implemented"); |
| 365 | } |
| 366 | |
Sean Condon | 96b896d | 2017-12-11 12:44:29 -0800 | [diff] [blame] | 367 | private class InternalMdListener implements MdListener { |
| 368 | @Override |
| 369 | public boolean isRelevant(MdEvent event) { |
| 370 | return event.type().equals(MdEvent.Type.MD_REMOVED) || |
| 371 | event.type().equals(MdEvent.Type.MA_REMOVED); |
| 372 | } |
| 373 | |
| 374 | @Override |
| 375 | public void event(MdEvent event) { |
| 376 | MdId mdName = event.subject(); |
| 377 | switch (event.type()) { |
| 378 | case MA_REMOVED: |
| 379 | case MD_REMOVED: |
| 380 | log.trace("Event {} receieved from MD Service for {}", event.type(), mdName); |
| 381 | scheduleEventHandlerIfNotScheduled(event); |
| 382 | break; |
| 383 | default: |
| 384 | log.warn("Unhandled Event {} received from MD Service", event.type()); |
| 385 | break; |
| 386 | } |
| 387 | } |
| 388 | } |
| 389 | |
Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 390 | private class InternalDeviceListener implements DeviceListener { |
| 391 | @Override |
Sean Condon | 96b896d | 2017-12-11 12:44:29 -0800 | [diff] [blame] | 392 | public boolean isRelevant(DeviceEvent event) { |
| 393 | return event.type().equals(DeviceEvent.Type.DEVICE_REMOVED); |
| 394 | } |
| 395 | |
| 396 | @Override |
Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 397 | public void event(DeviceEvent event) { |
Sean Condon | 96b896d | 2017-12-11 12:44:29 -0800 | [diff] [blame] | 398 | DeviceId deviceId = event.subject().id(); |
Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 399 | switch (event.type()) { |
Sean Condon | 96b896d | 2017-12-11 12:44:29 -0800 | [diff] [blame] | 400 | case DEVICE_ADDED: |
| 401 | case PORT_UPDATED: |
| 402 | case PORT_ADDED: |
| 403 | case DEVICE_UPDATED: |
Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 404 | case DEVICE_REMOVED: |
| 405 | case DEVICE_AVAILABILITY_CHANGED: |
Sean Condon | 96b896d | 2017-12-11 12:44:29 -0800 | [diff] [blame] | 406 | log.trace("Event {} received from Device Service", event.type()); |
| 407 | scheduleEventHandlerIfNotScheduled(event); |
Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 408 | break; |
| 409 | default: |
Sean Condon | 96b896d | 2017-12-11 12:44:29 -0800 | [diff] [blame] | 410 | log.warn("Unhandled Event {} received from Device Service", event.type()); |
Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 411 | break; |
| 412 | } |
| 413 | } |
| 414 | } |
Sean Condon | 96b896d | 2017-12-11 12:44:29 -0800 | [diff] [blame] | 415 | |
| 416 | @SuppressWarnings("rawtypes") |
| 417 | private void scheduleEventHandlerIfNotScheduled(Event event) { |
| 418 | synchronized (THREAD_SCHED_LOCK) { |
| 419 | eventQueue.add(event); |
| 420 | numOfEventsQueued++; |
| 421 | |
| 422 | if ((numOfHandlerScheduled - numOfHandlerExecution) == 0) { |
| 423 | //No pending scheduled event handling threads. So start a new one. |
| 424 | eventHandlerFuture = executorService |
| 425 | .schedule(eventHandler, 100, TimeUnit.MILLISECONDS); |
| 426 | numOfHandlerScheduled++; |
| 427 | } |
| 428 | log.trace("numOfEventsQueued {}, numOfEventHandlerScheduled {}", |
| 429 | numOfEventsQueued, |
| 430 | numOfHandlerScheduled); |
| 431 | } |
| 432 | } |
| 433 | |
| 434 | private class InternalEventHandler implements Runnable { |
| 435 | @Override |
| 436 | public void run() { |
| 437 | try { |
| 438 | while (true) { |
| 439 | @SuppressWarnings("rawtypes") |
| 440 | Event event; |
| 441 | synchronized (THREAD_SCHED_LOCK) { |
| 442 | if (!eventQueue.isEmpty()) { |
| 443 | event = eventQueue.poll(); |
| 444 | numOfEventsExecuted++; |
| 445 | } else { |
| 446 | numOfHandlerExecution++; |
| 447 | log.debug("numOfHandlerExecution {} numOfEventsExecuted {}", |
| 448 | numOfHandlerExecution, numOfEventsExecuted); |
| 449 | break; |
| 450 | } |
| 451 | } |
| 452 | if (event.type() == DeviceEvent.Type.DEVICE_REMOVED) { |
| 453 | DeviceId deviceId = ((Device) event.subject()).id(); |
| 454 | log.info("Processing device removal event for unavailable device {}", |
| 455 | deviceId); |
| 456 | processDeviceRemoved((Device) event.subject()); |
| 457 | } else if (event.type() == MdEvent.Type.MD_REMOVED) { |
| 458 | MdId mdName = (MdId) event.subject(); |
| 459 | log.info("Processing MD removal event for MD {}", |
| 460 | mdName); |
| 461 | processMdRemoved(mdName, ((MdEvent) event).md().get()); |
| 462 | } else if (event.type() == MdEvent.Type.MA_REMOVED) { |
| 463 | MdId mdName = (MdId) event.subject(); |
| 464 | MaIdShort maName = ((MdEvent) event).maId().get(); |
| 465 | log.info("Processing MA removal event for MA {}", |
| 466 | mdName.mdName() + "/" + maName.maName()); |
| 467 | processMaRemoved(mdName, maName, ((MdEvent) event).md().get()); |
| 468 | } |
| 469 | } |
| 470 | } catch (Exception e) { |
| 471 | log.error("CfmMepService event handler " |
| 472 | + "thread thrown an exception: {}", e); |
| 473 | } |
| 474 | } |
| 475 | } |
| 476 | |
| 477 | /** |
| 478 | * This removes a MEP from the internal list of Meps, and updates remote meps list on other Meps. |
| 479 | * Note: This does not call the device's CfmMepProgrammable, because there |
| 480 | * would be no point as the device has already been removed from ONOS. |
| 481 | * The configuration for this MEP may still be present on the actual device, and |
| 482 | * any future config would have to be careful to wipe the Mep from the device |
| 483 | * before applying a Mep again |
| 484 | * @param removedDevice The device that has been removed |
| 485 | */ |
| 486 | protected void processDeviceRemoved(Device removedDevice) { |
| 487 | log.warn("Remove Mep(s) associated with Device: " + removedDevice.id()); |
| 488 | Collection<Mep> mepListForDevice = mepStore.getMepsByDeviceId(removedDevice.id()); |
| 489 | |
| 490 | |
| 491 | for (Mep mep:mepStore.getAllMeps()) { |
| 492 | for (Mep mepForDevice:mepListForDevice) { |
| 493 | if (mep.mdId().equals(mepForDevice.mdId()) && mep.maId().equals(mepForDevice.maId())) { |
| 494 | Device mepDevice = deviceService.getDevice(mep.deviceId()); |
| 495 | log.info("Removing Remote Mep {} from MA{} on device {}", |
| 496 | mepForDevice.mepId(), |
| 497 | mep.mdId().mdName() + "/" + mep.maId().maName(), |
| 498 | mepDevice.id()); |
| 499 | try { |
| 500 | mepDevice.as(CfmMepProgrammable.class) |
| 501 | .deleteMaRemoteMepOnDevice(mep.mdId(), mep.maId(), mepForDevice.mepId()); |
| 502 | } catch (CfmConfigException e) { |
| 503 | log.error("Error when removing Remote Mep {} from MA {}. Continuing.", |
| 504 | mep.mdId().mdName() + "/" + mep.maId().maName(), |
| 505 | mepForDevice.mepId()); |
| 506 | } |
| 507 | } |
| 508 | } |
| 509 | } |
| 510 | |
| 511 | for (Iterator<Mep> iter = mepListForDevice.iterator(); iter.hasNext();) { |
| 512 | mepStore.deleteMep(new MepKeyId(iter.next())); |
| 513 | } |
| 514 | } |
| 515 | |
| 516 | protected void processMaRemoved(MdId mdId, MaIdShort maId, MaintenanceDomain oldMd) { |
| 517 | Set<DeviceId> deviceIdsRemoved = new HashSet<>(); |
| 518 | |
| 519 | for (Iterator<Mep> iter = mepStore.getMepsByMdMa(mdId, maId).iterator(); iter.hasNext();) { |
| 520 | Mep mepForMdMa = iter.next(); |
| 521 | DeviceId mepDeviceId = mepForMdMa.deviceId(); |
| 522 | try { |
| 523 | deviceService.getDevice(mepDeviceId).as(CfmMepProgrammable.class) |
| 524 | .deleteMep(mdId, maId, mepForMdMa.mepId(), Optional.of(oldMd)); |
| 525 | deviceIdsRemoved.add(mepDeviceId); |
| 526 | } catch (CfmConfigException e) { |
| 527 | log.warn("Could not delete MEP {} from Device {}", mepForMdMa.mepId(), mepDeviceId, e); |
| 528 | } |
| 529 | iter.remove(); |
| 530 | |
| 531 | log.info("Removed MEP {} from Device {} because MA {} was deleted", |
| 532 | mepForMdMa.mepId(), mepDeviceId, mdId.mdName() + "/" + maId.maName()); |
| 533 | } |
| 534 | |
| 535 | deviceIdsRemoved.forEach(deviceId -> { |
| 536 | try { |
| 537 | deviceService.getDevice(deviceId).as(CfmMepProgrammable.class) |
| 538 | .deleteMaOnDevice(mdId, maId, Optional.of(oldMd)); |
| 539 | } catch (CfmConfigException e) { |
| 540 | log.warn("Could not delete MA {} from Device {}", |
| 541 | mdId.mdName() + "/" + maId.maName(), deviceId, e); |
| 542 | } |
| 543 | }); |
| 544 | } |
| 545 | |
| 546 | protected void processMdRemoved(MdId mdId, MaintenanceDomain oldMd) { |
| 547 | Set<DeviceId> deviceIdsRemoved = new HashSet<>(); |
| 548 | for (Iterator<Mep> iter = mepStore.getMepsByMd(mdId).iterator(); iter.hasNext();) { |
| 549 | Mep mep = iter.next(); |
| 550 | DeviceId mepDeviceId = mep.deviceId(); |
| 551 | try { |
| 552 | deviceService.getDevice(mepDeviceId).as(CfmMepProgrammable.class) |
| 553 | .deleteMep(mdId, mep.maId(), mep.mepId(), Optional.of(oldMd)); |
| 554 | deviceIdsRemoved.add(mepDeviceId); |
| 555 | } catch (CfmConfigException e) { |
| 556 | log.warn("Could not delete MEP {} from Device {}", mep.mepId(), mepDeviceId, e); |
| 557 | } |
| 558 | iter.remove(); |
| 559 | log.info("Removed MEP {} from Device {} because MD {} was deleted", |
| 560 | mep.mepId(), mepDeviceId, mdId.mdName()); |
| 561 | } |
| 562 | |
| 563 | deviceIdsRemoved.forEach(deviceId -> { |
| 564 | try { |
| 565 | deviceService.getDevice(deviceId).as(CfmMepProgrammable.class) |
| 566 | .deleteMdOnDevice(mdId, Optional.of(oldMd)); |
| 567 | } catch (CfmConfigException e) { |
| 568 | log.warn("Could not delete MD {} from Device {}", |
| 569 | mdId.mdName(), deviceId, e); |
| 570 | } |
| 571 | }); |
| 572 | } |
| 573 | |
| 574 | private class InternalStoreDelegate implements MepStoreDelegate { |
| 575 | @Override |
| 576 | public void notify(CfmMepEvent event) { |
| 577 | log.debug("New Mep event: {}", event); |
| 578 | eventDispatcher.post(event); |
| 579 | } |
| 580 | } |
Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 581 | } |