blob: 67deccf791d2aea999a4034ee71697194fd66acc [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;
Sean Condon0e89bda2017-03-21 14:23:19 +000020import org.onlab.packet.VlanId;
21import org.onosproject.cli.AbstractShellCommand;
22import org.onosproject.incubator.net.l2monitoring.cfm.Component;
23import org.onosproject.incubator.net.l2monitoring.cfm.DefaultComponent;
24import org.onosproject.incubator.net.l2monitoring.cfm.DefaultMaintenanceAssociation;
25import org.onosproject.incubator.net.l2monitoring.cfm.MaintenanceAssociation;
Sean Condon0e89bda2017-03-21 14:23:19 +000026import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MaIdShort;
27import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MdId;
Sean Condon0e89bda2017-03-21 14:23:19 +000028import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MepId;
29import org.onosproject.incubator.net.l2monitoring.cfm.service.CfmConfigException;
30import 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.")
37public class CfmMaAddCommand extends AbstractShellCommand {
Sean Condon3a1efef2018-02-24 13:16:03 +000038 @Argument(name = "name",
Sean Condon0e89bda2017-03-21 14:23:19 +000039 description = "Maintenance Domain name and type (in brackets)",
Sean Condon3a1efef2018-02-24 13:16:03 +000040 required = true)
41 private String mdName = null;
Sean Condon0e89bda2017-03-21 14:23:19 +000042
43 @Argument(index = 1, name = "name-type",
44 description = "Maintenance Assocation name type",
Sean Condon3a1efef2018-02-24 13:16:03 +000045 required = true)
46 private String nameType = null;
Sean Condon0e89bda2017-03-21 14:23:19 +000047
48 @Argument(index = 2, name = "name",
49 description = "Maintenance Assocation name. Restrictions apply depending " +
50 "on name-type",
Sean Condon3a1efef2018-02-24 13:16:03 +000051 required = true)
52 private String name = null;
Sean Condon0e89bda2017-03-21 14:23:19 +000053
54 @Argument(index = 3, name = "ccm-interval",
55 description = "CCM Interval values from list",
Sean Condon3a1efef2018-02-24 13:16:03 +000056 required = true)
57 private String ccmInterval = null;
Sean Condon0e89bda2017-03-21 14:23:19 +000058
59 @Argument(index = 4, name = "numeric-id",
Sean Condon3a1efef2018-02-24 13:16:03 +000060 description = "An optional numeric id for Maintenance Association [1-65535]")
61 private Short numericId = null;
Sean Condon0e89bda2017-03-21 14:23:19 +000062
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 Condon3a1efef2018-02-24 13:16:03 +000066 "required use the REST interface")
67 private Short componentId = null;
Sean Condon0e89bda2017-03-21 14:23:19 +000068
69 @Argument(index = 6, name = "component-tag-type",
Sean Condon3a1efef2018-02-24 13:16:03 +000070 description = "Tag Type value for the component")
71 private String tagType = null;
Sean Condon0e89bda2017-03-21 14:23:19 +000072
73 @Argument(index = 7, name = "component-mhf-creation",
Sean Condon3a1efef2018-02-24 13:16:03 +000074 description = "MEP Half function creation type for the component")
75 private String mhfCreationType = null;
Sean Condon0e89bda2017-03-21 14:23:19 +000076
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 Condon3a1efef2018-02-24 13:16:03 +000080 "the REST interface")
81 private Short vid = null;
Sean Condon0e89bda2017-03-21 14:23:19 +000082
83 @Argument(index = 9, name = "rmep",
84 description = "Remote Mep numeric identifier [1-8192]",
85 required = true, multiValued = true)
Sean Condon3a1efef2018-02-24 13:16:03 +000086 private String[] rmepArray = null;
Sean Condon0e89bda2017-03-21 14:23:19 +000087
88 @Override
Ray Milkey86ad7bb2018-09-27 12:32:28 -070089 protected void doExecute() {
Sean Condon0e89bda2017-03-21 14:23:19 +000090 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 Condon3a1efef2018-02-24 13:16:03 +000098 MdId mdId = CfmMdListMdCommand.parseMdName(mdNameParts[0] + "(" + mdNameParts[1] + ")");
Sean Condon0e89bda2017-03-21 14:23:19 +000099
Sean Condon3a1efef2018-02-24 13:16:03 +0000100 MaIdShort maId = CfmMdListMdCommand.parseMaName(name + "(" + nameType + ")");
Sean Condon0e89bda2017-03-21 14:23:19 +0000101
Sean Condon0e89bda2017-03-21 14:23:19 +0000102 try {
Sean Condon3a1efef2018-02-24 13:16:03 +0000103 MaintenanceAssociation.MaBuilder builder = DefaultMaintenanceAssociation
104 .builder(maId, mdId.getNameLength());
Sean Condon0e89bda2017-03-21 14:23:19 +0000105 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 Condon3a1efef2018-02-24 13:16:03 +0000111 if (numericId != null) {
Sean Condon0e89bda2017-03-21 14:23:19 +0000112 builder = builder.maNumericId(numericId);
113 }
Sean Condon3a1efef2018-02-24 13:16:03 +0000114 if (componentId != null) {
Sean Condon0e89bda2017-03-21 14:23:19 +0000115 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 Condon3a1efef2018-02-24 13:16:03 +0000125 if (vid != null) {
Sean Condon0e89bda2017-03-21 14:23:19 +0000126 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}