package org.opendaylight.netconf.sal.rest.doc.impl;
import static org.opendaylight.netconf.sal.rest.doc.util.RestDocgenUtil.resolveNodesName;
+
import com.google.common.base.Preconditions;
import java.io.IOException;
import java.util.ArrayList;
import org.opendaylight.yangtools.yang.model.api.type.StringTypeDefinition;
import org.opendaylight.yangtools.yang.model.api.type.UnionTypeDefinition;
import org.opendaylight.yangtools.yang.model.api.type.UnsignedIntegerTypeDefinition;
-import org.opendaylight.yangtools.yang.model.util.ExtendedType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
- * Generates JSON Schema for data defined in Yang
+ * Generates JSON Schema for data defined in YANG.
*/
@NotThreadSafe
public class ModelGenerator {
}
private void processModules(final Module module, final JSONObject models) throws JSONException {
- createConcreteModelForPost(models, module.getName()+ BaseYangSwaggerGenerator.MODULE_NAME_SUFFIX, createPropertiesForPost(module));
+ createConcreteModelForPost(models, module.getName() + BaseYangSwaggerGenerator.MODULE_NAME_SUFFIX, createPropertiesForPost(module));
}
private void processContainersAndLists(final Module module, final JSONObject models, final SchemaContext schemaContext)
* 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 static void processIdentities(final Module module, final JSONObject models) throws JSONException {
}
/**
- * Processes the container and list nodes and populates the moduleJSON
- *
- * @param container
- * @param moduleName
- * @param isConfig
- * @throws JSONException
- * @throws IOException
+ * Processes the container and list nodes and populates the moduleJSON.
*/
private JSONObject processDataNodeContainer(final DataNodeContainer dataNode, final String moduleName, final JSONObject models,
final SchemaContext schemaContext) throws JSONException, IOException {
property.put(TYPE_KEY, childNode instanceof ListSchemaNode ? ARRAY_TYPE : OBJECT_TYPE);
property.put(ITEMS_KEY, items);
properties.put(childNode.getQName().getLocalName(), property);
- } else if (childNode instanceof LeafSchemaNode){
+ } else if (childNode instanceof LeafSchemaNode) {
JSONObject property = processLeafNode((LeafSchemaNode)childNode);
properties.put(childNode.getQName().getLocalName(), property);
}
}
/**
- * Processes the nodes
- *
- * @param nodes
- * @param parentQName
- * @param moduleName
- * @param isConfig
- * @return
- * @throws JSONException
- * @throws IOException
+ * Processes the nodes.
*/
- private JSONObject processChildren(final Iterable<DataSchemaNode> nodes, final QName parentQName, final String moduleName,
- final JSONObject models, final boolean isConfig, final SchemaContext schemaContext) throws JSONException, IOException {
+ private JSONObject processChildren(final Iterable<DataSchemaNode> nodes, final QName parentQName,
+ final String moduleName, final JSONObject models, final boolean isConfig, final SchemaContext schemaContext)
+ throws JSONException, IOException {
JSONObject properties = new JSONObject();
return properties;
}
- /**
- *
- * @param listNode
- * @throws JSONException
- */
private JSONObject processLeafListNode(final LeafListSchemaNode listNode) throws JSONException {
JSONObject props = new JSONObject();
props.put(TYPE_KEY, ARRAY_TYPE);
return props;
}
- /**
- *
- * @param choiceNode
- * @param moduleName
- * @throws JSONException
- * @throws IOException
- */
private JSONObject processChoiceNode(final ChoiceSchemaNode choiceNode, final String moduleName, final JSONObject models,
final SchemaContext schemaContext) throws JSONException, IOException {
return oneOfProps;
}
- /**
- *
- * @param constraints
- * @param props
- * @throws JSONException
- */
private static void processConstraints(final ConstraintDefinition constraints, final JSONObject props) throws JSONException {
boolean isMandatory = constraints.isMandatory();
props.put(REQUIRED_KEY, isMandatory);
}
}
- /**
- *
- * @param leafNode
- * @return
- * @throws JSONException
- */
private JSONObject processLeafNode(final LeafSchemaNode leafNode) throws JSONException {
JSONObject property = new JSONObject();
return property;
}
- /**
- *
- * @param leafNode
- * @return
- * @throws JSONException
- */
private static JSONObject processAnyXMLNode(final AnyXmlSchemaNode leafNode) throws JSONException {
JSONObject property = new JSONObject();
return property;
}
- /**
- * @param property
- * @throws JSONException
- */
private void processTypeDef(final TypeDefinition<?> leafTypeDef, final JSONObject property) throws JSONException {
-
- if (leafTypeDef instanceof ExtendedType) {
- processExtendedType(leafTypeDef, property);
- } else if (leafTypeDef instanceof BinaryTypeDefinition) {
+ if (leafTypeDef instanceof BinaryTypeDefinition) {
processBinaryType((BinaryTypeDefinition) leafTypeDef, property);
} else if (leafTypeDef instanceof BitsTypeDefinition) {
processBitsType((BitsTypeDefinition) leafTypeDef, property);
} else if (leafTypeDef instanceof EnumTypeDefinition) {
processEnumType((EnumTypeDefinition) leafTypeDef, property);
} else if (leafTypeDef instanceof IdentityrefTypeDefinition) {
- property.putOpt(TYPE_KEY, ((IdentityrefTypeDefinition) leafTypeDef).getIdentity().getQName().getLocalName());
+ property.putOpt(TYPE_KEY,
+ ((IdentityrefTypeDefinition) leafTypeDef).getIdentity().getQName().getLocalName());
} else if (leafTypeDef instanceof StringTypeDefinition) {
processStringType((StringTypeDefinition) leafTypeDef, property);
} else if (leafTypeDef instanceof UnionTypeDefinition) {
}
}
- /**
- *
- * @param leafTypeDef
- * @param property
- * @throws JSONException
- */
- private void processExtendedType(final TypeDefinition<?> leafTypeDef, final JSONObject property) throws JSONException {
- TypeDefinition<?> leafBaseType = leafTypeDef.getBaseType();
- if (leafBaseType instanceof ExtendedType) {
- // recursively process an extended type until we hit a base type
- processExtendedType(leafBaseType, property);
- } else {
- List<LengthConstraint> lengthConstraints = ((ExtendedType) leafTypeDef).getLengthConstraints();
- for (LengthConstraint lengthConstraint : lengthConstraints) {
- Number min = lengthConstraint.getMin();
- Number max = lengthConstraint.getMax();
- property.putOpt(MIN_LENGTH_KEY, min);
- property.putOpt(MAX_LENGTH_KEY, max);
- }
- String jsonType = jsonTypeFor(leafBaseType);
- property.putOpt(TYPE_KEY, jsonType);
- }
-
- }
-
private static void processBinaryType(final BinaryTypeDefinition binaryType, final JSONObject property) throws JSONException {
property.put(TYPE_KEY, STRING);
JSONObject media = new JSONObject();
property.put(MEDIA_KEY, media);
}
- /**
- *
- * @param enumLeafType
- * @param property
- * @throws JSONException
- */
private static void processEnumType(final EnumTypeDefinition enumLeafType, final JSONObject property) throws JSONException {
List<EnumPair> enumPairs = enumLeafType.getValues();
- List<String> enumNames = new ArrayList<String>();
+ List<String> enumNames = new ArrayList<>();
for (EnumPair enumPair : enumPairs) {
enumNames.add(enumPair.getName());
}
property.putOpt(ENUM, new JSONArray(enumNames));
}
- /**
- *
- * @param bitsType
- * @param property
- * @throws JSONException
- */
private static void processBitsType(final BitsTypeDefinition bitsType, final JSONObject property) throws JSONException {
property.put(TYPE_KEY, ARRAY_TYPE);
property.put(MIN_ITEMS, 0);
StringTypeDefinition type = stringType;
List<LengthConstraint> lengthConstraints = stringType.getLengthConstraints();
while (lengthConstraints.isEmpty() && type.getBaseType() != null) {
- type = type.getBaseType();
- lengthConstraints = type.getLengthConstraints();
+ type = type.getBaseType();
+ lengthConstraints = type.getLengthConstraints();
}
// FIXME: json-schema is not expressive enough to capture min/max laternatives. We should find the true minimum
property.put(TYPE_KEY, STRING);
}
- /**
- *
- * @param unionType
- * @param property
- * @throws JSONException
- */
private static void processUnionType(final UnionTypeDefinition unionType, final JSONObject property) throws JSONException {
-
StringBuilder type = new StringBuilder();
for (TypeDefinition<?> typeDef : unionType.getTypes()) {
if (type.length() > 0) {
/**
* Helper method to generate a pre-filled JSON schema object.
- *
- * @return
- * @throws JSONException
*/
private static JSONObject getSchemaTemplate() throws JSONException {
JSONObject schemaJSON = new JSONObject();