blob: d225248f535abe866fa75fb1fced05780de176fc [file] [log] [blame]
Thomas Vachuska96d55b12015-05-11 08:52:03 -07001/*
2 * Copyright 2015 Open Networking Laboratory
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.cfg;
17
18import com.fasterxml.jackson.databind.ObjectMapper;
19import com.fasterxml.jackson.databind.node.ObjectNode;
20import org.apache.karaf.shell.commands.Argument;
21import org.apache.karaf.shell.commands.Command;
22import org.onosproject.cli.AbstractShellCommand;
23import org.onosproject.incubator.net.config.Config;
24import org.onosproject.incubator.net.config.NetworkConfigService;
25import org.onosproject.incubator.net.config.SubjectFactory;
26
27import static com.google.common.base.Strings.isNullOrEmpty;
28
29/**
30 * Manages network configuration.
31 */
32@Command(scope = "onos", name = "netcfg",
33 description = "Manages network configuration")
34public class NetworkConfigCommand extends AbstractShellCommand {
35
36 @Argument(index = 0, name = "subjectKey", description = "Subject key",
37 required = false, multiValued = false)
38 String subjectKey = null;
39
40 @Argument(index = 1, name = "subject", description = "Subject",
41 required = false, multiValued = false)
42 String subject = null;
43
44 @Argument(index = 2, name = "configKey", description = "Config key",
45 required = false, multiValued = false)
46 String configKey = null;
47
48 private final ObjectMapper mapper = new ObjectMapper();
49 private NetworkConfigService service;
50
51 @Override
52 protected void execute() {
53 service = get(NetworkConfigService.class);
54 ObjectNode root = new ObjectMapper().createObjectNode();
55 if (isNullOrEmpty(subjectKey)) {
56 addAll(root);
57 } else {
58 SubjectFactory subjectFactory = service.getSubjectFactory(subjectKey);
59 if (isNullOrEmpty(subject)) {
60 addSubjectClass(root, subjectFactory);
61 } else {
62 Object s = subjectFactory.createSubject(subject);
63 if (isNullOrEmpty(configKey)) {
64 addSubject(root, s);
65 } else {
Thomas Vachuskab129fbf2015-07-23 00:49:03 -070066 root = getSubjectConfig(getConfig(s, configKey));
Thomas Vachuska96d55b12015-05-11 08:52:03 -070067 }
68 }
69 }
70 print("%s", root.toString());
71 }
72
73 @SuppressWarnings("unchecked")
74 private void addAll(ObjectNode root) {
75 service.getSubjectClasses()
76 .forEach(sc -> {
77 SubjectFactory sf = service.getSubjectFactory((Class) sc);
78 addSubjectClass(newObject(root, sf.subjectKey()), sf);
79 });
80 }
81
82 @SuppressWarnings("unchecked")
83 private void addSubjectClass(ObjectNode root, SubjectFactory sf) {
84 service.getSubjects(sf.subjectClass())
85 .forEach(s -> addSubject(newObject(root, s.toString()), s));
86 }
87
88 private void addSubject(ObjectNode root, Object s) {
Thomas Vachuskab129fbf2015-07-23 00:49:03 -070089 service.getConfigs(s).forEach(c -> root.set(c.key(), c.node()));
Thomas Vachuska96d55b12015-05-11 08:52:03 -070090 }
91
Thomas Vachuskab129fbf2015-07-23 00:49:03 -070092 private ObjectNode getSubjectConfig(Config config) {
93 return config != null ? config.node() : null;
Thomas Vachuska96d55b12015-05-11 08:52:03 -070094 }
95
96 private Config getConfig(Object s, String ck) {
97 Class<? extends Config> configClass = service.getConfigClass(ck);
98 return configClass != null ? service.getConfig(s, configClass) : null;
99 }
100
101 private ObjectNode newObject(ObjectNode parent, String key) {
102 ObjectNode node = mapper.createObjectNode();
103 parent.set(key, node);
104 return node;
105 }
106}