dd2984fb2dbed89bf4478a092cb846614ea3ebd5
[bgpcep.git] / bgp / cli / src / main / java / org / opendaylight / protocol / bgp / cli / utils / NeighborStateCliUtils.java
1 /*
2  * Copyright (c) 2017 AT&T Intellectual Property. 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 package org.opendaylight.protocol.bgp.cli.utils;
9
10 import java.io.PrintStream;
11 import java.util.List;
12 import org.apache.karaf.shell.table.ShellTable;
13 import org.eclipse.jdt.annotation.NonNull;
14 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.multiprotocol.rev151009.bgp.common.afi.safi.list.AfiSafi;
15 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.operational.rev151009.bgp.neighbor.prefix.counters_state.Prefixes;
16 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.neighbor.group.State;
17 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.neighbor.group.Timers;
18 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.neighbor.group.Transport;
19 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.neighbors.Neighbor;
20 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.types.rev151009.BgpCapability;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.openconfig.extensions.rev171207.BgpNeighborStateAugmentation;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.openconfig.extensions.rev171207.NeighborAfiSafiStateAugmentation;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.openconfig.extensions.rev171207.NeighborStateAugmentation;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.openconfig.extensions.rev171207.NeighborTimersStateAugmentation;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.openconfig.extensions.rev171207.NeighborTransportStateAugmentation;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.openconfig.extensions.rev171207.network.instances.network.instance.protocols.protocol.bgp.neighbors.neighbor.state.Messages;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.openconfig.extensions.rev171207.network.instances.network.instance.protocols.protocol.bgp.neighbors.neighbor.state.messages.Received;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.openconfig.extensions.rev171207.network.instances.network.instance.protocols.protocol.bgp.neighbors.neighbor.state.messages.Sent;
30
31 //NeighborStateCliUtils sends Neighbor Operational State to PrintStream
32 final class NeighborStateCliUtils {
33     private NeighborStateCliUtils() {
34         throw new UnsupportedOperationException();
35     }
36
37     static void displayNeighborOperationalState(@NonNull final String neighborId,
38             @NonNull final Neighbor neighbor, @NonNull final PrintStream stream) {
39         final State neighborState = neighbor.getState();
40         if (neighborState == null) {
41             stream.println(String.format("No BgpSessionState found for [%s]", neighborId));
42             return;
43         }
44
45         final ShellTable table = new ShellTable();
46         table.column("Attribute").alignLeft();
47         table.column("Value").alignLeft();
48         table.addRow().addContent("Neighbor Address", neighborId);
49
50         final NeighborStateAugmentation stateAug = neighborState.getAugmentation(NeighborStateAugmentation.class);
51         if (stateAug != null) {
52             table.addRow().addContent("Session State", stateAug.getSessionState());
53             printCapabilitiesState(stateAug.getSupportedCapabilities(), table);
54         }
55
56         printTimerState(neighbor.getTimers(), table);
57         printTransportState(neighbor.getTransport(), table);
58         printMessagesState(neighborState, table);
59         printAfiSafisState(neighbor.getAfiSafis().getAfiSafi(), table);
60
61         table.print(stream);
62     }
63
64     private static void printCapabilitiesState(final List<Class<? extends BgpCapability>> supportedCapabilities,
65             final ShellTable table) {
66         if (supportedCapabilities == null) {
67             return;
68         }
69         addHeader(table, "Supported Capabilities");
70         supportedCapabilities.forEach(capa -> table.addRow().addContent("", capa.getSimpleName()));
71     }
72
73     static void addHeader(final ShellTable table, final String header) {
74         table.addRow().addContent("                      ", "");
75         table.addRow().addContent(header, "");
76         table.addRow().addContent("======================", "");
77     }
78
79     private static void printAfiSafisState(final List<AfiSafi> afiSafis, final ShellTable table) {
80         afiSafis.forEach(afiSafi -> printAfiSafiState(afiSafi, table));
81
82     }
83
84     private static void printAfiSafiState(final AfiSafi afiSafi, final ShellTable table) {
85         final NeighborAfiSafiStateAugmentation state = afiSafi.getState()
86                 .getAugmentation(NeighborAfiSafiStateAugmentation.class);
87         addHeader(table, "AFI state");
88         table.addRow().addContent("Family", afiSafi.getAfiSafiName().getSimpleName());
89         table.addRow().addContent("Active", state.isActive());
90         final Prefixes prefixes = state.getPrefixes();
91         if (prefixes == null) {
92             return;
93         }
94         table.addRow().addContent("Prefixes", "");
95         table.addRow().addContent("Installed", prefixes.getInstalled());
96         table.addRow().addContent("Sent", prefixes.getSent());
97         table.addRow().addContent("Received", prefixes.getReceived());
98
99     }
100
101     private static void printMessagesState(final State neighborState, final ShellTable table) {
102         final BgpNeighborStateAugmentation state = neighborState.getAugmentation(BgpNeighborStateAugmentation.class);
103         if (state == null) {
104             return;
105         }
106         addHeader(table, "Messages state");
107         final Messages messages = state.getMessages();
108         table.addRow().addContent("Messages Received", "");
109
110         final Received received = messages.getReceived();
111         table.addRow().addContent("NOTIFICATION", received.getNOTIFICATION());
112         table.addRow().addContent("UPDATE", received.getUPDATE());
113
114         final Sent sent = messages.getSent();
115         table.addRow().addContent("Messages Sent", "");
116         table.addRow().addContent("NOTIFICATION", sent.getNOTIFICATION());
117         table.addRow().addContent("UPDATE", sent.getUPDATE());
118     }
119
120     private static void printTransportState(final Transport transport, final ShellTable table) {
121         if (transport == null) {
122             return;
123         }
124         final NeighborTransportStateAugmentation state = transport.getState()
125                 .getAugmentation(NeighborTransportStateAugmentation.class);
126         if (state == null) {
127             return;
128         }
129         addHeader(table, "Transport state");
130
131         final IpAddress remoteAddress = state.getRemoteAddress();
132         final String stringRemoteAddress;
133         if (remoteAddress.getIpv4Address() == null) {
134             stringRemoteAddress = remoteAddress.getIpv6Address().getValue();
135         } else {
136             stringRemoteAddress = remoteAddress.getIpv4Address().getValue();
137         }
138         table.addRow().addContent("Remote Address", stringRemoteAddress);
139         table.addRow().addContent("Remote Port", state.getRemotePort().getValue());
140         table.addRow().addContent("Local Port", state.getLocalPort().getValue());
141     }
142
143     private static void printTimerState(final Timers timers, final ShellTable table) {
144         if (timers == null) {
145             return;
146         }
147
148         final NeighborTimersStateAugmentation state = timers.getState()
149                 .getAugmentation(NeighborTimersStateAugmentation.class);
150         if (state == null) {
151             return;
152         }
153         addHeader(table, "Timer state");
154         table.addRow().addContent("Negotiated Hold Time", state.getNegotiatedHoldTime());
155         table.addRow().addContent("Uptime", state.getUptime().getValue());
156     }
157 }