3b933af5bd3f019e2be3a34c28c861030efb3183
[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     public static void displayAll(final ObjectName objectName, final BgpSessionState bgpSessionState, final PrintStream printStream) {
25         if (bgpSessionState == null) {
26             printStream.println(String.format("No BgpSessionState found for [%s]", objectName));
27             return;
28         }
29         final ShellTable table = new ShellTable();
30         table.column("Attribute").alignLeft();
31         table.column("Value").alignLeft();
32         table.addRow().addContent("Object Name", objectName.getCanonicalName());
33         table.addRow().addContent("HoldtimeCurrent", bgpSessionState.getHoldtimeCurrent());
34         table.addRow().addContent("KeepaliveCurrent", bgpSessionState.getKeepaliveCurrent());
35         table.addRow().addContent("SessionDuration", bgpSessionState.getSessionDuration());
36         table.addRow().addContent("SessionState", bgpSessionState.getSessionState());
37
38         //Messages Stats
39         final MessagesStats messageStats = bgpSessionState.getMessagesStats();
40         if (messageStats != null) {
41             table.addRow().addContent(MESSAGE_STATS_LABEL + "ErrorMsgsSent", messageStats.getErrorMsgs().getErrorSent().size());
42             table.addRow().addContent(MESSAGE_STATS_LABEL + "ErrorMsgsReceived", messageStats.getErrorMsgs().getErrorReceived().size());
43             table.addRow().addContent(MESSAGE_STATS_LABEL + "KeepAliveMsgsSent", messageStats.getKeepAliveMsgs().getSent().getCount());
44             table.addRow().addContent(MESSAGE_STATS_LABEL + "KeepAliveMsgsReceived", messageStats.getKeepAliveMsgs().getReceived().getCount());
45             table.addRow().addContent(MESSAGE_STATS_LABEL + "TotalMsgsSent", messageStats.getTotalMsgs().getSent().getCount());
46             table.addRow().addContent(MESSAGE_STATS_LABEL + "TotalMsgsReceived", messageStats.getTotalMsgs().getReceived().getCount());
47             table.addRow().addContent(MESSAGE_STATS_LABEL + "UpdateMsgsSent", messageStats.getUpdateMsgs().getSent().getCount());
48             table.addRow().addContent(MESSAGE_STATS_LABEL + "UpdateMsgsReceived", messageStats.getUpdateMsgs().getReceived().getCount());
49         }
50
51         //Peer Preferences
52         final LocalPeerPreferences peerPreferences = bgpSessionState.getLocalPeerPreferences();
53         if (peerPreferences != null) {
54             table.addRow().addContent(PEER_PREFERENCES_LABEL + "AddPathCapability", peerPreferences.getAddPathCapability());
55             table.addRow().addContent(PEER_PREFERENCES_LABEL + "AS", peerPreferences.getAs());
56             table.addRow().addContent(PEER_PREFERENCES_LABEL + "BgpExtendedMessageCapability", peerPreferences.getBgpExtendedMessageCapability());
57             table.addRow().addContent(PEER_PREFERENCES_LABEL + "BgpId", peerPreferences.getBgpId());
58             table.addRow().addContent(PEER_PREFERENCES_LABEL + "FourOctetAsCapability", peerPreferences.getFourOctetAsCapability());
59             table.addRow().addContent(PEER_PREFERENCES_LABEL + "GrCapability", peerPreferences.getGrCapability());
60             table.addRow().addContent(PEER_PREFERENCES_LABEL + "Port", peerPreferences.getPort());
61             table.addRow().addContent(PEER_PREFERENCES_LABEL + "RouteRefreshCapability", peerPreferences.getRouteRefreshCapability());
62         }
63
64         //Speaker Preferences
65         final RemotePeerPreferences speakerPreferences = bgpSessionState.getRemotePeerPreferences();
66         if (speakerPreferences != null) {
67             table.addRow().addContent(SPEAKER_PREFERENCES_LABEL + "AddPathCapability", speakerPreferences.getAddPathCapability());
68             table.addRow().addContent(SPEAKER_PREFERENCES_LABEL + "AS", speakerPreferences.getAs());
69             table.addRow().addContent(SPEAKER_PREFERENCES_LABEL + "BgpExtendedMessageCapability", speakerPreferences.getBgpExtendedMessageCapability());
70             table.addRow().addContent(SPEAKER_PREFERENCES_LABEL + "BgpId", speakerPreferences.getBgpId());
71             table.addRow().addContent(SPEAKER_PREFERENCES_LABEL + "FourOctetAsCapability", speakerPreferences.getFourOctetAsCapability());
72             table.addRow().addContent(SPEAKER_PREFERENCES_LABEL + "GrCapability", speakerPreferences.getGrCapability());
73             table.addRow().addContent(SPEAKER_PREFERENCES_LABEL + "Port", speakerPreferences.getPort());
74             table.addRow().addContent(SPEAKER_PREFERENCES_LABEL + "RouteRefreshCapability", speakerPreferences.getRouteRefreshCapability());
75         }
76
77         table.print(printStream);
78     }
79 }