blob: f3635838ad69ad5d759ac985fccd67eec2493388 [file] [log] [blame]
RafaƂ Szaleckide5cf842018-11-17 13:30:01 +01001/*
2 * Copyright 2018-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 */
16package org.onosproject.cli.net;
17
18import org.apache.karaf.shell.api.action.Argument;
19import org.apache.karaf.shell.api.action.Command;
20import org.apache.karaf.shell.api.action.Completion;
21import org.apache.karaf.shell.api.action.Option;
22import org.apache.karaf.shell.api.action.lifecycle.Service;
23import org.onosproject.cli.AbstractShellCommand;
24import org.onosproject.cli.net.completer.AnnotationKeysCompleter;
25import org.onosproject.net.HostId;
26import org.onosproject.net.config.NetworkConfigService;
27import org.onosproject.net.config.basics.HostAnnotationConfig;
28import org.onosproject.net.provider.ProviderId;
29
30/**
31 * Annotates host model.
32 */
33@Service
34@Command(scope = "onos", name = "annotate-host",
35 description = "Annotates host")
36public class AnnotateHostCommand extends AbstractShellCommand {
37
38 static final ProviderId PID = new ProviderId("cli", "org.onosproject.cli", true);
39
40 @Argument(index = 0, name = "uri", description = "Host ID",
41 required = true, multiValued = false)
42 @Completion(HostIdCompleter.class)
43 String uri = null;
44
45 @Argument(index = 1, name = "key", description = "Annotation key",
46 required = true, multiValued = false)
47 @Completion(AnnotationKeysCompleter.class)
48 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
55 @Option(name = "--remove-config",
56 description = "Remove annotation config")
57 private boolean removeCfg = false;
58
59 @Override
60 protected void doExecute() {
61 NetworkConfigService netcfgService = get(NetworkConfigService.class);
62 HostId hostId = HostId.hostId(uri);
63
64 if (key == null) {
65 print("[ERROR] Annotation key not specified.");
66 return;
67 }
68 HostAnnotationConfig cfg = netcfgService.getConfig(hostId, HostAnnotationConfig.class);
69 if (cfg == null) {
70 cfg = new HostAnnotationConfig(hostId);
71 }
72 if (removeCfg) {
73 // remove config about entry
74 cfg.annotation(key);
75 } else {
76 // add request config
77 cfg.annotation(key, value);
78 }
79 netcfgService.applyConfig(hostId, HostAnnotationConfig.class, cfg.node());
80 }
81}