Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Open Networking Laboratory |
| 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; |
| 24 | import org.onosproject.cli.AbstractShellCommand; |
Ray Milkey | a412236 | 2015-08-18 15:19:08 -0700 | [diff] [blame] | 25 | import org.onosproject.net.config.Config; |
| 26 | import org.onosproject.net.config.NetworkConfigService; |
| 27 | import org.onosproject.net.config.SubjectFactory; |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 28 | |
| 29 | import static com.google.common.base.Strings.isNullOrEmpty; |
| 30 | |
| 31 | /** |
| 32 | * Manages network configuration. |
| 33 | */ |
| 34 | @Command(scope = "onos", name = "netcfg", |
| 35 | description = "Manages network configuration") |
| 36 | public class NetworkConfigCommand extends AbstractShellCommand { |
| 37 | |
Thomas Vachuska | ea5adc6 | 2015-10-07 11:52:30 -0700 | [diff] [blame] | 38 | @Argument(index = 0, name = "subjectClassKey", description = "Subject class key", |
| 39 | required = false, multiValued = false) |
| 40 | String subjectClassKey = null; |
| 41 | |
| 42 | @Argument(index = 1, name = "subjectKey", description = "Subject key", |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 43 | required = false, multiValued = false) |
| 44 | String subjectKey = null; |
| 45 | |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 46 | @Argument(index = 2, name = "configKey", description = "Config key", |
| 47 | required = false, multiValued = false) |
| 48 | String configKey = null; |
| 49 | |
| 50 | private final ObjectMapper mapper = new ObjectMapper(); |
| 51 | private NetworkConfigService service; |
| 52 | |
| 53 | @Override |
| 54 | protected void execute() { |
| 55 | service = get(NetworkConfigService.class); |
Jonathan Hart | 2ddc411 | 2015-09-16 09:28:07 -0700 | [diff] [blame] | 56 | JsonNode root = mapper.createObjectNode(); |
Thomas Vachuska | ea5adc6 | 2015-10-07 11:52:30 -0700 | [diff] [blame] | 57 | if (isNullOrEmpty(subjectClassKey)) { |
Thomas Vachuska | 0a400ea | 2015-09-04 11:25:03 -0700 | [diff] [blame] | 58 | addAll((ObjectNode) root); |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 59 | } else { |
Thomas Vachuska | ea5adc6 | 2015-10-07 11:52:30 -0700 | [diff] [blame] | 60 | SubjectFactory subjectFactory = service.getSubjectFactory(subjectClassKey); |
| 61 | if (isNullOrEmpty(subjectKey)) { |
Thomas Vachuska | 0a400ea | 2015-09-04 11:25:03 -0700 | [diff] [blame] | 62 | addSubjectClass((ObjectNode) root, subjectFactory); |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 63 | } else { |
Thomas Vachuska | ea5adc6 | 2015-10-07 11:52:30 -0700 | [diff] [blame] | 64 | Object s = subjectFactory.createSubject(subjectKey); |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 65 | if (isNullOrEmpty(configKey)) { |
Thomas Vachuska | 0a400ea | 2015-09-04 11:25:03 -0700 | [diff] [blame] | 66 | addSubject((ObjectNode) root, s); |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 67 | } else { |
Thomas Vachuska | ea5adc6 | 2015-10-07 11:52:30 -0700 | [diff] [blame] | 68 | root = getSubjectConfig(getConfig(s, subjectClassKey, configKey)); |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 69 | } |
| 70 | } |
| 71 | } |
Jonathan Hart | 2ddc411 | 2015-09-16 09:28:07 -0700 | [diff] [blame] | 72 | |
| 73 | try { |
| 74 | print("%s", mapper.writerWithDefaultPrettyPrinter().writeValueAsString(root)); |
| 75 | } catch (JsonProcessingException e) { |
| 76 | throw new RuntimeException("Error writing JSON to string", e); |
| 77 | } |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | @SuppressWarnings("unchecked") |
| 81 | private void addAll(ObjectNode root) { |
| 82 | service.getSubjectClasses() |
| 83 | .forEach(sc -> { |
Jonathan Hart | 2ddc411 | 2015-09-16 09:28:07 -0700 | [diff] [blame] | 84 | SubjectFactory sf = service.getSubjectFactory(sc); |
Thomas Vachuska | ea5adc6 | 2015-10-07 11:52:30 -0700 | [diff] [blame] | 85 | addSubjectClass(newObject(root, sf.subjectClassKey()), sf); |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 86 | }); |
| 87 | } |
| 88 | |
| 89 | @SuppressWarnings("unchecked") |
| 90 | private void addSubjectClass(ObjectNode root, SubjectFactory sf) { |
| 91 | service.getSubjects(sf.subjectClass()) |
Thomas Vachuska | ea5adc6 | 2015-10-07 11:52:30 -0700 | [diff] [blame] | 92 | .forEach(s -> addSubject(newObject(root, sf.subjectKey(s)), s)); |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | private void addSubject(ObjectNode root, Object s) { |
Thomas Vachuska | b129fbf | 2015-07-23 00:49:03 -0700 | [diff] [blame] | 96 | service.getConfigs(s).forEach(c -> root.set(c.key(), c.node())); |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 97 | } |
| 98 | |
Thomas Vachuska | 0a400ea | 2015-09-04 11:25:03 -0700 | [diff] [blame] | 99 | private JsonNode getSubjectConfig(Config config) { |
Thomas Vachuska | b129fbf | 2015-07-23 00:49:03 -0700 | [diff] [blame] | 100 | return config != null ? config.node() : null; |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 101 | } |
| 102 | |
Jonathan Hart | 111b42b | 2015-07-14 13:28:05 -0700 | [diff] [blame] | 103 | private Config getConfig(Object s, String subjectKey, String ck) { |
| 104 | Class<? extends Config> configClass = service.getConfigClass(subjectKey, ck); |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 105 | return configClass != null ? service.getConfig(s, configClass) : null; |
| 106 | } |
| 107 | |
| 108 | private ObjectNode newObject(ObjectNode parent, String key) { |
| 109 | ObjectNode node = mapper.createObjectNode(); |
| 110 | parent.set(key, node); |
| 111 | return node; |
| 112 | } |
| 113 | } |