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 | |
| 18 | import org.apache.karaf.shell.commands.Command; |
| 19 | import org.apache.karaf.shell.commands.Argument; |
| 20 | import org.onosproject.cli.AbstractShellCommand; |
| 21 | import org.onosproject.incubator.net.l2monitoring.cfm.DefaultMaintenanceDomain; |
| 22 | import org.onosproject.incubator.net.l2monitoring.cfm.MaintenanceDomain; |
| 23 | import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MdId; |
| 24 | import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MdIdCharStr; |
| 25 | import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MdIdDomainName; |
| 26 | import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MdIdMacUint; |
| 27 | import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MdIdNone; |
| 28 | import org.onosproject.incubator.net.l2monitoring.cfm.service.CfmConfigException; |
| 29 | import org.onosproject.incubator.net.l2monitoring.cfm.service.CfmMdService; |
| 30 | |
| 31 | import java.util.Optional; |
| 32 | |
| 33 | /** |
| 34 | * Adds a Maintenance Domain to the existing list. |
| 35 | */ |
| 36 | @Command(scope = "onos", name = "cfm-md-add", |
| 37 | description = "Add a CFM Maintenance Domain.") |
| 38 | public class CfmMdAddCommand extends AbstractShellCommand { |
| 39 | |
| 40 | @Argument(index = 0, name = "name-type", |
| 41 | description = "Maintenance Domain name type", |
| 42 | required = true, multiValued = false) |
| 43 | String nameType = null; |
| 44 | |
| 45 | @Argument(index = 1, name = "name", |
| 46 | description = "Maintenance Domain name. Restrictions apply depending " + |
| 47 | "on name-type. Leave empty if name type is none", |
| 48 | required = true, multiValued = false) |
| 49 | String name = null; |
| 50 | |
| 51 | @Argument(index = 2, name = "level", |
| 52 | description = "Maintenance Domain level LEVEL0-LEVEL7", |
| 53 | required = true, multiValued = false) |
| 54 | String level = null; |
| 55 | |
| 56 | @Argument(index = 3, name = "numeric-id", |
| 57 | description = "An optional numeric id for Maintenance Domain [1-65535]", |
| 58 | required = false, multiValued = false) |
| 59 | short numericId = 0; |
| 60 | |
| 61 | @Override |
| 62 | protected void execute() { |
| 63 | CfmMdService service = get(CfmMdService.class); |
| 64 | MdId mdId = null; |
| 65 | MdId.MdNameType nameTypeEnum = MdId.MdNameType.valueOf(nameType); |
| 66 | switch (nameTypeEnum) { |
| 67 | case DOMAINNAME: |
| 68 | mdId = MdIdDomainName.asMdId(name); |
| 69 | break; |
| 70 | case MACANDUINT: |
| 71 | mdId = MdIdMacUint.asMdId(name); |
| 72 | break; |
| 73 | case NONE: |
| 74 | mdId = MdIdNone.asMdId(); |
| 75 | break; |
| 76 | case CHARACTERSTRING: |
| 77 | default: |
| 78 | mdId = MdIdCharStr.asMdId(name); |
| 79 | } |
| 80 | MaintenanceDomain.MdLevel levelEnum = |
| 81 | MaintenanceDomain.MdLevel.valueOf(level); |
| 82 | Optional<Short> numericIdOpt = Optional.empty(); |
| 83 | if (numericId > 0) { |
| 84 | numericIdOpt = Optional.of(numericId); |
| 85 | } |
| 86 | |
| 87 | MaintenanceDomain.MdBuilder builder = null; |
| 88 | try { |
| 89 | builder = DefaultMaintenanceDomain.builder(mdId).mdLevel(levelEnum); |
| 90 | if (numericIdOpt.isPresent()) { |
| 91 | builder = builder.mdNumericId(numericIdOpt.get()); |
| 92 | } |
| 93 | boolean created = service.createMaintenanceDomain(builder.build()); |
| 94 | print("Maintenance Domain with id %s is successfully %s.", |
| 95 | mdId, created ? "updated" : "created"); |
| 96 | } catch (CfmConfigException e) { |
| 97 | throw new IllegalArgumentException(e); |
| 98 | } |
| 99 | } |
| 100 | } |