Rework IOVisor model and validate IovisorModuleInstance on Endpoint created events
[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.renderer.iovisor.utils.IovisorModuleUtils;
20 import org.opendaylight.groupbasedpolicy.util.IidFactory;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.Endpoint;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.EndpointBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.EndpointKey;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.iovisor.rev151030.IovisorModuleAugmentation;
25 import org.opendaylight.yangtools.concepts.ListenerRegistration;
26 import org.opendaylight.yangtools.yang.binding.DataObject;
27 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 import com.google.common.base.Preconditions;
32
33 public class EndpointListener implements DataChangeListener, AutoCloseable {
34
35     private static final Logger LOG = LoggerFactory.getLogger(EndpointListener.class);
36
37     private final ListenerRegistration<DataChangeListener> registerListener;
38
39     private DataBroker dataBroker;
40
41     public EndpointListener(DataBroker dataBroker) {
42         this.dataBroker = dataBroker;
43         this.registerListener = dataBroker.registerDataChangeListener(
44                                                 LogicalDatastoreType.OPERATIONAL,
45                                                 IidFactory.endpointsIidWildcard().child(Endpoint.class),
46                                                 this,
47                                                 AsyncDataBroker.DataChangeScope.SUBTREE);
48     }
49
50     @Override
51     public void onDataChanged(AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> changes) {
52         created(changes.getCreatedData());
53         updated(changes.getUpdatedData());
54         removed(changes);
55     }
56
57     /**
58      * Process created events.
59      *
60      * @param created
61      *            Created data
62      */
63     private void created(Map<InstanceIdentifier<?>, DataObject> created) {
64         for (Entry<InstanceIdentifier<?>, DataObject> newEndpoint : created.entrySet()) {
65             Endpoint endpoint = fromMd(newEndpoint.getKey(), (Endpoint) newEndpoint.getValue());
66             LOG.debug("Endpoint CREATED {}", endpoint);
67
68             // Validate the IOVisorModuleInstance
69             IovisorModuleAugmentation iovisorModuleAugmentation = endpoint.getAugmentation(IovisorModuleAugmentation.class);
70             Preconditions.checkNotNull(iovisorModuleAugmentation.getUri(), "At this point, the Endpoint should be provided with a IovisorModuleInstance");
71             if (IovisorModuleUtils.validateIovisorModuleInstance(dataBroker, iovisorModuleAugmentation.getUri())) {
72                 LOG.debug("This Endpoint {} provides a valid IovisorModuleInstance {}", endpoint, iovisorModuleAugmentation.getUri());
73                 // TODO process validated endpoint
74             }
75         }
76     }
77
78     /**
79      * Process updated events.
80      *
81      * @param updated
82      *            updated data
83      */
84     private void updated(Map<InstanceIdentifier<?>, DataObject> updated) {
85         for (Entry<InstanceIdentifier<?>, DataObject> updatedEndpoint : updated.entrySet()) {
86             Endpoint endpoint = fromMd(updatedEndpoint.getKey(), (Endpoint) updatedEndpoint.getValue());
87             LOG.debug("Endpoint UPDATED {}", endpoint);
88             //TODO process updated event
89
90         }
91     }
92
93     /**
94      * Process REMOVED data.
95      *
96      * @param changes
97      *            Changes data
98      */
99     private void removed(AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> changes) {
100         for (InstanceIdentifier<?> deletedEndpointPath : changes.getRemovedPaths()) {
101             Endpoint endpoint = fromMd(deletedEndpointPath, (Endpoint) changes.getOriginalData().get(deletedEndpointPath));
102             LOG.debug("Endpoint REMOVED {}", endpoint);
103             // TODO process removed event
104         }
105     }
106
107     /**
108      * Get the object from MD-SAL based on the instance identifier.
109      *
110      * @param iid
111      *            {@link InstanceIdentifier} of the related event
112      * @param endpoint
113      *            Endpoint from the related event
114      * @return Endpoint constructed from the one gathered in the related event
115      */
116     private Endpoint fromMd(InstanceIdentifier<?> iid, Endpoint endpoint) {
117         EndpointBuilder result = new EndpointBuilder();
118
119         final EndpointKey endpointKey = iid.firstKeyOf(Endpoint.class);
120         if (endpointKey != null) {
121             result.setKey(endpointKey);
122         }
123
124         result.setCondition(endpoint.getCondition());
125         result.setEndpointGroup(endpoint.getEndpointGroup());
126         result.setEndpointGroups(endpoint.getEndpointGroups());
127         result.setL2Context(endpoint.getL2Context());
128         result.setL3Address(endpoint.getL3Address());
129         result.setMacAddress(endpoint.getMacAddress());
130         result.setNetworkContainment(endpoint.getNetworkContainment());
131         result.setTenant(endpoint.getTenant());
132         result.setTimestamp(endpoint.getTimestamp());
133
134         return result.build();
135     }
136
137     @Override
138     public void close() throws Exception {
139         if (registerListener != null)
140             registerListener.close();
141     }
142 }