blob: d449cce9b8a66be1c254277f11697768d17030e5 [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
18import org.apache.karaf.shell.commands.Argument;
19import org.apache.karaf.shell.commands.Command;
20import 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;
26import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MaId2Octet;
27import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MaIdCharStr;
28import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MaIdIccY1731;
29import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MaIdPrimaryVid;
30import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MaIdRfc2685VpnId;
31import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MaIdShort;
32import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MdId;
33import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MdIdCharStr;
34import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MdIdDomainName;
35import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MdIdMacUint;
36import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MdIdNone;
37import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MepId;
38import org.onosproject.incubator.net.l2monitoring.cfm.service.CfmConfigException;
39import org.onosproject.incubator.net.l2monitoring.cfm.service.CfmMdService;
40
41/**
42 * Adds a Maintenance Association to a Maintenance Domain.
43 */
44@Command(scope = "onos", name = "cfm-ma-add",
45 description = "Add a CFM Maintenance Association to a Maintenance Domain.")
46public class CfmMaAddCommand extends AbstractShellCommand {
47 @Argument(index = 0, name = "name",
48 description = "Maintenance Domain name and type (in brackets)",
49 required = true, multiValued = false)
50 String mdName = null;
51
52 @Argument(index = 1, name = "name-type",
53 description = "Maintenance Assocation name type",
54 required = true, multiValued = false)
55 String nameType = null;
56
57 @Argument(index = 2, name = "name",
58 description = "Maintenance Assocation name. Restrictions apply depending " +
59 "on name-type",
60 required = true, multiValued = false)
61 String name = null;
62
63 @Argument(index = 3, name = "ccm-interval",
64 description = "CCM Interval values from list",
65 required = true, multiValued = false)
66 String ccmInterval = null;
67
68 @Argument(index = 4, name = "numeric-id",
69 description = "An optional numeric id for Maintenance Association [1-65535]",
70 required = false, multiValued = false)
71 short numericId = 0;
72
73 @Argument(index = 5, name = "component-id",
74 description = "An id for a Component in the Component List [1-65535]." +
75 "The CLI allows creation of only 1 component. If more are " +
76 "required use the REST interface",
77 required = false, multiValued = false)
78 short componentId = 0;
79
80 @Argument(index = 6, name = "component-tag-type",
81 description = "Tag Type value for the component",
82 required = false, multiValued = false)
83 String tagType = null;
84
85 @Argument(index = 7, name = "component-mhf-creation",
86 description = "MEP Half function creation type for the component",
87 required = false, multiValued = false)
88 String mhfCreationType = null;
89
90 @Argument(index = 8, name = "component-vid",
91 description = "A VID for the component [1-4095]. This CLI allows " +
92 "only the specification of 1 VID. If more are required use " +
93 "the REST interface",
94 required = false, multiValued = false)
95 short vid = 0;
96
97 @Argument(index = 9, name = "rmep",
98 description = "Remote Mep numeric identifier [1-8192]",
99 required = true, multiValued = true)
100 String[] rmepArray = null;
101
102 @Override
103 protected void execute() {
104 CfmMdService service = get(CfmMdService.class);
105
106 String[] mdNameParts = mdName.split("[()]");
107 if (mdNameParts.length != 2) {
108 throw new IllegalArgumentException("Invalid name format. " +
109 "Must be in the format of <identifier(name-type)>");
110 }
111
112 MdId mdId = null;
113 MdId.MdNameType mdNameTypeEnum = MdId.MdNameType.valueOf(mdNameParts[1]);
114 switch (mdNameTypeEnum) {
115 case DOMAINNAME:
116 mdId = MdIdDomainName.asMdId(mdNameParts[0]);
117 break;
118 case MACANDUINT:
119 mdId = MdIdMacUint.asMdId(mdNameParts[0]);
120 break;
121 case NONE:
122 mdId = MdIdNone.asMdId();
123 break;
124 case CHARACTERSTRING:
125 default:
126 mdId = MdIdCharStr.asMdId(mdNameParts[0]);
127 }
128
129 MaIdShort maId = null;
130 MaIdShort.MaIdType maNameTypeEnum = MaIdShort.MaIdType.valueOf(nameType);
131 switch (maNameTypeEnum) {
132 case TWOOCTET:
133 maId = MaId2Octet.asMaId(name);
134 break;
135 case ICCY1731:
136 maId = MaIdIccY1731.asMaId(name);
137 break;
138 case PRIMARYVID:
139 maId = MaIdPrimaryVid.asMaId(name);
140 break;
141 case RFC2685VPNID:
142 maId = MaIdRfc2685VpnId.asMaIdHex(name);
143 break;
144 case CHARACTERSTRING:
145 default:
146 maId = MaIdCharStr.asMaId(name);
147 }
148
149 MaintenanceAssociation.MaBuilder builder = null;
150 try {
151 builder = DefaultMaintenanceAssociation.builder(maId, mdId.getNameLength());
152 if (ccmInterval != null && !ccmInterval.isEmpty()) {
153 builder = builder.ccmInterval(MaintenanceAssociation.CcmInterval.valueOf(ccmInterval));
154 }
155 for (String rmep:rmepArray) {
156 builder = builder.addToRemoteMepIdList(MepId.valueOf(Short.parseShort(rmep)));
157 }
158 if (numericId > 0) {
159 builder = builder.maNumericId(numericId);
160 }
161 if (componentId > 0) {
162 Component.ComponentBuilder compBuilder =
163 DefaultComponent.builder(componentId);
164 if (tagType != null && !tagType.isEmpty()) {
165 compBuilder = compBuilder.tagType(
166 Component.TagType.valueOf(tagType));
167 }
168 if (mhfCreationType != null && !mhfCreationType.isEmpty()) {
169 compBuilder = compBuilder.mhfCreationType(
170 Component.MhfCreationType.valueOf(mhfCreationType));
171 }
172 if (vid > 0) {
173 compBuilder = compBuilder.addToVidList(VlanId.vlanId(vid));
174 }
175 builder = builder.addToComponentList(compBuilder.build());
176 }
177 boolean created = service.createMaintenanceAssociation(mdId, builder.build());
178 print("Maintenance Association %s is successfully %s on MD %s",
179 maId, created ? "updated" : "created", mdId);
180 } catch (CfmConfigException e) {
181 throw new IllegalArgumentException(e);
182 }
183 }
184}