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