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