blob: 5c7cc918841ec33176bacfabd0d44f9b034e5f93 [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.Command;
19import org.apache.karaf.shell.api.action.Argument;
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;
Ray Milkeyc2b7b962018-10-02 15:00:02 -070022import org.onosproject.cfm.cli.completer.CfmMdLevelCompleter;
23import org.onosproject.cfm.cli.completer.CfmMdNameTypeCompleter;
Sean Condon0e89bda2017-03-21 14:23:19 +000024import org.onosproject.cli.AbstractShellCommand;
Ray Milkeyc2b7b962018-10-02 15:00:02 -070025import org.onosproject.cli.PlaceholderCompleter;
Sean Condon0e89bda2017-03-21 14:23:19 +000026import org.onosproject.incubator.net.l2monitoring.cfm.DefaultMaintenanceDomain;
27import org.onosproject.incubator.net.l2monitoring.cfm.MaintenanceDomain;
28import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MdId;
Sean Condon0e89bda2017-03-21 14:23:19 +000029import org.onosproject.incubator.net.l2monitoring.cfm.service.CfmConfigException;
30import org.onosproject.incubator.net.l2monitoring.cfm.service.CfmMdService;
31
Sean Condon0e89bda2017-03-21 14:23:19 +000032/**
33 * Adds a Maintenance Domain to the existing list.
34 */
Ray Milkey7a2dee52018-09-28 10:58:28 -070035@Service
Sean Condon0e89bda2017-03-21 14:23:19 +000036@Command(scope = "onos", name = "cfm-md-add",
37 description = "Add a CFM Maintenance Domain.")
38public class CfmMdAddCommand extends AbstractShellCommand {
39
Sean Condon3a1efef2018-02-24 13:16:03 +000040 @Argument(name = "name-type",
Sean Condon0e89bda2017-03-21 14:23:19 +000041 description = "Maintenance Domain name type",
Sean Condon3a1efef2018-02-24 13:16:03 +000042 required = true)
Ray Milkeyc2b7b962018-10-02 15:00:02 -070043 @Completion(CfmMdNameTypeCompleter.class)
Sean Condon3a1efef2018-02-24 13:16:03 +000044 private String nameType = null;
Sean Condon0e89bda2017-03-21 14:23:19 +000045
46 @Argument(index = 1, name = "name",
47 description = "Maintenance Domain name. Restrictions apply depending " +
48 "on name-type. Leave empty if name type is none",
Sean Condon3a1efef2018-02-24 13:16:03 +000049 required = true)
Ray Milkeyc2b7b962018-10-02 15:00:02 -070050 @Completion(PlaceholderCompleter.class)
Sean Condon3a1efef2018-02-24 13:16:03 +000051 private String name = null;
Sean Condon0e89bda2017-03-21 14:23:19 +000052
53 @Argument(index = 2, name = "level",
54 description = "Maintenance Domain level LEVEL0-LEVEL7",
Sean Condon3a1efef2018-02-24 13:16:03 +000055 required = true)
Ray Milkeyc2b7b962018-10-02 15:00:02 -070056 @Completion(CfmMdLevelCompleter.class)
Sean Condon3a1efef2018-02-24 13:16:03 +000057 private String level = null;
Sean Condon0e89bda2017-03-21 14:23:19 +000058
59 @Argument(index = 3, name = "numeric-id",
Sean Condon3a1efef2018-02-24 13:16:03 +000060 description = "An optional numeric id for Maintenance Domain [1-65535]")
Ray Milkeyc2b7b962018-10-02 15:00:02 -070061 @Completion(PlaceholderCompleter.class)
Sean Condon3a1efef2018-02-24 13:16:03 +000062 private Short numericId = null;
Sean Condon0e89bda2017-03-21 14:23:19 +000063
64 @Override
Ray Milkey86ad7bb2018-09-27 12:32:28 -070065 protected void doExecute() {
Sean Condon0e89bda2017-03-21 14:23:19 +000066 CfmMdService service = get(CfmMdService.class);
Sean Condon3a1efef2018-02-24 13:16:03 +000067 MdId mdId = CfmMdListMdCommand.parseMdName(name + "(" + nameType + ")");
68
Sean Condon0e89bda2017-03-21 14:23:19 +000069 MaintenanceDomain.MdLevel levelEnum =
70 MaintenanceDomain.MdLevel.valueOf(level);
Sean Condon0e89bda2017-03-21 14:23:19 +000071
Sean Condon0e89bda2017-03-21 14:23:19 +000072 try {
Sean Condon3a1efef2018-02-24 13:16:03 +000073 MaintenanceDomain.MdBuilder builder = DefaultMaintenanceDomain
74 .builder(mdId).mdLevel(levelEnum);
75 if (numericId != null) {
76 builder = builder.mdNumericId(numericId);
Sean Condon0e89bda2017-03-21 14:23:19 +000077 }
78 boolean created = service.createMaintenanceDomain(builder.build());
79 print("Maintenance Domain with id %s is successfully %s.",
80 mdId, created ? "updated" : "created");
81 } catch (CfmConfigException e) {
82 throw new IllegalArgumentException(e);
83 }
84 }
85}