2 * Copyright (c) 2015 Cisco Systems, Inc. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.lispflowmapping.implementation.mdsal;
13 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
14 import org.opendaylight.controller.md.sal.binding.api.NotificationPublishService;
15 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
16 import org.opendaylight.lispflowmapping.implementation.util.MSNotificationInputUtil;
17 import org.opendaylight.lispflowmapping.interfaces.mapcache.IMappingSystem;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.MappingChange;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.MappingDatabase;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.MappingOrigin;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.db.instance.Mapping;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.mapping.database.VirtualNetworkIdentifier;
23 import org.opendaylight.yangtools.yang.binding.DataObject;
24 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
29 * DataListener for all Mapping modification events.
31 * @author Lorand Jakab
32 * @author Florin Coras
35 public class MappingDataListener extends AbstractDataListener {
36 private static final Logger LOG = LoggerFactory.getLogger(MappingDataListener.class);
37 private IMappingSystem mapSystem;
38 private NotificationPublishService notificationPublishService;
40 public MappingDataListener(DataBroker broker, IMappingSystem msmr, NotificationPublishService nps) {
42 setMappingSystem(msmr);
43 setNotificationProviderService(nps);
44 setPath(InstanceIdentifier.create(MappingDatabase.class).child(VirtualNetworkIdentifier.class)
45 .child(Mapping.class));
46 LOG.trace("Registering Mapping listener.");
47 registerDataChangeListener();
50 public void setNotificationProviderService(NotificationPublishService nps) {
51 this.notificationPublishService = nps;
55 public void onDataChanged(
56 AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> change) {
58 // Process newly created mappings
59 Map<InstanceIdentifier<?>, DataObject> createdData = change.getCreatedData();
60 for (Map.Entry<InstanceIdentifier<?>, DataObject> entry : createdData.entrySet()) {
61 if (entry.getValue() instanceof Mapping) {
62 Mapping mapping = (Mapping)entry.getValue();
64 LOG.trace("Received created data");
65 LOG.trace("Key: {}", entry.getKey());
66 LOG.trace("Value: {}", mapping);
68 mapSystem.addMapping(mapping.getOrigin(), mapping.getMappingRecord().getEid(),
69 mapping.getMappingRecord());
71 if (mapping.getOrigin() == MappingOrigin.Northbound) {
72 // Only publish notifications for mapping changes caused by Northbound, since Southbound has a
73 // dedicated code path for detecting mapping updates. The notifications are used for sending SMR.
75 notificationPublishService.putNotification(MSNotificationInputUtil.toMappingChanged(mapping,
76 MappingChange.Created));
77 } catch (InterruptedException e) {
78 LOG.warn("Notification publication interrupted!");
84 // Process updated mappings
85 Map<InstanceIdentifier<?>, DataObject> updatedData = change.getUpdatedData();
86 for (Map.Entry<InstanceIdentifier<?>, DataObject> entry : updatedData.entrySet()) {
87 if (entry.getValue() instanceof Mapping) {
88 Mapping mapping = (Mapping)entry.getValue();
90 LOG.trace("Received changed data");
91 LOG.trace("Key: {}", entry.getKey());
92 LOG.trace("Value: {}", entry.getValue());
94 mapSystem.addMapping(mapping.getOrigin(), mapping.getMappingRecord().getEid(),
95 mapping.getMappingRecord());
97 if (mapping.getOrigin() == MappingOrigin.Northbound) {
98 // Only publish notifications for mapping changes caused by Northbound, since Southbound has a
99 // dedicated code path for detecting mapping updates. The notifications are used for sending SMR.
101 notificationPublishService.putNotification(MSNotificationInputUtil.toMappingChanged(mapping,
102 MappingChange.Updated));
103 } catch (InterruptedException e) {
104 LOG.warn("Notification publication interrupted!");
110 // Process deleted mappings
111 Set<InstanceIdentifier<?>> removedData = change.getRemovedPaths();
112 for (InstanceIdentifier<?> entry : removedData) {
113 DataObject dataObject = change.getOriginalData().get(entry);
114 if (dataObject instanceof Mapping) {
115 Mapping mapping = (Mapping)dataObject;
117 LOG.trace("Received deleted data");
118 LOG.trace("Key: {}", entry);
119 LOG.trace("Value: {}", dataObject);
121 mapSystem.removeMapping(mapping.getOrigin(), mapping.getMappingRecord().getEid());
123 notificationPublishService.putNotification(MSNotificationInputUtil.toMappingChanged(mapping, MappingChange.Removed));
124 } catch (InterruptedException e) {
125 LOG.warn("Notification publication interrupted!");
131 void setMappingSystem(IMappingSystem msmr) {
132 this.mapSystem = msmr;