blob: 911510e9bf221686ffe1fe5ba17fc3456ea182ec [file] [log] [blame]
Sean Condon0e89bda2017-03-21 14:23:19 +00001/*
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 */
16package org.onosproject.cfm.cli;
17
Ray Milkey86ad7bb2018-09-27 12:32:28 -070018import org.apache.karaf.shell.api.action.Argument;
19import org.apache.karaf.shell.api.action.Command;
Ray Milkey7a2dee52018-09-28 10:58:28 -070020import org.apache.karaf.shell.api.action.lifecycle.Service;
Sean Condon0e89bda2017-03-21 14:23:19 +000021import org.onlab.packet.VlanId;
22import org.onosproject.cli.AbstractShellCommand;
23import org.onosproject.incubator.net.l2monitoring.cfm.Component;
24import org.onosproject.incubator.net.l2monitoring.cfm.DefaultComponent;
25import org.onosproject.incubator.net.l2monitoring.cfm.DefaultMaintenanceAssociation;
26import org.onosproject.incubator.net.l2monitoring.cfm.MaintenanceAssociation;
Sean Condon0e89bda2017-03-21 14:23:19 +000027import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MaIdShort;
28import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MdId;
Sean Condon0e89bda2017-03-21 14:23:19 +000029import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MepId;
30import org.onosproject.incubator.net.l2monitoring.cfm.service.CfmConfigException;
31import org.onosproject.incubator.net.l2monitoring.cfm.service.CfmMdService;
32
33/**
34 * Adds a Maintenance Association to a Maintenance Domain.
35 */
Ray Milkey7a2dee52018-09-28 10:58:28 -070036@Service
Sean Condon0e89bda2017-03-21 14:23:19 +000037@Command(scope = "onos", name = "cfm-ma-add",
38 description = "Add a CFM Maintenance Association to a Maintenance Domain.")
39public class CfmMaAddCommand extends AbstractShellCommand {
Sean Condon3a1efef2018-02-24 13:16:03 +000040 @Argument(name = "name",
Sean Condon0e89bda2017-03-21 14:23:19 +000041 description = "Maintenance Domain name and type (in brackets)",
Sean Condon3a1efef2018-02-24 13:16:03 +000042 required = true)
43 private String mdName = null;
Sean Condon0e89bda2017-03-21 14:23:19 +000044
45 @Argument(index = 1, name = "name-type",
46 description = "Maintenance Assocation name type",
Sean Condon3a1efef2018-02-24 13:16:03 +000047 required = true)
48 private String nameType = null;
Sean Condon0e89bda2017-03-21 14:23:19 +000049
50 @Argument(index = 2, name = "name",
51 description = "Maintenance Assocation name. Restrictions apply depending " +
52 "on name-type",
Sean Condon3a1efef2018-02-24 13:16:03 +000053 required = true)
54 private String name = null;
Sean Condon0e89bda2017-03-21 14:23:19 +000055
56 @Argument(index = 3, name = "ccm-interval",
57 description = "CCM Interval values from list",
Sean Condon3a1efef2018-02-24 13:16:03 +000058 required = true)
59 private String ccmInterval = null;
Sean Condon0e89bda2017-03-21 14:23:19 +000060
61 @Argument(index = 4, name = "numeric-id",
Sean Condon3a1efef2018-02-24 13:16:03 +000062 description = "An optional numeric id for Maintenance Association [1-65535]")
63 private Short numericId = null;
Sean Condon0e89bda2017-03-21 14:23:19 +000064
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 Condon3a1efef2018-02-24 13:16:03 +000068 "required use the REST interface")
69 private Short componentId = null;
Sean Condon0e89bda2017-03-21 14:23:19 +000070
71 @Argument(index = 6, name = "component-tag-type",
Sean Condon3a1efef2018-02-24 13:16:03 +000072 description = "Tag Type value for the component")
73 private String tagType = null;
Sean Condon0e89bda2017-03-21 14:23:19 +000074
75 @Argument(index = 7, name = "component-mhf-creation",
Sean Condon3a1efef2018-02-24 13:16:03 +000076 description = "MEP Half function creation type for the component")
77 private String mhfCreationType = null;
Sean Condon0e89bda2017-03-21 14:23:19 +000078
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 Condon3a1efef2018-02-24 13:16:03 +000082 "the REST interface")
83 private Short vid = null;
Sean Condon0e89bda2017-03-21 14:23:19 +000084
85 @Argument(index = 9, name = "rmep",
86 description = "Remote Mep numeric identifier [1-8192]",
87 required = true, multiValued = true)
Sean Condon3a1efef2018-02-24 13:16:03 +000088 private String[] rmepArray = null;
Sean Condon0e89bda2017-03-21 14:23:19 +000089
90 @Override
Ray Milkey86ad7bb2018-09-27 12:32:28 -070091 protected void doExecute() {
Sean Condon0e89bda2017-03-21 14:23:19 +000092 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 Condon3a1efef2018-02-24 13:16:03 +0000100 MdId mdId = CfmMdListMdCommand.parseMdName(mdNameParts[0] + "(" + mdNameParts[1] + ")");
Sean Condon0e89bda2017-03-21 14:23:19 +0000101
Sean Condon3a1efef2018-02-24 13:16:03 +0000102 MaIdShort maId = CfmMdListMdCommand.parseMaName(name + "(" + nameType + ")");
Sean Condon0e89bda2017-03-21 14:23:19 +0000103
Sean Condon0e89bda2017-03-21 14:23:19 +0000104 try {
Sean Condon3a1efef2018-02-24 13:16:03 +0000105 MaintenanceAssociation.MaBuilder builder = DefaultMaintenanceAssociation
106 .builder(maId, mdId.getNameLength());
Sean Condon0e89bda2017-03-21 14:23:19 +0000107 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 Condon3a1efef2018-02-24 13:16:03 +0000113 if (numericId != null) {
Sean Condon0e89bda2017-03-21 14:23:19 +0000114 builder = builder.maNumericId(numericId);
115 }
Sean Condon3a1efef2018-02-24 13:16:03 +0000116 if (componentId != null) {
Sean Condon0e89bda2017-03-21 14:23:19 +0000117 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 Condon3a1efef2018-02-24 13:16:03 +0000127 if (vid != null) {
Sean Condon0e89bda2017-03-21 14:23:19 +0000128 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}