blob: b406947b4c01183128e70e9237839e63bcebe729 [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
18import org.apache.karaf.shell.commands.Argument;
19import org.apache.karaf.shell.commands.Command;
20import 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());
42 @Argument(index = 0, name = "md",
43 description = "Maintenance Domain name and type (in brackets) - will use all MDs if not specified",
44 required = false, multiValued = false)
45 String mdStr = null;
46 @Argument(index = 1, name = "ma",
47 description = "Maintenance Association name and type (in brackets) - requires MD",
48 required = false, multiValued = false)
49 String maStr = null;
50 @Argument(index = 2, name = "mep",
51 description = "MEP identifier - requires MD and MA",
52 required = false, multiValued = false)
53 String mepStr = null;
54
55 @Override
56 protected void execute() {
57 CfmMepService mepService = get(CfmMepService.class);
58 CfmMdService mdService = get(CfmMdService.class);
59
60 if (mdStr != null && !mdStr.isEmpty()) {
61
62
63 MdId mdId = parseMdName(mdStr);
64 print(printMdId(mdId));
65
66 if (maStr != null && !maStr.isEmpty()) {
67 MaIdShort maId = parseMaName(maStr);
68 print(printMaId(maId));
69
70 if (mepStr != null && !mepStr.isEmpty()) {
71 MepId mepId = MepId.valueOf(Short.parseShort(mepStr));
72 try {
73 MepEntry mep = mepService.getMep(mdId, maId, mepId);
74 if (mep != null) {
75 print(printMepEntry(mep));
76 }
77 } catch (CfmConfigException e) {
78 log.error("Error retrieving Mep details {}",
79 new MepKeyId(mdId, maId, mepId), e);
80 }
81
82 //MD, MA and MEP given
83 } else {
84 //MD and MA given but no MEP given
85 try {
86 mepService.getAllMeps(mdId, maId).forEach(mep -> {
87 print(printMepEntry(mep));
88 });
89 } catch (CfmConfigException e) {
90 log.error("Error retrieving Meps for {}/{}",
91 mdId.mdName(), maId.maName(), e);
92 }
93 }
94 } else {
95 //MD given but no MA given
96 mdService.getAllMaintenanceAssociation(mdId).forEach(ma -> {
97 print(printMaId(ma.maId()));
98 try {
99 mepService.getAllMeps(mdId, ma.maId()).forEach(mep -> {
100 print(printMepEntry(mep));
101 });
102 } 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 {
116 mepService.getAllMeps(md.mdId(), ma.maId()).forEach(mep -> {
117 print(printMepEntry(mep));
118 });
119 } catch (CfmConfigException e) {
120 log.error("Error retrieving Meps for {}/{}",
121 md.mdId().mdName(), ma.maId().maName(), e);
122 }
123 });
124 });
125 }
126 }
127
128 /**
129 * Print the whole MEP Entry (config and status).
130 * @param mep The MEPEntry to print
131 * @return A string with MepEntry details
132 */
133 public static String printMepEntry(MepEntry mep) {
134 StringBuffer sb = new StringBuffer("MEP: ");
135 sb.append(mep.mepId());
136 sb.append(" Device:" + mep.deviceId());
137 sb.append(", Port: " + mep.port());
138 sb.append(", Vlan: " + mep.primaryVid());
139 sb.append(", AdminSt: " + mep.administrativeState());
140 sb.append(", CciEnabled: " + mep.cciEnabled());
141 sb.append(", Priority: " + mep.ccmLtmPriority());
142 sb.append("\n"); //The following are state
143 sb.append(", Total CCMs: " + mep.totalCcmsTransmitted());
144 sb.append(", MAC: " + mep.macAddress());
145 sb.append(", Fault: " + mep.fngState());
146
147 mep.activeRemoteMepList().forEach(rmep -> {
148 sb.append("\n\tRmep: " + rmep.remoteMepId());
149 sb.append(", Mac: " + rmep.macAddress());
150 sb.append(", State: " + rmep.state());
151 sb.append(", Failed Time: " + rmep.failedOrOkTime());
152
153 });
154
155
156 return sb.toString();
157 }
158
159 public static String printMdId(MdId mdId) {
160 StringBuffer sb = new StringBuffer("MD: ");
161 sb.append(mdId.mdName());
162 sb.append("(" + mdId.nameType() + ")");
163 return sb.toString();
164 }
165
166 public static String printMaId(MaIdShort maId) {
167 StringBuffer sb = new StringBuffer("MA: ");
168 sb.append(maId.maName());
169 sb.append("(" + maId.nameType() + ")");
170 return sb.toString();
171 }
172
173}