Convert DataChangeListeners to DataTreeChangeListeners
[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.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;
29
30 public class FlowCapableNodeListener implements DataTreeChangeListener<FlowCapableNode>, AutoCloseable {
31
32     private static final Logger LOG = LoggerFactory.getLogger(FlowCapableNodeListener.class);
33
34     private final static InstanceIdentifier<FlowCapableNode> fcNodeIid = InstanceIdentifier.builder(Nodes.class)
35         .child(Node.class)
36         .augmentation(FlowCapableNode.class)
37         .build();
38     private final SwitchManager switchManager;
39     private final ListenerRegistration<?> listenerRegistration;
40
41     public FlowCapableNodeListener(DataBroker dataProvider, SwitchManager switchManager) {
42         this.switchManager = checkNotNull(switchManager);
43         listenerRegistration = checkNotNull(dataProvider).registerDataTreeChangeListener(new DataTreeIdentifier<>(
44                 LogicalDatastoreType.OPERATIONAL, fcNodeIid), this);
45     }
46
47     @Override
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:
54                 case WRITE:
55                     FlowCapableNode fcNode = rootNode.getDataAfter();
56                     LOG.trace("FlowCapableNode updated. NodeId: {} FlowCapableNode: {}", nodeId.getValue(), fcNode);
57                     switchManager.updateSwitch(nodeId, fcNode);
58                     break;
59                 case DELETE:
60                     LOG.trace("FlowCapableNode removed. NodeId: {}", nodeId.getValue());
61                     switchManager.updateSwitch(nodeId, null);
62                     break;
63                 default:
64                     break;
65             }
66         }
67     }
68
69     @Override
70     public void close() throws Exception {
71         if (listenerRegistration != null) {
72             listenerRegistration.close();
73         }
74     }
75
76 }