blob: 5f2f86ee95ed7e659e85cce122e0d3fbe64a3459 [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
Thomas Vachuska0a400ea2015-09-04 11:25:03 -070018import com.fasterxml.jackson.databind.JsonNode;
Thomas Vachuska96d55b12015-05-11 08:52:03 -070019import com.fasterxml.jackson.databind.ObjectMapper;
20import com.fasterxml.jackson.databind.node.ObjectNode;
21import org.apache.karaf.shell.commands.Argument;
22import org.apache.karaf.shell.commands.Command;
23import org.onosproject.cli.AbstractShellCommand;
Ray Milkeya4122362015-08-18 15:19:08 -070024import org.onosproject.net.config.Config;
25import org.onosproject.net.config.NetworkConfigService;
26import org.onosproject.net.config.SubjectFactory;
Thomas Vachuska96d55b12015-05-11 08:52:03 -070027
28import static com.google.common.base.Strings.isNullOrEmpty;
29
30/**
31 * Manages network configuration.
32 */
33@Command(scope = "onos", name = "netcfg",
34 description = "Manages network configuration")
35public class NetworkConfigCommand extends AbstractShellCommand {
36
37 @Argument(index = 0, name = "subjectKey", description = "Subject key",
38 required = false, multiValued = false)
39 String subjectKey = null;
40
41 @Argument(index = 1, name = "subject", description = "Subject",
42 required = false, multiValued = false)
43 String subject = null;
44
45 @Argument(index = 2, name = "configKey", description = "Config key",
46 required = false, multiValued = false)
47 String configKey = null;
48
49 private final ObjectMapper mapper = new ObjectMapper();
50 private NetworkConfigService service;
51
52 @Override
53 protected void execute() {
54 service = get(NetworkConfigService.class);
Thomas Vachuska0a400ea2015-09-04 11:25:03 -070055 JsonNode root = new ObjectMapper().createObjectNode();
Thomas Vachuska96d55b12015-05-11 08:52:03 -070056 if (isNullOrEmpty(subjectKey)) {
Thomas Vachuska0a400ea2015-09-04 11:25:03 -070057 addAll((ObjectNode) root);
Thomas Vachuska96d55b12015-05-11 08:52:03 -070058 } else {
59 SubjectFactory subjectFactory = service.getSubjectFactory(subjectKey);
60 if (isNullOrEmpty(subject)) {
Thomas Vachuska0a400ea2015-09-04 11:25:03 -070061 addSubjectClass((ObjectNode) root, subjectFactory);
Thomas Vachuska96d55b12015-05-11 08:52:03 -070062 } else {
63 Object s = subjectFactory.createSubject(subject);
64 if (isNullOrEmpty(configKey)) {
Thomas Vachuska0a400ea2015-09-04 11:25:03 -070065 addSubject((ObjectNode) root, s);
Thomas Vachuska96d55b12015-05-11 08:52:03 -070066 } else {
Jonathan Hart111b42b2015-07-14 13:28:05 -070067 root = getSubjectConfig(getConfig(s, subjectKey, configKey));
Thomas Vachuska96d55b12015-05-11 08:52:03 -070068 }
69 }
70 }
71 print("%s", root.toString());
72 }
73
74 @SuppressWarnings("unchecked")
75 private void addAll(ObjectNode root) {
76 service.getSubjectClasses()
77 .forEach(sc -> {
78 SubjectFactory sf = service.getSubjectFactory((Class) sc);
79 addSubjectClass(newObject(root, sf.subjectKey()), sf);
80 });
81 }
82
83 @SuppressWarnings("unchecked")
84 private void addSubjectClass(ObjectNode root, SubjectFactory sf) {
85 service.getSubjects(sf.subjectClass())
86 .forEach(s -> addSubject(newObject(root, s.toString()), s));
87 }
88
89 private void addSubject(ObjectNode root, Object s) {
Thomas Vachuskab129fbf2015-07-23 00:49:03 -070090 service.getConfigs(s).forEach(c -> root.set(c.key(), c.node()));
Thomas Vachuska96d55b12015-05-11 08:52:03 -070091 }
92
Thomas Vachuska0a400ea2015-09-04 11:25:03 -070093 private JsonNode getSubjectConfig(Config config) {
Thomas Vachuskab129fbf2015-07-23 00:49:03 -070094 return config != null ? config.node() : null;
Thomas Vachuska96d55b12015-05-11 08:52:03 -070095 }
96
Jonathan Hart111b42b2015-07-14 13:28:05 -070097 private Config getConfig(Object s, String subjectKey, String ck) {
98 Class<? extends Config> configClass = service.getConfigClass(subjectKey, ck);
Thomas Vachuska96d55b12015-05-11 08:52:03 -070099 return configClass != null ? service.getConfig(s, configClass) : null;
100 }
101
102 private ObjectNode newObject(ObjectNode parent, String key) {
103 ObjectNode node = mapper.createObjectNode();
104 parent.set(key, node);
105 return node;
106 }
107}