Migrate implementation/neutron/southbound to IETF YANG model
[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.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;
26
27 /**
28  * DataListener for all Mapping modification events.
29  *
30  * @author Lorand Jakab
31  * @author Florin Coras
32  *
33  */
34 public class MappingDataListener extends AbstractDataListener {
35     private static final Logger LOG = LoggerFactory.getLogger(MappingDataListener.class);
36     private IMappingSystem mapSystem;
37     private NotificationPublishService notificationPublishService;
38
39     public MappingDataListener(DataBroker broker, IMappingSystem msmr, NotificationPublishService nps) {
40         setBroker(broker);
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();
47     }
48
49     public void setNotificationProviderService(NotificationPublishService nps) {
50         this.notificationPublishService = nps;
51     }
52
53     @Override
54     public void onDataChanged(
55             AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> change) {
56
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();
62
63                 LOG.trace("Received created data");
64                 LOG.trace("Key: {}", entry.getKey());
65                 LOG.trace("Value: {}", mapping);
66
67                 mapSystem.addMapping(mapping.getOrigin(), mapping.getMappingRecord().getEid(),
68                         mapping.getMappingRecord());
69             }
70         }
71
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();
77
78                 LOG.trace("Received changed data");
79                 LOG.trace("Key: {}", entry.getKey());
80                 LOG.trace("Value: {}", entry.getValue());
81
82                 mapSystem.addMapping(mapping.getOrigin(), mapping.getMappingRecord().getEid(),
83                         mapping.getMappingRecord());
84                 try {
85                     notificationPublishService.putNotification(MSNotificationInputUtil.toMappingChanged(mapping, MappingChange.Updated));
86                 } catch (InterruptedException e) {
87                     LOG.warn("Notification publication interrupted!");
88                 }
89             }
90         }
91
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;
98
99                 LOG.trace("Received deleted data");
100                 LOG.trace("Key: {}", entry);
101                 LOG.trace("Value: {}", dataObject);
102
103                 mapSystem.removeMapping(mapping.getOrigin(), mapping.getMappingRecord().getEid());
104                 try {
105                     notificationPublishService.putNotification(MSNotificationInputUtil.toMappingChanged(mapping, MappingChange.Removed));
106                 } catch (InterruptedException e) {
107                     LOG.warn("Notification publication interrupted!");
108                 }
109             }
110         }
111     }
112
113     void setMappingSystem(IMappingSystem msmr) {
114         this.mapSystem = msmr;
115     }
116 }