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
9 package org.opendaylight.netvirt.bgpmanager.oam;
11 import java.util.HashMap;
12 import java.util.List;
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;
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";
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;
31 public BgpAlarms(BgpConfigurationManager bgpManager) {
32 bgpMgr = Objects.requireNonNull(bgpManager);
36 alarmAgent.registerMbean();
37 Bgp bgp = bgpMgr.getConfig();
38 if (bgp != null && bgp.getNeighborsContainer() != null) {
39 List<Neighbors> nbrs = bgp.getNeighborsContainer().getNeighbors();
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());
52 alarmAgent.unregisterMbean();
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<>();
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();
69 BgpCounters.parseIpBgpVpnv4AllSummary(neighborStatusMap);
71 bgpMgr.getBgpCounters().fetchCmdOutputs(BgpCounters.BGP_VPNV6_SUMMARY_FILE,
72 "show ip bgp vpnv6 all summary");
74 BgpCounters.parseIpBgpVpnv6AllSummary(neighborStatusMap);
76 bgpMgr.getBgpCounters().fetchCmdOutputs(BgpCounters.BGP_EVPN_SUMMARY_FILE,
77 "show bgp l2vpn evpn all summary");
79 BgpCounters.parseBgpL2vpnEvpnAllSummary(neighborStatusMap);
81 processNeighborStatusMap(neighborStatusMap, nbrList);
83 LOG.debug("Finished getting the status of BGP neighbors");
86 private void processNeighborStatusMap(Map<String, String> nbrStatusMap, List<Neighbors> nbrs) {
87 if (nbrs == null || nbrs.isEmpty()) {
88 LOG.trace("No BGP neighbors configured.");
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(),
100 Integer.parseInt(nbrshipStatus);
101 alarmToRaise = false;
102 } catch (NumberFormatException e) {
103 LOG.trace("Exception thrown in parsing the integers.", e);
106 final BgpAlarmStatus alarmStatus = neighborsRaisedAlarmStatusMap.get(nbr.getAddress().getValue());
108 if (alarmStatus == null || alarmStatus != BgpAlarmStatus.RAISED) {
109 LOG.trace("alarm raised for {}.", nbr.getAddress().getValue());
110 raiseBgpNbrDownAlarm(nbr.getAddress().getValue());
112 LOG.trace("alarm raised already for {}", nbr.getAddress().getValue());
115 if (alarmStatus == null || alarmStatus != BgpAlarmStatus.CLEARED) {
116 clearBgpNbrDownAlarm(nbr.getAddress().getValue());
117 LOG.trace("alarm cleared for {}", nbr.getAddress().getValue());
119 LOG.trace("alarm cleared already for {}", nbr.getAddress().getValue());
126 private void raiseBgpNbrDownAlarm(String nbrIp) {
128 StringBuilder source = new StringBuilder();
129 source.append("BGP_Neighbor=").append(nbrIp);
130 if (nbrIp == null || nbrIp.isEmpty()) {
133 LOG.trace("Raising BgpControlPathFailure alarm. {} alarmtext {} ", source, ALARM_TEXT);
134 //Invokes JMX raiseAlarm method
135 alarmAgent.invokeFMraisemethod("BgpControlPathFailure", ALARM_TEXT, source.toString());
137 neighborsRaisedAlarmStatusMap.put(nbrIp, BgpAlarmStatus.RAISED);
140 public void clearBgpNbrDownAlarm(String nbrIp) {
141 StringBuilder source = new StringBuilder();
142 source.append("BGP_Neighbor=").append(nbrIp);
143 if (nbrIp == null || nbrIp.isEmpty()) {
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());
150 neighborsRaisedAlarmStatusMap.put(nbrIp, BgpAlarmStatus.CLEARED);