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 | |
| 18 | import org.apache.karaf.shell.commands.Argument; |
| 19 | import org.apache.karaf.shell.commands.Command; |
| 20 | import org.onosproject.cli.AbstractShellCommand; |
| 21 | import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MaId2Octet; |
| 22 | import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MaIdCharStr; |
| 23 | import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MaIdIccY1731; |
| 24 | import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MaIdPrimaryVid; |
| 25 | import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MaIdRfc2685VpnId; |
| 26 | import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MaIdShort; |
| 27 | import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MdId; |
| 28 | import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MdIdCharStr; |
| 29 | import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MdIdDomainName; |
| 30 | import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MdIdMacUint; |
| 31 | import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MdIdNone; |
| 32 | import org.onosproject.incubator.net.l2monitoring.cfm.service.CfmConfigException; |
| 33 | import org.onosproject.incubator.net.l2monitoring.cfm.service.CfmMdService; |
| 34 | |
| 35 | /** |
| 36 | * Delete a Maintenance Association from the existing list of a Maintenance Domain. |
| 37 | */ |
| 38 | @Command(scope = "onos", name = "cfm-ma-delete", |
| 39 | description = "Delete a CFM Maintenance Association and its children.") |
| 40 | public class CfmMaDeleteCommand extends AbstractShellCommand { |
| 41 | |
| 42 | @Argument(index = 0, name = "name", |
| 43 | description = "Maintenance Domain name and type (in brackets) " + |
| 44 | "and the Maintenance Association name and type (in brackets)", |
| 45 | required = true, multiValued = false) |
| 46 | String name = null; |
| 47 | |
| 48 | @Override |
| 49 | protected void execute() { |
| 50 | CfmMdService service = get(CfmMdService.class); |
| 51 | |
| 52 | String[] nameParts = name.split("[()]"); |
| 53 | if (nameParts.length != 4) { |
| 54 | throw new IllegalArgumentException("Invalid name format. Must be in " + |
| 55 | "the format of <identifier(name-type)identifier(name-type)>"); |
| 56 | } |
| 57 | |
| 58 | MdId mdId = null; |
| 59 | MdId.MdNameType nameTypeEnum = MdId.MdNameType.valueOf(nameParts[1]); |
| 60 | switch (nameTypeEnum) { |
| 61 | case DOMAINNAME: |
| 62 | mdId = MdIdDomainName.asMdId(nameParts[0]); |
| 63 | break; |
| 64 | case MACANDUINT: |
| 65 | mdId = MdIdMacUint.asMdId(nameParts[0]); |
| 66 | break; |
| 67 | case NONE: |
| 68 | mdId = MdIdNone.asMdId(); |
| 69 | break; |
| 70 | case CHARACTERSTRING: |
| 71 | default: |
| 72 | mdId = MdIdCharStr.asMdId(nameParts[0]); |
| 73 | } |
| 74 | |
| 75 | MaIdShort maId = null; |
| 76 | MaIdShort.MaIdType maNameTypeEnum = MaIdShort.MaIdType.valueOf(nameParts[3]); |
| 77 | switch (maNameTypeEnum) { |
| 78 | case TWOOCTET: |
| 79 | maId = MaId2Octet.asMaId(nameParts[2]); |
| 80 | break; |
| 81 | case ICCY1731: |
| 82 | maId = MaIdIccY1731.asMaId(nameParts[2]); |
| 83 | break; |
| 84 | case PRIMARYVID: |
| 85 | maId = MaIdPrimaryVid.asMaId(nameParts[2]); |
| 86 | break; |
| 87 | case RFC2685VPNID: |
| 88 | maId = MaIdRfc2685VpnId.asMaIdHex(nameParts[2]); |
| 89 | break; |
| 90 | case CHARACTERSTRING: |
| 91 | default: |
| 92 | maId = MaIdCharStr.asMaId(nameParts[2]); |
| 93 | } |
| 94 | |
| 95 | try { |
| 96 | boolean deleted = service.deleteMaintenanceAssociation(mdId, maId); |
| 97 | print("Maintenance Association %s-%s is %ssuccessfully deleted.", |
| 98 | mdId, maId, deleted ? "" : "NOT "); |
| 99 | } catch (CfmConfigException e) { |
| 100 | throw new IllegalArgumentException(e); |
| 101 | } |
| 102 | } |
| 103 | } |