MRI version bumpup for Aluminium
[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.ArrayList;
12 import java.util.HashMap;
13 import java.util.List;
14 import java.util.Map;
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;
25
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";
29
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;
34
35     public BgpAlarms(BgpConfigurationManager bgpManager) {
36         bgpMgr = Objects.requireNonNull(bgpManager);
37     }
38
39     public void init() {
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());
49                 }
50             }
51         }
52     }
53
54     @Override
55     public void close() {
56         alarmAgent.unregisterMbean();
57     }
58
59     @Override
60     public void run() {
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<>();
66
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());
72             }
73             BgpCounters.parseIpBgpVpnv4AllSummary(neighborStatusMap);
74
75             bgpMgr.getBgpCounters().fetchCmdOutputs(BgpCounters.BGP_VPNV6_SUMMARY_FILE,
76                     "show ip bgp vpnv6 all summary");
77
78             BgpCounters.parseIpBgpVpnv6AllSummary(neighborStatusMap);
79
80             bgpMgr.getBgpCounters().fetchCmdOutputs(BgpCounters.BGP_EVPN_SUMMARY_FILE,
81                     "show bgp l2vpn evpn all summary");
82
83             BgpCounters.parseBgpL2vpnEvpnAllSummary(neighborStatusMap);
84
85             processNeighborStatusMap(neighborStatusMap, nbrList);
86         }
87         LOG.debug("Finished getting the status of BGP neighbors");
88     }
89
90     private void processNeighborStatusMap(Map<String, String> nbrStatusMap, List<Neighbors> nbrs) {
91         if (nbrs == null || nbrs.isEmpty()) {
92             LOG.trace("No BGP neighbors configured.");
93             return;
94         }
95
96         LOG.debug("Fetching neighbor status' from BGP, #of neighbors: {}", nbrList.size());
97         for (Neighbors nbr : nbrList) {
98             boolean alarmToRaise = false;
99             try {
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());
106                     alarmToRaise = true;
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());
111                 } else {
112                     LOG.info("nbr {} status is: Unknown, invalid BgpRouterException: ",
113                         nbr.getAddress().getValue(), bre);
114                 }
115             } catch (TException tae) {
116                 LOG.error("nbr {} status is: Unknown, received TException: ", nbr.getAddress().getValue(), tae);
117             }
118
119             final BgpAlarmStatus alarmStatus = neighborsRaisedAlarmStatusMap.get(nbr.getAddress().getValue());
120             if (alarmToRaise) {
121                 if (alarmStatus == null || alarmStatus != BgpAlarmStatus.RAISED) {
122                     LOG.trace("alarm raised for {}.", nbr.getAddress().getValue());
123                     raiseBgpNbrDownAlarm(nbr.getAddress().getValue());
124                 } else {
125                     LOG.trace("alarm raised already for {}", nbr.getAddress().getValue());
126                 }
127             } else {
128                 if (alarmStatus == null || alarmStatus != BgpAlarmStatus.CLEARED) {
129                     clearBgpNbrDownAlarm(nbr.getAddress().getValue());
130                     LOG.trace("alarm cleared for {}", nbr.getAddress().getValue());
131                 } else {
132                     LOG.trace("alarm cleared already for {}", nbr.getAddress().getValue());
133                 }
134             }
135         }
136     }
137
138
139     private void raiseBgpNbrDownAlarm(String nbrIp) {
140
141         StringBuilder source = new StringBuilder();
142         source.append("BGP_Neighbor=").append(nbrIp);
143         if (nbrIp == null || nbrIp.isEmpty()) {
144             return;
145         }
146         LOG.trace("Raising BgpControlPathFailure alarm. {} alarmtext {} ", source, ALARM_TEXT);
147         //Invokes JMX raiseAlarm method
148         alarmAgent.invokeFMraisemethod("BgpControlPathFailure", ALARM_TEXT, source.toString());
149
150         neighborsRaisedAlarmStatusMap.put(nbrIp, BgpAlarmStatus.RAISED);
151     }
152
153     public void clearBgpNbrDownAlarm(String nbrIp) {
154         StringBuilder source = new StringBuilder();
155         source.append("BGP_Neighbor=").append(nbrIp);
156         if (nbrIp == null || nbrIp.isEmpty()) {
157             return;
158         }
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());
162
163         neighborsRaisedAlarmStatusMap.put(nbrIp, BgpAlarmStatus.CLEARED);
164     }
165 }