2 * Copyright (c) 2015, 2017 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 org.opendaylight.controller.md.sal.binding.api.DataBroker;
18 import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier;
19 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
20 import org.opendaylight.openflowplugin.applications.frm.FlowNodeConnectorInventoryTranslator;
21 import org.opendaylight.openflowplugin.common.wait.SimpleTaskRetryLooper;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeConnector;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorKey;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
27 import org.opendaylight.yangtools.concepts.ListenerRegistration;
28 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
32 public class FlowNodeConnectorInventoryTranslatorImpl extends AbstractNodeConnectorCommitter<FlowCapableNodeConnector>
33 implements FlowNodeConnectorInventoryTranslator {
35 private static final Logger LOG = LoggerFactory.getLogger(FlowNodeConnectorInventoryTranslatorImpl.class);
37 private ListenerRegistration<FlowNodeConnectorInventoryTranslatorImpl> dataTreeChangeListenerRegistration;
39 private static final String SEPARATOR = ":";
41 private static final InstanceIdentifier<FlowCapableNodeConnector> II_TO_FLOW_CAPABLE_NODE_CONNECTOR
42 = InstanceIdentifier.builder(Nodes.class)
44 .child(NodeConnector.class)
45 .augmentation(FlowCapableNodeConnector.class)
48 private final Multimap<BigInteger, String> dpnToPortMultiMap = Multimaps
49 .synchronizedListMultimap(ArrayListMultimap.<BigInteger, String>create());
51 @SuppressWarnings("IllegalCatch")
52 public FlowNodeConnectorInventoryTranslatorImpl(final DataBroker dataBroker) {
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(() -> dataBroker
61 .registerDataTreeChangeListener(treeId, FlowNodeConnectorInventoryTranslatorImpl.this));
62 } catch (final Exception e) {
63 LOG.warn(" FlowNodeConnectorInventoryTranslatorImpl listener registration fail!");
64 LOG.debug("FlowNodeConnectorInventoryTranslatorImpl DataTreeChangeListener registration fail ..", e);
66 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) {
81 dataTreeChangeListenerRegistration.close();
82 dataTreeChangeListenerRegistration = null;
87 public void remove(InstanceIdentifier<FlowCapableNodeConnector> identifier, FlowCapableNodeConnector del,
88 InstanceIdentifier<FlowCapableNodeConnector> nodeConnIdent) {
89 if (compareInstanceIdentifierTail(identifier, II_TO_FLOW_CAPABLE_NODE_CONNECTOR)) {
90 LOG.debug("Node Connector removed");
91 String nodeConnectorIdentifier = nodeConnIdent.firstKeyOf(NodeConnector.class, NodeConnectorKey.class)
93 BigInteger dpId = getDpIdFromPortName(nodeConnectorIdentifier);
95 dpnToPortMultiMap.remove(dpId, nodeConnectorIdentifier);
100 public void update(InstanceIdentifier<FlowCapableNodeConnector> identifier, FlowCapableNodeConnector original,
101 FlowCapableNodeConnector update, InstanceIdentifier<FlowCapableNodeConnector> nodeConnIdent) {
102 if (compareInstanceIdentifierTail(identifier, II_TO_FLOW_CAPABLE_NODE_CONNECTOR)) {
103 LOG.debug("Node Connector updated");
104 // Don't need to do anything as we are not considering updates here
109 public void add(InstanceIdentifier<FlowCapableNodeConnector> identifier, FlowCapableNodeConnector add,
110 InstanceIdentifier<FlowCapableNodeConnector> nodeConnIdent) {
111 if (compareInstanceIdentifierTail(identifier, II_TO_FLOW_CAPABLE_NODE_CONNECTOR)) {
112 LOG.debug("Node Connector added");
113 String nodeConnectorIdentifier = nodeConnIdent
114 .firstKeyOf(NodeConnector.class, NodeConnectorKey.class).getId().getValue();
115 BigInteger dpId = getDpIdFromPortName(nodeConnectorIdentifier);
117 if (!dpnToPortMultiMap.containsEntry(dpId, nodeConnectorIdentifier)) {
118 dpnToPortMultiMap.put(dpId, nodeConnectorIdentifier);
120 LOG.error("Duplicate Event.Node Connector already added");
125 private boolean compareInstanceIdentifierTail(InstanceIdentifier<?> identifier1,
126 InstanceIdentifier<?> identifier2) {
127 return Iterables.getLast(identifier1.getPathArguments())
128 .equals(Iterables.getLast(identifier2.getPathArguments()));
132 public boolean isNodeConnectorUpdated(BigInteger dpId, String portName) {
133 return dpnToPortMultiMap.containsEntry(dpId,portName) ;
137 private BigInteger getDpIdFromPortName(String portName) {
138 String dpId = portName.substring(portName.indexOf(SEPARATOR) + 1, portName.lastIndexOf(SEPARATOR));
139 return new BigInteger(dpId);