2 * Copyright (c) 2016 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.genius.fcapsapp.performancecounter;
10 import com.google.common.base.Optional;
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.util.Collection;
17 import java.util.HashMap;
18 import java.util.concurrent.Callable;
19 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
20 import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier;
21 import org.opendaylight.controller.md.sal.common.api.clustering.Entity;
22 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipService;
23 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipState;
24 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
25 import org.opendaylight.genius.fcapsapp.portinfo.PortNameMapping;
26 import org.opendaylight.openflowplugin.common.wait.SimpleTaskRetryLooper;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeConnector;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
31 import org.opendaylight.yangtools.concepts.ListenerRegistration;
32 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
36 public class FlowNodeConnectorInventoryTranslatorImpl extends NodeConnectorEventListener<FlowCapableNodeConnector> {
37 public static final int STARTUP_LOOP_TICK = 500;
38 public static final int STARTUP_LOOP_MAX_RETRIES = 8;
39 private static final Logger LOG = LoggerFactory.getLogger(FlowNodeConnectorInventoryTranslatorImpl.class);
40 private final EntityOwnershipService entityOwnershipService;
42 private ListenerRegistration<FlowNodeConnectorInventoryTranslatorImpl> dataTreeChangeListenerRegistration;
44 public static final String SEPARATOR = ":";
45 private static final PMAgent pmAgent = new PMAgent();
47 private static final InstanceIdentifier<FlowCapableNodeConnector> II_TO_FLOW_CAPABLE_NODE_CONNECTOR
48 = InstanceIdentifier.builder(Nodes.class)
50 .child(NodeConnector.class)
51 .augmentation(FlowCapableNodeConnector.class)
54 private static Multimap<Long,String> dpnToPortMultiMap = Multimaps.synchronizedListMultimap(ArrayListMultimap.<Long,String>create());
56 private static HashMap<String, String> nodeConnectorCountermap = new HashMap<>();
58 public FlowNodeConnectorInventoryTranslatorImpl(final DataBroker dataBroker,final EntityOwnershipService eos) {
59 super( FlowCapableNodeConnector.class);
60 Preconditions.checkNotNull(dataBroker, "DataBroker can not be null!");
62 entityOwnershipService = eos;
63 final DataTreeIdentifier<FlowCapableNodeConnector> treeId =
64 new DataTreeIdentifier<>(LogicalDatastoreType.OPERATIONAL, getWildCardPath());
66 SimpleTaskRetryLooper looper = new SimpleTaskRetryLooper(STARTUP_LOOP_TICK,
67 STARTUP_LOOP_MAX_RETRIES);
68 dataTreeChangeListenerRegistration = looper.loopUntilNoException(new Callable<ListenerRegistration<FlowNodeConnectorInventoryTranslatorImpl>>() {
70 public ListenerRegistration<FlowNodeConnectorInventoryTranslatorImpl> call() throws Exception {
71 return dataBroker.registerDataTreeChangeListener(treeId, FlowNodeConnectorInventoryTranslatorImpl.this);
74 } catch (final Exception e) {
75 LOG.warn(" FlowNodeConnectorInventoryTranslatorImpl listener registration fail!");
76 LOG.debug("FlowNodeConnectorInventoryTranslatorImpl DataChange listener registration fail ..", e);
77 throw new IllegalStateException("FlowNodeConnectorInventoryTranslatorImpl startup fail! System needs restart.", e);
82 protected InstanceIdentifier<FlowCapableNodeConnector> getWildCardPath() {
83 return InstanceIdentifier.create(Nodes.class)
85 .child(NodeConnector.class)
86 .augmentation(FlowCapableNodeConnector.class);
91 if (dataTreeChangeListenerRegistration != null) {
93 dataTreeChangeListenerRegistration.close();
94 } catch (final Exception e) {
95 LOG.warn("Error by stop FRM FlowNodeConnectorInventoryTranslatorImpl: {}", e.getMessage());
96 LOG.debug("Error by stop FRM FlowNodeConnectorInventoryTranslatorImpl..", e);
98 dataTreeChangeListenerRegistration = null;
102 public void remove(InstanceIdentifier<FlowCapableNodeConnector> identifier, FlowCapableNodeConnector del, InstanceIdentifier<FlowCapableNodeConnector> nodeConnIdent) {
103 if(compareInstanceIdentifierTail(identifier,II_TO_FLOW_CAPABLE_NODE_CONNECTOR)) {
104 String sNodeConnectorIdentifier = getNodeConnectorId(String.valueOf(nodeConnIdent.firstKeyOf(NodeConnector.class).getId()));
105 long nDpId = getDpIdFromPortName(sNodeConnectorIdentifier);
106 if (dpnToPortMultiMap.containsKey(nDpId)) {
107 LOG.debug("Node Connector {} removed", sNodeConnectorIdentifier);
108 dpnToPortMultiMap.remove(nDpId, sNodeConnectorIdentifier);
109 sendNodeConnectorUpdation(nDpId);
110 PortNameMapping.updatePortMap("openflow:" + nDpId + ":" + del.getName(), sNodeConnectorIdentifier, "DELETE");
116 public void update(InstanceIdentifier<FlowCapableNodeConnector> identifier, FlowCapableNodeConnector original, FlowCapableNodeConnector update, InstanceIdentifier<FlowCapableNodeConnector> nodeConnIdent) {
117 if(compareInstanceIdentifierTail(identifier,II_TO_FLOW_CAPABLE_NODE_CONNECTOR)) {
119 //donot need to do anything as we are not considering updates here
120 String sNodeConnectorIdentifier = getNodeConnectorId(String.valueOf(nodeConnIdent.firstKeyOf(NodeConnector.class).getId()));
121 long nDpId = getDpIdFromPortName(sNodeConnectorIdentifier);
122 if (isNodeOwner(getNodeId(nDpId))) {
123 boolean original_portstatus = original.getConfiguration().isPORTDOWN();
124 boolean update_portstatus = update.getConfiguration().isPORTDOWN();
126 if (update_portstatus) {
128 LOG.debug("Node Connector {} updated port is down", sNodeConnectorIdentifier);
129 } else if (original_portstatus) {
131 LOG.debug("Node Connector {} updated port is up", sNodeConnectorIdentifier);
138 public void add(InstanceIdentifier<FlowCapableNodeConnector> identifier, FlowCapableNodeConnector add, InstanceIdentifier<FlowCapableNodeConnector> nodeConnIdent) {
139 if (compareInstanceIdentifierTail(identifier,II_TO_FLOW_CAPABLE_NODE_CONNECTOR)){
141 String sNodeConnectorIdentifier = getNodeConnectorId(String.valueOf(nodeConnIdent.firstKeyOf(NodeConnector.class).getId()));
142 long nDpId = getDpIdFromPortName(sNodeConnectorIdentifier);
143 if (isNodeOwner(getNodeId(nDpId))) {
144 if (!dpnToPortMultiMap.containsEntry(nDpId, sNodeConnectorIdentifier)) {
145 LOG.debug("Node Connector {} added", sNodeConnectorIdentifier);
146 dpnToPortMultiMap.put(nDpId, sNodeConnectorIdentifier);
147 sendNodeConnectorUpdation(nDpId);
148 PortNameMapping.updatePortMap("openflow:" + nDpId + ":" + add.getName(), sNodeConnectorIdentifier, "ADD");
150 LOG.error("Duplicate Event.Node Connector already added");
155 private String getNodeConnectorId(String node) {
156 //Uri [_value=openflow:1:1]
157 String temp[] = node.split("=");
158 String dpnId = temp[1].substring(0,temp[1].length() - 1);
162 private String getNodeId(Long dpnId){
163 return "openflow:" + dpnId;
166 * Method checks if *this* instance of controller is owner of
167 * the given openflow node.
168 * @param nodeId openflow node Id
169 * @return True if owner, else false
171 public boolean isNodeOwner(String nodeId) {
172 Entity entity = new Entity("openflow", nodeId);
173 Optional<EntityOwnershipState> eState = this.entityOwnershipService.getOwnershipState(entity);
174 if(eState.isPresent()) {
175 return eState.get().isOwner();
180 private boolean compareInstanceIdentifierTail(InstanceIdentifier<?> identifier1,
181 InstanceIdentifier<?> identifier2) {
182 return Iterables.getLast(identifier1.getPathArguments()).equals(Iterables.getLast(identifier2.getPathArguments()));
185 private long getDpIdFromPortName(String portName) {
186 String dpId = portName.substring(portName.indexOf(SEPARATOR) + 1, portName.lastIndexOf(SEPARATOR));
187 return Long.parseLong(dpId);
190 private void sendNodeConnectorUpdation(Long dpnId) {
191 Collection<String> portname = dpnToPortMultiMap.get(dpnId);
192 String nodeListPortsCountStr,counterkey;
193 nodeListPortsCountStr = "dpnId_" + dpnId + "_NumberOfOFPorts";
194 counterkey = "NumberOfOFPorts:" + nodeListPortsCountStr;
196 if (portname.size()!=0) {
197 nodeConnectorCountermap.put(counterkey, "" + portname.size());
199 nodeConnectorCountermap.remove(counterkey);
201 LOG.debug("NumberOfOFPorts:" + nodeListPortsCountStr + " portlistsize " + portname.size());
202 pmAgent.connectToPMAgentForNOOfPorts(nodeConnectorCountermap);