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