blob: 23eed3e27a23a5ab9f85fff222a52d81fba98285 [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;
Ray Milkeyc2b7b962018-10-02 15:00:02 -070020import org.apache.karaf.shell.api.action.Completion;
Ray Milkey7a2dee52018-09-28 10:58:28 -070021import org.apache.karaf.shell.api.action.lifecycle.Service;
Ray Milkeyc2b7b962018-10-02 15:00:02 -070022import org.onosproject.cfm.cli.completer.CfmMepIdCompleter;
Sean Condon96b896d2017-12-11 12:44:29 -080023import org.onosproject.cli.AbstractShellCommand;
24import org.onosproject.incubator.net.l2monitoring.cfm.MepEntry;
25import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MaIdShort;
26import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MdId;
27import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MepId;
28import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MepKeyId;
29import org.onosproject.incubator.net.l2monitoring.cfm.service.CfmConfigException;
30import org.onosproject.incubator.net.l2monitoring.cfm.service.CfmMdService;
31import org.onosproject.incubator.net.l2monitoring.cfm.service.CfmMepService;
32import org.slf4j.Logger;
33
34import static org.onosproject.cfm.cli.CfmMdListMdCommand.parseMaName;
35import static org.onosproject.cfm.cli.CfmMdListMdCommand.parseMdName;
36import static org.slf4j.LoggerFactory.getLogger;
37
38/**
39 * Lists a particular Maintenance Domain.
40 */
Ray Milkey7a2dee52018-09-28 10:58:28 -070041@Service
Sean Condon96b896d2017-12-11 12:44:29 -080042@Command(scope = "onos", name = "cfm-mep-list",
43 description = "Lists a filtered set of MEPs or all if no parameters specified.")
44public class CfmMepListCommand extends AbstractShellCommand {
45 private final Logger log = getLogger(getClass());
Sean Condon3a1efef2018-02-24 13:16:03 +000046 @Argument(name = "md",
47 description = "Maintenance Domain name and type (in brackets) - will use all MDs if not specified")
Ray Milkeyc2b7b962018-10-02 15:00:02 -070048 @Completion(CfmMepIdCompleter.class)
Sean Condon3a1efef2018-02-24 13:16:03 +000049 private String mdStr = null;
Sean Condon96b896d2017-12-11 12:44:29 -080050 @Argument(index = 1, name = "ma",
Sean Condon3a1efef2018-02-24 13:16:03 +000051 description = "Maintenance Association name and type (in brackets) - requires MD")
52 private String maStr = null;
Sean Condon96b896d2017-12-11 12:44:29 -080053 @Argument(index = 2, name = "mep",
Sean Condon3a1efef2018-02-24 13:16:03 +000054 description = "MEP identifier - requires MD and MA")
55 private String mepStr = null;
Sean Condon96b896d2017-12-11 12:44:29 -080056
57 @Override
Ray Milkey86ad7bb2018-09-27 12:32:28 -070058 protected void doExecute() {
Sean Condon96b896d2017-12-11 12:44:29 -080059 CfmMepService mepService = get(CfmMepService.class);
60 CfmMdService mdService = get(CfmMdService.class);
61
62 if (mdStr != null && !mdStr.isEmpty()) {
63
64
65 MdId mdId = parseMdName(mdStr);
66 print(printMdId(mdId));
67
68 if (maStr != null && !maStr.isEmpty()) {
69 MaIdShort maId = parseMaName(maStr);
70 print(printMaId(maId));
71
72 if (mepStr != null && !mepStr.isEmpty()) {
73 MepId mepId = MepId.valueOf(Short.parseShort(mepStr));
74 try {
75 MepEntry mep = mepService.getMep(mdId, maId, mepId);
76 if (mep != null) {
77 print(printMepEntry(mep));
78 }
79 } catch (CfmConfigException e) {
80 log.error("Error retrieving Mep details {}",
81 new MepKeyId(mdId, maId, mepId), e);
82 }
83
84 //MD, MA and MEP given
85 } else {
86 //MD and MA given but no MEP given
87 try {
Sean Condon3a1efef2018-02-24 13:16:03 +000088 mepService.getAllMeps(mdId, maId)
89 .forEach(mep -> print(printMepEntry(mep)));
Sean Condon96b896d2017-12-11 12:44:29 -080090 } catch (CfmConfigException e) {
91 log.error("Error retrieving Meps for {}/{}",
92 mdId.mdName(), maId.maName(), e);
93 }
94 }
95 } else {
96 //MD given but no MA given
97 mdService.getAllMaintenanceAssociation(mdId).forEach(ma -> {
98 print(printMaId(ma.maId()));
99 try {
Sean Condon3a1efef2018-02-24 13:16:03 +0000100 mepService.getAllMeps(mdId, ma.maId())
101 .forEach(mep -> print(printMepEntry(mep)));
Sean Condon96b896d2017-12-11 12:44:29 -0800102 } catch (CfmConfigException e) {
103 log.error("Error retrieving Meps for {}/{}",
104 mdId.mdName(), ma.maId().maName(), e);
105 }
106 });
107
108 }
109 } else {
110 mdService.getAllMaintenanceDomain().forEach(md -> {
111 print(printMdId(md.mdId()));
112
113 mdService.getAllMaintenanceAssociation(md.mdId()).forEach(ma -> {
114 print(printMaId(ma.maId()));
115 try {
Sean Condon3a1efef2018-02-24 13:16:03 +0000116 mepService.getAllMeps(md.mdId(), ma.maId())
117 .forEach(mep -> print(printMepEntry(mep)));
Sean Condon96b896d2017-12-11 12:44:29 -0800118 } catch (CfmConfigException e) {
119 log.error("Error retrieving Meps for {}/{}",
120 md.mdId().mdName(), ma.maId().maName(), e);
121 }
122 });
123 });
124 }
125 }
126
127 /**
128 * Print the whole MEP Entry (config and status).
129 * @param mep The MEPEntry to print
130 * @return A string with MepEntry details
131 */
Sean Condon3a1efef2018-02-24 13:16:03 +0000132 private static String printMepEntry(MepEntry mep) {
Sean Condon96b896d2017-12-11 12:44:29 -0800133 StringBuffer sb = new StringBuffer("MEP: ");
134 sb.append(mep.mepId());
Sean Condon3a1efef2018-02-24 13:16:03 +0000135 sb.append(" Device:");
136 sb.append(mep.deviceId());
137 sb.append(", Port: ");
138 sb.append(mep.port());
139 sb.append(", Vlan: ");
140 sb.append(mep.primaryVid());
141 sb.append(", AdminSt: ");
142 sb.append(mep.administrativeState());
143 sb.append(", CciEnabled: ");
144 sb.append(mep.cciEnabled());
145 sb.append(", Priority: ");
146 sb.append(mep.ccmLtmPriority());
Sean Condon96b896d2017-12-11 12:44:29 -0800147 sb.append("\n"); //The following are state
Sean Condon3a1efef2018-02-24 13:16:03 +0000148 sb.append(", Total CCMs: ");
149 sb.append(mep.totalCcmsTransmitted());
150 sb.append(", MAC: ");
151 sb.append(mep.macAddress());
152 sb.append(", Fault: ");
153 sb.append(mep.fngState());
Sean Condon96b896d2017-12-11 12:44:29 -0800154
155 mep.activeRemoteMepList().forEach(rmep -> {
Sean Condon3a1efef2018-02-24 13:16:03 +0000156 sb.append("\n\tRmep: ");
157 sb.append(rmep.remoteMepId());
158 sb.append(", Mac: ");
159 sb.append(rmep.macAddress());
160 sb.append(", State: ");
161 sb.append(rmep.state());
162 sb.append(", Failed Time: ");
163 sb.append(rmep.failedOrOkTime());
Sean Condon96b896d2017-12-11 12:44:29 -0800164
165 });
166
167
168 return sb.toString();
169 }
170
Sean Condon3a1efef2018-02-24 13:16:03 +0000171 private static String printMdId(MdId mdId) {
172 return "MD: " + mdId.mdName() + "(" + mdId.nameType() + ")";
Sean Condon96b896d2017-12-11 12:44:29 -0800173 }
174
Sean Condon3a1efef2018-02-24 13:16:03 +0000175 private static String printMaId(MaIdShort maId) {
176 return "MA: " + maId.maName() + "(" + maId.nameType() + ")";
Sean Condon96b896d2017-12-11 12:44:29 -0800177 }
178
179}