BGP forward porting & Update source enhancement.
[netvirt.git] / bgpmanager / impl / src / main / java / org / opendaylight / netvirt / bgpmanager / oam / BgpAlarms.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
9 package org.opendaylight.netvirt.bgpmanager.oam;
10
11 import java.util.HashMap;
12 import java.util.List;
13 import java.util.Map;
14 import java.util.Objects;
15 import java.util.concurrent.ConcurrentHashMap;
16 import org.opendaylight.netvirt.bgpmanager.BgpConfigurationManager;
17 import org.opendaylight.yang.gen.v1.urn.ericsson.params.xml.ns.yang.ebgp.rev150901.Bgp;
18 import org.opendaylight.yang.gen.v1.urn.ericsson.params.xml.ns.yang.ebgp.rev150901.bgp.neighborscontainer.Neighbors;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21
22 public class BgpAlarms implements Runnable, AutoCloseable {
23     private static final Logger LOG = LoggerFactory.getLogger(BgpAlarms.class);
24     private static final String ALARM_TEXT = "Bgp Neighbor TCP connection is down";
25
26     private final BgpJMXAlarmAgent alarmAgent = new BgpJMXAlarmAgent();
27     private final BgpConfigurationManager bgpMgr;
28     private final Map<String, BgpAlarmStatus> neighborsRaisedAlarmStatusMap = new ConcurrentHashMap<>();
29     private volatile List<Neighbors> nbrList = null;
30
31     public BgpAlarms(BgpConfigurationManager bgpManager) {
32         bgpMgr = Objects.requireNonNull(bgpManager);
33     }
34
35     public void init() {
36         alarmAgent.registerMbean();
37         Bgp bgp = bgpMgr.getConfig();
38         if (bgp != null && bgp.getNeighborsContainer() != null) {
39             List<Neighbors> nbrs = bgp.getNeighborsContainer().getNeighbors();
40             if (nbrs != null) {
41                 for (Neighbors nbr : nbrs) {
42                     LOG.trace("Clearing Neighbor DOWN alarm at the startup for Neighbor {}",
43                             nbr.getAddress().getValue());
44                     clearBgpNbrDownAlarm(nbr.getAddress().getValue());
45                 }
46             }
47         }
48     }
49
50     @Override
51     public void close() {
52         alarmAgent.unregisterMbean();
53     }
54
55     @Override
56     public void run() {
57         LOG.debug("Fetching neighbor status' from BGP");
58         BgpCounters.resetFile(BgpCounters.BGP_VPNV4_SUMMARY_FILE);
59         BgpCounters.resetFile(BgpCounters.BGP_VPNV6_SUMMARY_FILE);
60         BgpCounters.resetFile(BgpCounters.BGP_EVPN_SUMMARY_FILE);
61         Map<String, String> neighborStatusMap = new HashMap<>();
62
63         if (bgpMgr.getBgpCounters() != null) {
64             bgpMgr.getBgpCounters().fetchCmdOutputs(BgpCounters.BGP_VPNV4_SUMMARY_FILE,
65                     "show ip bgp vpnv4 all summary");
66             if (bgpMgr.getConfig() != null) {
67                 nbrList = bgpMgr.getConfig().getNeighborsContainer().getNeighbors();
68             }
69             BgpCounters.parseIpBgpVpnv4AllSummary(neighborStatusMap);
70
71             bgpMgr.getBgpCounters().fetchCmdOutputs(BgpCounters.BGP_VPNV6_SUMMARY_FILE,
72                     "show ip bgp vpnv6 all summary");
73
74             BgpCounters.parseIpBgpVpnv6AllSummary(neighborStatusMap);
75
76             bgpMgr.getBgpCounters().fetchCmdOutputs(BgpCounters.BGP_EVPN_SUMMARY_FILE,
77                     "show bgp l2vpn evpn all summary");
78
79             BgpCounters.parseBgpL2vpnEvpnAllSummary(neighborStatusMap);
80
81             processNeighborStatusMap(neighborStatusMap, nbrList);
82         }
83         LOG.debug("Finished getting the status of BGP neighbors");
84     }
85
86     private void processNeighborStatusMap(Map<String, String> nbrStatusMap, List<Neighbors> nbrs) {
87         if (nbrs == null || nbrs.isEmpty()) {
88             LOG.trace("No BGP neighbors configured.");
89             return;
90         }
91
92         for (Neighbors nbr : nbrs) {
93             boolean alarmToRaise = true;
94             if (nbrStatusMap != null && nbrStatusMap.containsKey(nbr.getAddress().getValue())) {
95                 String nbrshipStatus = nbrStatusMap.get(nbr.getAddress().getValue());
96                 LOG.trace("nbr {} status {}",
97                         nbr.getAddress().getValue(),
98                         nbrshipStatus);
99                 try {
100                     Integer.parseInt(nbrshipStatus);
101                     alarmToRaise = false;
102                 } catch (NumberFormatException e) {
103                     LOG.trace("Exception thrown in parsing the integers.", e);
104                 }
105
106                 final BgpAlarmStatus alarmStatus = neighborsRaisedAlarmStatusMap.get(nbr.getAddress().getValue());
107                 if (alarmToRaise) {
108                     if (alarmStatus == null || alarmStatus != BgpAlarmStatus.RAISED) {
109                         LOG.trace("alarm raised for {}.", nbr.getAddress().getValue());
110                         raiseBgpNbrDownAlarm(nbr.getAddress().getValue());
111                     } else {
112                         LOG.trace("alarm raised already for {}", nbr.getAddress().getValue());
113                     }
114                 } else {
115                     if (alarmStatus == null || alarmStatus != BgpAlarmStatus.CLEARED) {
116                         clearBgpNbrDownAlarm(nbr.getAddress().getValue());
117                         LOG.trace("alarm cleared for {}", nbr.getAddress().getValue());
118                     } else {
119                         LOG.trace("alarm cleared already for {}", nbr.getAddress().getValue());
120                     }
121                 }
122             }
123         }
124     }
125
126     private void raiseBgpNbrDownAlarm(String nbrIp) {
127
128         StringBuilder source = new StringBuilder();
129         source.append("BGP_Neighbor=").append(nbrIp);
130         if (nbrIp == null || nbrIp.isEmpty()) {
131             return;
132         }
133         LOG.trace("Raising BgpControlPathFailure alarm. {} alarmtext {} ", source, ALARM_TEXT);
134         //Invokes JMX raiseAlarm method
135         alarmAgent.invokeFMraisemethod("BgpControlPathFailure", ALARM_TEXT, source.toString());
136
137         neighborsRaisedAlarmStatusMap.put(nbrIp, BgpAlarmStatus.RAISED);
138     }
139
140     public void clearBgpNbrDownAlarm(String nbrIp) {
141         StringBuilder source = new StringBuilder();
142         source.append("BGP_Neighbor=").append(nbrIp);
143         if (nbrIp == null || nbrIp.isEmpty()) {
144             return;
145         }
146         LOG.trace("Clearing BgpControlPathFailure alarm of source {} alarmtext {} ", source, ALARM_TEXT);
147         //Invokes JMX clearAlarm method
148         alarmAgent.invokeFMclearmethod("BgpControlPathFailure", ALARM_TEXT, source.toString());
149
150         neighborsRaisedAlarmStatusMap.put(nbrIp, BgpAlarmStatus.CLEARED);
151     }
152 }