blob: 0680d59fb9c405db62cabb419ab4a8c061578aab [file] [log] [blame]
Thomas Vachuska8ceee942015-04-14 16:53:57 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Thomas Vachuska8ceee942015-04-14 16:53:57 -07003 *
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.cli.net;
17
18import org.apache.karaf.shell.commands.Argument;
19import org.apache.karaf.shell.commands.Command;
Palash Kalaa06a6162017-11-15 20:42:40 +090020import org.apache.karaf.shell.commands.Option;
Thomas Vachuska8ceee942015-04-14 16:53:57 -070021import org.onosproject.cli.AbstractShellCommand;
Thomas Vachuska8ceee942015-04-14 16:53:57 -070022import org.onosproject.net.DeviceId;
Palash Kalaa06a6162017-11-15 20:42:40 +090023import org.onosproject.net.config.NetworkConfigService;
24import org.onosproject.net.config.basics.DeviceAnnotationConfig;
Thomas Vachuska8ceee942015-04-14 16:53:57 -070025import org.onosproject.net.provider.ProviderId;
26
27/**
28 * Annotates network device model.
29 */
30@Command(scope = "onos", name = "annotate-device",
31 description = "Annotates network model entities")
32public class AnnotateDeviceCommand extends AbstractShellCommand {
33
34 static final ProviderId PID = new ProviderId("cli", "org.onosproject.cli", true);
35
36 @Argument(index = 0, name = "uri", description = "Device ID",
37 required = true, multiValued = false)
38 String uri = null;
39
40 @Argument(index = 1, name = "key", description = "Annotation key",
41 required = true, multiValued = false)
42 String key = null;
43
44 @Argument(index = 2, name = "value",
45 description = "Annotation value (null to remove)",
46 required = false, multiValued = false)
47 String value = null;
48
Palash Kalaa06a6162017-11-15 20:42:40 +090049 @Option(name = "--remove-config",
50 description = "Remove annotation config")
51 private boolean removeCfg = false;
52
Thomas Vachuska8ceee942015-04-14 16:53:57 -070053 @Override
54 protected void execute() {
Palash Kalaa06a6162017-11-15 20:42:40 +090055 NetworkConfigService netcfgService = get(NetworkConfigService.class);
56 DeviceId deviceId = DeviceId.deviceId(uri);
Thomas Vachuska8ceee942015-04-14 16:53:57 -070057
Palash Kalaa06a6162017-11-15 20:42:40 +090058 if (key == null) {
59 print("[ERROR] Annotation key not specified.");
60 return;
Thomas Vachuska8ceee942015-04-14 16:53:57 -070061 }
Palash Kalaa06a6162017-11-15 20:42:40 +090062 DeviceAnnotationConfig cfg = netcfgService.getConfig(deviceId, DeviceAnnotationConfig.class);
63 if (cfg == null) {
64 cfg = new DeviceAnnotationConfig(deviceId);
65 }
66 if (removeCfg) {
67 // remove config about entry
68 cfg.annotation(key);
Thomas Vachuska8ceee942015-04-14 16:53:57 -070069 } else {
Palash Kalaa06a6162017-11-15 20:42:40 +090070 // add remove request config
71 cfg.annotation(key, value);
Thomas Vachuska8ceee942015-04-14 16:53:57 -070072 }
Palash Kalaa06a6162017-11-15 20:42:40 +090073 netcfgService.applyConfig(deviceId, DeviceAnnotationConfig.class, cfg.node());
Thomas Vachuska8ceee942015-04-14 16:53:57 -070074 }
75}