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