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