75dc3c5f0ccceed61593f9b07ef0f2c41d9519fa
[bgpcep.git] / pcep / topology / cli / src / main / java / org / opendaylight / protocol / pcep / cli / utils / PcepStateUtils.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.pcep.cli.utils;
9
10 import java.io.PrintStream;
11 import java.util.Arrays;
12 import java.util.concurrent.ExecutionException;
13 import org.apache.karaf.shell.support.table.ShellTable;
14 import org.eclipse.jdt.annotation.NonNull;
15 import org.opendaylight.mdsal.binding.api.DataBroker;
16 import org.opendaylight.mdsal.binding.api.ReadTransaction;
17 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.stateful.stats.rev171113.PcepEntityIdStatsAug;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.stateful.stats.rev171113.StatefulCapabilitiesStatsAug;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.stateful.stats.rev171113.StatefulMessagesStatsAug;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.stats.rev171113.Error;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.stats.rev171113.Preferences;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.stats.rev171113.error.messages.grouping.ErrorMessages;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.stats.rev171113.pcep.session.state.LocalPref;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.stats.rev171113.pcep.session.state.Messages;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.stats.rev171113.pcep.session.state.PeerCapabilities;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.stats.rev171113.pcep.session.state.PeerPref;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.stats.rev171113.pcep.session.state.grouping.PcepSessionState;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.stats.rev171113.reply.time.grouping.ReplyTime;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.stats.rev171113.PcepTopologyNodeStatsAug;
31 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
32 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
33 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
34 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
35 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
36 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
37 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
38 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41
42
43 /**
44  * PcepStateUtils reads Pcep Topology Node state from DS and prints to stream.
45  */
46 public final class PcepStateUtils {
47
48     private static final Logger LOG = LoggerFactory.getLogger(PcepStateUtils.class);
49
50     @SuppressWarnings("checkstyle:HideUtilityClassConstructor")
51     private PcepStateUtils() {
52         throw new UnsupportedOperationException();
53     }
54
55     /**
56      * Display to stream operational state, rib Id is mandatory.
57      *
58      * @param dataBroker data broker
59      * @param stream     where to print
60      * @param topologyId mandatory, Topology where Node pertains
61      * @param nodeId     mandatory, State per given Node Id will be printed
62      */
63     public static void displayNodeState(@NonNull final DataBroker dataBroker,
64             @NonNull final PrintStream stream, @NonNull final String topologyId, @NonNull final String nodeId) {
65         final Node node = readNodeFromDataStore(dataBroker, topologyId, nodeId);
66         if (node == null) {
67             stream.println(String.format("Node [%s] not found", nodeId));
68             return;
69         }
70         final PcepTopologyNodeStatsAug state = node.augmentation(PcepTopologyNodeStatsAug.class);
71         if (state == null) {
72             stream.println(String.format("State not found for [%s]", nodeId));
73             return;
74         }
75         final PcepSessionState nodeState = state.getPcepSessionState();
76         displayNodeState(topologyId, nodeId, nodeState, stream);
77     }
78
79     private static void displayNodeState(
80             final String topologyId,
81             final String nodeId,
82             final PcepSessionState pcepSessionState,
83             final PrintStream stream) {
84         final ShellTable table = new ShellTable();
85         table.column("Attribute").alignLeft();
86         table.column("Value").alignLeft();
87
88         showNodeState(table, topologyId, nodeId, pcepSessionState);
89
90         addHeader(table, "Local preferences");
91         final LocalPref localPref = pcepSessionState.getLocalPref();
92         showPreferences(table, localPref);
93         final PcepEntityIdStatsAug entAug = localPref.augmentation(PcepEntityIdStatsAug.class);
94         if (entAug != null) {
95             table.addRow().addContent("Speaker Entity Identifier",
96                     Arrays.toString(entAug.getSpeakerEntityIdValue()));
97         }
98
99         addHeader(table, "Peer preferences");
100         final PeerPref peerPref = pcepSessionState.getPeerPref();
101         showPreferences(table, peerPref);
102
103         showCapabilities(table, pcepSessionState.getPeerCapabilities());
104
105         final Messages messages = pcepSessionState.getMessages();
106         showMessages(table, messages);
107
108         final ErrorMessages error = messages.getErrorMessages();
109         showErrorMessages(table, error);
110
111         final ReplyTime reply = messages.getReplyTime();
112         showReplyMessages(table, reply);
113
114         table.print(stream);
115     }
116
117     private static void showNodeState(final ShellTable table, final String topologyId, final String nodeId,
118             final PcepSessionState pcepSessionState) {
119         addHeader(table, "Node state");
120         table.addRow().addContent("Topology Id", topologyId);
121         table.addRow().addContent("Node Id", nodeId);
122         table.addRow().addContent("Session duration", pcepSessionState.getSessionDuration());
123         table.addRow().addContent("Synchronized", pcepSessionState.isSynchronized());
124         table.addRow().addContent("Delegated Lsp Count", pcepSessionState.getDelegatedLspsCount());
125     }
126
127     private static void showCapabilities(final ShellTable table, final PeerCapabilities capa) {
128         if (capa == null) {
129             return;
130         }
131         final StatefulCapabilitiesStatsAug stateFulCapa = capa.augmentation(StatefulCapabilitiesStatsAug.class);
132         if (stateFulCapa != null) {
133             addHeader(table, "Stateful Capabilities");
134             table.addRow().addContent("Stateful", stateFulCapa.isStateful());
135             table.addRow().addContent("Active", stateFulCapa.isActive());
136             table.addRow().addContent("Instantiation", stateFulCapa.isInstantiation());
137         }
138
139     }
140
141     private static void showMessages(final ShellTable table, final Messages messages) {
142         if (messages == null) {
143             return;
144         }
145         addHeader(table, "Messages");
146         table.addRow().addContent("Last Sent Msg Timestamp", messages.getLastSentMsgTimestamp());
147         table.addRow().addContent("Received Msg Count", messages.getReceivedMsgCount());
148         table.addRow().addContent("Sent Msg Count", messages.getSentMsgCount());
149         table.addRow().addContent("Unknown Msg Received", messages.getUnknownMsgReceived());
150
151         final StatefulMessagesStatsAug statefulMessages = messages.augmentation(StatefulMessagesStatsAug.class);
152         if (statefulMessages == null) {
153             return;
154         }
155         addHeader(table, " Stateful Messages");
156         table.addRow().addContent("Last Received RptMsg Timestamp", statefulMessages
157                 .getLastReceivedRptMsgTimestamp());
158         table.addRow().addContent("Received RptMsg", statefulMessages.getReceivedRptMsgCount());
159         table.addRow().addContent("Sent Init Msg", statefulMessages.getSentInitMsgCount());
160         table.addRow().addContent("Sent Upd Msg", statefulMessages.getSentUpdMsgCount());
161     }
162
163     private static void showReplyMessages(final ShellTable table, final ReplyTime reply) {
164         if (reply == null) {
165             return;
166         }
167         addHeader(table, "Reply Messages");
168         table.addRow().addContent("Average Time", reply.getAverageTime());
169         table.addRow().addContent("Max Timet", reply.getMaxTime());
170         table.addRow().addContent("Min Time", reply.getMinTime());
171     }
172
173     private static void showErrorMessages(final ShellTable table, final ErrorMessages error) {
174         if (error == null) {
175             return;
176         }
177         addHeader(table, "Error Messages");
178         table.addRow().addContent("Sent Error Msg Count", error.getSentErrorMsgCount());
179         table.addRow().addContent("Received Error Msg Count", error.getReceivedErrorMsgCount());
180
181         showError(table, error.getLastSentError(), "Last Sent Error");
182         showError(table, error.getLastReceivedError(), "Last Received Error");
183     }
184
185     private static void showError(final ShellTable table, final Error error, final String errorMsgHeader) {
186         if (error == null) {
187             return;
188         }
189         addHeader(table, errorMsgHeader);
190         table.addRow().addContent("Type", error.getErrorType());
191         table.addRow().addContent("Value", error.getErrorValue());
192
193     }
194
195     private static void showPreferences(final ShellTable table, final Preferences preferences) {
196         table.addRow().addContent("Session id", preferences.getSessionId());
197         table.addRow().addContent("Ip Address", preferences.getIpAddress());
198         table.addRow().addContent("Dead Timer", preferences.getDeadtimer());
199         table.addRow().addContent("Keep Alive", preferences.getKeepalive());
200     }
201
202     private static Node readNodeFromDataStore(final DataBroker dataBroker,
203             final String topologyId, final String nodeId) {
204         final InstanceIdentifier<Node> topology = InstanceIdentifier.builder(NetworkTopology.class)
205                 .child(Topology.class, new TopologyKey(new TopologyId(topologyId)))
206                 .child(Node.class, new NodeKey(new NodeId(nodeId))).build();
207
208         final ReadTransaction rot = dataBroker.newReadOnlyTransaction();
209
210         try {
211             return rot.read(LogicalDatastoreType.OPERATIONAL, topology).get().orElse(null);
212         } catch (final InterruptedException | ExecutionException e) {
213             LOG.warn("Failed to read node {}", nodeId, e);
214         }
215         return null;
216     }
217
218
219     private static void addHeader(final ShellTable table, final String header) {
220         table.addRow().addContent("                      ", "");
221         table.addRow().addContent(header, "");
222         table.addRow().addContent("======================", "");
223     }
224 }