2 * Copyright (c) 2014 Cisco Systems, Inc. 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.ofoverlay.node;
11 import static com.google.common.base.Preconditions.checkNotNull;
13 import java.util.Map.Entry;
15 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
16 import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
17 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;
18 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
19 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
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 public class FlowCapableNodeListener implements DataChangeListener, AutoCloseable {
33 private static final Logger LOG = LoggerFactory.getLogger(FlowCapableNodeListener.class);
35 private final static InstanceIdentifier<FlowCapableNode> fcNodeIid = InstanceIdentifier.builder(Nodes.class)
37 .augmentation(FlowCapableNode.class)
39 private final SwitchManager switchManager;
40 private final ListenerRegistration<DataChangeListener> listenerRegistration;
42 public FlowCapableNodeListener(DataBroker dataProvider, SwitchManager switchManager) {
43 this.switchManager = checkNotNull(switchManager);
44 listenerRegistration = checkNotNull(dataProvider).registerDataChangeListener(LogicalDatastoreType.OPERATIONAL,
45 fcNodeIid, this, DataChangeScope.BASE);
49 public void onDataChanged(AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> change) {
50 for (Entry<InstanceIdentifier<?>, DataObject> fcNodeEntry : change.getCreatedData().entrySet()) {
51 if (FlowCapableNode.class.equals(fcNodeEntry.getKey().getTargetType())) {
52 NodeId nodeId = fcNodeEntry.getKey().firstKeyOf(Node.class, NodeKey.class).getId();
53 FlowCapableNode fcNode = (FlowCapableNode) fcNodeEntry.getValue();
54 LOG.trace("FlowCapableNode created. NodeId: {} FlowCapableNode: {}", nodeId.getValue(), fcNode);
55 switchManager.updateSwitch(nodeId, fcNode);
58 for (Entry<InstanceIdentifier<?>, DataObject> fcNodeEntry : change.getUpdatedData().entrySet()) {
59 if (FlowCapableNode.class.equals(fcNodeEntry.getKey().getTargetType())) {
60 NodeId nodeId = fcNodeEntry.getKey().firstKeyOf(Node.class, NodeKey.class).getId();
61 FlowCapableNode fcNode = (FlowCapableNode) fcNodeEntry.getValue();
62 LOG.trace("FlowCapableNode updated. NodeId: {} FlowCapableNode: {}", nodeId.getValue(), fcNode);
63 switchManager.updateSwitch(nodeId, fcNode);
66 for (InstanceIdentifier<?> removedFcNodeIid : change.getRemovedPaths()) {
67 if (FlowCapableNode.class.equals(removedFcNodeIid.getTargetType())) {
68 NodeId nodeId = removedFcNodeIid.firstKeyOf(Node.class, NodeKey.class).getId();
69 LOG.trace("FlowCapableNode removed. NodeId: {}", nodeId.getValue());
70 switchManager.updateSwitch(nodeId, null);
76 public void close() throws Exception {
77 if (listenerRegistration != null) {
78 listenerRegistration.close();