2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.controller.sal.rest.doc.impl;
10 import java.io.IOException;
11 import java.util.ArrayList;
12 import java.util.Collections;
13 import java.util.HashMap;
14 import java.util.List;
17 import org.apache.commons.lang3.BooleanUtils;
18 import org.json.JSONArray;
19 import org.json.JSONException;
20 import org.json.JSONObject;
21 import org.opendaylight.controller.sal.rest.doc.model.builder.OperationBuilder;
22 import org.opendaylight.yangtools.yang.model.api.AnyXmlSchemaNode;
23 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
24 import org.opendaylight.yangtools.yang.model.api.ChoiceNode;
25 import org.opendaylight.yangtools.yang.model.api.ConstraintDefinition;
26 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
27 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
28 import org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode;
29 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
30 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
31 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
32 import org.opendaylight.yangtools.yang.model.api.Module;
33 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
34 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
35 import org.opendaylight.yangtools.yang.model.api.type.BinaryTypeDefinition;
36 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition;
37 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition.Bit;
38 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition.EnumPair;
39 import org.opendaylight.yangtools.yang.model.api.type.IdentityrefTypeDefinition;
40 import org.opendaylight.yangtools.yang.model.api.type.LengthConstraint;
41 import org.opendaylight.yangtools.yang.model.api.type.UnionTypeDefinition;
42 import org.opendaylight.yangtools.yang.model.util.BooleanType;
43 import org.opendaylight.yangtools.yang.model.util.Decimal64;
44 import org.opendaylight.yangtools.yang.model.util.EnumerationType;
45 import org.opendaylight.yangtools.yang.model.util.ExtendedType;
46 import org.opendaylight.yangtools.yang.model.util.Int16;
47 import org.opendaylight.yangtools.yang.model.util.Int32;
48 import org.opendaylight.yangtools.yang.model.util.Int64;
49 import org.opendaylight.yangtools.yang.model.util.Int8;
50 import org.opendaylight.yangtools.yang.model.util.StringType;
51 import org.opendaylight.yangtools.yang.model.util.Uint16;
52 import org.opendaylight.yangtools.yang.model.util.Uint32;
53 import org.opendaylight.yangtools.yang.model.util.Uint64;
54 import org.opendaylight.yangtools.yang.model.util.Uint8;
55 import org.slf4j.Logger;
56 import org.slf4j.LoggerFactory;
59 * Generates JSON Schema for data defined in Yang
61 public class ModelGenerator {
63 private static Logger _logger = LoggerFactory.getLogger(ModelGenerator.class);
65 private static final String BASE_64 = "base64";
66 private static final String BINARY_ENCODING_KEY = "binaryEncoding";
67 private static final String MEDIA_KEY = "media";
68 private static final String ONE_OF_KEY = "oneOf";
69 private static final String UNIQUE_ITEMS_KEY = "uniqueItems";
70 private static final String MAX_ITEMS = "maxItems";
71 private static final String MIN_ITEMS = "minItems";
72 private static final String SCHEMA_URL = "http://json-schema.org/draft-04/schema";
73 private static final String SCHEMA_KEY = "$schema";
74 private static final String MAX_LENGTH_KEY = "maxLength";
75 private static final String MIN_LENGTH_KEY = "minLength";
76 private static final String REQUIRED_KEY = "required";
77 private static final String REF_KEY = "$ref";
78 private static final String ITEMS_KEY = "items";
79 private static final String TYPE_KEY = "type";
80 private static final String PROPERTIES_KEY = "properties";
81 private static final String DESCRIPTION_KEY = "description";
82 private static final String OBJECT_TYPE = "object";
83 private static final String ARRAY_TYPE = "array";
84 private static final String ENUM = "enum";
85 private static final String INTEGER = "integer";
86 private static final String NUMBER = "number";
87 private static final String BOOLEAN = "boolean";
88 private static final String STRING = "string";
89 private static final String ID_KEY = "id";
90 private static final String SUB_TYPES_KEY = "subTypes";
92 private static final Map<Class<? extends TypeDefinition<?>>, String> YANG_TYPE_TO_JSON_TYPE_MAPPING;
95 Map<Class<? extends TypeDefinition<?>>, String> tempMap1 = new HashMap<Class<? extends TypeDefinition<?>>, String>(
97 tempMap1.put(StringType.class, STRING);
98 tempMap1.put(BooleanType.class, BOOLEAN);
99 tempMap1.put(Int8.class, INTEGER);
100 tempMap1.put(Int16.class, INTEGER);
101 tempMap1.put(Int32.class, INTEGER);
102 tempMap1.put(Int64.class, INTEGER);
103 tempMap1.put(Uint16.class, INTEGER);
104 tempMap1.put(Uint32.class, INTEGER);
105 tempMap1.put(Uint64.class, INTEGER);
106 tempMap1.put(Uint8.class, INTEGER);
107 tempMap1.put(Decimal64.class, NUMBER);
108 tempMap1.put(EnumerationType.class, ENUM);
111 YANG_TYPE_TO_JSON_TYPE_MAPPING = Collections.unmodifiableMap(tempMap1);
114 public ModelGenerator() {
117 public JSONObject convertToJsonSchema(Module module) throws IOException, JSONException {
118 JSONObject models = new JSONObject();
119 processContainers(module, models);
120 processRPCs(module, models);
121 processIdentities(module, models);
125 private void processContainers(Module module, JSONObject models) throws IOException,
128 String moduleName = module.getName();
129 Set<DataSchemaNode> childNodes = module.getChildNodes();
131 for (DataSchemaNode childNode : childNodes) {
132 JSONObject configModuleJSON = null;
133 JSONObject operationalModuleJSON = null;
135 String childNodeName = childNode.getQName().getLocalName();
137 * For every container in the module
139 if (childNode instanceof ContainerSchemaNode) {
140 configModuleJSON = processContainer((ContainerSchemaNode) childNode, moduleName,
142 operationalModuleJSON = processContainer((ContainerSchemaNode) childNode,
143 moduleName, true, models, false);
146 if (configModuleJSON != null) {
147 _logger.debug("Adding model for [{}]", OperationBuilder.CONFIG + childNodeName);
148 configModuleJSON.put("id", OperationBuilder.CONFIG + childNodeName);
149 models.put(OperationBuilder.CONFIG + childNodeName, configModuleJSON);
151 if (operationalModuleJSON != null) {
152 _logger.debug("Adding model for [{}]", OperationBuilder.OPERATIONAL + childNodeName);
153 operationalModuleJSON.put("id", OperationBuilder.OPERATIONAL + childNodeName);
154 models.put(OperationBuilder.OPERATIONAL + childNodeName, operationalModuleJSON);
161 * Process the RPCs for a Module Spits out a file each of the name
162 * <rpcName>-input.json and <rpcName>-output.json for each RPC that contains
163 * input & output elements
166 * @throws JSONException
167 * @throws IOException
169 private void processRPCs(Module module, JSONObject models) throws JSONException, IOException {
171 Set<RpcDefinition> rpcs = module.getRpcs();
172 String moduleName = module.getName();
173 for (RpcDefinition rpc : rpcs) {
175 ContainerSchemaNode input = rpc.getInput();
177 JSONObject inputJSON = processContainer(input, moduleName, true, models);
178 String filename = "(" + rpc.getQName().getLocalName() + ")input";
179 inputJSON.put("id", filename);
180 // writeToFile(filename, inputJSON.toString(2), moduleName);
181 models.put(filename, inputJSON);
184 ContainerSchemaNode output = rpc.getOutput();
185 if (output != null) {
186 JSONObject outputJSON = processContainer(output, moduleName, true, models);
187 String filename = "(" + rpc.getQName().getLocalName() + ")output";
188 outputJSON.put("id", filename);
189 models.put(filename, outputJSON);
195 * Processes the 'identity' statement in a yang model
196 * and maps it to a 'model' in the Swagger JSON spec.
198 * @param module The module from which the identity stmt will be processed
199 * @param models The JSONObject in which the parsed identity will be put as a 'model' obj
200 * @throws JSONException
202 private void processIdentities(Module module, JSONObject models) throws JSONException {
204 String moduleName = module.getName();
205 Set<IdentitySchemaNode> idNodes = module.getIdentities();
206 _logger.debug("Processing Identities for module {} . Found {} identity statements", moduleName, idNodes.size());
208 for(IdentitySchemaNode idNode : idNodes){
209 JSONObject identityObj=new JSONObject();
210 String identityName = idNode.getQName().getLocalName();
211 _logger.debug("Processing Identity: {}", identityName);
213 identityObj.put(ID_KEY, identityName);
214 identityObj.put(DESCRIPTION_KEY, idNode.getDescription());
216 JSONObject props = new JSONObject();
217 IdentitySchemaNode baseId = idNode.getBaseIdentity();
222 * This is a base identity. So lets see if
223 * it has sub types. If it does, then add them to the model definition.
225 Set<IdentitySchemaNode> derivedIds = idNode.getDerivedIdentities();
227 if(derivedIds != null) {
228 JSONArray subTypes = new JSONArray();
229 for(IdentitySchemaNode derivedId : derivedIds){
230 subTypes.put(derivedId.getQName().getLocalName());
232 identityObj.put(SUB_TYPES_KEY, subTypes);
236 * This is a derived entity. Add it's base type & move on.
238 props.put(TYPE_KEY, baseId.getQName().getLocalName());
241 //Add the properties. For a base type, this will be an empty object as required by the Swagger spec.
242 identityObj.put(PROPERTIES_KEY, props);
243 models.put(identityName, identityObj);
247 * Processes the container node and populates the moduleJSON
252 * @throws JSONException
253 * @throws IOException
255 private JSONObject processContainer(ContainerSchemaNode container, String moduleName,
256 boolean addSchemaStmt, JSONObject models) throws JSONException, IOException {
257 return processContainer(container, moduleName, addSchemaStmt, models, (Boolean) null);
260 private JSONObject processContainer(ContainerSchemaNode container, String moduleName,
261 boolean addSchemaStmt, JSONObject models, Boolean isConfig) throws JSONException,
263 JSONObject moduleJSON = getSchemaTemplate();
265 moduleJSON = getSchemaTemplate();
267 moduleJSON = new JSONObject();
269 moduleJSON.put(TYPE_KEY, OBJECT_TYPE);
271 String containerDescription = container.getDescription();
272 moduleJSON.put(DESCRIPTION_KEY, containerDescription);
274 Set<DataSchemaNode> containerChildren = container.getChildNodes();
275 JSONObject properties = processChildren(containerChildren, moduleName, models, isConfig);
276 moduleJSON.put(PROPERTIES_KEY, properties);
280 private JSONObject processChildren(Set<DataSchemaNode> nodes, String moduleName,
281 JSONObject models) throws JSONException, IOException {
282 return processChildren(nodes, moduleName, models, null);
286 * Processes the nodes
292 * @throws JSONException
293 * @throws IOException
295 private JSONObject processChildren(Set<DataSchemaNode> nodes, String moduleName,
296 JSONObject models, Boolean isConfig) throws JSONException, IOException {
298 JSONObject properties = new JSONObject();
300 for (DataSchemaNode node : nodes) {
301 if (isConfig == null || node.isConfiguration() == isConfig) {
303 String name = node.getQName().getLocalName();
304 JSONObject property = null;
305 if (node instanceof LeafSchemaNode) {
306 property = processLeafNode((LeafSchemaNode) node);
307 } else if (node instanceof ListSchemaNode) {
308 property = processListSchemaNode((ListSchemaNode) node, moduleName, models, isConfig);
310 } else if (node instanceof LeafListSchemaNode) {
311 property = processLeafListNode((LeafListSchemaNode) node);
313 } else if (node instanceof ChoiceNode) {
314 property = processChoiceNode((ChoiceNode) node, moduleName, models);
316 } else if (node instanceof AnyXmlSchemaNode) {
317 property = processAnyXMLNode((AnyXmlSchemaNode) node);
319 } else if (node instanceof ContainerSchemaNode) {
320 property = processContainer((ContainerSchemaNode) node, moduleName, false,
324 throw new IllegalArgumentException("Unknown DataSchemaNode type: "
328 property.putOpt(DESCRIPTION_KEY, node.getDescription());
329 properties.put(name, property);
338 * @throws JSONException
340 private JSONObject processLeafListNode(LeafListSchemaNode listNode) throws JSONException {
341 JSONObject props = new JSONObject();
342 props.put(TYPE_KEY, ARRAY_TYPE);
344 JSONObject itemsVal = new JSONObject();
345 processTypeDef(listNode.getType(), itemsVal);
346 props.put(ITEMS_KEY, itemsVal);
348 ConstraintDefinition constraints = listNode.getConstraints();
349 processConstraints(constraints, props);
358 * @throws JSONException
359 * @throws IOException
361 private JSONObject processChoiceNode(ChoiceNode choiceNode, String moduleName, JSONObject models)
362 throws JSONException, IOException {
364 Set<ChoiceCaseNode> cases = choiceNode.getCases();
366 JSONArray choiceProps = new JSONArray();
367 for (ChoiceCaseNode choiceCase : cases) {
368 String choiceName = choiceCase.getQName().getLocalName();
369 JSONObject choiceProp = processChildren(choiceCase.getChildNodes(), moduleName, models);
370 JSONObject choiceObj = new JSONObject();
371 choiceObj.put(choiceName, choiceProp);
372 choiceObj.put(TYPE_KEY, OBJECT_TYPE);
373 choiceProps.put(choiceObj);
376 JSONObject oneOfProps = new JSONObject();
377 oneOfProps.put(ONE_OF_KEY, choiceProps);
378 oneOfProps.put(TYPE_KEY, OBJECT_TYPE);
387 * @throws JSONException
389 private void processConstraints(ConstraintDefinition constraints, JSONObject props)
390 throws JSONException {
391 boolean isMandatory = constraints.isMandatory();
392 props.put(REQUIRED_KEY, isMandatory);
394 Integer minElements = constraints.getMinElements();
395 Integer maxElements = constraints.getMaxElements();
396 if (minElements != null) {
397 props.put(MIN_ITEMS, minElements);
399 if (maxElements != null) {
400 props.put(MAX_ITEMS, maxElements);
405 * Parses a ListSchema node.
407 * Due to a limitation of the RAML--->JAX-RS tool, sub-properties must be in
408 * a separate JSON schema file. Hence, we have to write some properties to a
409 * new file, while continuing to process the rest.
415 * @throws JSONException
416 * @throws IOException
418 private JSONObject processListSchemaNode(ListSchemaNode listNode, String moduleName,
419 JSONObject models, Boolean isConfig) throws JSONException, IOException {
421 Set<DataSchemaNode> listChildren = listNode.getChildNodes();
422 String fileName = (BooleanUtils.isNotFalse(isConfig)?OperationBuilder.CONFIG:OperationBuilder.OPERATIONAL) +
423 listNode.getQName().getLocalName();
425 JSONObject childSchemaProperties = processChildren(listChildren, moduleName, models);
426 JSONObject childSchema = getSchemaTemplate();
427 childSchema.put(TYPE_KEY, OBJECT_TYPE);
428 childSchema.put(PROPERTIES_KEY, childSchemaProperties);
431 * Due to a limitation of the RAML--->JAX-RS tool, sub-properties must
432 * be in a separate JSON schema file. Hence, we have to write some
433 * properties to a new file, while continuing to process the rest.
435 // writeToFile(fileName, childSchema.toString(2), moduleName);
436 childSchema.put("id", fileName);
437 models.put(fileName, childSchema);
439 JSONObject listNodeProperties = new JSONObject();
440 listNodeProperties.put(TYPE_KEY, ARRAY_TYPE);
442 JSONObject items = new JSONObject();
443 items.put(REF_KEY, fileName);
444 listNodeProperties.put(ITEMS_KEY, items);
446 return listNodeProperties;
454 * @throws JSONException
456 private JSONObject processLeafNode(LeafSchemaNode leafNode) throws JSONException {
457 JSONObject property = new JSONObject();
459 String leafDescription = leafNode.getDescription();
460 property.put(DESCRIPTION_KEY, leafDescription);
462 processConstraints(leafNode.getConstraints(), property);
463 processTypeDef(leafNode.getType(), property);
472 * @throws JSONException
474 private JSONObject processAnyXMLNode(AnyXmlSchemaNode leafNode) throws JSONException {
475 JSONObject property = new JSONObject();
477 String leafDescription = leafNode.getDescription();
478 property.put(DESCRIPTION_KEY, leafDescription);
480 processConstraints(leafNode.getConstraints(), property);
487 * @throws JSONException
489 private void processTypeDef(TypeDefinition<?> leafTypeDef, JSONObject property)
490 throws JSONException {
492 if (leafTypeDef instanceof ExtendedType) {
493 processExtendedType(leafTypeDef, property);
494 } else if (leafTypeDef instanceof EnumerationType) {
495 processEnumType((EnumerationType) leafTypeDef, property);
497 } else if (leafTypeDef instanceof BitsTypeDefinition) {
498 processBitsType((BitsTypeDefinition) leafTypeDef, property);
500 } else if (leafTypeDef instanceof UnionTypeDefinition) {
501 processUnionType((UnionTypeDefinition) leafTypeDef, property);
503 } else if (leafTypeDef instanceof IdentityrefTypeDefinition) {
504 property.putOpt(TYPE_KEY, ((IdentityrefTypeDefinition) leafTypeDef).getIdentity().getQName().getLocalName());
505 } else if (leafTypeDef instanceof BinaryTypeDefinition) {
506 processBinaryType((BinaryTypeDefinition) leafTypeDef, property);
508 // System.out.println("In else: " + leafTypeDef.getClass());
509 String jsonType = YANG_TYPE_TO_JSON_TYPE_MAPPING.get(leafTypeDef.getClass());
510 if (jsonType == null) {
513 property.putOpt(TYPE_KEY, jsonType);
521 * @throws JSONException
523 private void processExtendedType(TypeDefinition<?> leafTypeDef, JSONObject property)
524 throws JSONException {
525 Object leafBaseType = leafTypeDef.getBaseType();
526 if (leafBaseType instanceof ExtendedType) {
527 // recursively process an extended type until we hit a base type
528 processExtendedType((TypeDefinition<?>) leafBaseType, property);
530 List<LengthConstraint> lengthConstraints = ((ExtendedType) leafTypeDef)
531 .getLengthConstraints();
532 for (LengthConstraint lengthConstraint : lengthConstraints) {
533 Number min = lengthConstraint.getMin();
534 Number max = lengthConstraint.getMax();
535 property.putOpt(MIN_LENGTH_KEY, min);
536 property.putOpt(MAX_LENGTH_KEY, max);
538 String jsonType = YANG_TYPE_TO_JSON_TYPE_MAPPING.get(leafBaseType.getClass());
539 property.putOpt(TYPE_KEY, jsonType);
547 private void processBinaryType(BinaryTypeDefinition binaryType, JSONObject property)
548 throws JSONException {
549 property.put(TYPE_KEY, STRING);
550 JSONObject media = new JSONObject();
551 media.put(BINARY_ENCODING_KEY, BASE_64);
552 property.put(MEDIA_KEY, media);
557 * @param enumLeafType
559 * @throws JSONException
561 private void processEnumType(EnumerationType enumLeafType, JSONObject property)
562 throws JSONException {
563 List<EnumPair> enumPairs = enumLeafType.getValues();
564 List<String> enumNames = new ArrayList<String>();
565 for (EnumPair enumPair : enumPairs) {
566 enumNames.add(enumPair.getName());
568 property.putOpt(ENUM, new JSONArray(enumNames));
575 * @throws JSONException
577 private void processBitsType(BitsTypeDefinition bitsType, JSONObject property)
578 throws JSONException {
579 property.put(TYPE_KEY, ARRAY_TYPE);
580 property.put(MIN_ITEMS, 0);
581 property.put(UNIQUE_ITEMS_KEY, true);
582 JSONArray enumValues = new JSONArray();
584 List<Bit> bits = bitsType.getBits();
585 for (Bit bit : bits) {
586 enumValues.put(bit.getName());
588 JSONObject itemsValue = new JSONObject();
589 itemsValue.put(ENUM, enumValues);
590 property.put(ITEMS_KEY, itemsValue);
597 * @throws JSONException
599 private void processUnionType(UnionTypeDefinition unionType, JSONObject property)
600 throws JSONException {
602 StringBuilder type = new StringBuilder();
603 for (TypeDefinition<?> typeDef : unionType.getTypes() ) {
604 if( type.length() > 0 ){
605 type.append( " or " );
607 type.append(YANG_TYPE_TO_JSON_TYPE_MAPPING.get(typeDef.getClass()));
610 property.put(TYPE_KEY, type );
614 * Helper method to generate a pre-filled JSON schema object.
617 * @throws JSONException
619 private JSONObject getSchemaTemplate() throws JSONException {
620 JSONObject schemaJSON = new JSONObject();
621 schemaJSON.put(SCHEMA_KEY, SCHEMA_URL);