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