blob: b538db19edc404de71949068d699958829f19f51 [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;
Ray Milkey0068fd02018-10-11 15:45:39 -070020import org.apache.karaf.shell.api.action.Completion;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070021import org.apache.karaf.shell.api.action.lifecycle.Service;
22import org.apache.karaf.shell.api.action.Option;
Thomas Vachuska8ceee942015-04-14 16:53:57 -070023import org.onosproject.cli.AbstractShellCommand;
Ray Milkey0068fd02018-10-11 15:45:39 -070024import org.onosproject.cli.net.completer.AnnotationKeysCompleter;
Thomas Vachuska8ceee942015-04-14 16:53:57 -070025import org.onosproject.net.DeviceId;
Palash Kalaa06a6162017-11-15 20:42:40 +090026import org.onosproject.net.config.NetworkConfigService;
27import org.onosproject.net.config.basics.DeviceAnnotationConfig;
Thomas Vachuska8ceee942015-04-14 16:53:57 -070028import org.onosproject.net.provider.ProviderId;
29
30/**
31 * Annotates network device model.
32 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070033@Service
Thomas Vachuska8ceee942015-04-14 16:53:57 -070034@Command(scope = "onos", name = "annotate-device",
35 description = "Annotates network model entities")
36public class AnnotateDeviceCommand extends AbstractShellCommand {
37
38 static final ProviderId PID = new ProviderId("cli", "org.onosproject.cli", true);
39
40 @Argument(index = 0, name = "uri", description = "Device ID",
41 required = true, multiValued = false)
Ray Milkey0068fd02018-10-11 15:45:39 -070042 @Completion(DeviceIdCompleter.class)
Thomas Vachuska8ceee942015-04-14 16:53:57 -070043 String uri = null;
44
45 @Argument(index = 1, name = "key", description = "Annotation key",
46 required = true, multiValued = false)
Ray Milkey0068fd02018-10-11 15:45:39 -070047 @Completion(AnnotationKeysCompleter.class)
Thomas Vachuska8ceee942015-04-14 16:53:57 -070048 String key = null;
49
50 @Argument(index = 2, name = "value",
51 description = "Annotation value (null to remove)",
52 required = false, multiValued = false)
53 String value = null;
54
Palash Kalaa06a6162017-11-15 20:42:40 +090055 @Option(name = "--remove-config",
56 description = "Remove annotation config")
57 private boolean removeCfg = false;
58
Thomas Vachuska8ceee942015-04-14 16:53:57 -070059 @Override
Ray Milkeyd84f89b2018-08-17 14:54:17 -070060 protected void doExecute() {
Palash Kalaa06a6162017-11-15 20:42:40 +090061 NetworkConfigService netcfgService = get(NetworkConfigService.class);
62 DeviceId deviceId = DeviceId.deviceId(uri);
Thomas Vachuska8ceee942015-04-14 16:53:57 -070063
Palash Kalaa06a6162017-11-15 20:42:40 +090064 if (key == null) {
65 print("[ERROR] Annotation key not specified.");
66 return;
Thomas Vachuska8ceee942015-04-14 16:53:57 -070067 }
Palash Kalaa06a6162017-11-15 20:42:40 +090068 DeviceAnnotationConfig cfg = netcfgService.getConfig(deviceId, DeviceAnnotationConfig.class);
69 if (cfg == null) {
70 cfg = new DeviceAnnotationConfig(deviceId);
71 }
72 if (removeCfg) {
73 // remove config about entry
74 cfg.annotation(key);
Thomas Vachuska8ceee942015-04-14 16:53:57 -070075 } else {
Palash Kalaa06a6162017-11-15 20:42:40 +090076 // add remove request config
77 cfg.annotation(key, value);
Thomas Vachuska8ceee942015-04-14 16:53:57 -070078 }
Palash Kalaa06a6162017-11-15 20:42:40 +090079 netcfgService.applyConfig(deviceId, DeviceAnnotationConfig.class, cfg.node());
Thomas Vachuska8ceee942015-04-14 16:53:57 -070080 }
81}