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