Thomas Vachuska | 3031198 | 2017-05-26 11:11:38 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Thomas Vachuska | 3031198 | 2017-05-26 11:11:38 -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 | */ |
Thomas Vachuska | ad37e37 | 2017-08-03 12:07:01 -0700 | [diff] [blame] | 16 | package org.onosproject.yang.impl; |
Thomas Vachuska | 3031198 | 2017-05-26 11:11:38 -0700 | [diff] [blame] | 17 | |
| 18 | import com.fasterxml.jackson.databind.JsonNode; |
| 19 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 20 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 21 | import com.fasterxml.jackson.databind.node.ObjectNode; |
Ray Milkey | 86ad7bb | 2018-09-27 12:32:28 -0700 | [diff] [blame] | 22 | import org.apache.karaf.shell.api.action.Command; |
Ray Milkey | 7a2dee5 | 2018-09-28 10:58:28 -0700 | [diff] [blame^] | 23 | import org.apache.karaf.shell.api.action.lifecycle.Service; |
Thomas Vachuska | 3031198 | 2017-05-26 11:11:38 -0700 | [diff] [blame] | 24 | import org.onosproject.cli.AbstractShellCommand; |
| 25 | import org.onosproject.yang.model.YangModel; |
| 26 | import org.onosproject.yang.model.YangModule; |
| 27 | import org.onosproject.yang.runtime.YangModelRegistry; |
| 28 | |
| 29 | /** |
| 30 | * Lists registered YANG models. |
| 31 | */ |
Ray Milkey | 7a2dee5 | 2018-09-28 10:58:28 -0700 | [diff] [blame^] | 32 | @Service |
Thomas Vachuska | 3031198 | 2017-05-26 11:11:38 -0700 | [diff] [blame] | 33 | @Command(scope = "onos", name = "models", |
Thomas Vachuska | ad37e37 | 2017-08-03 12:07:01 -0700 | [diff] [blame] | 34 | description = "Lists registered YANG models") |
Thomas Vachuska | 3031198 | 2017-05-26 11:11:38 -0700 | [diff] [blame] | 35 | public class YangModelsListCommand extends AbstractShellCommand { |
| 36 | |
| 37 | |
Thomas Vachuska | ad37e37 | 2017-08-03 12:07:01 -0700 | [diff] [blame] | 38 | private static final String FORMAT = "modelId=%s moduleName=%s moduleRevision=%s"; |
Thomas Vachuska | 3031198 | 2017-05-26 11:11:38 -0700 | [diff] [blame] | 39 | private static final String MODULE_NAME = "moduleName"; |
| 40 | private static final String MODULE_REVISION = "moduleRevision"; |
| 41 | |
| 42 | @Override |
Ray Milkey | 86ad7bb | 2018-09-27 12:32:28 -0700 | [diff] [blame] | 43 | protected void doExecute() { |
Thomas Vachuska | 3031198 | 2017-05-26 11:11:38 -0700 | [diff] [blame] | 44 | YangModelRegistry service = get(YangModelRegistry.class); |
| 45 | |
| 46 | if (outputJson()) { |
| 47 | print("%s", json(service)); |
| 48 | } else { |
Thomas Vachuska | ad37e37 | 2017-08-03 12:07:01 -0700 | [diff] [blame] | 49 | for (YangModel model : service.getModels()) { |
Thomas Vachuska | 3031198 | 2017-05-26 11:11:38 -0700 | [diff] [blame] | 50 | for (YangModule module : model.getYangModules()) { |
Thomas Vachuska | ad37e37 | 2017-08-03 12:07:01 -0700 | [diff] [blame] | 51 | print(FORMAT, model.getYangModelId(), |
| 52 | module.getYangModuleId().moduleName(), |
| 53 | module.getYangModuleId().revision()); |
Thomas Vachuska | 3031198 | 2017-05-26 11:11:38 -0700 | [diff] [blame] | 54 | } |
| 55 | } |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | // Produces JSON structure. |
| 60 | private JsonNode json(YangModelRegistry service) { |
| 61 | ObjectMapper mapper = new ObjectMapper(); |
| 62 | ObjectNode result = mapper.createObjectNode(); |
Thomas Vachuska | ad37e37 | 2017-08-03 12:07:01 -0700 | [diff] [blame] | 63 | for (YangModel model : service.getModels()) { |
Thomas Vachuska | 3031198 | 2017-05-26 11:11:38 -0700 | [diff] [blame] | 64 | ArrayNode modelNode = mapper.createArrayNode(); |
Thomas Vachuska | ad37e37 | 2017-08-03 12:07:01 -0700 | [diff] [blame] | 65 | result.set(model.getYangModelId(), modelNode); |
Thomas Vachuska | 3031198 | 2017-05-26 11:11:38 -0700 | [diff] [blame] | 66 | for (YangModule module : model.getYangModules()) { |
| 67 | ObjectNode moduleNode = mapper.createObjectNode(); |
| 68 | modelNode.add(moduleNode); |
| 69 | moduleNode.put(MODULE_NAME, module.getYangModuleId().moduleName()); |
| 70 | moduleNode.put(MODULE_REVISION, module.getYangModuleId().revision()); |
| 71 | } |
| 72 | } |
| 73 | return result; |
| 74 | } |
| 75 | |
Thomas Vachuska | 3031198 | 2017-05-26 11:11:38 -0700 | [diff] [blame] | 76 | } |