a48c7411006fd3539f5b3f4b47a475327eaaf9c7
[groupbasedpolicy.git] / renderers / iovisor / src / main / java / org / opendaylight / groupbasedpolicy / renderer / iovisor / endpoint / EndpointListener.java
1 /*
2  * Copyright (c) 2015 Inocybe Technologies and others. 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
9 package org.opendaylight.groupbasedpolicy.renderer.iovisor.endpoint;
10
11 import java.util.Map;
12 import java.util.Map.Entry;
13
14 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
15 import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
16 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker;
17 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
18 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
19 import org.opendaylight.groupbasedpolicy.util.IidFactory;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.Endpoint;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.EndpointBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.EndpointKey;
23 import org.opendaylight.yangtools.concepts.ListenerRegistration;
24 import org.opendaylight.yangtools.yang.binding.DataObject;
25 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 public class EndpointListener implements DataChangeListener, AutoCloseable {
30
31     private static final Logger LOG = LoggerFactory.getLogger(EndpointListener.class);
32
33     private final ListenerRegistration<DataChangeListener> registerListener;
34
35     public EndpointListener(DataBroker dataBroker) {
36         this.registerListener = dataBroker.registerDataChangeListener(
37                                                 LogicalDatastoreType.OPERATIONAL,
38                                                 IidFactory.endpointsIidWildcard().child(Endpoint.class),
39                                                 this,
40                                                 AsyncDataBroker.DataChangeScope.SUBTREE);
41     }
42
43     @Override
44     public void onDataChanged(AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> changes) {
45         created(changes.getCreatedData());
46         updated(changes.getUpdatedData());
47         removed(changes);
48     }
49
50     /**
51      * Process created events.
52      *
53      * @param created
54      *            Created data
55      */
56     private void created(Map<InstanceIdentifier<?>, DataObject> created) {
57         for (Entry<InstanceIdentifier<?>, DataObject> newEndpoint : created.entrySet()) {
58             Endpoint endpoint = fromMd(newEndpoint.getKey(), (Endpoint) newEndpoint.getValue());
59             LOG.info("Endpoint CREATED {}", endpoint);
60             // TODO process created event
61         }
62     }
63
64     /**
65      * Process updated events.
66      *
67      * @param updated
68      *            updated data
69      */
70     private void updated(Map<InstanceIdentifier<?>, DataObject> updated) {
71         for (Entry<InstanceIdentifier<?>, DataObject> updatedEndpoint : updated.entrySet()) {
72             Endpoint endpoint = fromMd(updatedEndpoint.getKey(), (Endpoint) updatedEndpoint.getValue());
73             LOG.info("Endpoint UPDATED {}", endpoint);
74             //TODO process updated event
75         }
76     }
77
78     /**
79      * Process REMOVED data.
80      *
81      * @param changes
82      *            Changes data
83      */
84     private void removed(AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> changes) {
85         for (InstanceIdentifier<?> deletedEndpointPath : changes.getRemovedPaths()) {
86             Endpoint endpoint = fromMd(deletedEndpointPath, (Endpoint) changes.getOriginalData().get(deletedEndpointPath));
87             LOG.info("Endpoint REMOVED {}", endpoint);
88             // TODO process removed event
89         }
90     }
91
92     /**
93      * Get the object from MD-SAL based on the instance identifier.
94      *
95      * @param iid
96      *            {@link InstanceIdentifier} of the related event
97      * @param endpoint
98      *            Endpoint from the related event
99      * @return Endpoint constructed from the one gathered in the related event
100      */
101     @SuppressWarnings("deprecation")
102     private Endpoint fromMd(InstanceIdentifier<?> iid, Endpoint endpoint) {
103         EndpointBuilder result = new EndpointBuilder();
104
105         final EndpointKey endpointKey = iid.firstKeyOf(Endpoint.class, EndpointKey.class);
106         if (endpointKey != null) {
107             result.setKey(endpointKey);
108         }
109
110         result.setCondition(endpoint.getCondition());
111         result.setEndpointGroup(endpoint.getEndpointGroup());
112         result.setEndpointGroups(endpoint.getEndpointGroups());
113         result.setL2Context(endpoint.getL2Context());
114         result.setL3Address(endpoint.getL3Address());
115         result.setMacAddress(endpoint.getMacAddress());
116         result.setNetworkContainment(endpoint.getNetworkContainment());
117         result.setTenant(endpoint.getTenant());
118         result.setTimestamp(endpoint.getTimestamp());
119
120         return result.build();
121     }
122
123     @Override
124     public void close() throws Exception {
125         if (registerListener != null)
126             registerListener.close();
127     }
128 }