blob: 5915e882b556e3ce336be2d9de00170b716039d4 [file] [log] [blame]
Thomas Vachuska30311982017-05-26 11:11:38 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Thomas Vachuska30311982017-05-26 11:11:38 -07003 *
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 */
16package org.onosproject.yang;
17
18import com.fasterxml.jackson.databind.JsonNode;
19import com.fasterxml.jackson.databind.ObjectMapper;
20import com.fasterxml.jackson.databind.node.ArrayNode;
21import com.fasterxml.jackson.databind.node.ObjectNode;
22import org.apache.karaf.shell.commands.Command;
23import org.onosproject.cli.AbstractShellCommand;
24import org.onosproject.yang.model.YangModel;
25import org.onosproject.yang.model.YangModule;
26import org.onosproject.yang.runtime.YangModelRegistry;
27
28/**
29 * Lists registered YANG models.
30 */
31@Command(scope = "onos", name = "models",
32 description = "Lists registered YANG models")
33public class YangModelsListCommand extends AbstractShellCommand {
34
35
36 private static final String FORMAT = "moduleName=%s moduleRevision=%s modelId=%s";
37 private static final String MODULE_NAME = "moduleName";
38 private static final String MODULE_REVISION = "moduleRevision";
39
40 @Override
41 protected void execute() {
42 YangModelRegistry service = get(YangModelRegistry.class);
43
44 if (outputJson()) {
45 print("%s", json(service));
46 } else {
47 for (YangModel model: service.getModels()) {
48 for (YangModule module : model.getYangModules()) {
49 print(FORMAT, module.getYangModuleId().moduleName(),
50 module.getYangModuleId().revision(),
51 modelId(model));
52 }
53 }
54 }
55 }
56
57 // Produces JSON structure.
58 private JsonNode json(YangModelRegistry service) {
59 ObjectMapper mapper = new ObjectMapper();
60 ObjectNode result = mapper.createObjectNode();
61 for (YangModel model: service.getModels()) {
62 ArrayNode modelNode = mapper.createArrayNode();
63 result.set(modelId(model), modelNode);
64 for (YangModule module : model.getYangModules()) {
65 ObjectNode moduleNode = mapper.createObjectNode();
66 modelNode.add(moduleNode);
67 moduleNode.put(MODULE_NAME, module.getYangModuleId().moduleName());
68 moduleNode.put(MODULE_REVISION, module.getYangModuleId().revision());
69 }
70 }
71 return result;
72 }
73
74 private String modelId(YangModel m) {
75 return "YM" + Math.abs(m.hashCode());
76 }
77}