blob: 3f16c7747be8ed6b7221a11a2898754d6c6ed795 [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 Milkeyc2b7b962018-10-02 15:00:02 -070020import org.apache.karaf.shell.api.action.Completion;
Ray Milkey7a2dee52018-09-28 10:58:28 -070021import org.apache.karaf.shell.api.action.lifecycle.Service;
Sean Condon0e89bda2017-03-21 14:23:19 +000022import org.onlab.packet.VlanId;
Ray Milkeyc2b7b962018-10-02 15:00:02 -070023import org.onosproject.cfm.cli.completer.CfmMaCcmIntervalCompleter;
24import org.onosproject.cfm.cli.completer.CfmMaNameTypeCompleter;
25import org.onosproject.cfm.cli.completer.CfmMdNameCompleter;
Sean Condon0e89bda2017-03-21 14:23:19 +000026import org.onosproject.cli.AbstractShellCommand;
Ray Milkeyc2b7b962018-10-02 15:00:02 -070027import org.onosproject.cli.PlaceholderCompleter;
Sean Condon0e89bda2017-03-21 14:23:19 +000028import org.onosproject.incubator.net.l2monitoring.cfm.Component;
29import org.onosproject.incubator.net.l2monitoring.cfm.DefaultComponent;
30import org.onosproject.incubator.net.l2monitoring.cfm.DefaultMaintenanceAssociation;
31import org.onosproject.incubator.net.l2monitoring.cfm.MaintenanceAssociation;
Sean Condon0e89bda2017-03-21 14:23:19 +000032import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MaIdShort;
33import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MdId;
Sean Condon0e89bda2017-03-21 14:23:19 +000034import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MepId;
35import org.onosproject.incubator.net.l2monitoring.cfm.service.CfmConfigException;
36import org.onosproject.incubator.net.l2monitoring.cfm.service.CfmMdService;
37
38/**
39 * Adds a Maintenance Association to a Maintenance Domain.
40 */
Ray Milkey7a2dee52018-09-28 10:58:28 -070041@Service
Sean Condon0e89bda2017-03-21 14:23:19 +000042@Command(scope = "onos", name = "cfm-ma-add",
43 description = "Add a CFM Maintenance Association to a Maintenance Domain.")
44public class CfmMaAddCommand extends AbstractShellCommand {
Sean Condon3a1efef2018-02-24 13:16:03 +000045 @Argument(name = "name",
Sean Condon0e89bda2017-03-21 14:23:19 +000046 description = "Maintenance Domain name and type (in brackets)",
Sean Condon3a1efef2018-02-24 13:16:03 +000047 required = true)
Ray Milkeyc2b7b962018-10-02 15:00:02 -070048 @Completion(CfmMdNameCompleter.class)
Sean Condon3a1efef2018-02-24 13:16:03 +000049 private String mdName = null;
Sean Condon0e89bda2017-03-21 14:23:19 +000050
51 @Argument(index = 1, name = "name-type",
Ray Milkeyc2b7b962018-10-02 15:00:02 -070052 description = "Maintenance Association name type",
Sean Condon3a1efef2018-02-24 13:16:03 +000053 required = true)
Ray Milkeyc2b7b962018-10-02 15:00:02 -070054 @Completion(CfmMaNameTypeCompleter.class)
Sean Condon3a1efef2018-02-24 13:16:03 +000055 private String nameType = null;
Sean Condon0e89bda2017-03-21 14:23:19 +000056
57 @Argument(index = 2, name = "name",
58 description = "Maintenance Assocation name. Restrictions apply depending " +
59 "on name-type",
Sean Condon3a1efef2018-02-24 13:16:03 +000060 required = true)
Ray Milkeyc2b7b962018-10-02 15:00:02 -070061 @Completion(PlaceholderCompleter.class)
Sean Condon3a1efef2018-02-24 13:16:03 +000062 private String name = null;
Sean Condon0e89bda2017-03-21 14:23:19 +000063
64 @Argument(index = 3, name = "ccm-interval",
65 description = "CCM Interval values from list",
Sean Condon3a1efef2018-02-24 13:16:03 +000066 required = true)
Ray Milkeyc2b7b962018-10-02 15:00:02 -070067 @Completion(CfmMaCcmIntervalCompleter.class)
Sean Condon3a1efef2018-02-24 13:16:03 +000068 private String ccmInterval = null;
Sean Condon0e89bda2017-03-21 14:23:19 +000069
70 @Argument(index = 4, name = "numeric-id",
Sean Condon3a1efef2018-02-24 13:16:03 +000071 description = "An optional numeric id for Maintenance Association [1-65535]")
72 private Short numericId = null;
Sean Condon0e89bda2017-03-21 14:23:19 +000073
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 Condon3a1efef2018-02-24 13:16:03 +000077 "required use the REST interface")
78 private Short componentId = null;
Sean Condon0e89bda2017-03-21 14:23:19 +000079
80 @Argument(index = 6, name = "component-tag-type",
Sean Condon3a1efef2018-02-24 13:16:03 +000081 description = "Tag Type value for the component")
82 private String tagType = null;
Sean Condon0e89bda2017-03-21 14:23:19 +000083
84 @Argument(index = 7, name = "component-mhf-creation",
Sean Condon3a1efef2018-02-24 13:16:03 +000085 description = "MEP Half function creation type for the component")
86 private String mhfCreationType = null;
Sean Condon0e89bda2017-03-21 14:23:19 +000087
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 Condon3a1efef2018-02-24 13:16:03 +000091 "the REST interface")
92 private Short vid = null;
Sean Condon0e89bda2017-03-21 14:23:19 +000093
94 @Argument(index = 9, name = "rmep",
95 description = "Remote Mep numeric identifier [1-8192]",
96 required = true, multiValued = true)
Sean Condon3a1efef2018-02-24 13:16:03 +000097 private String[] rmepArray = null;
Sean Condon0e89bda2017-03-21 14:23:19 +000098
99 @Override
Ray Milkey86ad7bb2018-09-27 12:32:28 -0700100 protected void doExecute() {
Sean Condon0e89bda2017-03-21 14:23:19 +0000101 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 Condon3a1efef2018-02-24 13:16:03 +0000109 MdId mdId = CfmMdListMdCommand.parseMdName(mdNameParts[0] + "(" + mdNameParts[1] + ")");
Sean Condon0e89bda2017-03-21 14:23:19 +0000110
Sean Condon3a1efef2018-02-24 13:16:03 +0000111 MaIdShort maId = CfmMdListMdCommand.parseMaName(name + "(" + nameType + ")");
Sean Condon0e89bda2017-03-21 14:23:19 +0000112
Sean Condon0e89bda2017-03-21 14:23:19 +0000113 try {
Sean Condon3a1efef2018-02-24 13:16:03 +0000114 MaintenanceAssociation.MaBuilder builder = DefaultMaintenanceAssociation
115 .builder(maId, mdId.getNameLength());
Sean Condon0e89bda2017-03-21 14:23:19 +0000116 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 Condon3a1efef2018-02-24 13:16:03 +0000122 if (numericId != null) {
Sean Condon0e89bda2017-03-21 14:23:19 +0000123 builder = builder.maNumericId(numericId);
124 }
Sean Condon3a1efef2018-02-24 13:16:03 +0000125 if (componentId != null) {
Sean Condon0e89bda2017-03-21 14:23:19 +0000126 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 Condon3a1efef2018-02-24 13:16:03 +0000136 if (vid != null) {
Sean Condon0e89bda2017-03-21 14:23:19 +0000137 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}