X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-rest-docgen%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fsal%2Frest%2Fdoc%2Fimpl%2FModelGenerator.java;h=8bac0d211e71cb2d88969c72d9b3f21a9fb00919;hb=refs%2Fchanges%2F90%2F7790%2F3;hp=0e929afc8461c5da04bafa6dc44e7c782f83417d;hpb=6c5efc6eed65b8a351edeaa525f36de1edb77a3d;p=controller.git diff --git a/opendaylight/md-sal/sal-rest-docgen/src/main/java/org/opendaylight/controller/sal/rest/doc/impl/ModelGenerator.java b/opendaylight/md-sal/sal-rest-docgen/src/main/java/org/opendaylight/controller/sal/rest/doc/impl/ModelGenerator.java index 0e929afc84..8bac0d211e 100644 --- a/opendaylight/md-sal/sal-rest-docgen/src/main/java/org/opendaylight/controller/sal/rest/doc/impl/ModelGenerator.java +++ b/opendaylight/md-sal/sal-rest-docgen/src/main/java/org/opendaylight/controller/sal/rest/doc/impl/ModelGenerator.java @@ -14,7 +14,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Set; - +import org.apache.commons.lang3.BooleanUtils; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; @@ -25,6 +25,7 @@ import org.opendaylight.yangtools.yang.model.api.ChoiceNode; import org.opendaylight.yangtools.yang.model.api.ConstraintDefinition; import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode; import org.opendaylight.yangtools.yang.model.api.DataSchemaNode; +import org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode; import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode; import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode; import org.opendaylight.yangtools.yang.model.api.ListSchemaNode; @@ -85,6 +86,8 @@ public class ModelGenerator { private static final String NUMBER = "number"; private static final String BOOLEAN = "boolean"; private static final String STRING = "string"; + private static final String ID_KEY = "id"; + private static final String SUB_TYPES_KEY = "subTypes"; private static final Map>, String> YANG_TYPE_TO_JSON_TYPE_MAPPING; @@ -115,6 +118,7 @@ public class ModelGenerator { JSONObject models = new JSONObject(); processContainers(module, models); processRPCs(module, models); + processIdentities(module, models); return models; } @@ -187,6 +191,58 @@ public class ModelGenerator { } } + /** + * Processes the 'identity' statement in a yang model + * and maps it to a 'model' in the Swagger JSON spec. + * + * @param module The module from which the identity stmt will be processed + * @param models The JSONObject in which the parsed identity will be put as a 'model' obj + * @throws JSONException + */ + private void processIdentities(Module module, JSONObject models) throws JSONException { + + String moduleName = module.getName(); + Set idNodes = module.getIdentities(); + _logger.debug("Processing Identities for module {} . Found {} identity statements", moduleName, idNodes.size()); + + for(IdentitySchemaNode idNode : idNodes){ + JSONObject identityObj=new JSONObject(); + String identityName = idNode.getQName().getLocalName(); + _logger.debug("Processing Identity: {}", identityName); + + identityObj.put(ID_KEY, identityName); + identityObj.put(DESCRIPTION_KEY, idNode.getDescription()); + + JSONObject props = new JSONObject(); + IdentitySchemaNode baseId = idNode.getBaseIdentity(); + + + if(baseId==null) { + /** + * This is a base identity. So lets see if + * it has sub types. If it does, then add them to the model definition. + */ + Set derivedIds = idNode.getDerivedIdentities(); + + if(derivedIds != null) { + JSONArray subTypes = new JSONArray(); + for(IdentitySchemaNode derivedId : derivedIds){ + subTypes.put(derivedId.getQName().getLocalName()); + } + identityObj.put(SUB_TYPES_KEY, subTypes); + } + } else { + /** + * This is a derived entity. Add it's base type & move on. + */ + props.put(TYPE_KEY, baseId.getQName().getLocalName()); + } + + //Add the properties. For a base type, this will be an empty object as required by the Swagger spec. + identityObj.put(PROPERTIES_KEY, props); + models.put(identityName, identityObj); + } + } /** * Processes the container node and populates the moduleJSON * @@ -249,7 +305,7 @@ public class ModelGenerator { if (node instanceof LeafSchemaNode) { property = processLeafNode((LeafSchemaNode) node); } else if (node instanceof ListSchemaNode) { - property = processListSchemaNode((ListSchemaNode) node, moduleName, models); + property = processListSchemaNode((ListSchemaNode) node, moduleName, models, isConfig); } else if (node instanceof LeafListSchemaNode) { property = processLeafListNode((LeafListSchemaNode) node); @@ -354,15 +410,17 @@ public class ModelGenerator { * * @param listNode * @param moduleName + * @param isConfig * @return * @throws JSONException * @throws IOException */ private JSONObject processListSchemaNode(ListSchemaNode listNode, String moduleName, - JSONObject models) throws JSONException, IOException { + JSONObject models, Boolean isConfig) throws JSONException, IOException { Set listChildren = listNode.getChildNodes(); - String fileName = listNode.getQName().getLocalName(); + String fileName = (BooleanUtils.isNotFalse(isConfig)?OperationBuilder.CONFIG:OperationBuilder.OPERATIONAL) + + listNode.getQName().getLocalName(); JSONObject childSchemaProperties = processChildren(listChildren, moduleName, models); JSONObject childSchema = getSchemaTemplate(); @@ -443,7 +501,7 @@ public class ModelGenerator { processUnionType((UnionTypeDefinition) leafTypeDef, property); } else if (leafTypeDef instanceof IdentityrefTypeDefinition) { - property.putOpt(TYPE_KEY, "object"); + property.putOpt(TYPE_KEY, ((IdentityrefTypeDefinition) leafTypeDef).getIdentity().getQName().getLocalName()); } else if (leafTypeDef instanceof BinaryTypeDefinition) { processBinaryType((BinaryTypeDefinition) leafTypeDef, property); } else { @@ -541,12 +599,15 @@ public class ModelGenerator { private void processUnionType(UnionTypeDefinition unionType, JSONObject property) throws JSONException { - List> unionTypes = unionType.getTypes(); - JSONArray unionArray = new JSONArray(); - for (TypeDefinition typeDef : unionTypes) { - unionArray.put(YANG_TYPE_TO_JSON_TYPE_MAPPING.get(typeDef.getClass())); + StringBuilder type = new StringBuilder(); + for (TypeDefinition typeDef : unionType.getTypes() ) { + if( type.length() > 0 ){ + type.append( " or " ); + } + type.append(YANG_TYPE_TO_JSON_TYPE_MAPPING.get(typeDef.getClass())); } - property.put(TYPE_KEY, unionArray); + + property.put(TYPE_KEY, type ); } /**