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