blob: 4a4cb066c2a299ae85105088498550edaf69340b [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
Ray Milkeyd84f89b2018-08-17 14:54:17 -070018import org.apache.karaf.shell.api.action.Argument;
19import org.apache.karaf.shell.api.action.Command;
20import org.apache.karaf.shell.api.action.lifecycle.Service;
21import org.apache.karaf.shell.api.action.Option;
Thomas Vachuska8ceee942015-04-14 16:53:57 -070022import org.onosproject.cli.AbstractShellCommand;
Thomas Vachuska8ceee942015-04-14 16:53:57 -070023import org.onosproject.net.DeviceId;
Palash Kalaa06a6162017-11-15 20:42:40 +090024import org.onosproject.net.config.NetworkConfigService;
25import org.onosproject.net.config.basics.DeviceAnnotationConfig;
Thomas Vachuska8ceee942015-04-14 16:53:57 -070026import org.onosproject.net.provider.ProviderId;
27
28/**
29 * Annotates network device model.
30 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070031@Service
Thomas Vachuska8ceee942015-04-14 16:53:57 -070032@Command(scope = "onos", name = "annotate-device",
33 description = "Annotates network model entities")
34public class AnnotateDeviceCommand extends AbstractShellCommand {
35
36 static final ProviderId PID = new ProviderId("cli", "org.onosproject.cli", true);
37
38 @Argument(index = 0, name = "uri", description = "Device ID",
39 required = true, multiValued = false)
40 String uri = null;
41
42 @Argument(index = 1, name = "key", description = "Annotation key",
43 required = true, multiValued = false)
44 String key = null;
45
46 @Argument(index = 2, name = "value",
47 description = "Annotation value (null to remove)",
48 required = false, multiValued = false)
49 String value = null;
50
Palash Kalaa06a6162017-11-15 20:42:40 +090051 @Option(name = "--remove-config",
52 description = "Remove annotation config")
53 private boolean removeCfg = false;
54
Thomas Vachuska8ceee942015-04-14 16:53:57 -070055 @Override
Ray Milkeyd84f89b2018-08-17 14:54:17 -070056 protected void doExecute() {
Palash Kalaa06a6162017-11-15 20:42:40 +090057 NetworkConfigService netcfgService = get(NetworkConfigService.class);
58 DeviceId deviceId = DeviceId.deviceId(uri);
Thomas Vachuska8ceee942015-04-14 16:53:57 -070059
Palash Kalaa06a6162017-11-15 20:42:40 +090060 if (key == null) {
61 print("[ERROR] Annotation key not specified.");
62 return;
Thomas Vachuska8ceee942015-04-14 16:53:57 -070063 }
Palash Kalaa06a6162017-11-15 20:42:40 +090064 DeviceAnnotationConfig cfg = netcfgService.getConfig(deviceId, DeviceAnnotationConfig.class);
65 if (cfg == null) {
66 cfg = new DeviceAnnotationConfig(deviceId);
67 }
68 if (removeCfg) {
69 // remove config about entry
70 cfg.annotation(key);
Thomas Vachuska8ceee942015-04-14 16:53:57 -070071 } else {
Palash Kalaa06a6162017-11-15 20:42:40 +090072 // add remove request config
73 cfg.annotation(key, value);
Thomas Vachuska8ceee942015-04-14 16:53:57 -070074 }
Palash Kalaa06a6162017-11-15 20:42:40 +090075 netcfgService.applyConfig(deviceId, DeviceAnnotationConfig.class, cfg.node());
Thomas Vachuska8ceee942015-04-14 16:53:57 -070076 }
77}