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