blob: 52b8050d49f7a562650417e21f00712581a7a003 [file] [log] [blame]
Thomas Vachuska96d55b12015-05-11 08:52:03 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Thomas Vachuska96d55b12015-05-11 08:52:03 -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.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;
kavitha Alagesan1f1fef72016-12-08 17:07:12 +053030import static org.onlab.util.Tools.nullIsIllegal;
Thomas Vachuska96d55b12015-05-11 08:52:03 -070031
32/**
33 * Manages network configuration.
34 */
35@Command(scope = "onos", name = "netcfg",
36 description = "Manages network configuration")
37public class NetworkConfigCommand extends AbstractShellCommand {
38
kavitha Alagesan1f1fef72016-12-08 17:07:12 +053039 private static final String E_CLASSKEY_NOT_REGISTERED = " is not a registered SubjectClassKey";
40
Thomas Vachuskaea5adc62015-10-07 11:52:30 -070041 @Argument(index = 0, name = "subjectClassKey", description = "Subject class key",
42 required = false, multiValued = false)
43 String subjectClassKey = null;
44
45 @Argument(index = 1, name = "subjectKey", description = "Subject key",
Thomas Vachuska96d55b12015-05-11 08:52:03 -070046 required = false, multiValued = false)
47 String subjectKey = null;
48
Thomas Vachuska96d55b12015-05-11 08:52:03 -070049 @Argument(index = 2, name = "configKey", description = "Config key",
50 required = false, multiValued = false)
51 String configKey = null;
52
53 private final ObjectMapper mapper = new ObjectMapper();
54 private NetworkConfigService service;
55
56 @Override
57 protected void execute() {
58 service = get(NetworkConfigService.class);
Jonathan Hart2ddc4112015-09-16 09:28:07 -070059 JsonNode root = mapper.createObjectNode();
Thomas Vachuskaea5adc62015-10-07 11:52:30 -070060 if (isNullOrEmpty(subjectClassKey)) {
Thomas Vachuska0a400ea2015-09-04 11:25:03 -070061 addAll((ObjectNode) root);
Thomas Vachuska96d55b12015-05-11 08:52:03 -070062 } else {
kavitha Alagesan1f1fef72016-12-08 17:07:12 +053063 SubjectFactory subjectFactory = nullIsIllegal(service.getSubjectFactory(subjectClassKey),
64 subjectClassKey + E_CLASSKEY_NOT_REGISTERED);
Thomas Vachuskaea5adc62015-10-07 11:52:30 -070065 if (isNullOrEmpty(subjectKey)) {
Thomas Vachuska0a400ea2015-09-04 11:25:03 -070066 addSubjectClass((ObjectNode) root, subjectFactory);
Thomas Vachuska96d55b12015-05-11 08:52:03 -070067 } else {
Thomas Vachuskaea5adc62015-10-07 11:52:30 -070068 Object s = subjectFactory.createSubject(subjectKey);
Thomas Vachuska96d55b12015-05-11 08:52:03 -070069 if (isNullOrEmpty(configKey)) {
Thomas Vachuska0a400ea2015-09-04 11:25:03 -070070 addSubject((ObjectNode) root, s);
Thomas Vachuska96d55b12015-05-11 08:52:03 -070071 } else {
Thomas Vachuskaea5adc62015-10-07 11:52:30 -070072 root = getSubjectConfig(getConfig(s, subjectClassKey, configKey));
Thomas Vachuska96d55b12015-05-11 08:52:03 -070073 }
74 }
75 }
Jonathan Hart2ddc4112015-09-16 09:28:07 -070076
77 try {
78 print("%s", mapper.writerWithDefaultPrettyPrinter().writeValueAsString(root));
79 } catch (JsonProcessingException e) {
80 throw new RuntimeException("Error writing JSON to string", e);
81 }
Thomas Vachuska96d55b12015-05-11 08:52:03 -070082 }
83
84 @SuppressWarnings("unchecked")
85 private void addAll(ObjectNode root) {
86 service.getSubjectClasses()
87 .forEach(sc -> {
Jonathan Hart2ddc4112015-09-16 09:28:07 -070088 SubjectFactory sf = service.getSubjectFactory(sc);
Thomas Vachuskaea5adc62015-10-07 11:52:30 -070089 addSubjectClass(newObject(root, sf.subjectClassKey()), sf);
Thomas Vachuska96d55b12015-05-11 08:52:03 -070090 });
91 }
92
93 @SuppressWarnings("unchecked")
94 private void addSubjectClass(ObjectNode root, SubjectFactory sf) {
95 service.getSubjects(sf.subjectClass())
Thomas Vachuskaea5adc62015-10-07 11:52:30 -070096 .forEach(s -> addSubject(newObject(root, sf.subjectKey(s)), s));
Thomas Vachuska96d55b12015-05-11 08:52:03 -070097 }
98
99 private void addSubject(ObjectNode root, Object s) {
Thomas Vachuskab129fbf2015-07-23 00:49:03 -0700100 service.getConfigs(s).forEach(c -> root.set(c.key(), c.node()));
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700101 }
102
Thomas Vachuska0a400ea2015-09-04 11:25:03 -0700103 private JsonNode getSubjectConfig(Config config) {
Thomas Vachuskab129fbf2015-07-23 00:49:03 -0700104 return config != null ? config.node() : null;
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700105 }
106
Jonathan Hart111b42b2015-07-14 13:28:05 -0700107 private Config getConfig(Object s, String subjectKey, String ck) {
108 Class<? extends Config> configClass = service.getConfigClass(subjectKey, ck);
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700109 return configClass != null ? service.getConfig(s, configClass) : null;
110 }
111
112 private ObjectNode newObject(ObjectNode parent, String key) {
113 ObjectNode node = mapper.createObjectNode();
114 parent.set(key, node);
115 return node;
116 }
117}