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; |
Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 20 | import org.onlab.packet.VlanId; |
| 21 | import org.onosproject.cli.AbstractShellCommand; |
| 22 | import org.onosproject.incubator.net.l2monitoring.cfm.Component; |
| 23 | import org.onosproject.incubator.net.l2monitoring.cfm.DefaultComponent; |
| 24 | import org.onosproject.incubator.net.l2monitoring.cfm.DefaultMaintenanceAssociation; |
| 25 | import org.onosproject.incubator.net.l2monitoring.cfm.MaintenanceAssociation; |
Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 26 | import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MaIdShort; |
| 27 | import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MdId; |
Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 28 | import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MepId; |
| 29 | import org.onosproject.incubator.net.l2monitoring.cfm.service.CfmConfigException; |
| 30 | import org.onosproject.incubator.net.l2monitoring.cfm.service.CfmMdService; |
| 31 | |
| 32 | /** |
| 33 | * Adds a Maintenance Association to a Maintenance Domain. |
| 34 | */ |
| 35 | @Command(scope = "onos", name = "cfm-ma-add", |
| 36 | description = "Add a CFM Maintenance Association to a Maintenance Domain.") |
| 37 | public class CfmMaAddCommand extends AbstractShellCommand { |
Sean Condon | 3a1efef | 2018-02-24 13:16:03 +0000 | [diff] [blame] | 38 | @Argument(name = "name", |
Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 39 | description = "Maintenance Domain name and type (in brackets)", |
Sean Condon | 3a1efef | 2018-02-24 13:16:03 +0000 | [diff] [blame] | 40 | required = true) |
| 41 | private String mdName = null; |
Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 42 | |
| 43 | @Argument(index = 1, name = "name-type", |
| 44 | description = "Maintenance Assocation name type", |
Sean Condon | 3a1efef | 2018-02-24 13:16:03 +0000 | [diff] [blame] | 45 | required = true) |
| 46 | private String nameType = null; |
Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 47 | |
| 48 | @Argument(index = 2, name = "name", |
| 49 | description = "Maintenance Assocation name. Restrictions apply depending " + |
| 50 | "on name-type", |
Sean Condon | 3a1efef | 2018-02-24 13:16:03 +0000 | [diff] [blame] | 51 | required = true) |
| 52 | private String name = null; |
Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 53 | |
| 54 | @Argument(index = 3, name = "ccm-interval", |
| 55 | description = "CCM Interval values from list", |
Sean Condon | 3a1efef | 2018-02-24 13:16:03 +0000 | [diff] [blame] | 56 | required = true) |
| 57 | private String ccmInterval = null; |
Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 58 | |
| 59 | @Argument(index = 4, name = "numeric-id", |
Sean Condon | 3a1efef | 2018-02-24 13:16:03 +0000 | [diff] [blame] | 60 | description = "An optional numeric id for Maintenance Association [1-65535]") |
| 61 | private Short numericId = null; |
Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 62 | |
| 63 | @Argument(index = 5, name = "component-id", |
| 64 | description = "An id for a Component in the Component List [1-65535]." + |
| 65 | "The CLI allows creation of only 1 component. If more are " + |
Sean Condon | 3a1efef | 2018-02-24 13:16:03 +0000 | [diff] [blame] | 66 | "required use the REST interface") |
| 67 | private Short componentId = null; |
Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 68 | |
| 69 | @Argument(index = 6, name = "component-tag-type", |
Sean Condon | 3a1efef | 2018-02-24 13:16:03 +0000 | [diff] [blame] | 70 | description = "Tag Type value for the component") |
| 71 | private String tagType = null; |
Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 72 | |
| 73 | @Argument(index = 7, name = "component-mhf-creation", |
Sean Condon | 3a1efef | 2018-02-24 13:16:03 +0000 | [diff] [blame] | 74 | description = "MEP Half function creation type for the component") |
| 75 | private String mhfCreationType = null; |
Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 76 | |
| 77 | @Argument(index = 8, name = "component-vid", |
| 78 | description = "A VID for the component [1-4095]. This CLI allows " + |
| 79 | "only the specification of 1 VID. If more are required use " + |
Sean Condon | 3a1efef | 2018-02-24 13:16:03 +0000 | [diff] [blame] | 80 | "the REST interface") |
| 81 | private Short vid = null; |
Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 82 | |
| 83 | @Argument(index = 9, name = "rmep", |
| 84 | description = "Remote Mep numeric identifier [1-8192]", |
| 85 | required = true, multiValued = true) |
Sean Condon | 3a1efef | 2018-02-24 13:16:03 +0000 | [diff] [blame] | 86 | private String[] rmepArray = null; |
Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 87 | |
| 88 | @Override |
Ray Milkey | 86ad7bb | 2018-09-27 12:32:28 -0700 | [diff] [blame^] | 89 | protected void doExecute() { |
Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 90 | CfmMdService service = get(CfmMdService.class); |
| 91 | |
| 92 | String[] mdNameParts = mdName.split("[()]"); |
| 93 | if (mdNameParts.length != 2) { |
| 94 | throw new IllegalArgumentException("Invalid name format. " + |
| 95 | "Must be in the format of <identifier(name-type)>"); |
| 96 | } |
| 97 | |
Sean Condon | 3a1efef | 2018-02-24 13:16:03 +0000 | [diff] [blame] | 98 | MdId mdId = CfmMdListMdCommand.parseMdName(mdNameParts[0] + "(" + mdNameParts[1] + ")"); |
Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 99 | |
Sean Condon | 3a1efef | 2018-02-24 13:16:03 +0000 | [diff] [blame] | 100 | MaIdShort maId = CfmMdListMdCommand.parseMaName(name + "(" + nameType + ")"); |
Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 101 | |
Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 102 | try { |
Sean Condon | 3a1efef | 2018-02-24 13:16:03 +0000 | [diff] [blame] | 103 | MaintenanceAssociation.MaBuilder builder = DefaultMaintenanceAssociation |
| 104 | .builder(maId, mdId.getNameLength()); |
Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 105 | if (ccmInterval != null && !ccmInterval.isEmpty()) { |
| 106 | builder = builder.ccmInterval(MaintenanceAssociation.CcmInterval.valueOf(ccmInterval)); |
| 107 | } |
| 108 | for (String rmep:rmepArray) { |
| 109 | builder = builder.addToRemoteMepIdList(MepId.valueOf(Short.parseShort(rmep))); |
| 110 | } |
Sean Condon | 3a1efef | 2018-02-24 13:16:03 +0000 | [diff] [blame] | 111 | if (numericId != null) { |
Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 112 | builder = builder.maNumericId(numericId); |
| 113 | } |
Sean Condon | 3a1efef | 2018-02-24 13:16:03 +0000 | [diff] [blame] | 114 | if (componentId != null) { |
Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 115 | Component.ComponentBuilder compBuilder = |
| 116 | DefaultComponent.builder(componentId); |
| 117 | if (tagType != null && !tagType.isEmpty()) { |
| 118 | compBuilder = compBuilder.tagType( |
| 119 | Component.TagType.valueOf(tagType)); |
| 120 | } |
| 121 | if (mhfCreationType != null && !mhfCreationType.isEmpty()) { |
| 122 | compBuilder = compBuilder.mhfCreationType( |
| 123 | Component.MhfCreationType.valueOf(mhfCreationType)); |
| 124 | } |
Sean Condon | 3a1efef | 2018-02-24 13:16:03 +0000 | [diff] [blame] | 125 | if (vid != null) { |
Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 126 | compBuilder = compBuilder.addToVidList(VlanId.vlanId(vid)); |
| 127 | } |
| 128 | builder = builder.addToComponentList(compBuilder.build()); |
| 129 | } |
| 130 | boolean created = service.createMaintenanceAssociation(mdId, builder.build()); |
| 131 | print("Maintenance Association %s is successfully %s on MD %s", |
| 132 | maId, created ? "updated" : "created", mdId); |
| 133 | } catch (CfmConfigException e) { |
| 134 | throw new IllegalArgumentException(e); |
| 135 | } |
| 136 | } |
| 137 | } |