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