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.io.IOException;
12 import java.util.HashMap;
13 import java.util.List;
15 import java.util.TimerTask;
16 import org.opendaylight.netvirt.bgpmanager.BgpConfigurationManager;
17 import org.opendaylight.yang.gen.v1.urn.ericsson.params.xml.ns.yang.ebgp.rev150901.bgp.Neighbors;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
21 public class BgpAlarms extends TimerTask {
22 private static final Logger LOG = LoggerFactory.getLogger(BgpAlarms.class);
23 private static final BgpJMXAlarmAgent ALARM_AGENT = new BgpJMXAlarmAgent();
24 private static Map<String, String> neighborStatusMap = new HashMap<>();
25 private BgpConfigurationManager bgpMgr;
27 private static Map<String, BgpAlarmStatus> neighborsRaisedAlarmStatusMap = new HashMap<>();
28 private static final String ALARM_TEXT = "Bgp Neighbor TCP connection is down";
32 List<Neighbors> nbrList = null;
34 LOG.debug("Fetching neighbor status' from BGP");
35 BgpCounters.resetFile(BgpCounters.BGP_VPNV4_SUMMARY_FILE);
36 BgpCounters.resetFile(BgpCounters.BGP_VPNV6_SUMMARY_FILE);
37 neighborStatusMap.clear();
39 if (bgpMgr != null && bgpMgr.getBgpCounters() != null) {
40 bgpMgr.getBgpCounters().fetchCmdOutputs(BgpCounters.BGP_VPNV4_SUMMARY_FILE,
41 "show ip bgp vpnv4 all summary");
42 if (bgpMgr.getConfig() != null) {
43 nbrList = bgpMgr.getConfig().getNeighbors();
45 BgpCounters.parseIpBgpVpnv4AllSummary(neighborStatusMap);
47 bgpMgr.getBgpCounters().fetchCmdOutputs(BgpCounters.BGP_VPNV6_SUMMARY_FILE,
48 "show ip bgp vpnv6 all summary");
50 BgpCounters.parseIpBgpVpnv6AllSummary(neighborStatusMap);
51 processNeighborStatusMap(neighborStatusMap, nbrList, neighborsRaisedAlarmStatusMap);
53 LOG.debug("Finished getting the status of BGP neighbors");
54 } catch (IOException e) {
55 LOG.error("Failed to publish bgp counters ", e);
59 public BgpAlarms(BgpConfigurationManager bgpManager) {
61 ALARM_AGENT.registerMbean();
62 if (bgpMgr != null && bgpMgr.getConfig() != null) {
63 List<Neighbors> nbrs = bgpMgr.getConfig().getNeighbors();
65 for (Neighbors nbr : nbrs) {
66 LOG.trace("Clearing Neighbor DOWN alarm at the startup for Neighbor {}",
67 nbr.getAddress().getValue());
68 clearBgpNbrDownAlarm(nbr.getAddress().getValue());
69 neighborsRaisedAlarmStatusMap.put(nbr.getAddress().getValue(),
70 BgpAlarmStatus.CLEARED);
76 private void processNeighborStatusMap(Map<String, String> nbrStatusMap,
77 List<Neighbors> nbrs, Map<String, BgpAlarmStatus>
78 nbrsRaisedAlarmStatusMap) {
81 if ((nbrs == null) || (nbrs.size() == 0)) {
82 LOG.trace("No BGP neighbors configured.");
85 for (Neighbors nbr : nbrs) {
87 if ((nbrStatusMap != null) && nbrStatusMap.containsKey(nbr.getAddress().getValue())) {
88 nbrshipStatus = nbrStatusMap.get(nbr.getAddress().getValue());
89 LOG.trace("nbr {} status {}",
90 nbr.getAddress().getValue(),
93 Integer.parseInt(nbrshipStatus);
95 } catch (NumberFormatException e) {
96 LOG.trace("Exception thrown in parsing the integers. {}", e);
99 if ((!nbrsRaisedAlarmStatusMap.containsKey(
100 nbr.getAddress().getValue())) || (nbrsRaisedAlarmStatusMap.get(
101 nbr.getAddress().getValue()) != BgpAlarmStatus.RAISED)) {
102 LOG.trace("alarm raised for {}.", nbr.getAddress().getValue());
103 raiseBgpNbrDownAlarm(nbr.getAddress().getValue());
104 nbrsRaisedAlarmStatusMap.put(nbr.getAddress().getValue(), BgpAlarmStatus.RAISED);
106 LOG.trace("alarm raised already for {}", nbr.getAddress().getValue());
109 if ((!nbrsRaisedAlarmStatusMap.containsKey(
110 nbr.getAddress().getValue())) || (nbrsRaisedAlarmStatusMap.get(
111 nbr.getAddress().getValue()) != BgpAlarmStatus.CLEARED)) {
112 clearBgpNbrDownAlarm(nbr.getAddress().getValue());
113 LOG.trace("alarm cleared for {}", nbr.getAddress().getValue());
114 nbrsRaisedAlarmStatusMap.put(nbr.getAddress().getValue(), BgpAlarmStatus.CLEARED);
116 LOG.trace("alarm cleared already for {}", nbr.getAddress().getValue());
123 public void raiseBgpNbrDownAlarm(String nbrIp) {
125 StringBuilder source = new StringBuilder();
126 source.append("BGP_Neighbor=").append(nbrIp);
127 if ((nbrIp == null) || (nbrIp.isEmpty())) {
130 LOG.trace("Raising BgpControlPathFailure alarm. {} alarmtext {} ", source, ALARM_TEXT);
131 //Invokes JMX raiseAlarm method
132 ALARM_AGENT.invokeFMraisemethod("BgpControlPathFailure", ALARM_TEXT, source.toString());
135 public void clearBgpNbrDownAlarm(String nbrIp) {
136 StringBuilder source = new StringBuilder();
137 source.append("BGP_Neighbor=").append(nbrIp);
138 if ((nbrIp == null) || (nbrIp.isEmpty())) {
141 LOG.trace("Clearing BgpControlPathFailure alarm of source {} alarmtext {} ", source, ALARM_TEXT);
142 //Invokes JMX clearAlarm method
143 ALARM_AGENT.invokeFMclearmethod("BgpControlPathFailure", ALARM_TEXT, source.toString());