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.ArrayList;
12 import java.util.HashMap;
13 import java.util.List;
15 import java.util.Objects;
16 import java.util.concurrent.ConcurrentHashMap;
17 import org.apache.thrift.TException;
18 import org.opendaylight.netvirt.bgpmanager.BgpConfigurationManager;
19 import org.opendaylight.netvirt.bgpmanager.thrift.client.BgpRouterException;
20 import org.opendaylight.yang.gen.v1.urn.ericsson.params.xml.ns.yang.ebgp.rev150901.Bgp;
21 import org.opendaylight.yang.gen.v1.urn.ericsson.params.xml.ns.yang.ebgp.rev150901.bgp.neighborscontainer.Neighbors;
22 import org.opendaylight.yang.gen.v1.urn.ericsson.params.xml.ns.yang.ebgp.rev150901.bgp.neighborscontainer.NeighborsKey;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
26 public class BgpAlarms implements Runnable, AutoCloseable {
27 private static final Logger LOG = LoggerFactory.getLogger(BgpAlarms.class);
28 private static final String ALARM_TEXT = "Bgp Neighbor TCP connection is down";
30 private final BgpJMXAlarmAgent alarmAgent = new BgpJMXAlarmAgent();
31 private final BgpConfigurationManager bgpMgr;
32 private final Map<String, BgpAlarmStatus> neighborsRaisedAlarmStatusMap = new ConcurrentHashMap<>();
33 private volatile List<Neighbors> nbrList = null;
35 public BgpAlarms(BgpConfigurationManager bgpManager) {
36 bgpMgr = Objects.requireNonNull(bgpManager);
40 alarmAgent.registerMbean();
41 Bgp bgp = bgpMgr.getConfig();
42 if (bgp != null && bgp.getNeighborsContainer() != null) {
43 Map<NeighborsKey, Neighbors> keyNeighborsMap = bgp.getNeighborsContainer().getNeighbors();
44 if (keyNeighborsMap != null) {
45 for (Neighbors nbr : keyNeighborsMap.values()) {
46 LOG.trace("Clearing Neighbor DOWN alarm at the startup for Neighbor {}",
47 nbr.getAddress().getValue());
48 clearBgpNbrDownAlarm(nbr.getAddress().getValue());
56 alarmAgent.unregisterMbean();
61 LOG.debug("Fetching neighbor status' from BGP");
62 BgpCounters.resetFile(BgpCounters.BGP_VPNV4_SUMMARY_FILE);
63 BgpCounters.resetFile(BgpCounters.BGP_VPNV6_SUMMARY_FILE);
64 BgpCounters.resetFile(BgpCounters.BGP_EVPN_SUMMARY_FILE);
65 Map<String, String> neighborStatusMap = new HashMap<>();
67 if (bgpMgr.getBgpCounters() != null) {
68 bgpMgr.getBgpCounters().fetchCmdOutputs(BgpCounters.BGP_VPNV4_SUMMARY_FILE,
69 "show ip bgp vpnv4 all summary");
70 if (bgpMgr.getConfig() != null) {
71 nbrList = new ArrayList<Neighbors>(bgpMgr.getConfig().getNeighborsContainer().getNeighbors().values());
73 BgpCounters.parseIpBgpVpnv4AllSummary(neighborStatusMap);
75 bgpMgr.getBgpCounters().fetchCmdOutputs(BgpCounters.BGP_VPNV6_SUMMARY_FILE,
76 "show ip bgp vpnv6 all summary");
78 BgpCounters.parseIpBgpVpnv6AllSummary(neighborStatusMap);
80 bgpMgr.getBgpCounters().fetchCmdOutputs(BgpCounters.BGP_EVPN_SUMMARY_FILE,
81 "show bgp l2vpn evpn all summary");
83 BgpCounters.parseBgpL2vpnEvpnAllSummary(neighborStatusMap);
85 processNeighborStatusMap(neighborStatusMap, nbrList);
87 LOG.debug("Finished getting the status of BGP neighbors");
90 private void processNeighborStatusMap(Map<String, String> nbrStatusMap, List<Neighbors> nbrs) {
91 if (nbrs == null || nbrs.isEmpty()) {
92 LOG.trace("No BGP neighbors configured.");
96 LOG.debug("Fetching neighbor status' from BGP, #of neighbors: {}", nbrList.size());
97 for (Neighbors nbr : nbrList) {
98 boolean alarmToRaise = false;
100 LOG.trace("nbr {} checking status, AS num: {}", nbr.getAddress().getValue(), nbr.getRemoteAs());
101 bgpMgr.getPeerStatus(nbr.getAddress().getValue(), nbr.getRemoteAs().longValue());
102 LOG.trace("nbr {} status is: PEER UP", nbr.getAddress().getValue());
103 } catch (BgpRouterException bre) {
104 if (bre.getErrorCode() == BgpRouterException.BGP_PEER_DOWN) {
105 LOG.error("nbr {} status is: DOWN", nbr.getAddress().getValue());
107 } else if (bre.getErrorCode() == BgpRouterException.BGP_PEER_NOTCONFIGURED) {
108 LOG.info("nbr {} status is: NOT CONFIGURED", nbr.getAddress().getValue());
109 } else if (bre.getErrorCode() == BgpRouterException.BGP_PEER_UNKNOWN) {
110 LOG.info("nbr {} status is: Unknown", nbr.getAddress().getValue());
112 LOG.info("nbr {} status is: Unknown, invalid BgpRouterException: ",
113 nbr.getAddress().getValue(), bre);
115 } catch (TException tae) {
116 LOG.error("nbr {} status is: Unknown, received TException: ", nbr.getAddress().getValue(), tae);
119 final BgpAlarmStatus alarmStatus = neighborsRaisedAlarmStatusMap.get(nbr.getAddress().getValue());
121 if (alarmStatus == null || alarmStatus != BgpAlarmStatus.RAISED) {
122 LOG.trace("alarm raised for {}.", nbr.getAddress().getValue());
123 raiseBgpNbrDownAlarm(nbr.getAddress().getValue());
125 LOG.trace("alarm raised already for {}", nbr.getAddress().getValue());
128 if (alarmStatus == null || alarmStatus != BgpAlarmStatus.CLEARED) {
129 clearBgpNbrDownAlarm(nbr.getAddress().getValue());
130 LOG.trace("alarm cleared for {}", nbr.getAddress().getValue());
132 LOG.trace("alarm cleared already for {}", nbr.getAddress().getValue());
139 private void raiseBgpNbrDownAlarm(String nbrIp) {
141 StringBuilder source = new StringBuilder();
142 source.append("BGP_Neighbor=").append(nbrIp);
143 if (nbrIp == null || nbrIp.isEmpty()) {
146 LOG.trace("Raising BgpControlPathFailure alarm. {} alarmtext {} ", source, ALARM_TEXT);
147 //Invokes JMX raiseAlarm method
148 alarmAgent.invokeFMraisemethod("BgpControlPathFailure", ALARM_TEXT, source.toString());
150 neighborsRaisedAlarmStatusMap.put(nbrIp, BgpAlarmStatus.RAISED);
153 public void clearBgpNbrDownAlarm(String nbrIp) {
154 StringBuilder source = new StringBuilder();
155 source.append("BGP_Neighbor=").append(nbrIp);
156 if (nbrIp == null || nbrIp.isEmpty()) {
159 LOG.trace("Clearing BgpControlPathFailure alarm of source {} alarmtext {} ", source, ALARM_TEXT);
160 //Invokes JMX clearAlarm method
161 alarmAgent.invokeFMclearmethod("BgpControlPathFailure", ALARM_TEXT, source.toString());
163 neighborsRaisedAlarmStatusMap.put(nbrIp, BgpAlarmStatus.CLEARED);