Fix sonar complains
[bgpcep.git] / bgp / cli / src / main / java / org / opendaylight / protocol / bgp / cli / utils / BgpCliUtils.java
1 /*
2  * Copyright © 2016 Inocybe Technologies 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.protocol.bgp.cli.utils;
10
11 import java.io.PrintStream;
12 import javax.management.ObjectName;
13 import org.apache.karaf.shell.table.ShellTable;
14 import org.opendaylight.controller.config.yang.bgp.rib.impl.BgpSessionState;
15 import org.opendaylight.controller.config.yang.bgp.rib.impl.LocalPeerPreferences;
16 import org.opendaylight.controller.config.yang.bgp.rib.impl.MessagesStats;
17 import org.opendaylight.controller.config.yang.bgp.rib.impl.RemotePeerPreferences;
18
19 public final class BgpCliUtils {
20     private static final String MESSAGE_STATS_LABEL = "MessagesStats.";
21     private static final String PEER_PREFERENCES_LABEL = "PeerPreferences.";
22     private static final String SPEAKER_PREFERENCES_LABEL = "SpeakerPreferences.";
23
24     private BgpCliUtils() {
25         throw new UnsupportedOperationException();
26     }
27     public static void displayAll(final ObjectName objectName, final BgpSessionState bgpSessionState, final PrintStream printStream) {
28         if (bgpSessionState == null) {
29             printStream.println(String.format("No BgpSessionState found for [%s]", objectName));
30             return;
31         }
32         final ShellTable table = new ShellTable();
33         table.column("Attribute").alignLeft();
34         table.column("Value").alignLeft();
35         table.addRow().addContent("Object Name", objectName.getCanonicalName());
36         table.addRow().addContent("HoldtimeCurrent", bgpSessionState.getHoldtimeCurrent());
37         table.addRow().addContent("KeepaliveCurrent", bgpSessionState.getKeepaliveCurrent());
38         table.addRow().addContent("SessionDuration", bgpSessionState.getSessionDuration());
39         table.addRow().addContent("SessionState", bgpSessionState.getSessionState());
40
41         //Messages Stats
42         final MessagesStats messageStats = bgpSessionState.getMessagesStats();
43         if (messageStats != null) {
44             table.addRow().addContent(MESSAGE_STATS_LABEL + "ErrorMsgsSent", messageStats.getErrorMsgs().getErrorSent().size());
45             table.addRow().addContent(MESSAGE_STATS_LABEL + "ErrorMsgsReceived", messageStats.getErrorMsgs().getErrorReceived().size());
46             table.addRow().addContent(MESSAGE_STATS_LABEL + "KeepAliveMsgsSent", messageStats.getKeepAliveMsgs().getSent().getCount());
47             table.addRow().addContent(MESSAGE_STATS_LABEL + "KeepAliveMsgsReceived", messageStats.getKeepAliveMsgs().getReceived().getCount());
48             table.addRow().addContent(MESSAGE_STATS_LABEL + "TotalMsgsSent", messageStats.getTotalMsgs().getSent().getCount());
49             table.addRow().addContent(MESSAGE_STATS_LABEL + "TotalMsgsReceived", messageStats.getTotalMsgs().getReceived().getCount());
50             table.addRow().addContent(MESSAGE_STATS_LABEL + "UpdateMsgsSent", messageStats.getUpdateMsgs().getSent().getCount());
51             table.addRow().addContent(MESSAGE_STATS_LABEL + "UpdateMsgsReceived", messageStats.getUpdateMsgs().getReceived().getCount());
52         }
53
54         //Peer Preferences
55         final LocalPeerPreferences peerPreferences = bgpSessionState.getLocalPeerPreferences();
56         if (peerPreferences != null) {
57             table.addRow().addContent(PEER_PREFERENCES_LABEL + "AddPathCapability", peerPreferences.getAddPathCapability());
58             table.addRow().addContent(PEER_PREFERENCES_LABEL + "AS", peerPreferences.getAs());
59             table.addRow().addContent(PEER_PREFERENCES_LABEL + "BgpExtendedMessageCapability", peerPreferences.getBgpExtendedMessageCapability());
60             table.addRow().addContent(PEER_PREFERENCES_LABEL + "BgpId", peerPreferences.getBgpId());
61             table.addRow().addContent(PEER_PREFERENCES_LABEL + "FourOctetAsCapability", peerPreferences.getFourOctetAsCapability());
62             table.addRow().addContent(PEER_PREFERENCES_LABEL + "GrCapability", peerPreferences.getGrCapability());
63             table.addRow().addContent(PEER_PREFERENCES_LABEL + "Port", peerPreferences.getPort());
64             table.addRow().addContent(PEER_PREFERENCES_LABEL + "RouteRefreshCapability", peerPreferences.getRouteRefreshCapability());
65         }
66
67         //Speaker Preferences
68         final RemotePeerPreferences speakerPreferences = bgpSessionState.getRemotePeerPreferences();
69         if (speakerPreferences != null) {
70             table.addRow().addContent(SPEAKER_PREFERENCES_LABEL + "AddPathCapability", speakerPreferences.getAddPathCapability());
71             table.addRow().addContent(SPEAKER_PREFERENCES_LABEL + "AS", speakerPreferences.getAs());
72             table.addRow().addContent(SPEAKER_PREFERENCES_LABEL + "BgpExtendedMessageCapability", speakerPreferences.getBgpExtendedMessageCapability());
73             table.addRow().addContent(SPEAKER_PREFERENCES_LABEL + "BgpId", speakerPreferences.getBgpId());
74             table.addRow().addContent(SPEAKER_PREFERENCES_LABEL + "FourOctetAsCapability", speakerPreferences.getFourOctetAsCapability());
75             table.addRow().addContent(SPEAKER_PREFERENCES_LABEL + "GrCapability", speakerPreferences.getGrCapability());
76             table.addRow().addContent(SPEAKER_PREFERENCES_LABEL + "Port", speakerPreferences.getPort());
77             table.addRow().addContent(SPEAKER_PREFERENCES_LABEL + "RouteRefreshCapability", speakerPreferences.getRouteRefreshCapability());
78         }
79
80         table.print(printStream);
81     }
82 }