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.netconf.sal.rest.doc.impl;
10 import static org.opendaylight.netconf.sal.rest.doc.util.RestDocgenUtil.resolveNodesName;
12 import com.google.common.base.Optional;
13 import com.mifmif.common.regex.Generex;
14 import java.io.IOException;
16 import java.util.ArrayList;
17 import java.util.Date;
18 import java.util.List;
20 import java.util.regex.Pattern;
21 import javax.annotation.concurrent.NotThreadSafe;
22 import org.json.JSONArray;
23 import org.json.JSONException;
24 import org.json.JSONObject;
25 import org.opendaylight.netconf.sal.rest.doc.model.builder.OperationBuilder;
26 import org.opendaylight.netconf.sal.rest.doc.model.builder.OperationBuilder.Post;
27 import org.opendaylight.yangtools.yang.model.api.AnyXmlSchemaNode;
28 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
29 import org.opendaylight.yangtools.yang.model.api.ConstraintDefinition;
30 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
31 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
32 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
33 import org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode;
34 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
35 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
36 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
37 import org.opendaylight.yangtools.yang.model.api.Module;
38 import org.opendaylight.yangtools.yang.model.api.RevisionAwareXPath;
39 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
40 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
41 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
42 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
43 import org.opendaylight.yangtools.yang.model.api.TypedSchemaNode;
44 import org.opendaylight.yangtools.yang.model.api.type.BinaryTypeDefinition;
45 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition;
46 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition.Bit;
47 import org.opendaylight.yangtools.yang.model.api.type.BooleanTypeDefinition;
48 import org.opendaylight.yangtools.yang.model.api.type.DecimalTypeDefinition;
49 import org.opendaylight.yangtools.yang.model.api.type.EmptyTypeDefinition;
50 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition;
51 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition.EnumPair;
52 import org.opendaylight.yangtools.yang.model.api.type.IdentityrefTypeDefinition;
53 import org.opendaylight.yangtools.yang.model.api.type.IntegerTypeDefinition;
54 import org.opendaylight.yangtools.yang.model.api.type.LeafrefTypeDefinition;
55 import org.opendaylight.yangtools.yang.model.api.type.LengthConstraint;
56 import org.opendaylight.yangtools.yang.model.api.type.PatternConstraint;
57 import org.opendaylight.yangtools.yang.model.api.type.StringTypeDefinition;
58 import org.opendaylight.yangtools.yang.model.api.type.UnionTypeDefinition;
59 import org.opendaylight.yangtools.yang.model.api.type.UnsignedIntegerTypeDefinition;
60 import org.opendaylight.yangtools.yang.model.util.RevisionAwareXPathImpl;
61 import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil;
62 import org.slf4j.Logger;
63 import org.slf4j.LoggerFactory;
66 * Generates JSON Schema for data defined in YANG.
69 public class ModelGenerator {
71 private static final Logger LOG = LoggerFactory.getLogger(ModelGenerator.class);
73 private static final Pattern STRIP_PATTERN = Pattern.compile("\\[[^\\[\\]]*\\]");
74 private static final String BASE_64 = "base64";
75 private static final String BINARY_ENCODING_KEY = "binaryEncoding";
76 private static final String MEDIA_KEY = "media";
77 private static final String UNIQUE_ITEMS_KEY = "uniqueItems";
78 private static final String MAX_ITEMS = "maxItems";
79 private static final String MIN_ITEMS = "minItems";
80 private static final String SCHEMA_URL = "http://json-schema.org/draft-04/schema";
81 private static final String SCHEMA_KEY = "$schema";
82 private static final String MAX_LENGTH_KEY = "maxLength";
83 private static final String MIN_LENGTH_KEY = "minLength";
84 private static final String REQUIRED_KEY = "required";
85 private static final String REF_KEY = "$ref";
86 private static final String ITEMS_KEY = "items";
87 private static final String TYPE_KEY = "type";
88 private static final String PROPERTIES_KEY = "properties";
89 private static final String DESCRIPTION_KEY = "description";
90 private static final String OBJECT_TYPE = "object";
91 private static final String ARRAY_TYPE = "array";
92 private static final String ENUM = "enum";
93 private static final String ID_KEY = "id";
94 private static final String SUB_TYPES_KEY = "subTypes";
95 private static final String UNIQUE_EMPTY_IDENTIFIER = "unique_empty_identifier";
97 private Module topLevelModule;
99 public ModelGenerator() {
103 * Creates Json models from provided module according to swagger spec
105 * @param module - Yang module to be converted
106 * @param schemaContext - SchemaContext of all Yang files used by Api Doc
107 * @return JSONObject containing data used for creating examples and models in Api Doc
108 * @throws IOException
109 * @throws JSONException
111 public JSONObject convertToJsonSchema(final Module module, final SchemaContext schemaContext) throws IOException, JSONException {
112 final JSONObject models = new JSONObject();
113 models.put(UNIQUE_EMPTY_IDENTIFIER, new JSONObject());
114 topLevelModule = module;
115 processModules(module, models, schemaContext);
116 processContainersAndLists(module, models, schemaContext);
117 processRPCs(module, models, schemaContext);
118 processIdentities(module, models);
122 private void processModules(final Module module, final JSONObject models, final SchemaContext schemaContext) throws JSONException {
123 createConcreteModelForPost(models, module.getName() + BaseYangSwaggerGenerator.MODULE_NAME_SUFFIX,
124 createPropertiesForPost(module, schemaContext, module.getName()));
127 private void processContainersAndLists(final Module module, final JSONObject models, final SchemaContext schemaContext)
128 throws IOException, JSONException {
129 final String moduleName = module.getName();
131 for (final DataSchemaNode childNode : module.getChildNodes()) {
132 // For every container and list in the module
133 if (childNode instanceof ContainerSchemaNode || childNode instanceof ListSchemaNode) {
134 processDataNodeContainer((DataNodeContainer) childNode, moduleName, models, true, schemaContext);
135 processDataNodeContainer((DataNodeContainer) childNode, moduleName, models, false, schemaContext);
141 * Process the RPCs for a Module Spits out a file each of the name <rpcName>-input.json and <rpcName>-output.json
142 * for each RPC that contains input & output elements
145 * @throws JSONException
146 * @throws IOException
148 private void processRPCs(final Module module, final JSONObject models, final SchemaContext schemaContext) throws JSONException,
150 final Set<RpcDefinition> rpcs = module.getRpcs();
151 final String moduleName = module.getName();
152 for (final RpcDefinition rpc : rpcs) {
153 final ContainerSchemaNode input = rpc.getInput();
154 if (!input.getChildNodes().isEmpty()) {
155 final JSONObject properties = processChildren(input.getChildNodes(), moduleName, models, true, schemaContext);
157 final String filename = "(" + rpc.getQName().getLocalName() + ")input";
158 final JSONObject childSchema = getSchemaTemplate();
159 childSchema.put(TYPE_KEY, OBJECT_TYPE);
160 childSchema.put(PROPERTIES_KEY, properties);
161 childSchema.put(ID_KEY, filename);
162 models.put(filename, childSchema);
164 processTopData(filename, models, input);
167 final ContainerSchemaNode output = rpc.getOutput();
168 if (!output.getChildNodes().isEmpty()) {
169 final JSONObject properties = processChildren(output.getChildNodes(), moduleName, models, true, schemaContext);
170 final String filename = "(" + rpc.getQName().getLocalName() + ")output";
171 final JSONObject childSchema = getSchemaTemplate();
172 childSchema.put(TYPE_KEY, OBJECT_TYPE);
173 childSchema.put(PROPERTIES_KEY, properties);
174 childSchema.put(ID_KEY, filename);
175 models.put(filename, childSchema);
177 processTopData(filename, models, output);
182 private JSONObject processTopData(final String filename, final JSONObject models, final SchemaNode schemaNode) {
183 final JSONObject items = new JSONObject();
185 items.put(REF_KEY, filename);
186 final JSONObject dataNodeProperties = new JSONObject();
187 dataNodeProperties.put(TYPE_KEY, schemaNode instanceof ListSchemaNode ? ARRAY_TYPE : OBJECT_TYPE);
188 dataNodeProperties.put(ITEMS_KEY, items);
190 dataNodeProperties.putOpt(DESCRIPTION_KEY, schemaNode.getDescription());
191 final JSONObject properties = new JSONObject();
192 properties.put(topLevelModule.getName() + ":" + schemaNode.getQName().getLocalName(), dataNodeProperties);
193 final JSONObject finalChildSchema = getSchemaTemplate();
194 finalChildSchema.put(TYPE_KEY, OBJECT_TYPE);
195 finalChildSchema.put(PROPERTIES_KEY, properties);
196 finalChildSchema.put(ID_KEY, filename + OperationBuilder.TOP);
197 models.put(filename + OperationBuilder.TOP, finalChildSchema);
199 return dataNodeProperties;
203 * Processes the 'identity' statement in a yang model and maps it to a 'model' in the Swagger JSON spec.
205 * @param module The module from which the identity stmt will be processed
206 * @param models The JSONObject in which the parsed identity will be put as a 'model' obj
208 private static void processIdentities(final Module module, final JSONObject models) throws JSONException {
210 final String moduleName = module.getName();
211 final Set<IdentitySchemaNode> idNodes = module.getIdentities();
212 LOG.debug("Processing Identities for module {} . Found {} identity statements", moduleName, idNodes.size());
214 for (final IdentitySchemaNode idNode : idNodes) {
215 final JSONObject identityObj = new JSONObject();
216 final String identityName = idNode.getQName().getLocalName();
217 LOG.debug("Processing Identity: {}", identityName);
219 identityObj.put(ID_KEY, identityName);
220 identityObj.put(DESCRIPTION_KEY, idNode.getDescription());
222 final JSONObject props = new JSONObject();
223 final IdentitySchemaNode baseId = idNode.getBaseIdentity();
225 if (baseId == null) {
227 * This is a base identity. So lets see if it has sub types. If it does, then add them to the model
230 final Set<IdentitySchemaNode> derivedIds = idNode.getDerivedIdentities();
232 if (derivedIds != null) {
233 final JSONArray subTypes = new JSONArray();
234 for (final IdentitySchemaNode derivedId : derivedIds) {
235 subTypes.put(derivedId.getQName().getLocalName());
237 identityObj.put(SUB_TYPES_KEY, subTypes);
242 * This is a derived entity. Add it's base type & move on.
244 props.put(TYPE_KEY, baseId.getQName().getLocalName());
247 // Add the properties. For a base type, this will be an empty object as required by the Swagger spec.
248 identityObj.put(PROPERTIES_KEY, props);
249 models.put(identityName, identityObj);
253 private JSONObject processDataNodeContainer(final DataNodeContainer dataNode, final String parentName, final JSONObject models,
254 final boolean isConfig, final SchemaContext schemaContext) throws JSONException, IOException {
255 if (dataNode instanceof ListSchemaNode || dataNode instanceof ContainerSchemaNode) {
256 final Iterable<DataSchemaNode> containerChildren = dataNode.getChildNodes();
257 final String localName = ((SchemaNode) dataNode).getQName().getLocalName();
258 final JSONObject properties = processChildren(containerChildren, parentName + "/" + localName, models, isConfig, schemaContext);
259 final String nodeName = parentName + (isConfig ? OperationBuilder.CONFIG : OperationBuilder.OPERATIONAL)
262 final JSONObject childSchema = getSchemaTemplate();
263 childSchema.put(TYPE_KEY, OBJECT_TYPE);
264 childSchema.put(PROPERTIES_KEY, properties);
266 childSchema.put(ID_KEY, nodeName);
267 models.put(nodeName, childSchema);
270 createConcreteModelForPost(models, localName,
271 createPropertiesForPost(dataNode, schemaContext, parentName + "/" + localName));
274 return processTopData(nodeName, models, (SchemaNode) dataNode);
279 private static void createConcreteModelForPost(final JSONObject models, final String localName,
280 final JSONObject properties) throws JSONException {
281 final String nodePostName = OperationBuilder.CONFIG + localName + Post.METHOD_NAME;
282 final JSONObject postSchema = getSchemaTemplate();
283 postSchema.put(TYPE_KEY, OBJECT_TYPE);
284 postSchema.put(ID_KEY, nodePostName);
285 postSchema.put(PROPERTIES_KEY, properties);
286 models.put(nodePostName, postSchema);
289 private JSONObject createPropertiesForPost(final DataNodeContainer dataNodeContainer, final SchemaContext schemaContext, final String parentName)
290 throws JSONException {
291 final JSONObject properties = new JSONObject();
292 for (final DataSchemaNode childNode : dataNodeContainer.getChildNodes()) {
293 if (childNode instanceof ListSchemaNode || childNode instanceof ContainerSchemaNode) {
294 final JSONObject items = new JSONObject();
295 items.put(REF_KEY, parentName + "(config)" + childNode.getQName().getLocalName());
296 final JSONObject property = new JSONObject();
297 property.put(TYPE_KEY, childNode instanceof ListSchemaNode ? ARRAY_TYPE : OBJECT_TYPE);
298 property.put(ITEMS_KEY, items);
299 properties.put(childNode.getQName().getLocalName(), property);
300 } else if (childNode instanceof LeafSchemaNode) {
301 final JSONObject property = processLeafNode((LeafSchemaNode) childNode, schemaContext);
302 properties.put(childNode.getQName().getLocalName(), property);
309 * Processes the nodes.
311 private JSONObject processChildren(final Iterable<DataSchemaNode> nodes, final String parentName, final JSONObject models,
312 final boolean isConfig, final SchemaContext schemaContext)
313 throws JSONException, IOException {
314 final JSONObject properties = new JSONObject();
315 for (final DataSchemaNode node : nodes) {
316 if (node.isConfiguration() == isConfig) {
317 final String name = resolveNodesName(node, topLevelModule, schemaContext);
318 final JSONObject property;
319 if (node instanceof LeafSchemaNode) {
320 property = processLeafNode((LeafSchemaNode) node, schemaContext);
322 } else if (node instanceof ListSchemaNode) {
323 property = processDataNodeContainer((ListSchemaNode) node, parentName, models, isConfig,
326 } else if (node instanceof LeafListSchemaNode) {
327 property = processLeafListNode((LeafListSchemaNode) node, schemaContext);
329 } else if (node instanceof ChoiceSchemaNode) {
330 if (((ChoiceSchemaNode) node).getCases().iterator().hasNext()) {
331 processChoiceNode(((ChoiceSchemaNode) node).getCases().iterator().next().getChildNodes(),
332 parentName, models, schemaContext, isConfig, properties);
336 } else if (node instanceof AnyXmlSchemaNode) {
337 property = processAnyXMLNode((AnyXmlSchemaNode) node);
339 } else if (node instanceof ContainerSchemaNode) {
340 property = processDataNodeContainer((ContainerSchemaNode) node, parentName, models, isConfig,
344 throw new IllegalArgumentException("Unknown DataSchemaNode type: " + node.getClass());
346 property.putOpt(DESCRIPTION_KEY, node.getDescription());
347 properties.put(topLevelModule.getName() + ":" + name, property);
353 private JSONObject processLeafListNode(final LeafListSchemaNode listNode, final SchemaContext schemaContext) throws JSONException {
354 final JSONObject props = new JSONObject();
355 props.put(TYPE_KEY, ARRAY_TYPE);
357 final JSONObject itemsVal = new JSONObject();
358 final ConstraintDefinition constraints = listNode.getConstraints();
359 final Optional<Integer> maxOptional = Optional.fromNullable(constraints.getMaxElements());
360 if (maxOptional.or(2) >= 2) {
361 processTypeDef(listNode.getType(), listNode, itemsVal, schemaContext);
362 processTypeDef(listNode.getType(), listNode, itemsVal, schemaContext);
364 processTypeDef(listNode.getType(), listNode, itemsVal, schemaContext);
366 props.put(ITEMS_KEY, itemsVal);
369 processConstraints(constraints, props);
374 private void processChoiceNode(final Iterable<DataSchemaNode> nodes, final String moduleName, final JSONObject models,
375 final SchemaContext schemaContext, final boolean isConfig, final JSONObject properties)
376 throws JSONException, IOException {
377 for (final DataSchemaNode node : nodes) {
378 final String name = resolveNodesName(node, topLevelModule, schemaContext);
379 final JSONObject property;
381 if (node instanceof LeafSchemaNode) {
382 property = processLeafNode((LeafSchemaNode) node, schemaContext);
384 } else if (node instanceof ListSchemaNode) {
385 property = processDataNodeContainer((ListSchemaNode) node, moduleName, models, isConfig,
388 } else if (node instanceof LeafListSchemaNode) {
389 property = processLeafListNode((LeafListSchemaNode) node, schemaContext);
391 } else if (node instanceof ChoiceSchemaNode) {
392 if (((ChoiceSchemaNode) node).getCases().iterator().hasNext())
393 processChoiceNode(((ChoiceSchemaNode) node).getCases().iterator().next().getChildNodes(),
394 moduleName, models, schemaContext, isConfig, properties);
397 } else if (node instanceof AnyXmlSchemaNode) {
398 property = processAnyXMLNode((AnyXmlSchemaNode) node);
400 } else if (node instanceof ContainerSchemaNode) {
401 property = processDataNodeContainer((ContainerSchemaNode) node, moduleName, models, isConfig,
405 throw new IllegalArgumentException("Unknown DataSchemaNode type: " + node.getClass());
408 property.putOpt(DESCRIPTION_KEY, node.getDescription());
409 properties.put(name, property);
413 private static void processConstraints(final ConstraintDefinition constraints, final JSONObject props) throws JSONException {
414 final boolean isMandatory = constraints.isMandatory();
415 props.put(REQUIRED_KEY, isMandatory);
417 final Integer minElements = constraints.getMinElements();
418 final Integer maxElements = constraints.getMaxElements();
419 if (minElements != null) {
420 props.put(MIN_ITEMS, minElements);
422 if (maxElements != null) {
423 props.put(MAX_ITEMS, maxElements);
427 private JSONObject processLeafNode(final LeafSchemaNode leafNode, final SchemaContext schemaContext) throws JSONException {
428 final JSONObject property = new JSONObject();
430 final String leafDescription = leafNode.getDescription();
431 property.put(DESCRIPTION_KEY, leafDescription);
432 processConstraints(leafNode.getConstraints(), property);
433 processTypeDef(leafNode.getType(), leafNode, property, schemaContext);
438 private static JSONObject processAnyXMLNode(final AnyXmlSchemaNode leafNode) throws JSONException {
439 final JSONObject property = new JSONObject();
441 final String leafDescription = leafNode.getDescription();
442 property.put(DESCRIPTION_KEY, leafDescription);
444 processConstraints(leafNode.getConstraints(), property);
445 final String localName = leafNode.getQName().getLocalName();
446 property.put(TYPE_KEY, "example of anyxml " + localName);
451 private String processTypeDef(final TypeDefinition<?> leafTypeDef, final DataSchemaNode node,
452 final JSONObject property, final SchemaContext schemaContext) throws JSONException {
453 final String jsonType;
454 if (leafTypeDef.getDefaultValue() == null) {
455 if (leafTypeDef instanceof BinaryTypeDefinition) {
456 jsonType = processBinaryType(property);
458 } else if (leafTypeDef instanceof BitsTypeDefinition) {
459 jsonType = processBitsType((BitsTypeDefinition) leafTypeDef, property);
461 } else if (leafTypeDef instanceof EnumTypeDefinition) {
462 jsonType = processEnumType((EnumTypeDefinition) leafTypeDef, property);
464 } else if (leafTypeDef instanceof IdentityrefTypeDefinition) {
465 final String name = topLevelModule.getName();
466 jsonType = name + ":" + ((IdentityrefTypeDefinition) leafTypeDef).getIdentity().getQName().getLocalName();
468 } else if (leafTypeDef instanceof StringTypeDefinition) {
469 jsonType = processStringType(leafTypeDef, property, node.getQName().getLocalName());
471 } else if (leafTypeDef instanceof UnionTypeDefinition) {
472 jsonType = processUnionType((UnionTypeDefinition) leafTypeDef, property, schemaContext, node);
474 } else if (leafTypeDef instanceof EmptyTypeDefinition) {
475 jsonType = UNIQUE_EMPTY_IDENTIFIER;
477 } else if (leafTypeDef instanceof LeafrefTypeDefinition) {
478 return processLeafRef(node, property, schemaContext, leafTypeDef);
480 } else if (leafTypeDef instanceof BooleanTypeDefinition) {
483 } else if (leafTypeDef instanceof DecimalTypeDefinition) {
484 jsonType = String.valueOf(((DecimalTypeDefinition) leafTypeDef).getRangeConstraints()
485 .iterator().next().getMin());
487 } else if (leafTypeDef instanceof IntegerTypeDefinition) {
488 jsonType = String.valueOf(((IntegerTypeDefinition) leafTypeDef).getRangeConstraints()
489 .iterator().next().getMin());
491 } else if (leafTypeDef instanceof UnsignedIntegerTypeDefinition) {
492 jsonType = String.valueOf(((UnsignedIntegerTypeDefinition) leafTypeDef).getRangeConstraints()
493 .iterator().next().getMin());
496 jsonType = OBJECT_TYPE;
500 jsonType = String.valueOf(leafTypeDef.getDefaultValue());
502 property.putOpt(TYPE_KEY, jsonType);
506 private String processLeafRef(final DataSchemaNode node, final JSONObject property, final SchemaContext schemaContext,
507 final TypeDefinition<?> leafTypeDef) {
508 RevisionAwareXPath xPath = ((LeafrefTypeDefinition) leafTypeDef).getPathStatement();
509 final URI namespace = leafTypeDef.getQName().getNamespace();
510 final Date revision = leafTypeDef.getQName().getRevision();
511 final Module module = schemaContext.findModuleByNamespaceAndRevision(namespace, revision);
512 final SchemaNode schemaNode;
514 final String xPathString = STRIP_PATTERN.matcher(xPath.toString()).replaceAll("");
515 xPath = new RevisionAwareXPathImpl(xPathString, xPath.isAbsolute());
517 if (xPath.isAbsolute()) {
518 schemaNode = SchemaContextUtil.findDataSchemaNode(schemaContext, module, xPath);
520 schemaNode = SchemaContextUtil.findDataSchemaNodeForRelativeXPath(schemaContext, module, node, xPath);
523 return processTypeDef(((TypedSchemaNode) schemaNode).getType(), (DataSchemaNode) schemaNode, property, schemaContext);
526 private static String processBinaryType(final JSONObject property) throws JSONException {
527 final JSONObject media = new JSONObject();
528 media.put(BINARY_ENCODING_KEY, BASE_64);
529 property.put(MEDIA_KEY, media);
533 private static String processEnumType(final EnumTypeDefinition enumLeafType, final JSONObject property) throws JSONException {
534 final List<EnumPair> enumPairs = enumLeafType.getValues();
535 final List<String> enumNames = new ArrayList<>();
536 for (final EnumPair enumPair : enumPairs) {
537 enumNames.add(enumPair.getName());
540 property.putOpt(ENUM, new JSONArray(enumNames));
541 return enumLeafType.getValues().iterator().next().getName();
544 private static String processBitsType(final BitsTypeDefinition bitsType, final JSONObject property) throws JSONException {
545 property.put(MIN_ITEMS, 0);
546 property.put(UNIQUE_ITEMS_KEY, true);
547 final List<String> enumNames = new ArrayList<>();
548 final List<Bit> bits = bitsType.getBits();
549 for (final Bit bit : bits) {
550 enumNames.add(bit.getName());
552 property.put(ENUM, new JSONArray(enumNames));
554 return enumNames.iterator().next() + " " + enumNames.get(enumNames.size() - 1);
557 private static String processStringType(final TypeDefinition<?> stringType, final JSONObject property, final String nodeName)
558 throws JSONException {
559 StringTypeDefinition type = (StringTypeDefinition) stringType;
560 List<LengthConstraint> lengthConstraints = ((StringTypeDefinition) stringType).getLengthConstraints();
561 while (lengthConstraints.isEmpty() && type.getBaseType() != null) {
562 type = type.getBaseType();
563 lengthConstraints = type.getLengthConstraints();
566 // FIXME: json-schema is not expressive enough to capture min/max laternatives. We should find the true minimum
567 // and true maximum implied by the constraints and use that.
568 for (final LengthConstraint lengthConstraint : lengthConstraints) {
569 final Number min = lengthConstraint.getMin();
570 final Number max = lengthConstraint.getMax();
571 property.putOpt(MIN_LENGTH_KEY, min);
572 property.putOpt(MAX_LENGTH_KEY, max);
574 if (type.getPatternConstraints().iterator().hasNext()) {
575 final PatternConstraint pattern = type.getPatternConstraints().iterator().next();
576 String regex = pattern.getRegularExpression();
577 regex = regex.substring(1, regex.length() - 1);
578 final Generex generex = new Generex(regex);
579 return generex.random();
581 return "Some " + nodeName;
585 private String processUnionType(final UnionTypeDefinition unionType, final JSONObject property,
586 final SchemaContext schemaContext, final DataSchemaNode node)
587 throws JSONException {
588 final List<String> unionNames = new ArrayList<>();
589 for (final TypeDefinition<?> typeDef : unionType.getTypes()) {
590 unionNames.add(processTypeDef(typeDef, node, property, schemaContext));
592 property.put(ENUM, new JSONArray(unionNames));
593 return unionNames.iterator().next();
597 * Helper method to generate a pre-filled JSON schema object.
599 private static JSONObject getSchemaTemplate() throws JSONException {
600 final JSONObject schemaJSON = new JSONObject();
601 schemaJSON.put(SCHEMA_KEY, SCHEMA_URL);