Merge branch 'master' into topic/ietf_yang
[lispflowmapping.git] / mappingservice / implementation / src / main / java / org / opendaylight / lispflowmapping / implementation / mdsal / MappingDataListener.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.lispflowmapping.implementation.mdsal;
9
10 import java.util.Map;
11 import java.util.Set;
12
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.lisp.proto.rev151105.eidtolocatorrecords.EidToLocatorRecordBuilder;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.MappingChange;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.MappingDatabase;
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.InstanceId;
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;
27
28 /**
29  * DataListener for all Mapping modification events.
30  *
31  * @author Lorand Jakab
32  * @author Florin Coras
33  *
34  */
35 public class MappingDataListener extends AbstractDataListener {
36     private static final Logger LOG = LoggerFactory.getLogger(MappingDataListener.class);
37     private IMappingSystem mapSystem;
38     private NotificationPublishService notificationPublishService;
39
40     public MappingDataListener(DataBroker broker, IMappingSystem msmr, NotificationPublishService nps) {
41         setBroker(broker);
42         setMappingSystem(msmr);
43         setNotificationProviderService(nps);
44         setPath(InstanceIdentifier.create(MappingDatabase.class).child(InstanceId.class)
45                 .child(Mapping.class));
46         LOG.trace("Registering Mapping listener.");
47         registerDataChangeListener();
48     }
49
50     public void setNotificationProviderService(NotificationPublishService nps) {
51         this.notificationPublishService = nps;
52     }
53
54     @Override
55     public void onDataChanged(
56             AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> change) {
57
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();
63
64                 LOG.trace("Received created data");
65                 LOG.trace("Key: {}", entry.getKey());
66                 LOG.trace("Value: {}", mapping);
67
68                 mapSystem.addMapping(mapping.getOrigin(), mapping.getLispAddressContainer(),
69                         new EidToLocatorRecordBuilder(mapping).build());
70             }
71         }
72
73         // Process updated mappings
74         Map<InstanceIdentifier<?>, DataObject> updatedData = change.getUpdatedData();
75         for (Map.Entry<InstanceIdentifier<?>, DataObject> entry : updatedData.entrySet()) {
76             if (entry.getValue() instanceof Mapping) {
77                 Mapping mapping = (Mapping)entry.getValue();
78
79                 LOG.trace("Received changed data");
80                 LOG.trace("Key: {}", entry.getKey());
81                 LOG.trace("Value: {}", entry.getValue());
82
83                 mapSystem.addMapping(mapping.getOrigin(), mapping.getLispAddressContainer(),
84                         new EidToLocatorRecordBuilder(mapping).build());
85                 try {
86                     notificationPublishService.putNotification(MSNotificationInputUtil.toMappingChanged(mapping, MappingChange.Updated));
87                 } catch (InterruptedException e) {
88                     LOG.warn("Notification publication interrupted!");
89                 }
90             }
91         }
92
93         // Process deleted mappings
94         Set<InstanceIdentifier<?>> removedData = change.getRemovedPaths();
95         for (InstanceIdentifier<?> entry : removedData) {
96             DataObject dataObject = change.getOriginalData().get(entry);
97             if (dataObject instanceof Mapping) {
98                 Mapping mapping = (Mapping)dataObject;
99
100                 LOG.trace("Received deleted data");
101                 LOG.trace("Key: {}", entry);
102                 LOG.trace("Value: {}", dataObject);
103
104                 mapSystem.removeMapping(mapping.getOrigin(), mapping.getLispAddressContainer());
105                 try {
106                     notificationPublishService.putNotification(MSNotificationInputUtil.toMappingChanged(mapping, MappingChange.Removed));
107                 } catch (InterruptedException e) {
108                     LOG.warn("Notification publication interrupted!");
109                 }
110             }
111         }
112     }
113
114     void setMappingSystem(IMappingSystem msmr) {
115         this.mapSystem = msmr;
116     }
117 }