Merge "Add period after inheritDoc for Sonar"
[policy/apex-pdp.git] / model / basic-model / src / main / java / org / onap / policy / apex / model / basicmodel / concepts / AxValidationResult.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.apex.model.basicmodel.concepts;
22
23 import java.util.LinkedList;
24 import java.util.List;
25
26 /**
27  * This class records the result of a validation and holds all validatino observation messages.
28  *
29  * @author Liam Fallon (liam.fallon@ericsson.com)
30  */
31 public class AxValidationResult {
32     /**
33      * The ValidationResult enumeration describes the severity of a validation result.
34      */
35     public enum ValidationResult {
36         /** No problems or observations were detected during validation. */
37         VALID,
38         /**
39          * Observations were made on a concept (such as blank descriptions) of a nature that will not affect the use of
40          * the concept.
41          */
42         OBSERVATION,
43         /**
44          * Warnings were made on a concept (such as defined but unused concepts) of a nature that may affect the use of
45          * the concept.
46          */
47         WARNING,
48         /**
49          * Errors were detected on a concept (such as referenced but undefined concepts) of a nature that will affect
50          * the use of the concept.
51          */
52         INVALID
53     }
54
55     // The actual verification result
56     private ValidationResult validationResult = ValidationResult.VALID;
57
58     // Messages collected during the verification process
59     private final List<AxValidationMessage> messageList = new LinkedList<>();
60
61     /**
62      * Check if a validation reported a valid concept, returns true if the model is usable (that is, even if the model
63      * has warnings or observations).
64      *
65      * @return true, if the concept is reported as valid and can be used
66      */
67     public boolean isValid() {
68         return validationResult != ValidationResult.INVALID;
69     }
70
71     /**
72      * Check if a validation reported a concept with no errors or warnings, returns true if the model is OK to use.
73      *
74      * @return true, if the concept has no warnings or errors
75      */
76     public boolean isOk() {
77         return validationResult == ValidationResult.VALID || validationResult == ValidationResult.OBSERVATION;
78     }
79
80     /**
81      * Gets the validation result.
82      *
83      * @return the validation result on a concept
84      */
85     public ValidationResult getValidationResult() {
86         return validationResult;
87     }
88
89     /**
90      * Gets the list of validation results on the concept.
91      *
92      * @return the list of validaiton results
93      */
94     public List<AxValidationMessage> getMessageList() {
95         return messageList;
96     }
97
98     /**
99      * Adds a validation message to the validation result, used by validate() implementations on {@link AxConcept}
100      * subclasses to report validaiton observations.
101      *
102      * @param validationMessage the validation message
103      */
104     public void addValidationMessage(final AxValidationMessage validationMessage) {
105         messageList.add(validationMessage);
106
107         // Check if the incoming message has a more sever status than the
108         // current one on the overall validation result,
109         // if so, the overall result goes to that level
110         if (validationMessage.getValidationResult().ordinal() > validationResult.ordinal()) {
111             validationResult = validationMessage.getValidationResult();
112         }
113     }
114
115     /*
116      * (non-Javadoc)
117      *
118      * @see java.lang.Object#toString()
119      */
120     @Override
121     public String toString() {
122         final StringBuilder builder = new StringBuilder();
123
124         switch (validationResult) {
125             case VALID:
126
127                 builder.append("***validation of model successful***");
128                 return builder.toString();
129             case OBSERVATION:
130
131                 builder.append("\n***observations noted during validation of model***\n");
132                 break;
133             case WARNING:
134
135                 builder.append("\n***warnings issued during validation of model***\n");
136                 break;
137             case INVALID:
138                 builder.append("\n***validation of model failed***\n");
139                 break;
140             default:
141                 break;
142         }
143
144         for (final AxValidationMessage message : messageList) {
145             builder.append(message);
146             builder.append("\n");
147         }
148
149         builder.append("********************************");
150         return builder.toString();
151     }
152 }