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.db.instance.Mapping;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.mapping.database.VirtualNetworkIdentifier;
22 import org.opendaylight.yangtools.yang.binding.DataObject;
23 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
28 * DataListener for all Mapping modification events.
30 * @author Lorand Jakab
31 * @author Florin Coras
34 public class MappingDataListener extends AbstractDataListener {
35 private static final Logger LOG = LoggerFactory.getLogger(MappingDataListener.class);
36 private IMappingSystem mapSystem;
37 private NotificationPublishService notificationPublishService;
39 public MappingDataListener(DataBroker broker, IMappingSystem msmr, NotificationPublishService nps) {
41 setMappingSystem(msmr);
42 setNotificationProviderService(nps);
43 setPath(InstanceIdentifier.create(MappingDatabase.class).child(VirtualNetworkIdentifier.class)
44 .child(Mapping.class));
45 LOG.trace("Registering Mapping listener.");
46 registerDataChangeListener();
49 public void setNotificationProviderService(NotificationPublishService nps) {
50 this.notificationPublishService = nps;
54 public void onDataChanged(
55 AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> change) {
57 // Process newly created mappings
58 Map<InstanceIdentifier<?>, DataObject> createdData = change.getCreatedData();
59 for (Map.Entry<InstanceIdentifier<?>, DataObject> entry : createdData.entrySet()) {
60 if (entry.getValue() instanceof Mapping) {
61 Mapping mapping = (Mapping)entry.getValue();
63 LOG.trace("Received created data");
64 LOG.trace("Key: {}", entry.getKey());
65 LOG.trace("Value: {}", mapping);
67 mapSystem.addMapping(mapping.getOrigin(), mapping.getMappingRecord().getEid(),
68 mapping.getMappingRecord());
72 // Process updated mappings
73 Map<InstanceIdentifier<?>, DataObject> updatedData = change.getUpdatedData();
74 for (Map.Entry<InstanceIdentifier<?>, DataObject> entry : updatedData.entrySet()) {
75 if (entry.getValue() instanceof Mapping) {
76 Mapping mapping = (Mapping)entry.getValue();
78 LOG.trace("Received changed data");
79 LOG.trace("Key: {}", entry.getKey());
80 LOG.trace("Value: {}", entry.getValue());
82 mapSystem.addMapping(mapping.getOrigin(), mapping.getMappingRecord().getEid(),
83 mapping.getMappingRecord());
85 notificationPublishService.putNotification(MSNotificationInputUtil.toMappingChanged(mapping, MappingChange.Updated));
86 } catch (InterruptedException e) {
87 LOG.warn("Notification publication interrupted!");
92 // Process deleted mappings
93 Set<InstanceIdentifier<?>> removedData = change.getRemovedPaths();
94 for (InstanceIdentifier<?> entry : removedData) {
95 DataObject dataObject = change.getOriginalData().get(entry);
96 if (dataObject instanceof Mapping) {
97 Mapping mapping = (Mapping)dataObject;
99 LOG.trace("Received deleted data");
100 LOG.trace("Key: {}", entry);
101 LOG.trace("Value: {}", dataObject);
103 mapSystem.removeMapping(mapping.getOrigin(), mapping.getMappingRecord().getEid());
105 notificationPublishService.putNotification(MSNotificationInputUtil.toMappingChanged(mapping, MappingChange.Removed));
106 } catch (InterruptedException e) {
107 LOG.warn("Notification publication interrupted!");
113 void setMappingSystem(IMappingSystem msmr) {
114 this.mapSystem = msmr;