30b06d271acc2b3bf1e693bb92a071a91849fb81
[groupbasedpolicy.git] / renderers / ofoverlay / src / main / java / org / opendaylight / groupbasedpolicy / renderer / ofoverlay / endpoint / EndpointManagerListener.java
1 package org.opendaylight.groupbasedpolicy.renderer.ofoverlay.endpoint;
2
3 import static com.google.common.base.Preconditions.checkNotNull;
4
5 import java.util.Map;
6
7 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
8 import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
9 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker;
10 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
11 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
12 import org.opendaylight.groupbasedpolicy.util.IidFactory;
13 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.Endpoint;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.EndpointL3;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.EndpointL3Prefix;
16 import org.opendaylight.yangtools.concepts.ListenerRegistration;
17 import org.opendaylight.yangtools.yang.binding.DataObject;
18 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
19
20 public class EndpointManagerListener implements DataChangeListener, AutoCloseable {
21
22     private final ListenerRegistration<DataChangeListener> registerListener;
23     private final EndpointManager endpointManager;
24
25     public EndpointManagerListener(DataBroker dataProvider, EndpointManager endpointManager) {
26         this.endpointManager = checkNotNull(endpointManager);
27         this.registerListener = checkNotNull(dataProvider).registerDataChangeListener(
28                 LogicalDatastoreType.OPERATIONAL, IidFactory.endpointsIidWildcard(), this, AsyncDataBroker.DataChangeScope.SUBTREE);
29     }
30
31     @Override
32     public void onDataChanged(AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> change) {
33         //Create
34         for (DataObject dao : change.getCreatedData().values()) {
35             if (dao instanceof Endpoint) {
36                 endpointManager.processEndpoint(null, (Endpoint) dao);
37             } else if (dao instanceof EndpointL3) {
38                 endpointManager.processL3Endpoint(null, (EndpointL3) dao);
39             } else if (dao instanceof EndpointL3Prefix) {
40                 //noinspection UnnecessaryContinue
41                 continue;
42             }
43         }
44         //Update
45         Map<InstanceIdentifier<?>, DataObject> dao = change.getUpdatedData();
46         for (Map.Entry<InstanceIdentifier<?>, DataObject> entry : dao.entrySet()) {
47             if (entry.getValue() instanceof Endpoint) {
48                 Endpoint oldEp = (Endpoint) change.getOriginalData().get(entry.getKey());
49                 endpointManager.processEndpoint(oldEp, (Endpoint) entry.getValue());
50             } else if (entry.getValue() instanceof EndpointL3) {
51                 EndpointL3 oldEp3 = (EndpointL3) change.getOriginalData().get(entry.getKey());
52                 endpointManager.processL3Endpoint(oldEp3, (EndpointL3) entry.getValue());
53             } else if (entry.getValue() instanceof EndpointL3Prefix) {
54                 //noinspection UnnecessaryContinue
55                 continue;
56             }
57         }
58         //Remove
59         for (InstanceIdentifier<?> iid : change.getRemovedPaths()) {
60             DataObject old = change.getOriginalData().get(iid);
61             if (old == null) {
62                 continue;
63             }
64             if (old instanceof Endpoint) {
65                 endpointManager.processEndpoint((Endpoint) old, null);
66             } else if (old instanceof EndpointL3) {
67                 endpointManager.processL3Endpoint((EndpointL3) old, null);
68             } else if (old instanceof EndpointL3Prefix) {
69                 //noinspection UnnecessaryContinue
70                 continue;
71             }
72         }
73     }
74
75     @Override
76     public void close() throws Exception {
77         if (registerListener != null)
78             registerListener.close();
79     }
80 }