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.util.concurrent.Callable;
30 public class FlowNodeConnectorInventoryTranslatorImpl extends AbstractNodeConnectorCommitter<FlowCapableNodeConnector> implements FlowNodeConnectorInventoryTranslator {
32 private static final Logger LOG = LoggerFactory.getLogger(FlowNodeConnectorInventoryTranslatorImpl.class);
34 private ListenerRegistration<FlowNodeConnectorInventoryTranslatorImpl> dataTreeChangeListenerRegistration;
36 public static final String SEPARATOR = ":";
38 private static final InstanceIdentifier<FlowCapableNodeConnector> II_TO_FLOW_CAPABLE_NODE_CONNECTOR
39 = InstanceIdentifier.builder(Nodes.class)
41 .child(NodeConnector.class)
42 .augmentation(FlowCapableNodeConnector.class)
45 private Multimap<Long,String> dpnToPortMultiMap = Multimaps.synchronizedListMultimap(ArrayListMultimap.<Long,String>create());
47 public FlowNodeConnectorInventoryTranslatorImpl(final ForwardingRulesManager manager, final DataBroker dataBroker){
48 super(manager, FlowCapableNodeConnector.class);
49 Preconditions.checkNotNull(dataBroker, "DataBroker can not be null!");
51 final DataTreeIdentifier<FlowCapableNodeConnector> treeId =
52 new DataTreeIdentifier<>(LogicalDatastoreType.OPERATIONAL, getWildCardPath());
54 SimpleTaskRetryLooper looper = new SimpleTaskRetryLooper(ForwardingRulesManagerImpl.STARTUP_LOOP_TICK,
55 ForwardingRulesManagerImpl.STARTUP_LOOP_MAX_RETRIES);
56 dataTreeChangeListenerRegistration = looper.loopUntilNoException(new Callable<ListenerRegistration<FlowNodeConnectorInventoryTranslatorImpl>>() {
58 public ListenerRegistration<FlowNodeConnectorInventoryTranslatorImpl> call() throws Exception {
59 return dataBroker.registerDataTreeChangeListener(treeId, FlowNodeConnectorInventoryTranslatorImpl.this);
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);
70 protected InstanceIdentifier<FlowCapableNodeConnector> getWildCardPath(){
71 return InstanceIdentifier.create(Nodes.class)
73 .child(NodeConnector.class)
74 .augmentation(FlowCapableNodeConnector.class);
79 if (dataTreeChangeListenerRegistration != null) {
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);
86 dataTreeChangeListenerRegistration = null;
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();
98 dpnToPortMultiMap.remove(nDpId, sNodeConnectorIdentifier);
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
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);
118 String portName = add.getName();
119 if(!dpnToPortMultiMap.containsEntry(nDpId,sNodeConnectorIdentifier)) {
120 dpnToPortMultiMap.put(nDpId, sNodeConnectorIdentifier);
122 LOG.error("Duplicate Event.Node Connector already added");
127 private boolean compareInstanceIdentifierTail(InstanceIdentifier<?> identifier1,
128 InstanceIdentifier<?> identifier2) {
129 return Iterables.getLast(identifier1.getPathArguments()).equals(Iterables.getLast(identifier2.getPathArguments()));
133 public boolean isNodeConnectorUpdated(long dpId, String portName){
134 return dpnToPortMultiMap.containsEntry(dpId,portName) ;
138 private long getDpIdFromPortName(String portName) {
139 String dpId = portName.substring(portName.indexOf(SEPARATOR) + 1, portName.lastIndexOf(SEPARATOR));
140 return Long.parseLong(dpId);