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.decima.core.assessment;
28
29 import gov.nist.secauto.decima.core.Decima;
30 import gov.nist.secauto.decima.core.assessment.result.AssessmentResultBuilder;
31 import gov.nist.secauto.decima.core.assessment.result.AssessmentResults;
32 import gov.nist.secauto.decima.core.document.Document;
33 import gov.nist.secauto.decima.core.requirement.RequirementsManager;
34
35 import java.util.LinkedList;
36 import java.util.Objects;
37 import java.util.Queue;
38
39 /**
40 * Provides a reusable execution environment for executing assessments using a a common set of
41 * requirements.
42 */
43 public class AssessmentReactor {
44 private final RequirementsManager requirementsManager;
45 private final Queue<AssessmentExecution<?>> assessmentExecutions = new LinkedList<>();
46
47 /**
48 * Creates a new reactor supporting assessments related to the requirements managed by the provided
49 * {@link RequirementsManager}.
50 *
51 * @param requirementsManager
52 * the manager providing a set of managed requirements to perform assessments against
53 */
54 public AssessmentReactor(RequirementsManager requirementsManager) {
55 Objects.requireNonNull(requirementsManager, "requirementsManager");
56 this.requirementsManager = requirementsManager;
57 }
58
59 /**
60 * Retrieves the managed requirements associated with this reactor.
61 *
62 * @return the managed requirements
63 */
64 public RequirementsManager getRequirementsManager() {
65 return requirementsManager;
66 }
67
68 /**
69 * Adds a new assessment execution for the reactor to perform.
70 *
71 * @param <DOC>
72 * the type of document to be assessed
73 * @param document
74 * the document to assess
75 * @param executor
76 * the executor to use to perform the provided assessment
77 * @return this reactor
78 */
79 public synchronized <DOC extends Document> AssessmentReactor pushAssessmentExecution(DOC document,
80 AssessmentExecutor<DOC> executor) {
81 assessmentExecutions.add(new AssessmentExecution<DOC>(document, executor));
82 return this;
83 }
84
85 /**
86 * Conducts all queued assessments.
87 *
88 * @return a set of assessment results based on the evaluated assessments
89 * @throws AssessmentException
90 * if an error occurred while conducting the assessments
91 */
92 public AssessmentResults react() throws AssessmentException {
93 return react(newAssessmentResultBuilder());
94 }
95
96 /**
97 * Conducts all queued assessments.
98 *
99 * @param builder
100 * the {@link AssessmentResultBuilder} to append results to
101 * @return a set of assessment results based on the evaluated assessments
102 * @throws AssessmentException
103 * if an error occurred while conducting the assessments
104 */
105 public AssessmentResults react(AssessmentResultBuilder builder) throws AssessmentException {
106 builder.start();
107
108 synchronized (this) {
109
110 while (!assessmentExecutions.isEmpty()) {
111 AssessmentExecution<?> execution = assessmentExecutions.poll();
112 execution.execute(builder);
113 }
114 }
115
116 return builder.end().build(getRequirementsManager());
117 }
118
119 /**
120 * Creates a new {@link AssessmentResultBuilder} to use when producing assessment results.
121 * <p>
122 * Sub-classes can use this method to customize the assessment result production pipeline.
123 *
124 * @return a new result builder instance
125 */
126 protected AssessmentResultBuilder newAssessmentResultBuilder() {
127 return Decima.newAssessmentResultBuilder();
128 }
129 }