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.oscal.lib.model.control.catalog;
28  
29  import gov.nist.secauto.metaschema.binding.io.IDeserializationHandler;
30  import gov.nist.secauto.metaschema.model.common.datatype.markup.MarkupLine;
31  import gov.nist.secauto.metaschema.model.common.util.CollectionUtil;
32  import gov.nist.secauto.metaschema.model.common.util.ObjectUtils;
33  import gov.nist.secauto.oscal.lib.model.Control;
34  import gov.nist.secauto.oscal.lib.model.ControlPart;
35  import gov.nist.secauto.oscal.lib.model.Link;
36  import gov.nist.secauto.oscal.lib.model.Parameter;
37  import gov.nist.secauto.oscal.lib.model.Property;
38  
39  import java.util.LinkedList;
40  import java.util.List;
41  import java.util.Objects;
42  import java.util.stream.Stream;
43  
44  import edu.umd.cs.findbugs.annotations.NonNull;
45  
46  public abstract class AbstractControl
47      implements IDeserializationHandler, IControl {
48    private Control parent;
49  
50    @Override
51    public Control getParentControl() {
52      return parent;
53    }
54  
55    @Override
56    public void setParentControl(Control parent) {
57      this.parent = parent;
58    }
59  
60    @Override
61    public void beforeDeserialize(Object parent) { // NOPMD noop default
62      // do nothing
63    }
64  
65    @Override
66    public void afterDeserialize(Object parent) {
67      if (parent instanceof Control) {
68        setParentControl((Control) parent);
69      }
70    }
71  
72    @NonNull
73    @Override
74    public Stream<String> getReferencedParameterIds() {
75  
76      // get parameters referenced by the group's parts
77      Stream<String> insertIds = CollectionUtil.listOrEmpty(getParts()).stream()
78          // Get the full part hierarchy
79          .flatMap(part -> Stream.concat(Stream.of(part), part.getPartsRecursively()))
80          // Get the inserts for each part
81          .flatMap(part -> part.getInserts(insert -> "param".equals(insert.getType().toString())))
82          // Get the param ids for each insert
83          .map(insert -> insert.getIdReference().toString())
84          .flatMap(ObjectUtils::filterNull);
85  
86      // get parameters referenced by the control's parameters
87      Stream<String> parameterIds = CollectionUtil.listOrEmpty(getParams()).stream()
88          .flatMap(ObjectUtils::filterNull)
89          .flatMap(param -> param.getParameterReferences());
90  
91      return ObjectUtils.notNull(
92          Stream.concat(insertIds, parameterIds).distinct());
93    }
94  
95    @NonNull
96    public static Builder builder(@NonNull String id) {
97      return new Builder(id);
98    }
99  
100   public static class Builder {
101     @NonNull
102     private final String id;
103 
104     private String clazz;
105     private MarkupLine title;
106     private final List<Parameter> params = new LinkedList<>();
107     private final List<Property> props = new LinkedList<>();
108     private final List<Link> links = new LinkedList<>();
109     private final List<ControlPart> parts = new LinkedList<>();
110     private final List<Control> controls = new LinkedList<>();
111 
112     public Builder(@NonNull String id) {
113       this.id = Objects.requireNonNull(id, "id");
114     }
115 
116     @NonNull
117     public Builder clazz(@NonNull String value) {
118       this.clazz = Objects.requireNonNull(value);
119       return this;
120     }
121 
122     @NonNull
123     public Builder title(@NonNull String markdown) {
124       this.title = MarkupLine.fromMarkdown(Objects.requireNonNull(markdown));
125       return this;
126     }
127 
128     @NonNull
129     public Builder title(@NonNull MarkupLine value) {
130       this.title = Objects.requireNonNull(value);
131       return this;
132     }
133 
134     @NonNull
135     public Builder param(@NonNull Parameter value) {
136       this.params.add(Objects.requireNonNull(value));
137       return this;
138     }
139 
140     @NonNull
141     public Builder prop(@NonNull Property value) {
142       this.props.add(Objects.requireNonNull(value));
143       return this;
144     }
145 
146     @NonNull
147     public Builder link(@NonNull Link value) {
148       this.links.add(Objects.requireNonNull(value));
149       return this;
150     }
151 
152     @NonNull
153     public Builder part(@NonNull ControlPart value) {
154       this.parts.add(Objects.requireNonNull(value));
155       return this;
156     }
157 
158     @NonNull
159     public Builder control(@NonNull Control value) {
160       this.controls.add(Objects.requireNonNull(value));
161       return this;
162     }
163 
164     @NonNull
165     public Control build() {
166       Control retval = new Control();
167       retval.setId(id);
168 
169       if (title == null) {
170         throw new IllegalStateException("a title must be provided");
171       }
172       retval.setTitle(title);
173 
174       if (clazz != null) {
175         retval.setClazz(clazz);
176       }
177       if (!params.isEmpty()) {
178         retval.setParams(params);
179       }
180       if (!props.isEmpty()) {
181         retval.setProps(props);
182       }
183       if (!links.isEmpty()) {
184         retval.setLinks(links);
185       }
186       if (!parts.isEmpty()) {
187         retval.setParts(parts);
188       }
189       if (!controls.isEmpty()) {
190         controls.forEach(control -> control.setParentControl(retval));
191         retval.setControls(controls);
192       }
193 
194       return retval;
195     }
196   }
197 }