Merge "Bug 5257: Increase subscriber TTL to 1 day"
[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.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;
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(VirtualNetworkIdentifier.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                 // Only treat mapping changes caused by Northbound, since Southbound changes are already handled
65                 // before being persisted. XXX separate NB and SB to avoid ignoring SB notifications
66                 if (mapping.getOrigin() == MappingOrigin.Southbound) {
67                     continue;
68                 }
69
70                 LOG.trace("Received created data");
71                 LOG.trace("Key: {}", entry.getKey());
72                 LOG.trace("Value: {}", mapping);
73
74                 mapSystem.addMapping(mapping.getOrigin(), mapping.getMappingRecord().getEid(),
75                         mapping.getMappingRecord());
76
77                 try {
78                     // The notifications are used for sending SMR.
79                     notificationPublishService.putNotification(MSNotificationInputUtil.toMappingChanged(mapping,
80                             MappingChange.Created));
81                 } catch (InterruptedException e) {
82                     LOG.warn("Notification publication interrupted!");
83                 }
84             }
85         }
86
87         // Process updated mappings
88         Map<InstanceIdentifier<?>, DataObject> updatedData = change.getUpdatedData();
89         for (Map.Entry<InstanceIdentifier<?>, DataObject> entry : updatedData.entrySet()) {
90             if (entry.getValue() instanceof Mapping) {
91                 Mapping mapping = (Mapping)entry.getValue();
92
93                 // Only treat mapping changes caused by Northbound, since Southbound changes are already handled
94                 // before being persisted.
95                 if (mapping.getOrigin() == MappingOrigin.Southbound) {
96                     continue;
97                 }
98
99                 LOG.trace("Received changed data");
100                 LOG.trace("Key: {}", entry.getKey());
101                 LOG.trace("Value: {}", entry.getValue());
102
103                 mapSystem.addMapping(mapping.getOrigin(), mapping.getMappingRecord().getEid(),
104                         mapping.getMappingRecord());
105
106                 // The notifications are used for sending SMR.
107                 try {
108                     notificationPublishService.putNotification(MSNotificationInputUtil.toMappingChanged(mapping,
109                             MappingChange.Updated));
110                 } catch (InterruptedException e) {
111                     LOG.warn("Notification publication interrupted!");
112                 }
113             }
114         }
115
116         // Process deleted mappings
117         Set<InstanceIdentifier<?>> removedData = change.getRemovedPaths();
118         for (InstanceIdentifier<?> entry : removedData) {
119             DataObject dataObject = change.getOriginalData().get(entry);
120             if (dataObject instanceof Mapping) {
121                 Mapping mapping = (Mapping)dataObject;
122
123                 // Only treat mapping changes caused by Northbound, since Southbound changes are already handled
124                 // before being persisted.
125                 if (mapping.getOrigin() == MappingOrigin.Southbound) {
126                     continue;
127                 }
128
129                 LOG.trace("Received deleted data");
130                 LOG.trace("Key: {}", entry);
131                 LOG.trace("Value: {}", dataObject);
132
133                 mapSystem.removeMapping(mapping.getOrigin(), mapping.getMappingRecord().getEid());
134                 try {
135                     notificationPublishService.putNotification(MSNotificationInputUtil.toMappingChanged(mapping, MappingChange.Removed));
136                 } catch (InterruptedException e) {
137                     LOG.warn("Notification publication interrupted!");
138                 }
139             }
140         }
141     }
142
143     void setMappingSystem(IMappingSystem msmr) {
144         this.mapSystem = msmr;
145     }
146 }