View Javadoc
1   /*
2    * Portions of this software was developed by employees of the National Institute
3    * of Standards and Technology (NIST), an agency of the Federal Government and is
4    * being made available as a public service. Pursuant to title 17 United States
5    * Code Section 105, works of NIST employees are not subject to copyright
6    * protection in the United States. This software may be subject to foreign
7    * copyright. Permission in the United States and in foreign countries, to the
8    * extent that NIST may hold copyright, to use, copy, modify, create derivative
9    * works, and distribute this software and its documentation without fee is hereby
10   * granted on a non-exclusive basis, provided that this notice and disclaimer
11   * of warranty appears in all copies.
12   *
13   * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER
14   * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY
15   * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF
16   * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM
17   * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE
18   * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE.  IN NO EVENT
19   * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT,
20   * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM,
21   * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY,
22   * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR
23   * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT
24   * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER.
25   */
26  
27  package gov.nist.secauto.metaschema.schemagen.json.schema;
28  
29  import com.fasterxml.jackson.databind.node.ArrayNode;
30  import com.fasterxml.jackson.databind.node.JsonNodeFactory;
31  import com.fasterxml.jackson.databind.node.ObjectNode;
32  
33  import gov.nist.secauto.metaschema.core.model.IAssemblyDefinition;
34  import gov.nist.secauto.metaschema.core.model.IChoiceInstance;
35  import gov.nist.secauto.metaschema.core.model.IFlagInstance;
36  import gov.nist.secauto.metaschema.core.model.IModelInstance;
37  import gov.nist.secauto.metaschema.core.model.INamedModelInstance;
38  import gov.nist.secauto.metaschema.core.model.ModelType;
39  import gov.nist.secauto.metaschema.core.util.CollectionUtil;
40  import gov.nist.secauto.metaschema.core.util.ObjectUtils;
41  import gov.nist.secauto.metaschema.schemagen.FlagInstanceFilter;
42  import gov.nist.secauto.metaschema.schemagen.json.impl.JsonGenerationState;
43  import gov.nist.secauto.metaschema.schemagen.json.property.FlagInstanceJsonProperty;
44  import gov.nist.secauto.metaschema.schemagen.json.property.IJsonProperty.PropertyCollection;
45  import gov.nist.secauto.metaschema.schemagen.json.property.INamedModelInstanceJsonProperty;
46  
47  import java.io.IOException;
48  import java.util.Collection;
49  import java.util.LinkedList;
50  import java.util.List;
51  
52  import edu.umd.cs.findbugs.annotations.NonNull;
53  
54  public class AssemblyDefinitionJsonSchema
55      extends AbstractDefinitionJsonSchema<IAssemblyDefinition> {
56  
57    public AssemblyDefinitionJsonSchema(
58        @NonNull IAssemblyDefinition definition) {
59      super(definition);
60    }
61  
62    @Override
63    public void resolveSubSchemas(JsonGenerationState state) {
64      for (IFlagInstance instance : getDefinition().getFlagInstances()) {
65        state.getSchema(instance.getDefinition());
66      }
67  
68      for (INamedModelInstance instance : getDefinition().getNamedModelInstances()) {
69        state.getSchema(instance.getDefinition());
70      }
71    }
72  
73    @Override
74    protected void generateBody(JsonGenerationState state, ObjectNode obj) throws IOException {
75      IAssemblyDefinition definition = getDefinition();
76  
77      obj.put("type", "object");
78  
79      // determine the flag instances to generate
80      IFlagInstance jsonKeyFlag = definition.getJsonKeyFlagInstance();
81      Collection<? extends IFlagInstance> flags
82          = FlagInstanceFilter.filterFlags(definition.getFlagInstances(), jsonKeyFlag);
83  
84      PropertyCollection properties = new PropertyCollection();
85  
86      // generate flag properties
87      for (IFlagInstance flag : flags) {
88        assert flag != null;
89        new FlagInstanceJsonProperty(flag).generateProperty(properties, state); // NOPMD instantiation needed
90      }
91      // generate model properties
92      Collection<? extends INamedModelInstance> instances = definition.getNamedModelInstances();
93      for (INamedModelInstance instance : instances) {
94        assert instance != null;
95        INamedModelInstanceJsonProperty.newProperty(instance)
96            .generateProperty(properties, state);
97      }
98  
99      Collection<? extends IChoiceInstance> choices = definition.getChoiceInstances();
100     if (choices.isEmpty()) {
101       properties.generate(obj);
102 
103       obj.put("additionalProperties", false);
104     } else {
105       generateChoices(choices, properties, obj, state);
106     }
107   }
108 
109   protected void generateChoices(
110       @NonNull Collection<? extends IChoiceInstance> choices,
111       @NonNull PropertyCollection properties,
112       @NonNull ObjectNode definitionNode,
113       @NonNull JsonGenerationState state) throws IOException {
114     List<PropertyCollection> propertyChoices = CollectionUtil.singletonList(properties);
115     propertyChoices = explodeChoices(choices, propertyChoices, state);
116 
117     if (propertyChoices.size() == 1) {
118       propertyChoices.iterator().next().generate(definitionNode);
119     } else if (propertyChoices.size() > 1) {
120       ArrayNode anyOfdNode = ObjectUtils.notNull(JsonNodeFactory.instance.arrayNode());
121       for (PropertyCollection propertyChoice : propertyChoices) {
122         ObjectNode choiceDefinitionNode = ObjectUtils.notNull(JsonNodeFactory.instance.objectNode());
123         propertyChoice.generate(choiceDefinitionNode);
124         choiceDefinitionNode.put("additionalProperties", false);
125         anyOfdNode.add(choiceDefinitionNode);
126       }
127       definitionNode.set("anyOf", anyOfdNode);
128     }
129   }
130 
131   protected List<PropertyCollection> explodeChoices(
132       @NonNull Collection<? extends IChoiceInstance> choices,
133       @NonNull List<PropertyCollection> propertyChoices,
134       @NonNull JsonGenerationState state) throws IOException {
135 
136     List<PropertyCollection> retval = propertyChoices;
137 
138     for (IChoiceInstance choice : choices) {
139       List<PropertyCollection> newRetval = new LinkedList<>(); // NOPMD - intentional
140       for (IModelInstance optionInstance : choice.getModelInstances()) {
141         if (ModelType.CHOICE.equals(optionInstance.getModelType())) {
142           // recurse
143           newRetval.addAll(explodeChoices(
144               CollectionUtil.singleton((IChoiceInstance) optionInstance),
145               retval,
146               state));
147         } else {
148           // iterate over the old array of choices and append new choice
149           for (PropertyCollection oldInstanceProperties : retval) {
150             @SuppressWarnings("null")
151             @NonNull PropertyCollection newInstanceProperties = oldInstanceProperties.copy();
152 
153             // add the choice
154             INamedModelInstanceJsonProperty.newProperty((INamedModelInstance) optionInstance)
155                 .generateProperty(newInstanceProperties, state);
156 
157             newRetval.add(newInstanceProperties);
158           }
159         }
160       }
161       retval = newRetval;
162     }
163     return retval;
164   }
165 }