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.Collection;
14 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
15 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
16 import org.opendaylight.controller.md.sal.binding.api.DataTreeChangeListener;
17 import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier;
18 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
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.InstanceIdentifier;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
30 public class FlowCapableNodeListener implements DataTreeChangeListener<FlowCapableNode>, AutoCloseable {
32 private static final Logger LOG = LoggerFactory.getLogger(FlowCapableNodeListener.class);
34 private final static InstanceIdentifier<FlowCapableNode> fcNodeIid = InstanceIdentifier.builder(Nodes.class)
36 .augmentation(FlowCapableNode.class)
38 private final SwitchManager switchManager;
39 private final ListenerRegistration<?> listenerRegistration;
41 public FlowCapableNodeListener(DataBroker dataProvider, SwitchManager switchManager) {
42 this.switchManager = checkNotNull(switchManager);
43 listenerRegistration = checkNotNull(dataProvider).registerDataTreeChangeListener(new DataTreeIdentifier<>(
44 LogicalDatastoreType.OPERATIONAL, fcNodeIid), this);
48 public void onDataTreeChanged(Collection<DataTreeModification<FlowCapableNode>> changes) {
49 for (DataTreeModification<FlowCapableNode> change: changes) {
50 DataObjectModification<FlowCapableNode> rootNode = change.getRootNode();
51 NodeId nodeId = change.getRootPath().getRootIdentifier().firstKeyOf(Node.class, NodeKey.class).getId();
52 switch (rootNode.getModificationType()) {
53 case SUBTREE_MODIFIED:
55 FlowCapableNode fcNode = rootNode.getDataAfter();
56 LOG.trace("FlowCapableNode updated. NodeId: {} FlowCapableNode: {}", nodeId.getValue(), fcNode);
57 switchManager.updateSwitch(nodeId, fcNode);
60 LOG.trace("FlowCapableNode removed. NodeId: {}", nodeId.getValue());
61 switchManager.updateSwitch(nodeId, null);
70 public void close() throws Exception {
71 if (listenerRegistration != null) {
72 listenerRegistration.close();