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.*;
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;
28 import java.math.BigInteger;
29 import java.util.concurrent.Callable;
31 public class FlowNodeConnectorInventoryTranslatorImpl extends AbstractNodeConnectorCommitter<FlowCapableNodeConnector> implements FlowNodeConnectorInventoryTranslator {
33 private static final Logger LOG = LoggerFactory.getLogger(FlowNodeConnectorInventoryTranslatorImpl.class);
35 private ListenerRegistration<FlowNodeConnectorInventoryTranslatorImpl> dataTreeChangeListenerRegistration;
37 public static final String SEPARATOR = ":";
39 private static final InstanceIdentifier<FlowCapableNodeConnector> II_TO_FLOW_CAPABLE_NODE_CONNECTOR
40 = InstanceIdentifier.builder(Nodes.class)
42 .child(NodeConnector.class)
43 .augmentation(FlowCapableNodeConnector.class)
46 private Multimap<BigInteger,String> dpnToPortMultiMap = Multimaps.synchronizedListMultimap(ArrayListMultimap.<BigInteger,String>create());
48 public FlowNodeConnectorInventoryTranslatorImpl(final ForwardingRulesManager manager, final DataBroker dataBroker){
49 super(manager, FlowCapableNodeConnector.class);
50 Preconditions.checkNotNull(dataBroker, "DataBroker can not be null!");
52 final DataTreeIdentifier<FlowCapableNodeConnector> treeId =
53 new DataTreeIdentifier<>(LogicalDatastoreType.OPERATIONAL, getWildCardPath());
55 SimpleTaskRetryLooper looper = new SimpleTaskRetryLooper(ForwardingRulesManagerImpl.STARTUP_LOOP_TICK,
56 ForwardingRulesManagerImpl.STARTUP_LOOP_MAX_RETRIES);
57 dataTreeChangeListenerRegistration = looper.loopUntilNoException(new Callable<ListenerRegistration<FlowNodeConnectorInventoryTranslatorImpl>>() {
59 public ListenerRegistration<FlowNodeConnectorInventoryTranslatorImpl> call() throws Exception {
60 return dataBroker.registerDataTreeChangeListener(treeId, FlowNodeConnectorInventoryTranslatorImpl.this);
63 } catch (final Exception e) {
64 LOG.warn(" FlowNodeConnectorInventoryTranslatorImpl listener registration fail!");
65 LOG.debug("FlowNodeConnectorInventoryTranslatorImpl DataChange listener registration fail ..", e);
66 throw new IllegalStateException("FlowNodeConnectorInventoryTranslatorImpl startup fail! System needs restart.", e);
71 protected InstanceIdentifier<FlowCapableNodeConnector> getWildCardPath(){
72 return InstanceIdentifier.create(Nodes.class)
74 .child(NodeConnector.class)
75 .augmentation(FlowCapableNodeConnector.class);
80 if (dataTreeChangeListenerRegistration != null) {
82 dataTreeChangeListenerRegistration.close();
83 } catch (final Exception e) {
84 LOG.warn("Error by stop FRM FlowNodeConnectorInventoryTranslatorImpl: {}", e.getMessage());
85 LOG.debug("Error by stop FRM FlowNodeConnectorInventoryTranslatorImpl..", e);
87 dataTreeChangeListenerRegistration = null;
91 public void remove(InstanceIdentifier<FlowCapableNodeConnector> identifier, FlowCapableNodeConnector del, InstanceIdentifier<FlowCapableNodeConnector> nodeConnIdent) {
92 if(compareInstanceIdentifierTail(identifier,II_TO_FLOW_CAPABLE_NODE_CONNECTOR)){
93 LOG.info("Node Connector removed");
94 String sNodeConnectorIdentifier = nodeConnIdent
95 .firstKeyOf(NodeConnector.class, NodeConnectorKey.class).getId().getValue();
96 BigInteger nDpId = getDpIdFromPortName(sNodeConnectorIdentifier);
97 String portName = del.getName();
99 dpnToPortMultiMap.remove(nDpId, sNodeConnectorIdentifier);
104 public void update(InstanceIdentifier<FlowCapableNodeConnector> identifier, FlowCapableNodeConnector original, FlowCapableNodeConnector update, InstanceIdentifier<FlowCapableNodeConnector> nodeConnIdent) {
105 if(compareInstanceIdentifierTail(identifier,II_TO_FLOW_CAPABLE_NODE_CONNECTOR)){
106 LOG.info("Node Connector updated");
107 //donot need to do anything as we are not considering updates here
112 public void add(InstanceIdentifier<FlowCapableNodeConnector> identifier, FlowCapableNodeConnector add, InstanceIdentifier<FlowCapableNodeConnector> nodeConnIdent) {
113 if(compareInstanceIdentifierTail(identifier,II_TO_FLOW_CAPABLE_NODE_CONNECTOR)){
114 LOG.info("Node Connector added");
115 String sNodeConnectorIdentifier = nodeConnIdent
116 .firstKeyOf(NodeConnector.class, NodeConnectorKey.class).getId().getValue();
117 BigInteger nDpId = getDpIdFromPortName(sNodeConnectorIdentifier);
119 String portName = add.getName();
120 if(!dpnToPortMultiMap.containsEntry(nDpId,sNodeConnectorIdentifier)) {
121 dpnToPortMultiMap.put(nDpId, sNodeConnectorIdentifier);
123 LOG.error("Duplicate Event.Node Connector already added");
128 private boolean compareInstanceIdentifierTail(InstanceIdentifier<?> identifier1,
129 InstanceIdentifier<?> identifier2) {
130 return Iterables.getLast(identifier1.getPathArguments()).equals(Iterables.getLast(identifier2.getPathArguments()));
134 public boolean isNodeConnectorUpdated(BigInteger dpId, String portName){
135 return dpnToPortMultiMap.containsEntry(dpId,portName) ;
139 private BigInteger getDpIdFromPortName(String portName) {
140 String dpId = portName.substring(portName.indexOf(SEPARATOR) + 1, portName.lastIndexOf(SEPARATOR));
141 return new BigInteger(dpId);