blob: e641f3c8ca4959cc211e82bcb8f45d775e29a228 [file] [log] [blame]
Sean Condon0e89bda2017-03-21 14:23:19 +00001/*
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 */
16package org.onosproject.incubator.net.l2monitoring.cfm.impl;
17
Sean Condon0e89bda2017-03-21 14:23:19 +000018import org.onosproject.core.CoreService;
19import org.onosproject.core.IdGenerator;
20import org.onosproject.event.AbstractListenerManager;
Sean Condon96b896d2017-12-11 12:44:29 -080021import org.onosproject.event.Event;
22import org.onosproject.incubator.net.l2monitoring.cfm.MaintenanceDomain;
Sean Condon0e89bda2017-03-21 14:23:19 +000023import org.onosproject.incubator.net.l2monitoring.cfm.Mep;
24import org.onosproject.incubator.net.l2monitoring.cfm.MepEntry;
25import org.onosproject.incubator.net.l2monitoring.cfm.MepLbCreate;
26import org.onosproject.incubator.net.l2monitoring.cfm.MepLtCreate;
27import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MaIdShort;
28import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MdId;
29import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MepId;
Sean Condon96b896d2017-12-11 12:44:29 -080030import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MepKeyId;
Sean Condon0e89bda2017-03-21 14:23:19 +000031import org.onosproject.incubator.net.l2monitoring.cfm.service.CfmConfigException;
32import org.onosproject.incubator.net.l2monitoring.cfm.service.CfmMdService;
33import org.onosproject.incubator.net.l2monitoring.cfm.service.CfmMepEvent;
34import org.onosproject.incubator.net.l2monitoring.cfm.service.CfmMepListener;
35import org.onosproject.incubator.net.l2monitoring.cfm.service.CfmMepProgrammable;
36import org.onosproject.incubator.net.l2monitoring.cfm.service.CfmMepService;
Sean Condon96b896d2017-12-11 12:44:29 -080037import org.onosproject.incubator.net.l2monitoring.cfm.service.MdEvent;
38import org.onosproject.incubator.net.l2monitoring.cfm.service.MdListener;
39import org.onosproject.incubator.net.l2monitoring.cfm.service.MepStore;
40import org.onosproject.incubator.net.l2monitoring.cfm.service.MepStoreDelegate;
Sean Condon0e89bda2017-03-21 14:23:19 +000041import org.onosproject.mastership.MastershipService;
42import org.onosproject.net.Device;
43import org.onosproject.net.DeviceId;
44import org.onosproject.net.device.DeviceEvent;
45import org.onosproject.net.device.DeviceListener;
46import org.onosproject.net.device.DeviceService;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070047import org.osgi.service.component.annotations.Activate;
48import org.osgi.service.component.annotations.Component;
49import org.osgi.service.component.annotations.Deactivate;
50import org.osgi.service.component.annotations.Reference;
51import org.osgi.service.component.annotations.ReferenceCardinality;
Sean Condon0e89bda2017-03-21 14:23:19 +000052import org.slf4j.Logger;
53
Ray Milkeyd84f89b2018-08-17 14:54:17 -070054import java.util.ArrayList;
55import java.util.Collection;
56import java.util.HashSet;
57import java.util.Iterator;
58import java.util.List;
59import java.util.Optional;
60import java.util.Set;
61import java.util.concurrent.ConcurrentLinkedQueue;
62import java.util.concurrent.Executors;
63import java.util.concurrent.ScheduledExecutorService;
64import java.util.concurrent.ScheduledFuture;
65import java.util.concurrent.TimeUnit;
66
67import static org.onlab.util.Tools.groupedThreads;
68import static org.slf4j.LoggerFactory.getLogger;
69
Sean Condon0e89bda2017-03-21 14:23:19 +000070/**
71 * Provides implementation of the CFM North and South Bound Interfaces.
72 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070073@Component(immediate = true, service = CfmMepService.class)
Sean Condon0e89bda2017-03-21 14:23:19 +000074public class CfmMepManager
75 extends AbstractListenerManager<CfmMepEvent, CfmMepListener>
76 implements CfmMepService {
77
78 private final Logger log = getLogger(getClass());
79
Sean Condon96b896d2017-12-11 12:44:29 -080080 private InternalDeviceListener deviceListener = null;
81 private InternalMdListener mdListener = null;
Sean Condon0e89bda2017-03-21 14:23:19 +000082
Ray Milkeyd84f89b2018-08-17 14:54:17 -070083 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Sean Condon0e89bda2017-03-21 14:23:19 +000084 protected DeviceService deviceService;
85
Ray Milkeyd84f89b2018-08-17 14:54:17 -070086 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Sean Condon0e89bda2017-03-21 14:23:19 +000087 protected CoreService coreService;
88
Ray Milkeyd84f89b2018-08-17 14:54:17 -070089 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Sean Condon0e89bda2017-03-21 14:23:19 +000090 protected MastershipService mastershipService;
91
Ray Milkeyd84f89b2018-08-17 14:54:17 -070092 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Sean Condon0e89bda2017-03-21 14:23:19 +000093 protected CfmMdService cfmMdService;
94
95 private static final int DEFAULT_POLL_FREQUENCY = 30;
96 private int fallbackMepPollFrequency = DEFAULT_POLL_FREQUENCY;
97
Sean Condon96b896d2017-12-11 12:44:29 -080098 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 Condon0e89bda2017-03-21 14:23:19 +0000115 private IdGenerator idGenerator;
116
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700117 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Sean Condon96b896d2017-12-11 12:44:29 -0800118 protected MepStore mepStore;
Sean Condon0e89bda2017-03-21 14:23:19 +0000119
Sean Condon96b896d2017-12-11 12:44:29 -0800120 protected final MepStoreDelegate delegate = new InternalStoreDelegate();
Sean Condon0e89bda2017-03-21 14:23:19 +0000121
122 @Activate
123 public void activate() {
Sean Condon96b896d2017-12-11 12:44:29 -0800124 mepStore.setDelegate(delegate);
Sean Condon0e89bda2017-03-21 14:23:19 +0000125
Sean Condon96b896d2017-12-11 12:44:29 -0800126 deviceListener = new InternalDeviceListener();
Sean Condon0e89bda2017-03-21 14:23:19 +0000127 deviceService.addListener(deviceListener);
Sean Condon96b896d2017-12-11 12:44:29 -0800128 mdListener = new InternalMdListener();
129 cfmMdService.addListener(mdListener);
130 eventDispatcher.addSink(CfmMepEvent.class, listenerRegistry);
Sean Condon0e89bda2017-03-21 14:23:19 +0000131 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 Condon96b896d2017-12-11 12:44:29 -0800138 cfmMdService.removeListener(mdListener);
Sean Condon0e89bda2017-03-21 14:23:19 +0000139 eventDispatcher.removeSink(CfmMepEvent.class);
140 log.info("CFM MEP Manager Stopped");
Sean Condon96b896d2017-12-11 12:44:29 -0800141 mepStore.unsetDelegate(delegate);
142 deviceListener = null;
143 mdListener = null;
Sean Condon0e89bda2017-03-21 14:23:19 +0000144 }
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 Condon96b896d2017-12-11 12:44:29 -0800152 Collection<Mep> mepStoreCollection = mepStore.getAllMeps();
Sean Condon0e89bda2017-03-21 14:23:19 +0000153 Collection<MepEntry> mepEntryCollection = new ArrayList<>();
154
Sean Condon96b896d2017-12-11 12:44:29 -0800155 for (Mep mep : mepStoreCollection) {
Sean Condon0e89bda2017-03-21 14:23:19 +0000156 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 Condon96b896d2017-12-11 12:44:29 -0800160 " for MEP: " + mdName + "/" + maName + "/" + mep.mepId());
Sean Condon0e89bda2017-03-21 14:23:19 +0000161 continue;
162 } else if (!deviceService.getDevice(mepDeviceId)
Sean Condon96b896d2017-12-11 12:44:29 -0800163 .is(CfmMepProgrammable.class)) {
Sean Condon0e89bda2017-03-21 14:23:19 +0000164 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 Condon96b896d2017-12-11 12:44:29 -0800169 + "on Device {}", mep.mepId(), mdName, maName, mepDeviceId);
Sean Condon0e89bda2017-03-21 14:23:19 +0000170 mepEntryCollection.add(deviceService
Sean Condon96b896d2017-12-11 12:44:29 -0800171 .getDevice(mepDeviceId)
172 .as(CfmMepProgrammable.class)
173 .getMep(mdName, maName, mep.mepId()));
Sean Condon0e89bda2017-03-21 14:23:19 +0000174 }
175 }
176
177 return mepEntryCollection;
178 }
179
180 @Override
Sean Condon96b896d2017-12-11 12:44:29 -0800181 public Collection<Mep> getAllMepsByDevice(DeviceId deviceId) throws CfmConfigException {
182 return mepStore.getMepsByDeviceId(deviceId);
Sean Condon0e89bda2017-03-21 14:23:19 +0000183 }
184
185 @Override
Sean Condon96b896d2017-12-11 12:44:29 -0800186 public MepEntry getMep(MdId mdName, MaIdShort maName, MepId mepId) throws CfmConfigException {
187 MepKeyId key = new MepKeyId(mdName, maName, mepId);
188
Sean Condon0e89bda2017-03-21 14:23:19 +0000189 //Will throw IllegalArgumentException if ma does not exist
190 cfmMdService.getMaintenanceAssociation(mdName, maName);
191
Sean Condon96b896d2017-12-11 12:44:29 -0800192 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 Condon0e89bda2017-03-21 14:23:19 +0000252 }
253 }
Sean Condon96b896d2017-12-11 12:44:29 -0800254 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 Condon0e89bda2017-03-21 14:23:19 +0000266 }
Sean Condon96b896d2017-12-11 12:44:29 -0800267
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 Condon0e89bda2017-03-21 14:23:19 +0000282 }
283
284 @Override
285 public boolean createMep(MdId mdName, MaIdShort maName, Mep newMep) throws CfmConfigException {
Sean Condon96b896d2017-12-11 12:44:29 -0800286 MepKeyId key = new MepKeyId(mdName, maName, newMep.mepId());
Sean Condon0e89bda2017-03-21 14:23:19 +0000287 log.debug("Creating MEP " + newMep.mepId() + " on MD {}, MA {} on Device {}",
288 mdName, maName, newMep.deviceId().toString());
Sean Condon96b896d2017-12-11 12:44:29 -0800289 if (mepStore.getMep(key).isPresent()) {
290 return false;
Sean Condon0e89bda2017-03-21 14:23:19 +0000291 }
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 Condon96b896d2017-12-11 12:44:29 -0800307 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 Condon0e89bda2017-03-21 14:23:19 +0000325 } else {
326 return deviceResult;
327 }
328 }
329
330 @Override
331 public void transmitLoopback(MdId mdName, MaIdShort maName,
Sean Condon96b896d2017-12-11 12:44:29 -0800332 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 Condon0e89bda2017-03-21 14:23:19 +0000343 }
344
345 @Override
346 public void abortLoopback(MdId mdName, MaIdShort maName, MepId mepId)
347 throws CfmConfigException {
Sean Condon96b896d2017-12-11 12:44:29 -0800348
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 Condon0e89bda2017-03-21 14:23:19 +0000359 }
360
361 @Override
362 public void transmitLinktrace(MdId mdName, MaIdShort maName, MepId mepId,
Sean Condon96b896d2017-12-11 12:44:29 -0800363 MepLtCreate ltCreate) {
Sean Condon0e89bda2017-03-21 14:23:19 +0000364 throw new UnsupportedOperationException("Not yet implemented");
365 }
366
Sean Condon96b896d2017-12-11 12:44:29 -0800367 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 Condon0e89bda2017-03-21 14:23:19 +0000390 private class InternalDeviceListener implements DeviceListener {
391 @Override
Sean Condon96b896d2017-12-11 12:44:29 -0800392 public boolean isRelevant(DeviceEvent event) {
393 return event.type().equals(DeviceEvent.Type.DEVICE_REMOVED);
394 }
395
396 @Override
Sean Condon0e89bda2017-03-21 14:23:19 +0000397 public void event(DeviceEvent event) {
Sean Condon96b896d2017-12-11 12:44:29 -0800398 DeviceId deviceId = event.subject().id();
Sean Condon0e89bda2017-03-21 14:23:19 +0000399 switch (event.type()) {
Sean Condon96b896d2017-12-11 12:44:29 -0800400 case DEVICE_ADDED:
401 case PORT_UPDATED:
402 case PORT_ADDED:
403 case DEVICE_UPDATED:
Sean Condon0e89bda2017-03-21 14:23:19 +0000404 case DEVICE_REMOVED:
405 case DEVICE_AVAILABILITY_CHANGED:
Sean Condon96b896d2017-12-11 12:44:29 -0800406 log.trace("Event {} received from Device Service", event.type());
407 scheduleEventHandlerIfNotScheduled(event);
Sean Condon0e89bda2017-03-21 14:23:19 +0000408 break;
409 default:
Sean Condon96b896d2017-12-11 12:44:29 -0800410 log.warn("Unhandled Event {} received from Device Service", event.type());
Sean Condon0e89bda2017-03-21 14:23:19 +0000411 break;
412 }
413 }
414 }
Sean Condon96b896d2017-12-11 12:44:29 -0800415
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 Condon0e89bda2017-03-21 14:23:19 +0000581}