36c8309448f90e975d7ef655c8b2fe37c447f5ed
[vpnservice.git] / fcapsapplication / fcapsapplication-impl / src / main / java / org / opendaylight / vpnservice / fcapsapp / performancecounter / PacketInCounterHandler.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.performancecounter;
9
10 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketProcessingListener;
11 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketReceived;
12 import org.slf4j.Logger;
13 import org.slf4j.LoggerFactory;
14
15 import java.util.HashMap;
16 import java.util.concurrent.ConcurrentHashMap;
17 import java.util.concurrent.atomic.AtomicLong;
18
19 public class PacketInCounterHandler implements PacketProcessingListener {
20     private static final Logger LOG = LoggerFactory.getLogger(PacketInCounterHandler.class);
21     private static ConcurrentHashMap<String,AtomicLong> ingressPacketMap = new ConcurrentHashMap<>();
22     private static HashMap<String,String> packetInMap = new HashMap<>();
23     private static final Integer FIRST_VALUE = 1;
24     private static final PMAgent pmAgent =new PMAgent();
25
26     @Override
27     public void onPacketReceived(PacketReceived notification) {
28         String dpnId = null;
29         String nodeListEgressStr, nodekey;
30         LOG.debug("Ingress packet notification received");
31         try {
32             if (notification.getIngress() == null) {
33                 if (LOG.isWarnEnabled()) {
34                     LOG.warn("invalid PacketReceived notification");
35                 }
36                 return;
37             }
38             dpnId = getDpnId(notification.getIngress().getValue().toString());
39         } catch (Exception e) {
40             if (LOG.isWarnEnabled()) {
41                 LOG.warn("DPN Parsing failed in onPacketReceived");
42             }
43         }
44         if (dpnId != null) {
45             nodeListEgressStr = "dpnId_" + dpnId + "_InjectedOFMessagesSent";
46             nodekey = "InjectedOFMessagesSent:" + nodeListEgressStr;
47             if (ingressPacketMap.containsKey(dpnId)) {
48                 ingressPacketMap.put(dpnId,new AtomicLong(ingressPacketMap.get(dpnId).incrementAndGet()));
49                 packetInMap.put(nodekey,""+ingressPacketMap.get(dpnId));
50             } else {
51                 ingressPacketMap.put(dpnId, new AtomicLong(FIRST_VALUE));
52                 packetInMap.put(nodekey,""+FIRST_VALUE);
53             }
54             connectToPMAgent();
55         } else {
56             LOG.error("DpnId is null");
57         }
58     }
59     private void connectToPMAgent(){
60         pmAgent.sendPacketInCounterUpdate(packetInMap);
61     }
62     /*
63      * Method to extract DpnId
64      */
65     public static String getDpnId(String id) {
66         String[] nodeNo = id.split(":");
67         String[] dpnId = nodeNo[1].split("]");
68         return dpnId[0];
69     }
70
71     public void nodeRemovedNotification(String dpnId){
72         String nodeListEgressStr, nodekey;
73         if (dpnId != null) {
74             dpnId = dpnId.split(":")[1];
75             LOG.debug("Dpnvalue Id {}",dpnId);
76             if (ingressPacketMap.containsKey(dpnId)) {
77                 nodeListEgressStr = "dpnId_" + dpnId + "_InjectedOFMessagesSent";
78                 nodekey = "InjectedOFMessagesSent:" + nodeListEgressStr;
79                 synchronized (this) {
80                     ingressPacketMap.remove(dpnId);
81                     packetInMap.remove(nodekey);
82                     connectToPMAgent();
83                 }
84                 LOG.debug("Node {} Removed for PacketIn counter", dpnId);
85             }
86         } else {
87             LOG.error("DpnId is null upon nodeRemovedNotification");
88         }
89     }
90 }