blob: 7ce56692445e1833466b6ea2b87ae8421721c6fa [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
Jonathan Hart2ddc4112015-09-16 09:28:07 -070018import com.fasterxml.jackson.core.JsonProcessingException;
Thomas Vachuska0a400ea2015-09-04 11:25:03 -070019import com.fasterxml.jackson.databind.JsonNode;
Thomas Vachuska96d55b12015-05-11 08:52:03 -070020import com.fasterxml.jackson.databind.ObjectMapper;
21import com.fasterxml.jackson.databind.node.ObjectNode;
22import org.apache.karaf.shell.commands.Argument;
23import org.apache.karaf.shell.commands.Command;
24import org.onosproject.cli.AbstractShellCommand;
Ray Milkeya4122362015-08-18 15:19:08 -070025import org.onosproject.net.config.Config;
26import org.onosproject.net.config.NetworkConfigService;
27import org.onosproject.net.config.SubjectFactory;
Thomas Vachuska96d55b12015-05-11 08:52:03 -070028
29import static com.google.common.base.Strings.isNullOrEmpty;
30
31/**
32 * Manages network configuration.
33 */
34@Command(scope = "onos", name = "netcfg",
35 description = "Manages network configuration")
36public class NetworkConfigCommand extends AbstractShellCommand {
37
Thomas Vachuskaea5adc62015-10-07 11:52:30 -070038 @Argument(index = 0, name = "subjectClassKey", description = "Subject class key",
39 required = false, multiValued = false)
40 String subjectClassKey = null;
41
42 @Argument(index = 1, name = "subjectKey", description = "Subject key",
Thomas Vachuska96d55b12015-05-11 08:52:03 -070043 required = false, multiValued = false)
44 String subjectKey = null;
45
Thomas Vachuska96d55b12015-05-11 08:52:03 -070046 @Argument(index = 2, name = "configKey", description = "Config key",
47 required = false, multiValued = false)
48 String configKey = null;
49
50 private final ObjectMapper mapper = new ObjectMapper();
51 private NetworkConfigService service;
52
53 @Override
54 protected void execute() {
55 service = get(NetworkConfigService.class);
Jonathan Hart2ddc4112015-09-16 09:28:07 -070056 JsonNode root = mapper.createObjectNode();
Thomas Vachuskaea5adc62015-10-07 11:52:30 -070057 if (isNullOrEmpty(subjectClassKey)) {
Thomas Vachuska0a400ea2015-09-04 11:25:03 -070058 addAll((ObjectNode) root);
Thomas Vachuska96d55b12015-05-11 08:52:03 -070059 } else {
Thomas Vachuskaea5adc62015-10-07 11:52:30 -070060 SubjectFactory subjectFactory = service.getSubjectFactory(subjectClassKey);
61 if (isNullOrEmpty(subjectKey)) {
Thomas Vachuska0a400ea2015-09-04 11:25:03 -070062 addSubjectClass((ObjectNode) root, subjectFactory);
Thomas Vachuska96d55b12015-05-11 08:52:03 -070063 } else {
Thomas Vachuskaea5adc62015-10-07 11:52:30 -070064 Object s = subjectFactory.createSubject(subjectKey);
Thomas Vachuska96d55b12015-05-11 08:52:03 -070065 if (isNullOrEmpty(configKey)) {
Thomas Vachuska0a400ea2015-09-04 11:25:03 -070066 addSubject((ObjectNode) root, s);
Thomas Vachuska96d55b12015-05-11 08:52:03 -070067 } else {
Thomas Vachuskaea5adc62015-10-07 11:52:30 -070068 root = getSubjectConfig(getConfig(s, subjectClassKey, configKey));
Thomas Vachuska96d55b12015-05-11 08:52:03 -070069 }
70 }
71 }
Jonathan Hart2ddc4112015-09-16 09:28:07 -070072
73 try {
74 print("%s", mapper.writerWithDefaultPrettyPrinter().writeValueAsString(root));
75 } catch (JsonProcessingException e) {
76 throw new RuntimeException("Error writing JSON to string", e);
77 }
Thomas Vachuska96d55b12015-05-11 08:52:03 -070078 }
79
80 @SuppressWarnings("unchecked")
81 private void addAll(ObjectNode root) {
82 service.getSubjectClasses()
83 .forEach(sc -> {
Jonathan Hart2ddc4112015-09-16 09:28:07 -070084 SubjectFactory sf = service.getSubjectFactory(sc);
Thomas Vachuskaea5adc62015-10-07 11:52:30 -070085 addSubjectClass(newObject(root, sf.subjectClassKey()), sf);
Thomas Vachuska96d55b12015-05-11 08:52:03 -070086 });
87 }
88
89 @SuppressWarnings("unchecked")
90 private void addSubjectClass(ObjectNode root, SubjectFactory sf) {
91 service.getSubjects(sf.subjectClass())
Thomas Vachuskaea5adc62015-10-07 11:52:30 -070092 .forEach(s -> addSubject(newObject(root, sf.subjectKey(s)), s));
Thomas Vachuska96d55b12015-05-11 08:52:03 -070093 }
94
95 private void addSubject(ObjectNode root, Object s) {
Thomas Vachuskab129fbf2015-07-23 00:49:03 -070096 service.getConfigs(s).forEach(c -> root.set(c.key(), c.node()));
Thomas Vachuska96d55b12015-05-11 08:52:03 -070097 }
98
Thomas Vachuska0a400ea2015-09-04 11:25:03 -070099 private JsonNode getSubjectConfig(Config config) {
Thomas Vachuskab129fbf2015-07-23 00:49:03 -0700100 return config != null ? config.node() : null;
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700101 }
102
Jonathan Hart111b42b2015-07-14 13:28:05 -0700103 private Config getConfig(Object s, String subjectKey, String ck) {
104 Class<? extends Config> configClass = service.getConfigClass(subjectKey, ck);
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700105 return configClass != null ? service.getConfig(s, configClass) : null;
106 }
107
108 private ObjectNode newObject(ObjectNode parent, String key) {
109 ObjectNode node = mapper.createObjectNode();
110 parent.set(key, node);
111 return node;
112 }
113}