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
8 package org.opendaylight.openflowplugin.applications.frm.impl;
10 import com.google.common.base.Preconditions;
11 import com.google.common.collect.ArrayListMultimap;
12 import com.google.common.collect.Iterables;
13 import com.google.common.collect.Multimap;
14 import com.google.common.collect.Multimaps;
15 import java.math.BigInteger;
16 import org.opendaylight.mdsal.binding.api.DataBroker;
17 import org.opendaylight.mdsal.binding.api.DataTreeIdentifier;
18 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
19 import org.opendaylight.openflowplugin.applications.frm.FlowNodeConnectorInventoryTranslator;
20 import org.opendaylight.openflowplugin.common.wait.SimpleTaskRetryLooper;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeConnector;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
25 import org.opendaylight.yangtools.concepts.ListenerRegistration;
26 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
30 public class FlowNodeConnectorInventoryTranslatorImpl extends AbstractNodeConnectorCommitter<FlowCapableNodeConnector>
31 implements FlowNodeConnectorInventoryTranslator {
33 private static final Logger LOG = LoggerFactory.getLogger(FlowNodeConnectorInventoryTranslatorImpl.class);
35 private ListenerRegistration<FlowNodeConnectorInventoryTranslatorImpl> dataTreeChangeListenerRegistration;
37 private 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 final Multimap<BigInteger, String> dpnToPortMultiMap = Multimaps
47 .synchronizedListMultimap(ArrayListMultimap.<BigInteger, String>create());
49 @SuppressWarnings("IllegalCatch")
50 public FlowNodeConnectorInventoryTranslatorImpl(final DataBroker dataBroker) {
51 Preconditions.checkNotNull(dataBroker, "DataBroker can not be null!");
53 final DataTreeIdentifier<FlowCapableNodeConnector> treeId =
54 DataTreeIdentifier.create(LogicalDatastoreType.OPERATIONAL, getWildCardPath());
56 SimpleTaskRetryLooper looper = new SimpleTaskRetryLooper(ForwardingRulesManagerImpl.STARTUP_LOOP_TICK,
57 ForwardingRulesManagerImpl.STARTUP_LOOP_MAX_RETRIES);
58 dataTreeChangeListenerRegistration = looper.loopUntilNoException(() -> dataBroker
59 .registerDataTreeChangeListener(treeId, FlowNodeConnectorInventoryTranslatorImpl.this));
60 } catch (final Exception e) {
61 LOG.warn(" FlowNodeConnectorInventoryTranslatorImpl listener registration fail!");
62 LOG.debug("FlowNodeConnectorInventoryTranslatorImpl DataTreeChangeListener registration fail ..", e);
64 IllegalStateException("FlowNodeConnectorInventoryTranslatorImpl startup fail! System needs restart.", e);
69 protected InstanceIdentifier<FlowCapableNodeConnector> getWildCardPath() {
70 return InstanceIdentifier.create(Nodes.class)
72 .child(NodeConnector.class)
73 .augmentation(FlowCapableNodeConnector.class);
78 if (dataTreeChangeListenerRegistration != null) {
79 dataTreeChangeListenerRegistration.close();
80 dataTreeChangeListenerRegistration = null;
85 public void remove(InstanceIdentifier<FlowCapableNodeConnector> identifier, FlowCapableNodeConnector del,
86 InstanceIdentifier<FlowCapableNodeConnector> nodeConnIdent) {
87 if (compareInstanceIdentifierTail(identifier, II_TO_FLOW_CAPABLE_NODE_CONNECTOR)) {
88 LOG.debug("Node Connector removed");
89 String nodeConnectorIdentifier = nodeConnIdent.firstKeyOf(NodeConnector.class)
91 BigInteger dpId = getDpIdFromPortName(nodeConnectorIdentifier);
93 dpnToPortMultiMap.remove(dpId, nodeConnectorIdentifier);
98 public void update(InstanceIdentifier<FlowCapableNodeConnector> identifier, FlowCapableNodeConnector original,
99 FlowCapableNodeConnector update, InstanceIdentifier<FlowCapableNodeConnector> nodeConnIdent) {
100 if (compareInstanceIdentifierTail(identifier, II_TO_FLOW_CAPABLE_NODE_CONNECTOR)) {
101 LOG.debug("Node Connector updated");
102 // Don't need to do anything as we are not considering updates here
107 public void add(InstanceIdentifier<FlowCapableNodeConnector> identifier, FlowCapableNodeConnector add,
108 InstanceIdentifier<FlowCapableNodeConnector> nodeConnIdent) {
109 if (compareInstanceIdentifierTail(identifier, II_TO_FLOW_CAPABLE_NODE_CONNECTOR)) {
110 LOG.debug("Node Connector added");
111 String nodeConnectorIdentifier = nodeConnIdent
112 .firstKeyOf(NodeConnector.class).getId().getValue();
113 BigInteger dpId = getDpIdFromPortName(nodeConnectorIdentifier);
115 if (!dpnToPortMultiMap.containsEntry(dpId, nodeConnectorIdentifier)) {
116 dpnToPortMultiMap.put(dpId, nodeConnectorIdentifier);
118 LOG.error("Duplicate Event.Node Connector already added");
123 private boolean compareInstanceIdentifierTail(InstanceIdentifier<?> identifier1,
124 InstanceIdentifier<?> identifier2) {
125 return Iterables.getLast(identifier1.getPathArguments())
126 .equals(Iterables.getLast(identifier2.getPathArguments()));
130 public boolean isNodeConnectorUpdated(BigInteger dpId, String portName) {
131 return dpnToPortMultiMap.containsEntry(dpId,portName) ;
135 private BigInteger getDpIdFromPortName(String portName) {
136 String dpId = portName.substring(portName.indexOf(SEPARATOR) + 1, portName.lastIndexOf(SEPARATOR));
137 return new BigInteger(dpId);