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