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.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;
29 public class EndpointListener implements DataChangeListener, AutoCloseable {
31 private static final Logger LOG = LoggerFactory.getLogger(EndpointListener.class);
33 private final ListenerRegistration<DataChangeListener> registerListener;
35 public EndpointListener(DataBroker dataBroker) {
36 this.registerListener = dataBroker.registerDataChangeListener(
37 LogicalDatastoreType.OPERATIONAL,
38 IidFactory.endpointsIidWildcard().child(Endpoint.class),
40 AsyncDataBroker.DataChangeScope.SUBTREE);
44 public void onDataChanged(AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> changes) {
45 created(changes.getCreatedData());
46 updated(changes.getUpdatedData());
51 * Process created events.
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
65 * Process updated events.
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
79 * Process REMOVED data.
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
93 * Get the object from MD-SAL based on the instance identifier.
96 * {@link InstanceIdentifier} of the related event
98 * Endpoint from the related event
99 * @return Endpoint constructed from the one gathered in the related event
101 @SuppressWarnings("deprecation")
102 private Endpoint fromMd(InstanceIdentifier<?> iid, Endpoint endpoint) {
103 EndpointBuilder result = new EndpointBuilder();
105 final EndpointKey endpointKey = iid.firstKeyOf(Endpoint.class, EndpointKey.class);
106 if (endpointKey != null) {
107 result.setKey(endpointKey);
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());
120 return result.build();
124 public void close() throws Exception {
125 if (registerListener != null)
126 registerListener.close();