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.vpnservice.fcapsapp;
10 import com.google.common.base.Optional;
11 import org.opendaylight.controller.md.sal.binding.api.*;
13 import org.opendaylight.controller.md.sal.common.api.clustering.Entity;
14 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipService;
15 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipState;
16 import org.opendaylight.vpnservice.fcapsapp.alarm.AlarmAgent;
17 import org.opendaylight.vpnservice.fcapsapp.performancecounter.NodeUpdateCounter;
18 import org.opendaylight.vpnservice.fcapsapp.performancecounter.PacketInCounterHandler;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
21 import org.opendaylight.yangtools.yang.binding.DataObject;
22 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25 import java.net.InetAddress;
26 import java.util.Collection;
28 public class NodeEventListener<D extends DataObject> implements ClusteredDataTreeChangeListener<D>,AutoCloseable {
29 private static final Logger LOG = LoggerFactory.getLogger(NodeEventListener.class);
30 public static final AlarmAgent alarmAgent = new AlarmAgent();
31 public static final NodeUpdateCounter nodeUpdateCounter = new NodeUpdateCounter();
32 public static final PacketInCounterHandler packetInCounter = new PacketInCounterHandler();
33 private final EntityOwnershipService entityOwnershipService;
36 * Construcor set EntityOwnershipService
39 public NodeEventListener(final EntityOwnershipService eos) {
40 this.entityOwnershipService = eos;
44 public void onDataTreeChanged(Collection<DataTreeModification<D>> changes) {
45 for (DataTreeModification<D> change : changes) {
46 final InstanceIdentifier<D> key = change.getRootPath().getRootIdentifier();
47 final DataObjectModification<D> mod = change.getRootNode();
48 final InstanceIdentifier<FlowCapableNode> nodeConnIdent =
49 key.firstIdentifierOf(FlowCapableNode.class);
50 String nodeId = null, hostName = null;
52 nodeId = getDpnId(String.valueOf(nodeConnIdent.firstKeyOf(Node.class).getId()));
53 } catch (Exception ex) {
54 LOG.error("Dpn retrieval failed");
58 hostName = System.getenv().get("HOSTNAME");
59 if (hostName == null) {
61 hostName = InetAddress.getLocalHost().getHostName();
62 } catch (Exception e) {
63 LOG.error("Retrieving hostName failed {}", e);
66 LOG.debug("retrieved hostname {}", hostName);
68 switch (mod.getModificationType()) {
70 LOG.debug("NodeRemoved {} notification is received on host {}", nodeId, hostName);
71 if (nodeUpdateCounter.isDpnConnectedLocal(nodeId)) {
72 alarmAgent.raiseControlPathAlarm(nodeId, hostName);
73 nodeUpdateCounter.nodeRemovedNotification(nodeId, hostName);
75 packetInCounter.nodeRemovedNotification(nodeId);
77 case SUBTREE_MODIFIED:
78 if (isNodeOwner(nodeId)) {
79 LOG.debug("NodeUpdated {} notification is received", nodeId);
81 LOG.debug("UPDATE: Node {} is not connected to host {}", nodeId, hostName);
85 if (mod.getDataBefore() == null) {
86 if (isNodeOwner(nodeId)) {
87 LOG.debug("NodeAdded {} notification is received on host {}", nodeId, hostName);
88 alarmAgent.clearControlPathAlarm(nodeId);
89 nodeUpdateCounter.nodeAddedNotification(nodeId, hostName);
91 LOG.debug("ADD: Node {} is not connected to host {}", nodeId, hostName);
96 LOG.debug("Unhandled Modification type {}", mod.getModificationType());
97 throw new IllegalArgumentException("Unhandled modification type " + mod.getModificationType());
101 LOG.error("DpnID is null");
106 private String getDpnId(String node) {
107 //Uri [_value=openflow:1]
108 String temp[] = node.split("=");
109 String dpnId = temp[1].substring(0,temp[1].length() - 1);
115 * Method checks if *this* instance of controller is owner of
116 * the given openflow node.
117 * @param nodeId DpnId
118 * @return True if owner, else false
120 public boolean isNodeOwner(String nodeId) {
121 Entity entity = new Entity("openflow", nodeId);
122 Optional<EntityOwnershipState> entityState = this.entityOwnershipService.getOwnershipState(entity);
123 if (entityState.isPresent()) {
124 return entityState.get().isOwner();
130 public void close() throws Exception {