2 * Copyright (c) 2015 Inocybe Technologies and others. All rights reserved.
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
9 package org.opendaylight.groupbasedpolicy.renderer.iovisor.endpoint;
12 import java.util.Map.Entry;
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;
31 import com.google.common.base.Preconditions;
33 public class EndpointListener implements DataChangeListener, AutoCloseable {
35 private static final Logger LOG = LoggerFactory.getLogger(EndpointListener.class);
37 private final ListenerRegistration<DataChangeListener> registerListener;
39 private DataBroker dataBroker;
41 public EndpointListener(DataBroker dataBroker) {
42 this.dataBroker = dataBroker;
43 this.registerListener = dataBroker.registerDataChangeListener(
44 LogicalDatastoreType.OPERATIONAL,
45 IidFactory.endpointsIidWildcard().child(Endpoint.class),
47 AsyncDataBroker.DataChangeScope.SUBTREE);
51 public void onDataChanged(AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> changes) {
52 created(changes.getCreatedData());
53 updated(changes.getUpdatedData());
58 * Process created events.
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);
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
79 * Process updated events.
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
94 * Process REMOVED data.
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
108 * Get the object from MD-SAL based on the instance identifier.
111 * {@link InstanceIdentifier} of the related event
113 * Endpoint from the related event
114 * @return Endpoint constructed from the one gathered in the related event
116 private Endpoint fromMd(InstanceIdentifier<?> iid, Endpoint endpoint) {
117 EndpointBuilder result = new EndpointBuilder();
119 final EndpointKey endpointKey = iid.firstKeyOf(Endpoint.class);
120 if (endpointKey != null) {
121 result.setKey(endpointKey);
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());
134 return result.build();
138 public void close() throws Exception {
139 if (registerListener != null)
140 registerListener.close();