Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 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 | */ |
| 16 | package org.onosproject.cli.cfg; |
| 17 | |
Jonathan Hart | 2ddc411 | 2015-09-16 09:28:07 -0700 | [diff] [blame] | 18 | import com.fasterxml.jackson.core.JsonProcessingException; |
Thomas Vachuska | 0a400ea | 2015-09-04 11:25:03 -0700 | [diff] [blame] | 19 | import com.fasterxml.jackson.databind.JsonNode; |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 20 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 21 | import com.fasterxml.jackson.databind.node.ObjectNode; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 22 | import org.apache.karaf.shell.api.action.Argument; |
| 23 | import org.apache.karaf.shell.api.action.Command; |
Ray Milkey | 0068fd0 | 2018-10-11 15:45:39 -0700 | [diff] [blame] | 24 | import org.apache.karaf.shell.api.action.Completion; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 25 | import org.apache.karaf.shell.api.action.lifecycle.Service; |
| 26 | import org.apache.karaf.shell.api.action.Option; |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 27 | import org.onosproject.cli.AbstractShellCommand; |
Ray Milkey | a412236 | 2015-08-18 15:19:08 -0700 | [diff] [blame] | 28 | import org.onosproject.net.config.Config; |
| 29 | import org.onosproject.net.config.NetworkConfigService; |
| 30 | import org.onosproject.net.config.SubjectFactory; |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 31 | |
| 32 | import static com.google.common.base.Strings.isNullOrEmpty; |
kavitha Alagesan | 1f1fef7 | 2016-12-08 17:07:12 +0530 | [diff] [blame] | 33 | import static org.onlab.util.Tools.nullIsIllegal; |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 34 | |
| 35 | /** |
| 36 | * Manages network configuration. |
| 37 | */ |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 38 | @Service |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 39 | @Command(scope = "onos", name = "netcfg", |
| 40 | description = "Manages network configuration") |
| 41 | public class NetworkConfigCommand extends AbstractShellCommand { |
| 42 | |
kavitha Alagesan | 1f1fef7 | 2016-12-08 17:07:12 +0530 | [diff] [blame] | 43 | private static final String E_CLASSKEY_NOT_REGISTERED = " is not a registered SubjectClassKey"; |
| 44 | |
Thomas Vachuska | ea5adc6 | 2015-10-07 11:52:30 -0700 | [diff] [blame] | 45 | @Argument(index = 0, name = "subjectClassKey", description = "Subject class key", |
| 46 | required = false, multiValued = false) |
Ray Milkey | 0068fd0 | 2018-10-11 15:45:39 -0700 | [diff] [blame] | 47 | @Completion(ComponentConfigCommandCompleter.class) |
Thomas Vachuska | ea5adc6 | 2015-10-07 11:52:30 -0700 | [diff] [blame] | 48 | String subjectClassKey = null; |
| 49 | |
| 50 | @Argument(index = 1, name = "subjectKey", description = "Subject key", |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 51 | required = false, multiValued = false) |
Ray Milkey | 0068fd0 | 2018-10-11 15:45:39 -0700 | [diff] [blame] | 52 | @Completion(ComponentNameCompleter.class) |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 53 | String subjectKey = null; |
| 54 | |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 55 | @Argument(index = 2, name = "configKey", description = "Config key", |
| 56 | required = false, multiValued = false) |
Ray Milkey | 0068fd0 | 2018-10-11 15:45:39 -0700 | [diff] [blame] | 57 | @Completion(ConfigKeyCompleter.class) |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 58 | String configKey = null; |
| 59 | |
Yuta HIGUCHI | 5d025c3 | 2017-03-13 20:32:48 -0700 | [diff] [blame] | 60 | @Option(name = "--remove", |
| 61 | description = "Remove specified configuration tree", |
| 62 | required = false) |
| 63 | private boolean remove = false; |
| 64 | |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 65 | private final ObjectMapper mapper = new ObjectMapper(); |
| 66 | private NetworkConfigService service; |
| 67 | |
| 68 | @Override |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 69 | protected void doExecute() { |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 70 | service = get(NetworkConfigService.class); |
Jonathan Hart | 2ddc411 | 2015-09-16 09:28:07 -0700 | [diff] [blame] | 71 | JsonNode root = mapper.createObjectNode(); |
Thomas Vachuska | ea5adc6 | 2015-10-07 11:52:30 -0700 | [diff] [blame] | 72 | if (isNullOrEmpty(subjectClassKey)) { |
Yuta HIGUCHI | 5d025c3 | 2017-03-13 20:32:48 -0700 | [diff] [blame] | 73 | if (remove) { |
| 74 | service.removeConfig(); |
| 75 | } |
Thomas Vachuska | 0a400ea | 2015-09-04 11:25:03 -0700 | [diff] [blame] | 76 | addAll((ObjectNode) root); |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 77 | } else { |
kavitha Alagesan | 1f1fef7 | 2016-12-08 17:07:12 +0530 | [diff] [blame] | 78 | SubjectFactory subjectFactory = nullIsIllegal(service.getSubjectFactory(subjectClassKey), |
| 79 | subjectClassKey + E_CLASSKEY_NOT_REGISTERED); |
Thomas Vachuska | ea5adc6 | 2015-10-07 11:52:30 -0700 | [diff] [blame] | 80 | if (isNullOrEmpty(subjectKey)) { |
Thomas Vachuska | 0a400ea | 2015-09-04 11:25:03 -0700 | [diff] [blame] | 81 | addSubjectClass((ObjectNode) root, subjectFactory); |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 82 | } else { |
Thomas Vachuska | ea5adc6 | 2015-10-07 11:52:30 -0700 | [diff] [blame] | 83 | Object s = subjectFactory.createSubject(subjectKey); |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 84 | if (isNullOrEmpty(configKey)) { |
Yuta HIGUCHI | 5d025c3 | 2017-03-13 20:32:48 -0700 | [diff] [blame] | 85 | if (remove) { |
| 86 | service.removeConfig(s); |
| 87 | } |
Thomas Vachuska | 0a400ea | 2015-09-04 11:25:03 -0700 | [diff] [blame] | 88 | addSubject((ObjectNode) root, s); |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 89 | } else { |
Yuta HIGUCHI | 5d025c3 | 2017-03-13 20:32:48 -0700 | [diff] [blame] | 90 | if (remove) { |
| 91 | service.removeConfig(subjectClassKey, s, configKey); |
| 92 | } |
Thomas Vachuska | ea5adc6 | 2015-10-07 11:52:30 -0700 | [diff] [blame] | 93 | root = getSubjectConfig(getConfig(s, subjectClassKey, configKey)); |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 94 | } |
| 95 | } |
| 96 | } |
Jonathan Hart | 2ddc411 | 2015-09-16 09:28:07 -0700 | [diff] [blame] | 97 | |
| 98 | try { |
| 99 | print("%s", mapper.writerWithDefaultPrettyPrinter().writeValueAsString(root)); |
| 100 | } catch (JsonProcessingException e) { |
Ray Milkey | 2b4958a | 2018-02-06 18:59:06 -0800 | [diff] [blame] | 101 | throw new IllegalStateException("Error writing JSON to string", e); |
Jonathan Hart | 2ddc411 | 2015-09-16 09:28:07 -0700 | [diff] [blame] | 102 | } |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | @SuppressWarnings("unchecked") |
| 106 | private void addAll(ObjectNode root) { |
| 107 | service.getSubjectClasses() |
| 108 | .forEach(sc -> { |
Jonathan Hart | 2ddc411 | 2015-09-16 09:28:07 -0700 | [diff] [blame] | 109 | SubjectFactory sf = service.getSubjectFactory(sc); |
Thomas Vachuska | ea5adc6 | 2015-10-07 11:52:30 -0700 | [diff] [blame] | 110 | addSubjectClass(newObject(root, sf.subjectClassKey()), sf); |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 111 | }); |
| 112 | } |
| 113 | |
| 114 | @SuppressWarnings("unchecked") |
| 115 | private void addSubjectClass(ObjectNode root, SubjectFactory sf) { |
| 116 | service.getSubjects(sf.subjectClass()) |
Thomas Vachuska | ea5adc6 | 2015-10-07 11:52:30 -0700 | [diff] [blame] | 117 | .forEach(s -> addSubject(newObject(root, sf.subjectKey(s)), s)); |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | private void addSubject(ObjectNode root, Object s) { |
Thomas Vachuska | b129fbf | 2015-07-23 00:49:03 -0700 | [diff] [blame] | 121 | service.getConfigs(s).forEach(c -> root.set(c.key(), c.node())); |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 122 | } |
| 123 | |
Thomas Vachuska | 0a400ea | 2015-09-04 11:25:03 -0700 | [diff] [blame] | 124 | private JsonNode getSubjectConfig(Config config) { |
Thomas Vachuska | b129fbf | 2015-07-23 00:49:03 -0700 | [diff] [blame] | 125 | return config != null ? config.node() : null; |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 126 | } |
| 127 | |
Jonathan Hart | 111b42b | 2015-07-14 13:28:05 -0700 | [diff] [blame] | 128 | private Config getConfig(Object s, String subjectKey, String ck) { |
| 129 | Class<? extends Config> configClass = service.getConfigClass(subjectKey, ck); |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 130 | return configClass != null ? service.getConfig(s, configClass) : null; |
| 131 | } |
| 132 | |
| 133 | private ObjectNode newObject(ObjectNode parent, String key) { |
| 134 | ObjectNode node = mapper.createObjectNode(); |
| 135 | parent.set(key, node); |
| 136 | return node; |
| 137 | } |
| 138 | } |