1114e319d4d48004ba7c31fe0ae1f8d0104e6aeb
[groupbasedpolicy.git] / renderers / ofoverlay / src / main / java / org / opendaylight / groupbasedpolicy / renderer / ofoverlay / node / FlowCapableNodeListener.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. 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.ofoverlay.node;
10
11 import static com.google.common.base.Preconditions.checkNotNull;
12
13 import java.util.Map.Entry;
14
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;
30
31 public class FlowCapableNodeListener implements DataChangeListener, AutoCloseable {
32
33     private static final Logger LOG = LoggerFactory.getLogger(FlowCapableNodeListener.class);
34
35     private final static InstanceIdentifier<FlowCapableNode> fcNodeIid = InstanceIdentifier.builder(Nodes.class)
36         .child(Node.class)
37         .augmentation(FlowCapableNode.class)
38         .build();
39     private final SwitchManager switchManager;
40     private final ListenerRegistration<DataChangeListener> listenerRegistration;
41
42     public FlowCapableNodeListener(DataBroker dataProvider, SwitchManager switchManager) {
43         this.switchManager = checkNotNull(switchManager);
44         listenerRegistration = checkNotNull(dataProvider).registerDataChangeListener(LogicalDatastoreType.OPERATIONAL,
45                 fcNodeIid, this, DataChangeScope.BASE);
46     }
47
48     @Override
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);
56             }
57         }
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);
64             }
65         }
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);
71             }
72         }
73     }
74
75     @Override
76     public void close() throws Exception {
77         if (listenerRegistration != null) {
78             listenerRegistration.close();
79         }
80     }
81
82 }