Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.cfm.cli; |
| 17 | |
Ray Milkey | 86ad7bb | 2018-09-27 12:32:28 -0700 | [diff] [blame] | 18 | import org.apache.karaf.shell.api.action.Command; |
| 19 | import org.apache.karaf.shell.api.action.Argument; |
Ray Milkey | 7a2dee5 | 2018-09-28 10:58:28 -0700 | [diff] [blame] | 20 | import org.apache.karaf.shell.api.action.lifecycle.Service; |
Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 21 | import org.onosproject.cli.AbstractShellCommand; |
| 22 | import org.onosproject.incubator.net.l2monitoring.cfm.DefaultMaintenanceDomain; |
| 23 | import org.onosproject.incubator.net.l2monitoring.cfm.MaintenanceDomain; |
| 24 | import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MdId; |
Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 25 | import org.onosproject.incubator.net.l2monitoring.cfm.service.CfmConfigException; |
| 26 | import org.onosproject.incubator.net.l2monitoring.cfm.service.CfmMdService; |
| 27 | |
Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 28 | /** |
| 29 | * Adds a Maintenance Domain to the existing list. |
| 30 | */ |
Ray Milkey | 7a2dee5 | 2018-09-28 10:58:28 -0700 | [diff] [blame] | 31 | @Service |
Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 32 | @Command(scope = "onos", name = "cfm-md-add", |
| 33 | description = "Add a CFM Maintenance Domain.") |
| 34 | public class CfmMdAddCommand extends AbstractShellCommand { |
| 35 | |
Sean Condon | 3a1efef | 2018-02-24 13:16:03 +0000 | [diff] [blame] | 36 | @Argument(name = "name-type", |
Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 37 | description = "Maintenance Domain name type", |
Sean Condon | 3a1efef | 2018-02-24 13:16:03 +0000 | [diff] [blame] | 38 | required = true) |
| 39 | private String nameType = null; |
Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 40 | |
| 41 | @Argument(index = 1, name = "name", |
| 42 | description = "Maintenance Domain name. Restrictions apply depending " + |
| 43 | "on name-type. Leave empty if name type is none", |
Sean Condon | 3a1efef | 2018-02-24 13:16:03 +0000 | [diff] [blame] | 44 | required = true) |
| 45 | private String name = null; |
Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 46 | |
| 47 | @Argument(index = 2, name = "level", |
| 48 | description = "Maintenance Domain level LEVEL0-LEVEL7", |
Sean Condon | 3a1efef | 2018-02-24 13:16:03 +0000 | [diff] [blame] | 49 | required = true) |
| 50 | private String level = null; |
Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 51 | |
| 52 | @Argument(index = 3, name = "numeric-id", |
Sean Condon | 3a1efef | 2018-02-24 13:16:03 +0000 | [diff] [blame] | 53 | description = "An optional numeric id for Maintenance Domain [1-65535]") |
| 54 | private Short numericId = null; |
Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 55 | |
| 56 | @Override |
Ray Milkey | 86ad7bb | 2018-09-27 12:32:28 -0700 | [diff] [blame] | 57 | protected void doExecute() { |
Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 58 | CfmMdService service = get(CfmMdService.class); |
Sean Condon | 3a1efef | 2018-02-24 13:16:03 +0000 | [diff] [blame] | 59 | MdId mdId = CfmMdListMdCommand.parseMdName(name + "(" + nameType + ")"); |
| 60 | |
Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 61 | MaintenanceDomain.MdLevel levelEnum = |
| 62 | MaintenanceDomain.MdLevel.valueOf(level); |
Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 63 | |
Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 64 | try { |
Sean Condon | 3a1efef | 2018-02-24 13:16:03 +0000 | [diff] [blame] | 65 | MaintenanceDomain.MdBuilder builder = DefaultMaintenanceDomain |
| 66 | .builder(mdId).mdLevel(levelEnum); |
| 67 | if (numericId != null) { |
| 68 | builder = builder.mdNumericId(numericId); |
Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 69 | } |
| 70 | boolean created = service.createMaintenanceDomain(builder.build()); |
| 71 | print("Maintenance Domain with id %s is successfully %s.", |
| 72 | mdId, created ? "updated" : "created"); |
| 73 | } catch (CfmConfigException e) { |
| 74 | throw new IllegalArgumentException(e); |
| 75 | } |
| 76 | } |
| 77 | } |