blob: b36d7dcae4228ee1700dd3769429fac521ccbc45 [file] [log] [blame]
Sean Condon96b896d2017-12-11 12:44:29 -08001/*
2 * Copyright 2018-present Open Networking Foundation
3 *
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.cfm.cli;
17
Ray Milkey86ad7bb2018-09-27 12:32:28 -070018import org.apache.karaf.shell.api.action.Argument;
19import org.apache.karaf.shell.api.action.Command;
Sean Condon96b896d2017-12-11 12:44:29 -080020import org.onosproject.cli.AbstractShellCommand;
21import org.onosproject.incubator.net.l2monitoring.cfm.MepEntry;
22import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MaIdShort;
23import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MdId;
24import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MepId;
25import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MepKeyId;
26import org.onosproject.incubator.net.l2monitoring.cfm.service.CfmConfigException;
27import org.onosproject.incubator.net.l2monitoring.cfm.service.CfmMdService;
28import org.onosproject.incubator.net.l2monitoring.cfm.service.CfmMepService;
29import org.slf4j.Logger;
30
31import static org.onosproject.cfm.cli.CfmMdListMdCommand.parseMaName;
32import static org.onosproject.cfm.cli.CfmMdListMdCommand.parseMdName;
33import static org.slf4j.LoggerFactory.getLogger;
34
35/**
36 * Lists a particular Maintenance Domain.
37 */
38@Command(scope = "onos", name = "cfm-mep-list",
39 description = "Lists a filtered set of MEPs or all if no parameters specified.")
40public class CfmMepListCommand extends AbstractShellCommand {
41 private final Logger log = getLogger(getClass());
Sean Condon3a1efef2018-02-24 13:16:03 +000042 @Argument(name = "md",
43 description = "Maintenance Domain name and type (in brackets) - will use all MDs if not specified")
44 private String mdStr = null;
Sean Condon96b896d2017-12-11 12:44:29 -080045 @Argument(index = 1, name = "ma",
Sean Condon3a1efef2018-02-24 13:16:03 +000046 description = "Maintenance Association name and type (in brackets) - requires MD")
47 private String maStr = null;
Sean Condon96b896d2017-12-11 12:44:29 -080048 @Argument(index = 2, name = "mep",
Sean Condon3a1efef2018-02-24 13:16:03 +000049 description = "MEP identifier - requires MD and MA")
50 private String mepStr = null;
Sean Condon96b896d2017-12-11 12:44:29 -080051
52 @Override
Ray Milkey86ad7bb2018-09-27 12:32:28 -070053 protected void doExecute() {
Sean Condon96b896d2017-12-11 12:44:29 -080054 CfmMepService mepService = get(CfmMepService.class);
55 CfmMdService mdService = get(CfmMdService.class);
56
57 if (mdStr != null && !mdStr.isEmpty()) {
58
59
60 MdId mdId = parseMdName(mdStr);
61 print(printMdId(mdId));
62
63 if (maStr != null && !maStr.isEmpty()) {
64 MaIdShort maId = parseMaName(maStr);
65 print(printMaId(maId));
66
67 if (mepStr != null && !mepStr.isEmpty()) {
68 MepId mepId = MepId.valueOf(Short.parseShort(mepStr));
69 try {
70 MepEntry mep = mepService.getMep(mdId, maId, mepId);
71 if (mep != null) {
72 print(printMepEntry(mep));
73 }
74 } catch (CfmConfigException e) {
75 log.error("Error retrieving Mep details {}",
76 new MepKeyId(mdId, maId, mepId), e);
77 }
78
79 //MD, MA and MEP given
80 } else {
81 //MD and MA given but no MEP given
82 try {
Sean Condon3a1efef2018-02-24 13:16:03 +000083 mepService.getAllMeps(mdId, maId)
84 .forEach(mep -> print(printMepEntry(mep)));
Sean Condon96b896d2017-12-11 12:44:29 -080085 } catch (CfmConfigException e) {
86 log.error("Error retrieving Meps for {}/{}",
87 mdId.mdName(), maId.maName(), e);
88 }
89 }
90 } else {
91 //MD given but no MA given
92 mdService.getAllMaintenanceAssociation(mdId).forEach(ma -> {
93 print(printMaId(ma.maId()));
94 try {
Sean Condon3a1efef2018-02-24 13:16:03 +000095 mepService.getAllMeps(mdId, ma.maId())
96 .forEach(mep -> print(printMepEntry(mep)));
Sean Condon96b896d2017-12-11 12:44:29 -080097 } catch (CfmConfigException e) {
98 log.error("Error retrieving Meps for {}/{}",
99 mdId.mdName(), ma.maId().maName(), e);
100 }
101 });
102
103 }
104 } else {
105 mdService.getAllMaintenanceDomain().forEach(md -> {
106 print(printMdId(md.mdId()));
107
108 mdService.getAllMaintenanceAssociation(md.mdId()).forEach(ma -> {
109 print(printMaId(ma.maId()));
110 try {
Sean Condon3a1efef2018-02-24 13:16:03 +0000111 mepService.getAllMeps(md.mdId(), ma.maId())
112 .forEach(mep -> print(printMepEntry(mep)));
Sean Condon96b896d2017-12-11 12:44:29 -0800113 } catch (CfmConfigException e) {
114 log.error("Error retrieving Meps for {}/{}",
115 md.mdId().mdName(), ma.maId().maName(), e);
116 }
117 });
118 });
119 }
120 }
121
122 /**
123 * Print the whole MEP Entry (config and status).
124 * @param mep The MEPEntry to print
125 * @return A string with MepEntry details
126 */
Sean Condon3a1efef2018-02-24 13:16:03 +0000127 private static String printMepEntry(MepEntry mep) {
Sean Condon96b896d2017-12-11 12:44:29 -0800128 StringBuffer sb = new StringBuffer("MEP: ");
129 sb.append(mep.mepId());
Sean Condon3a1efef2018-02-24 13:16:03 +0000130 sb.append(" Device:");
131 sb.append(mep.deviceId());
132 sb.append(", Port: ");
133 sb.append(mep.port());
134 sb.append(", Vlan: ");
135 sb.append(mep.primaryVid());
136 sb.append(", AdminSt: ");
137 sb.append(mep.administrativeState());
138 sb.append(", CciEnabled: ");
139 sb.append(mep.cciEnabled());
140 sb.append(", Priority: ");
141 sb.append(mep.ccmLtmPriority());
Sean Condon96b896d2017-12-11 12:44:29 -0800142 sb.append("\n"); //The following are state
Sean Condon3a1efef2018-02-24 13:16:03 +0000143 sb.append(", Total CCMs: ");
144 sb.append(mep.totalCcmsTransmitted());
145 sb.append(", MAC: ");
146 sb.append(mep.macAddress());
147 sb.append(", Fault: ");
148 sb.append(mep.fngState());
Sean Condon96b896d2017-12-11 12:44:29 -0800149
150 mep.activeRemoteMepList().forEach(rmep -> {
Sean Condon3a1efef2018-02-24 13:16:03 +0000151 sb.append("\n\tRmep: ");
152 sb.append(rmep.remoteMepId());
153 sb.append(", Mac: ");
154 sb.append(rmep.macAddress());
155 sb.append(", State: ");
156 sb.append(rmep.state());
157 sb.append(", Failed Time: ");
158 sb.append(rmep.failedOrOkTime());
Sean Condon96b896d2017-12-11 12:44:29 -0800159
160 });
161
162
163 return sb.toString();
164 }
165
Sean Condon3a1efef2018-02-24 13:16:03 +0000166 private static String printMdId(MdId mdId) {
167 return "MD: " + mdId.mdName() + "(" + mdId.nameType() + ")";
Sean Condon96b896d2017-12-11 12:44:29 -0800168 }
169
Sean Condon3a1efef2018-02-24 13:16:03 +0000170 private static String printMaId(MaIdShort maId) {
171 return "MA: " + maId.maName() + "(" + maId.nameType() + ")";
Sean Condon96b896d2017-12-11 12:44:29 -0800172 }
173
174}