Integration of fcaps applications
[vpnservice.git] / fcapsapplication / fcapsapplication-impl / src / main / java / org / opendaylight / vpnservice / fcapsapp / NodeEventListener.java
1 /*
2  * Copyright (c) 2016 Ericsson India Global Services Pvt Ltd. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.vpnservice.fcapsapp;
9
10 import com.google.common.base.Optional;
11 import org.opendaylight.controller.md.sal.binding.api.*;
12
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;
27
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;
34
35     /**
36      * Construcor set EntityOwnershipService
37      * @param eos
38      */
39     public NodeEventListener(final EntityOwnershipService eos) {
40         this.entityOwnershipService = eos;
41     }
42
43     @Override
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;
51             try {
52                 nodeId = getDpnId(String.valueOf(nodeConnIdent.firstKeyOf(Node.class).getId()));
53             } catch (Exception ex) {
54                 LOG.error("Dpn retrieval failed");
55                 return;
56             }
57
58             hostName = System.getenv().get("HOSTNAME");
59             if (hostName == null) {
60                 try {
61                     hostName = InetAddress.getLocalHost().getHostName();
62                 } catch (Exception e) {
63                     LOG.error("Retrieving hostName failed {}", e);
64                 }
65             }
66             LOG.debug("retrieved hostname {}", hostName);
67             if (nodeId != null) {
68                 switch (mod.getModificationType()) {
69                     case DELETE:
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);
74                         }
75                         packetInCounter.nodeRemovedNotification(nodeId);
76                         break;
77                     case SUBTREE_MODIFIED:
78                         if (isNodeOwner(nodeId)) {
79                             LOG.debug("NodeUpdated {} notification is received", nodeId);
80                         } else {
81                             LOG.debug("UPDATE: Node {} is not connected to host {}", nodeId, hostName);
82                         }
83                         break;
84                     case WRITE:
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);
90                             } else {
91                                 LOG.debug("ADD: Node {} is not connected to host {}", nodeId, hostName);
92                             }
93                         }
94                         break;
95                     default:
96                         LOG.debug("Unhandled Modification type {}", mod.getModificationType());
97                         throw new IllegalArgumentException("Unhandled modification type " + mod.getModificationType());
98
99                 }
100             } else {
101                 LOG.error("DpnID is null");
102             }
103         }
104     }
105
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);
110         return dpnId;
111
112     }
113
114     /**
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
119      */
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();
125         }
126         return false;
127     }
128
129     @Override
130     public void close() throws Exception {
131     }
132 }