Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-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 | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 21 | import org.onosproject.cli.AbstractShellCommand; |
| 22 | import org.onosproject.incubator.net.l2monitoring.cfm.MaintenanceAssociation; |
| 23 | import org.onosproject.incubator.net.l2monitoring.cfm.MaintenanceDomain; |
Sean Condon | 96b896d | 2017-12-11 12:44:29 -0800 | [diff] [blame] | 24 | import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MaIdShort; |
Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 25 | import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MdId; |
Sean Condon | 3a1efef | 2018-02-24 13:16:03 +0000 | [diff] [blame] | 26 | import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MdMaNameUtil; |
Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 27 | import org.onosproject.incubator.net.l2monitoring.cfm.service.CfmMdService; |
| 28 | |
Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 29 | /** |
| 30 | * Lists a particular Maintenance Domain. |
| 31 | */ |
Ray Milkey | 7a2dee5 | 2018-09-28 10:58:28 -0700 | [diff] [blame] | 32 | @Service |
Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 33 | @Command(scope = "onos", name = "cfm-md-list", |
| 34 | description = "Lists a single CFM Maintenance Domain or all if none specified.") |
| 35 | public class CfmMdListMdCommand extends AbstractShellCommand { |
Sean Condon | 3a1efef | 2018-02-24 13:16:03 +0000 | [diff] [blame] | 36 | @Argument(name = "name", |
| 37 | description = "Maintenance Domain name and type (in brackets)") |
| 38 | private String name = null; |
Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 39 | |
| 40 | @Override |
Ray Milkey | 86ad7bb | 2018-09-27 12:32:28 -0700 | [diff] [blame] | 41 | protected void doExecute() { |
Sean Condon | 3a1efef | 2018-02-24 13:16:03 +0000 | [diff] [blame] | 42 | CfmMdService service; |
| 43 | service = get(CfmMdService.class); |
Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 44 | |
| 45 | if (name != null) { |
Sean Condon | 96b896d | 2017-12-11 12:44:29 -0800 | [diff] [blame] | 46 | MdId mdId = parseMdName(name); |
Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 47 | print("Maintenance Domain:"); |
Sean Condon | 3a1efef | 2018-02-24 13:16:03 +0000 | [diff] [blame] | 48 | service.getMaintenanceDomain(mdId).ifPresent(md -> { |
| 49 | print(printMd(md)); |
| 50 | md.maintenanceAssociationList().forEach(CfmMdListMdCommand::printMa); |
Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 51 | }); |
Sean Condon | 3a1efef | 2018-02-24 13:16:03 +0000 | [diff] [blame] | 52 | } else { |
| 53 | service.getAllMaintenanceDomain().forEach(md -> print(printMd(md))); |
Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 54 | } |
| 55 | } |
| 56 | |
Sean Condon | 3a1efef | 2018-02-24 13:16:03 +0000 | [diff] [blame] | 57 | private static String printMd(MaintenanceDomain md) { |
| 58 | if (md == null) { |
| 59 | return "MD not found"; |
Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 60 | } else { |
Sean Condon | 3a1efef | 2018-02-24 13:16:03 +0000 | [diff] [blame] | 61 | StringBuilder sb = new StringBuilder("\tMD: "); |
| 62 | sb.append(md.mdId().mdName()); |
| 63 | sb.append("("); |
| 64 | sb.append(md.mdId().nameType()); |
| 65 | sb.append(") Lvl:"); |
| 66 | sb.append(md.mdLevel().ordinal()); |
| 67 | sb.append(", Num: "); |
| 68 | sb.append(md.mdNumericId()); |
Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 69 | |
Sean Condon | 3a1efef | 2018-02-24 13:16:03 +0000 | [diff] [blame] | 70 | md.maintenanceAssociationList(). |
| 71 | forEach(ma -> sb.append(printMa(ma))); |
Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 72 | return sb.toString(); |
| 73 | } |
| 74 | } |
| 75 | |
Sean Condon | 3a1efef | 2018-02-24 13:16:03 +0000 | [diff] [blame] | 76 | private static String printMa(MaintenanceAssociation ma) { |
| 77 | if (ma == null) { |
Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 78 | return "\n\tNo MA found"; |
| 79 | } |
| 80 | |
| 81 | StringBuilder sb = new StringBuilder("\n\t\tMA: "); |
Sean Condon | 3a1efef | 2018-02-24 13:16:03 +0000 | [diff] [blame] | 82 | sb.append(ma.maId().maName()); |
Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 83 | sb.append("("); |
Sean Condon | 3a1efef | 2018-02-24 13:16:03 +0000 | [diff] [blame] | 84 | sb.append(ma.maId().nameType()); |
Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 85 | sb.append(") CCM: "); |
Sean Condon | 3a1efef | 2018-02-24 13:16:03 +0000 | [diff] [blame] | 86 | sb.append(ma.ccmInterval()); |
Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 87 | sb.append(" Num: "); |
Sean Condon | 3a1efef | 2018-02-24 13:16:03 +0000 | [diff] [blame] | 88 | sb.append(ma.maNumericId()); |
Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 89 | |
Sean Condon | 3a1efef | 2018-02-24 13:16:03 +0000 | [diff] [blame] | 90 | ma.remoteMepIdList().forEach(rmep -> { |
Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 91 | sb.append("\n\t\t\tRmep: "); |
| 92 | sb.append(rmep); |
| 93 | }); |
Sean Condon | 3a1efef | 2018-02-24 13:16:03 +0000 | [diff] [blame] | 94 | ma.componentList().forEach(comp -> { |
Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 95 | sb.append("\n\t\t\tComponent: "); |
| 96 | sb.append(comp.componentId()); |
| 97 | sb.append(" Perm: "); |
| 98 | sb.append(comp.idPermission()); |
| 99 | sb.append(" MHF: "); |
| 100 | sb.append(comp.mhfCreationType()); |
| 101 | sb.append(" Tag: "); |
| 102 | sb.append(comp.tagType()); |
| 103 | |
| 104 | comp.vidList().forEach(vid -> { |
| 105 | sb.append("\n\t\t\t\tVID: "); |
| 106 | sb.append(vid); |
| 107 | }); |
| 108 | }); |
| 109 | |
| 110 | return sb.toString(); |
| 111 | } |
Sean Condon | 96b896d | 2017-12-11 12:44:29 -0800 | [diff] [blame] | 112 | |
Sean Condon | 3a1efef | 2018-02-24 13:16:03 +0000 | [diff] [blame] | 113 | static MdId parseMdName(String mdStr) { |
Sean Condon | 96b896d | 2017-12-11 12:44:29 -0800 | [diff] [blame] | 114 | String[] nameParts = mdStr.split("[()]"); |
| 115 | if (nameParts.length != 2) { |
| 116 | throw new IllegalArgumentException("Invalid name format. " + |
| 117 | "Must be in the format of <identifier(name-type)>"); |
| 118 | } |
| 119 | |
Sean Condon | 3a1efef | 2018-02-24 13:16:03 +0000 | [diff] [blame] | 120 | return MdMaNameUtil.parseMdName(nameParts[1], nameParts[0]); |
Sean Condon | 96b896d | 2017-12-11 12:44:29 -0800 | [diff] [blame] | 121 | } |
| 122 | |
Sean Condon | 3a1efef | 2018-02-24 13:16:03 +0000 | [diff] [blame] | 123 | static MaIdShort parseMaName(String maStr) { |
Sean Condon | 96b896d | 2017-12-11 12:44:29 -0800 | [diff] [blame] | 124 | String[] nameParts = maStr.split("[()]"); |
| 125 | if (nameParts.length != 2) { |
| 126 | throw new IllegalArgumentException("Invalid name format. " + |
| 127 | "Must be in the format of <identifier(name-type)>"); |
| 128 | } |
| 129 | |
Sean Condon | 3a1efef | 2018-02-24 13:16:03 +0000 | [diff] [blame] | 130 | return MdMaNameUtil.parseMaName(nameParts[1], nameParts[0]); |
Sean Condon | 96b896d | 2017-12-11 12:44:29 -0800 | [diff] [blame] | 131 | } |
| 132 | |
Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 133 | } |