blob: e427a8ca40e4d817417d9e524e6d258aa15dafc9 [file] [log] [blame]
Thomas Vachuska96d55b12015-05-11 08:52:03 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
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;
Yuta HIGUCHI5d025c32017-03-13 20:32:48 -070024import org.apache.karaf.shell.commands.Option;
Thomas Vachuska96d55b12015-05-11 08:52:03 -070025import org.onosproject.cli.AbstractShellCommand;
Ray Milkeya4122362015-08-18 15:19:08 -070026import org.onosproject.net.config.Config;
27import org.onosproject.net.config.NetworkConfigService;
28import org.onosproject.net.config.SubjectFactory;
Thomas Vachuska96d55b12015-05-11 08:52:03 -070029
30import static com.google.common.base.Strings.isNullOrEmpty;
kavitha Alagesan1f1fef72016-12-08 17:07:12 +053031import static org.onlab.util.Tools.nullIsIllegal;
Thomas Vachuska96d55b12015-05-11 08:52:03 -070032
33/**
34 * Manages network configuration.
35 */
36@Command(scope = "onos", name = "netcfg",
37 description = "Manages network configuration")
38public class NetworkConfigCommand extends AbstractShellCommand {
39
kavitha Alagesan1f1fef72016-12-08 17:07:12 +053040 private static final String E_CLASSKEY_NOT_REGISTERED = " is not a registered SubjectClassKey";
41
Thomas Vachuskaea5adc62015-10-07 11:52:30 -070042 @Argument(index = 0, name = "subjectClassKey", description = "Subject class key",
43 required = false, multiValued = false)
44 String subjectClassKey = null;
45
46 @Argument(index = 1, name = "subjectKey", description = "Subject key",
Thomas Vachuska96d55b12015-05-11 08:52:03 -070047 required = false, multiValued = false)
48 String subjectKey = null;
49
Thomas Vachuska96d55b12015-05-11 08:52:03 -070050 @Argument(index = 2, name = "configKey", description = "Config key",
51 required = false, multiValued = false)
52 String configKey = null;
53
Yuta HIGUCHI5d025c32017-03-13 20:32:48 -070054 @Option(name = "--remove",
55 description = "Remove specified configuration tree",
56 required = false)
57 private boolean remove = false;
58
Thomas Vachuska96d55b12015-05-11 08:52:03 -070059 private final ObjectMapper mapper = new ObjectMapper();
60 private NetworkConfigService service;
61
62 @Override
63 protected void execute() {
64 service = get(NetworkConfigService.class);
Jonathan Hart2ddc4112015-09-16 09:28:07 -070065 JsonNode root = mapper.createObjectNode();
Thomas Vachuskaea5adc62015-10-07 11:52:30 -070066 if (isNullOrEmpty(subjectClassKey)) {
Yuta HIGUCHI5d025c32017-03-13 20:32:48 -070067 if (remove) {
68 service.removeConfig();
69 }
Thomas Vachuska0a400ea2015-09-04 11:25:03 -070070 addAll((ObjectNode) root);
Thomas Vachuska96d55b12015-05-11 08:52:03 -070071 } else {
kavitha Alagesan1f1fef72016-12-08 17:07:12 +053072 SubjectFactory subjectFactory = nullIsIllegal(service.getSubjectFactory(subjectClassKey),
73 subjectClassKey + E_CLASSKEY_NOT_REGISTERED);
Thomas Vachuskaea5adc62015-10-07 11:52:30 -070074 if (isNullOrEmpty(subjectKey)) {
Thomas Vachuska0a400ea2015-09-04 11:25:03 -070075 addSubjectClass((ObjectNode) root, subjectFactory);
Thomas Vachuska96d55b12015-05-11 08:52:03 -070076 } else {
Thomas Vachuskaea5adc62015-10-07 11:52:30 -070077 Object s = subjectFactory.createSubject(subjectKey);
Thomas Vachuska96d55b12015-05-11 08:52:03 -070078 if (isNullOrEmpty(configKey)) {
Yuta HIGUCHI5d025c32017-03-13 20:32:48 -070079 if (remove) {
80 service.removeConfig(s);
81 }
Thomas Vachuska0a400ea2015-09-04 11:25:03 -070082 addSubject((ObjectNode) root, s);
Thomas Vachuska96d55b12015-05-11 08:52:03 -070083 } else {
Yuta HIGUCHI5d025c32017-03-13 20:32:48 -070084 if (remove) {
85 service.removeConfig(subjectClassKey, s, configKey);
86 }
Thomas Vachuskaea5adc62015-10-07 11:52:30 -070087 root = getSubjectConfig(getConfig(s, subjectClassKey, configKey));
Thomas Vachuska96d55b12015-05-11 08:52:03 -070088 }
89 }
90 }
Jonathan Hart2ddc4112015-09-16 09:28:07 -070091
92 try {
93 print("%s", mapper.writerWithDefaultPrettyPrinter().writeValueAsString(root));
94 } catch (JsonProcessingException e) {
95 throw new RuntimeException("Error writing JSON to string", e);
96 }
Thomas Vachuska96d55b12015-05-11 08:52:03 -070097 }
98
99 @SuppressWarnings("unchecked")
100 private void addAll(ObjectNode root) {
101 service.getSubjectClasses()
102 .forEach(sc -> {
Jonathan Hart2ddc4112015-09-16 09:28:07 -0700103 SubjectFactory sf = service.getSubjectFactory(sc);
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700104 addSubjectClass(newObject(root, sf.subjectClassKey()), sf);
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700105 });
106 }
107
108 @SuppressWarnings("unchecked")
109 private void addSubjectClass(ObjectNode root, SubjectFactory sf) {
110 service.getSubjects(sf.subjectClass())
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700111 .forEach(s -> addSubject(newObject(root, sf.subjectKey(s)), s));
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700112 }
113
114 private void addSubject(ObjectNode root, Object s) {
Thomas Vachuskab129fbf2015-07-23 00:49:03 -0700115 service.getConfigs(s).forEach(c -> root.set(c.key(), c.node()));
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700116 }
117
Thomas Vachuska0a400ea2015-09-04 11:25:03 -0700118 private JsonNode getSubjectConfig(Config config) {
Thomas Vachuskab129fbf2015-07-23 00:49:03 -0700119 return config != null ? config.node() : null;
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700120 }
121
Jonathan Hart111b42b2015-07-14 13:28:05 -0700122 private Config getConfig(Object s, String subjectKey, String ck) {
123 Class<? extends Config> configClass = service.getConfigClass(subjectKey, ck);
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700124 return configClass != null ? service.getConfig(s, configClass) : null;
125 }
126
127 private ObjectNode newObject(ObjectNode parent, String key) {
128 ObjectNode node = mapper.createObjectNode();
129 parent.set(key, node);
130 return node;
131 }
132}