blob: a625d553b6ac8aa5cd0c358c5f0b91e28e276819 [file] [log] [blame]
sunishvkf7c56552016-07-18 16:02:39 +05301/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002* Copyright 2016-present Open Networking Foundation
sunishvkf7c56552016-07-18 16:02:39 +05303*
4* Licensed under the Apache License, Version 2.0 (the "License");
5* you may not use this file except in compliance with the License.
6* You may obtain a copy of the License at
7*
8* http://www.apache.org/licenses/LICENSE-2.0
9*
10* Unless required by applicable law or agreed to in writing, software
11* distributed under the License is distributed on an "AS IS" BASIS,
12* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13* See the License for the specific language governing permissions and
14* limitations under the License.
15*/
16package org.onosproject.ospf.cli;
17
Ray Milkeyd84f89b2018-08-17 14:54:17 -070018import org.osgi.service.component.annotations.Activate;
19import org.osgi.service.component.annotations.Component;
20import org.osgi.service.component.annotations.Deactivate;
21import org.osgi.service.component.annotations.Reference;
22import org.osgi.service.component.annotations.ReferenceCardinality;
Ray Milkey86ad7bb2018-09-27 12:32:28 -070023import org.apache.karaf.shell.api.action.Argument;
24import org.apache.karaf.shell.api.action.Command;
sunishvkf7c56552016-07-18 16:02:39 +053025import org.onosproject.cli.AbstractShellCommand;
26import org.onosproject.ospf.controller.OspfArea;
27import org.onosproject.ospf.controller.OspfController;
28import org.onosproject.ospf.controller.OspfInterface;
29import org.onosproject.ospf.controller.OspfLsaType;
30import org.onosproject.ospf.controller.OspfNbr;
31import org.onosproject.ospf.controller.OspfProcess;
32import org.onosproject.ospf.protocol.lsa.LsaHeader;
33import org.onosproject.ospf.protocol.lsa.types.RouterLsa;
34
35import java.util.ArrayList;
36import java.util.Iterator;
37import java.util.List;
38
39/**
40 * Representation of OSPF cli commands.
41 */
42@Component(immediate = true)
43@Command(scope = "onos", name = "ospf", description = "list database")
44public class ApplicationOspfCommand extends AbstractShellCommand {
45
46 protected static final String FORMAT6 = "%-20s%-20s%-20s%-20s%-20s%-20s\n";
47 protected static final String FORMAT5 = "%-20s%-20s%-20s%-20s%-20s\n";
48 protected static final String NETWORK = "NETWORK";
49 protected static final String SUMMARY = "SUMMARY";
50 protected static final String ASBR = "ABSR";
51 protected static final String EXTERNAL = "EXTERNAL";
52 protected static final String LINKLOOPAQ = "LINKLOCALOPAQUE";
53 protected static final String AREALOCOPAQ = "AREALOCALOPAQUE";
54 protected static final String ASOPAQ = "ASOPAQUE";
55 protected static final String DR = "DR";
56 protected static final String BACKUP = "BACKUP";
57 protected static final String DROTHER = "DROther";
58 static final String DATABASE = "database";
59 static final String NEIGHBORLIST = "neighbors";
Ray Milkeyd84f89b2018-08-17 14:54:17 -070060 @Reference(cardinality = ReferenceCardinality.MANDATORY)
sunishvkf7c56552016-07-18 16:02:39 +053061 protected OspfController ospfController;
62 @Argument(index = 0, name = "name",
63 description = "database|neighborlist",
64 required = true, multiValued = false)
65 private String name = null;
66 @Argument(index = 1, name = "processid",
67 description = "processId",
68 required = true, multiValued = false)
69 private String process = null;
70 @Argument(index = 2, name = "areaid",
71 description = "areaId",
72 required = false, multiValued = false)
73 private String area = null;
74 private List<String> routerLsa = new ArrayList<>();
75 private List<String> networkLsa = new ArrayList<>();
76 private List<String> summaryLsa = new ArrayList<>();
77 private List<String> externalLsa = new ArrayList<>();
78 private List<String> asbrSumm = new ArrayList<>();
79 private List<String> areaLocalOpaqLsa = new ArrayList<>();
80 private List<String> linkLocalOpqLsa = new ArrayList<>();
81 private List<String> asOpqLsa = new ArrayList<>();
82 private List<String> undefinedLsa = new ArrayList<>();
83 private List<OspfArea> areaList = new ArrayList<>();
84
85
86 @Activate
87 public void activate() {
88 print("OSPF cli activated...!!!");
89 log.debug("OSPF cli activated...!!!");
90 }
91
92 @Deactivate
93 public void deactivate() {
94 log.debug("OSPF cli deactivated...!!!");
95 }
96
97 @Override
Ray Milkey86ad7bb2018-09-27 12:32:28 -070098 protected void doExecute() {
sunishvkf7c56552016-07-18 16:02:39 +053099 if (DATABASE.equals(name)) {
100 buildOspfDatabaseInformation();
101 } else if (NEIGHBORLIST.equals(name)) {
102 buildNeighborInformation();
103 } else {
104 print("Please check the command (database|neighbor)");
105 }
106 }
107
108 /**
109 * Clears all the lists.
110 */
111 private void clearLists() {
112 routerLsa.clear();
113 networkLsa.clear();
114 summaryLsa.clear();
115 externalLsa.clear();
116 asbrSumm.clear();
117 areaLocalOpaqLsa.clear();
118 linkLocalOpqLsa.clear();
119 asOpqLsa.clear();
120 undefinedLsa.clear();
121 areaList.clear();
122 }
123
124 /**
125 * Builds OSPF database information.
126 */
127 private void buildOspfDatabaseInformation() {
128 try {
129 //Builds LSA details
130 buildLsaLists();
131 for (OspfArea area : areaList) {
132 if (routerLsa.size() > 0) {
133 printRouterFormat(area.areaId().toString(), area.routerId().toString(), process);
134 for (String str : routerLsa) {
135 String[] lsaVal = str.split("\\,");
136 if (area.areaId().toString().equalsIgnoreCase(lsaVal[0])) {
137 print(FORMAT6, lsaVal[2], lsaVal[3], lsaVal[4], lsaVal[5], lsaVal[6], lsaVal[7]);
138 }
139 }
140 }
141 if (networkLsa.size() > 0) {
142 printNetworkFormat(area.areaId().toString(), NETWORK);
143 printDetails(networkLsa, area.areaId().toString());
144 }
145 if (summaryLsa.size() > 0) {
146 printNetworkFormat(area.areaId().toString(), SUMMARY);
147 printDetails(summaryLsa, area.areaId().toString());
148 }
149 if (externalLsa.size() > 0) {
150 printNetworkFormat(area.areaId().toString(), EXTERNAL);
151 printDetails(externalLsa, area.areaId().toString());
152 }
153 if (asbrSumm.size() > 0) {
154 printNetworkFormat(area.areaId().toString(), ASBR);
155 printDetails(asbrSumm, area.areaId().toString());
156 }
157 if (areaLocalOpaqLsa.size() > 0) {
158 printNetworkFormat(area.areaId().toString(), AREALOCOPAQ);
159 printDetails(areaLocalOpaqLsa, area.areaId().toString());
160 }
161 if (linkLocalOpqLsa.size() > 0) {
162 printNetworkFormat(area.areaId().toString(), LINKLOOPAQ);
163 printDetails(linkLocalOpqLsa, area.areaId().toString());
164 }
165 if (asOpqLsa.size() > 0) {
166 printNetworkFormat(area.areaId().toString(), ASOPAQ);
167 printDetails(asOpqLsa, area.areaId().toString());
168 }
169 if (undefinedLsa.size() > 0) {
170 printRouterFormat(area.areaId().toString(), area.routerId().toString(), process);
171 printDetails(undefinedLsa, area.areaId().toString());
172 }
173 }
174 clearLists();
175 } catch (Exception ex) {
176 clearLists();
177 print("Error occured while Ospf controller getting called" + ex.getMessage());
178 }
179 }
180
181 /**
182 * Prints LSA details.
183 *
184 * @param lsaDetails LSA details
185 * @param areaId area ID
186 */
187 private void printDetails(List<String> lsaDetails, String areaId) {
188 for (String str : lsaDetails) {
189 String[] lsaVal = str.split("\\,");
190 if (areaId.equalsIgnoreCase(lsaVal[0])) {
191 print(FORMAT5, lsaVal[2], lsaVal[3], lsaVal[4], lsaVal[5], lsaVal[6]);
192 }
193 }
194 }
195
196 /**
197 * Builds all LSA lists with LSA details.
198 */
199 private void buildLsaLists() {
200 this.ospfController = get(OspfController.class);
201 List<OspfProcess> listOfProcess = ospfController.getAllConfiguredProcesses();
202 Iterator<OspfProcess> itrProcess = listOfProcess.iterator();
203 while (itrProcess.hasNext()) {
204 OspfProcess ospfProcess = itrProcess.next();
205 if (process.equalsIgnoreCase(ospfProcess.processId())) {
206 List<OspfArea> listAreas = ospfProcess.areas();
207 Iterator<OspfArea> itrArea = listAreas.iterator();
208 while (itrArea.hasNext()) {
209 OspfArea area = itrArea.next();
210 List<LsaHeader> lsas = area.database()
211 .getAllLsaHeaders(false, area.isOpaqueEnabled());
212 List<LsaHeader> tmpLsaList = new ArrayList<>(lsas);
213 log.debug("OSPFController::size of database::" + (lsas != null ? lsas.size() : null));
214 Iterator<LsaHeader> itrLsaHeader = tmpLsaList.iterator();
215 areaList.add(area);
216 if (itrLsaHeader != null) {
217 while (itrLsaHeader.hasNext()) {
218 LsaHeader header = itrLsaHeader.next();
219 populateLsaLists(header, area);
220 }
221 }
222 }
223 }
224 }
225 }
226
227 /**
228 * Populates the LSA lists based on the input.
229 *
230 * @param header LSA header instance
231 * @param area OSPF area instance
232 */
233 private void populateLsaLists(LsaHeader header, OspfArea area) {
234 String seqNo = Long.toHexString(header.lsSequenceNo());
235 String checkSum = Long.toHexString(header.lsCheckSum());
236 if (seqNo.length() == 16) {
237 seqNo = seqNo.substring(8, seqNo.length());
238 }
239 if (checkSum.length() == 16) {
240 checkSum = checkSum.substring(8, checkSum.length());
241 }
242 StringBuffer strBuf = getBuffList(area.areaId().toString(), area.routerId().toString(),
243 header.linkStateId(),
244 header.advertisingRouter().toString(),
245 header.age(), seqNo, checkSum);
246 if (header.lsType() == OspfLsaType.ROUTER.value()) {
247 strBuf.append(",");
248 strBuf.append(((RouterLsa) header).noLink());
249 routerLsa.add(strBuf.toString());
250 } else if (header.lsType() == OspfLsaType.NETWORK.value()) {
251 strBuf.append(",");
252 strBuf.append("0");
253 networkLsa.add(strBuf.toString());
254 } else if (header.lsType() == OspfLsaType.SUMMARY.value()) {
255 strBuf.append(",");
256 strBuf.append("0");
257 summaryLsa.add(strBuf.toString());
258 } else if (header.lsType() == OspfLsaType.EXTERNAL_LSA.value()) {
259 strBuf.append(",");
260 strBuf.append("0");
261 externalLsa.add(strBuf.toString());
262 } else if (header.lsType() == OspfLsaType.ASBR_SUMMARY.value()) {
263 strBuf.append(",");
264 strBuf.append("0");
265 asbrSumm.add(strBuf.toString());
266 } else if (header.lsType() == OspfLsaType.AREA_LOCAL_OPAQUE_LSA.value()) {
267 strBuf.append(",");
268 strBuf.append("0");
269 areaLocalOpaqLsa.add(strBuf.toString());
270 } else if (header.lsType() == OspfLsaType.LINK_LOCAL_OPAQUE_LSA.value()) {
271 strBuf.append(",");
272 strBuf.append("0");
273 linkLocalOpqLsa.add(strBuf.toString());
274 } else if (header.lsType() == OspfLsaType.AS_OPAQUE_LSA.value()) {
275 strBuf.append(",");
276 strBuf.append("0");
277 asOpqLsa.add(strBuf.toString());
278 } else {
279 strBuf.append(",");
280 strBuf.append("0");
281 undefinedLsa.add(strBuf.toString());
282 }
283 }
284
285 /**
286 * Builds OSPF neighbor information.
287 */
288 private void buildNeighborInformation() {
289 try {
290 this.ospfController = get(OspfController.class);
291 List<OspfProcess> listOfProcess = ospfController.getAllConfiguredProcesses();
292 boolean flag = false;
293 printNeighborsFormat();
294 Iterator<OspfProcess> itrProcess = listOfProcess.iterator();
295 while (itrProcess.hasNext()) {
296 OspfProcess process = itrProcess.next();
297 List<OspfArea> listAreas = process.areas();
298 Iterator<OspfArea> itrArea = listAreas.iterator();
299 while (itrArea.hasNext()) {
300 OspfArea area = itrArea.next();
301 List<OspfInterface> itrefaceList = area.ospfInterfaceList();
302 for (OspfInterface interfc : itrefaceList) {
303 List<OspfNbr> nghbrList = new ArrayList<>(interfc.listOfNeighbors().values());
304 for (OspfNbr neigbor : nghbrList) {
305 print("%-20s%-20s%-20s%-20s%-20s\n", neigbor.neighborId(), neigbor.routerPriority(),
306 neigbor.getState() + "/" + checkDrBdrOther(neigbor.neighborIpAddr().toString(),
307 neigbor.neighborDr().toString(),
308 neigbor.neighborBdr().toString()),
309 neigbor.neighborIpAddr().toString(), interfc.ipAddress());
310 }
311 }
312 }
313 }
314 } catch (Exception ex) {
315 print("Error occured while Ospf controller getting called" + ex.getMessage());
316 }
317 }
318
319 /**
320 * Prints input after formatting.
321 *
322 * @param areaId area ID
323 * @param routerId router ID
324 * @param processId process ID
325 */
326 private void printRouterFormat(String areaId, String routerId, String processId) {
327 print("%s (%s) %s %s\n", "OSPF Router with ID", routerId, "Process Id", processId);
328 print("%s ( Area %s)\n", "Router Link States", areaId);
329 print("%-20s%-20s%-20s%-20s%-20s%-20s\n", "Link Id", "ADV Router", "Age", "Seq#",
330 "CkSum", "Link Count");
331 }
332
333 /**
334 * Prints input after formatting.
335 *
336 * @param areaId area ID
337 * @param type network type
338 */
339 private void printNetworkFormat(String areaId, String type) {
340 print("%s %s ( Area %s)\n", type, "Link States", areaId);
341 print("%-20s%-20s%-20s%-20s%-20s\n", "Link Id", "ADV Router", "Age", "Seq#", "CkSum");
342 }
343
344 /**
345 * Prints input after formatting.
346 */
347 private void printNeighborsFormat() {
348 print("%-20s%-20s%-20s%-20s%-20s\n", "Neighbor Id", "Pri", "State",
349 "Address", "Interface");
350 }
351
352 /**
353 * Checks whether the neighbor is DR or BDR.
354 *
355 * @param ip IP address to check
356 * @param drIP DRs IP address
357 * @param bdrIp BDRs IP address
358 * @return 1- neighbor is DR, 2- neighbor is BDR, 3- DROTHER
359 */
360 public String checkDrBdrOther(String ip, String drIP, String bdrIp) {
361
362 if (ip.equalsIgnoreCase(drIP)) {
363 return DR;
364 } else if (ip.equalsIgnoreCase(bdrIp)) {
365 return BACKUP;
366 } else {
367 return DROTHER;
368 }
369 }
370
371 /**
372 * Returns inputs as formatted string.
373 *
374 * @param areaId area id
375 * @param routerId router id
376 * @param linkStateId link state id
377 * @param advertisingRouter advertising router
378 * @param age age
379 * @param seqNo sequence number
380 * @param checkSum checksum
381 * @return formatted string
382 */
383 private StringBuffer getBuffList(String areaId, String routerId, String linkStateId,
384 String advertisingRouter, int age, String seqNo, String checkSum) {
385 StringBuffer strBuf = new StringBuffer();
386 strBuf.append(areaId);
387 strBuf.append(",");
388 strBuf.append(routerId);
389 strBuf.append(",");
390 strBuf.append(linkStateId);
391 strBuf.append(",");
392 strBuf.append(advertisingRouter);
393 strBuf.append(",");
394 strBuf.append(age);
395 strBuf.append(",");
396 strBuf.append(seqNo);
397 strBuf.append(",");
398 strBuf.append(checkSum);
399 return strBuf;
400 }
401}