e4b032e370b5875e5852e1d4f4d484e9ed325f62
[openflowplugin.git] / applications / forwardingrules-manager / src / main / java / org / opendaylight / openflowplugin / applications / frm / impl / FlowNodeConnectorInventoryTranslatorImpl.java
1 /**
2  * Copyright (c) 2015 Ericsson India Global Services Pvt Ltd. 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.openflowplugin.applications.frm.impl;
10
11 import com.google.common.base.Preconditions;
12 import com.google.common.collect.*;
13 import org.opendaylight.controller.md.sal.binding.api.*;
14 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
15 import org.opendaylight.openflowplugin.applications.frm.FlowNodeConnectorInventoryTranslator;
16 import org.opendaylight.openflowplugin.applications.frm.ForwardingRulesManager;
17 import org.opendaylight.openflowplugin.common.wait.SimpleTaskRetryLooper;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeConnector;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorKey;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
23 import org.opendaylight.yangtools.concepts.ListenerRegistration;
24 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 import java.util.concurrent.Callable;
29
30 public class FlowNodeConnectorInventoryTranslatorImpl extends AbstractNodeConnectorCommitter<FlowCapableNodeConnector> implements FlowNodeConnectorInventoryTranslator {
31
32     private static final Logger LOG = LoggerFactory.getLogger(FlowNodeConnectorInventoryTranslatorImpl.class);
33
34     private ListenerRegistration<FlowNodeConnectorInventoryTranslatorImpl> dataTreeChangeListenerRegistration;
35
36     public static final String SEPARATOR = ":";
37
38     private static final InstanceIdentifier<FlowCapableNodeConnector> II_TO_FLOW_CAPABLE_NODE_CONNECTOR
39             = InstanceIdentifier.builder(Nodes.class)
40             .child(Node.class)
41             .child(NodeConnector.class)
42             .augmentation(FlowCapableNodeConnector.class)
43             .build();
44
45     private Multimap<Long,String> dpnToPortMultiMap = Multimaps.synchronizedListMultimap(ArrayListMultimap.<Long,String>create());
46
47     public FlowNodeConnectorInventoryTranslatorImpl(final ForwardingRulesManager manager, final DataBroker dataBroker){
48         super(manager, FlowCapableNodeConnector.class);
49         Preconditions.checkNotNull(dataBroker, "DataBroker can not be null!");
50
51         final DataTreeIdentifier<FlowCapableNodeConnector> treeId =
52                 new DataTreeIdentifier<>(LogicalDatastoreType.OPERATIONAL, getWildCardPath());
53         try {
54             SimpleTaskRetryLooper looper = new SimpleTaskRetryLooper(ForwardingRulesManagerImpl.STARTUP_LOOP_TICK,
55                     ForwardingRulesManagerImpl.STARTUP_LOOP_MAX_RETRIES);
56             dataTreeChangeListenerRegistration = looper.loopUntilNoException(new Callable<ListenerRegistration<FlowNodeConnectorInventoryTranslatorImpl>>() {
57                 @Override
58                 public ListenerRegistration<FlowNodeConnectorInventoryTranslatorImpl> call() throws Exception {
59                     return dataBroker.registerDataTreeChangeListener(treeId, FlowNodeConnectorInventoryTranslatorImpl.this);
60                 }
61             });
62         } catch (final Exception e) {
63             LOG.warn(" FlowNodeConnectorInventoryTranslatorImpl listener registration fail!");
64             LOG.debug("FlowNodeConnectorInventoryTranslatorImpl DataChange listener registration fail ..", e);
65             throw new IllegalStateException("FlowNodeConnectorInventoryTranslatorImpl startup fail! System needs restart.", e);
66         }
67     }
68
69     @Override
70     protected InstanceIdentifier<FlowCapableNodeConnector> getWildCardPath(){
71         return InstanceIdentifier.create(Nodes.class)
72                 .child(Node.class)
73                 .child(NodeConnector.class)
74                 .augmentation(FlowCapableNodeConnector.class);
75     }
76
77     @Override
78     public void close() {
79         if (dataTreeChangeListenerRegistration != null) {
80             try {
81                 dataTreeChangeListenerRegistration.close();
82             } catch (final Exception e) {
83                 LOG.warn("Error by stop FRM FlowNodeConnectorInventoryTranslatorImpl: {}", e.getMessage());
84                 LOG.debug("Error by stop FRM FlowNodeConnectorInventoryTranslatorImpl..", e);
85             }
86             dataTreeChangeListenerRegistration = null;
87         }
88     }
89     @Override
90     public void remove(InstanceIdentifier<FlowCapableNodeConnector> identifier, FlowCapableNodeConnector del, InstanceIdentifier<FlowCapableNodeConnector> nodeConnIdent) {
91         if(compareInstanceIdentifierTail(identifier,II_TO_FLOW_CAPABLE_NODE_CONNECTOR)){
92             LOG.info("Node Connector removed");
93             String sNodeConnectorIdentifier = nodeConnIdent
94                     .firstKeyOf(NodeConnector.class, NodeConnectorKey.class).getId().getValue();
95             long nDpId = getDpIdFromPortName(sNodeConnectorIdentifier);
96             String portName = del.getName();
97
98             dpnToPortMultiMap.remove(nDpId, sNodeConnectorIdentifier);
99         }
100     }
101
102     @Override
103     public void update(InstanceIdentifier<FlowCapableNodeConnector> identifier, FlowCapableNodeConnector original, FlowCapableNodeConnector update, InstanceIdentifier<FlowCapableNodeConnector> nodeConnIdent) {
104         if(compareInstanceIdentifierTail(identifier,II_TO_FLOW_CAPABLE_NODE_CONNECTOR)){
105             LOG.info("Node Connector updated");
106             //donot need to do anything as we are not considering updates here
107         }
108     }
109
110     @Override
111     public void add(InstanceIdentifier<FlowCapableNodeConnector> identifier, FlowCapableNodeConnector add, InstanceIdentifier<FlowCapableNodeConnector> nodeConnIdent) {
112         if(compareInstanceIdentifierTail(identifier,II_TO_FLOW_CAPABLE_NODE_CONNECTOR)){
113             LOG.info("Node Connector added");
114             String sNodeConnectorIdentifier = nodeConnIdent
115                     .firstKeyOf(NodeConnector.class, NodeConnectorKey.class).getId().getValue();
116             long nDpId = getDpIdFromPortName(sNodeConnectorIdentifier);
117
118             String portName = add.getName();
119             if(!dpnToPortMultiMap.containsEntry(nDpId,sNodeConnectorIdentifier)) {
120                 dpnToPortMultiMap.put(nDpId, sNodeConnectorIdentifier);
121             }else{
122                 LOG.error("Duplicate Event.Node Connector already added");
123             }
124         }
125     }
126
127     private boolean compareInstanceIdentifierTail(InstanceIdentifier<?> identifier1,
128                                   InstanceIdentifier<?> identifier2) {
129         return Iterables.getLast(identifier1.getPathArguments()).equals(Iterables.getLast(identifier2.getPathArguments()));
130     }
131
132     @Override
133     public boolean isNodeConnectorUpdated(long dpId, String portName){
134         return dpnToPortMultiMap.containsEntry(dpId,portName) ;
135     }
136
137
138     private long getDpIdFromPortName(String portName) {
139         String dpId = portName.substring(portName.indexOf(SEPARATOR) + 1, portName.lastIndexOf(SEPARATOR));
140         return Long.parseLong(dpId);
141     }
142 }
143