2 * Copyright (c) 2015 Ericsson India Global Services Pvt Ltd. 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.openflowplugin.applications.frm.impl;
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;
34 public class FlowNodeConnectorInventoryTranslatorImpl extends AbstractNodeConnectorCommitter<FlowCapableNodeConnector> implements FlowNodeConnectorInventoryTranslator {
36 private static final Logger LOG = LoggerFactory.getLogger(FlowNodeConnectorInventoryTranslatorImpl.class);
38 private ListenerRegistration<FlowNodeConnectorInventoryTranslatorImpl> dataTreeChangeListenerRegistration;
40 private static final String SEPARATOR = ":";
42 private static final InstanceIdentifier<FlowCapableNodeConnector> II_TO_FLOW_CAPABLE_NODE_CONNECTOR
43 = InstanceIdentifier.builder(Nodes.class)
45 .child(NodeConnector.class)
46 .augmentation(FlowCapableNodeConnector.class)
49 private Multimap<BigInteger,String> dpnToPortMultiMap = Multimaps.synchronizedListMultimap(ArrayListMultimap.<BigInteger,String>create());
51 public FlowNodeConnectorInventoryTranslatorImpl(final ForwardingRulesManager manager, final DataBroker dataBroker){
52 super(manager, FlowCapableNodeConnector.class);
53 Preconditions.checkNotNull(dataBroker, "DataBroker can not be null!");
55 final DataTreeIdentifier<FlowCapableNodeConnector> treeId =
56 new DataTreeIdentifier<>(LogicalDatastoreType.OPERATIONAL, getWildCardPath());
58 SimpleTaskRetryLooper looper = new SimpleTaskRetryLooper(ForwardingRulesManagerImpl.STARTUP_LOOP_TICK,
59 ForwardingRulesManagerImpl.STARTUP_LOOP_MAX_RETRIES);
60 dataTreeChangeListenerRegistration = looper.loopUntilNoException(new Callable<ListenerRegistration<FlowNodeConnectorInventoryTranslatorImpl>>() {
62 public ListenerRegistration<FlowNodeConnectorInventoryTranslatorImpl> call() throws Exception {
63 return dataBroker.registerDataTreeChangeListener(treeId, FlowNodeConnectorInventoryTranslatorImpl.this);
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);
74 protected InstanceIdentifier<FlowCapableNodeConnector> getWildCardPath(){
75 return InstanceIdentifier.create(Nodes.class)
77 .child(NodeConnector.class)
78 .augmentation(FlowCapableNodeConnector.class);
83 if (dataTreeChangeListenerRegistration != null) {
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);
90 dataTreeChangeListenerRegistration = null;
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);
101 dpnToPortMultiMap.remove(nDpId, sNodeConnectorIdentifier);
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
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);
121 if(!dpnToPortMultiMap.containsEntry(nDpId,sNodeConnectorIdentifier)) {
122 dpnToPortMultiMap.put(nDpId, sNodeConnectorIdentifier);
124 LOG.error("Duplicate Event.Node Connector already added");
129 private boolean compareInstanceIdentifierTail(InstanceIdentifier<?> identifier1,
130 InstanceIdentifier<?> identifier2) {
131 return Iterables.getLast(identifier1.getPathArguments()).equals(Iterables.getLast(identifier2.getPathArguments()));
135 public boolean isNodeConnectorUpdated(BigInteger dpId, String portName){
136 return dpnToPortMultiMap.containsEntry(dpId,portName) ;
140 private BigInteger getDpIdFromPortName(String portName) {
141 String dpId = portName.substring(portName.indexOf(SEPARATOR) + 1, portName.lastIndexOf(SEPARATOR));
142 return new BigInteger(dpId);