Add basic model object concepts
[policy/models.git] / models-base / src / main / java / org / onap / policy / models / base / PfValidationResult.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 Nordix Foundation.
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.models.base;
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 public class PfValidationResult {
30     /**
31      * The ValidationResult enumeration describes the severity of a validation result.
32      */
33     public enum ValidationResult {
34     /** No problems or observations were detected during validation. */
35     VALID,
36     /**
37      * Observations were made on a concept (such as blank descriptions) of a nature that will not
38      * affect the use of the concept.
39      */
40     OBSERVATION,
41     /**
42      * Warnings were made on a concept (such as defined but unused concepts) of a nature that may
43      * affect the use of the concept.
44      */
45     WARNING,
46     /**
47      * Errors were detected on a concept (such as referenced but undefined concepts) of a nature
48      * that will affect the use of the concept.
49      */
50     INVALID
51     }
52
53     // The actual verification result
54     private ValidationResult validationResult = ValidationResult.VALID;
55
56     // Messages collected during the verification process
57     private final List<PfValidationMessage> messageList = new LinkedList<>();
58
59     /**
60      * Check if a validation reported a valid concept, returns true if the model is usable (that is,
61      * even if the model has warnings or observations).
62      *
63      * @return true, if the concept is reported as valid and can be used
64      */
65     public boolean isValid() {
66         return validationResult != ValidationResult.INVALID;
67     }
68
69     /**
70      * Check if a validation reported a concept with no errors or warnings, returns true if the
71      * model is OK to use.
72      *
73      * @return true, if the concept has no warnings or errors
74      */
75     public boolean isOk() {
76         return validationResult == ValidationResult.VALID || validationResult == ValidationResult.OBSERVATION;
77     }
78
79     /**
80      * Gets the validation result.
81      *
82      * @return the validation result on a concept
83      */
84     public ValidationResult getValidationResult() {
85         return validationResult;
86     }
87
88     /**
89      * Gets the list of validation results on the concept.
90      *
91      * @return the list of validaiton results
92      */
93     public List<PfValidationMessage> getMessageList() {
94         return messageList;
95     }
96
97     /**
98      * Adds a validation message to the validation result, used by validate() implementations on
99      * {@link PfConcept} subclasses to report validaiton observations.
100      *
101      * @param validationMessage the validation message
102      */
103     public void addValidationMessage(final PfValidationMessage validationMessage) {
104         messageList.add(validationMessage);
105
106         // Check if the incoming message has a more sever status than the
107         // current one on the overall validation result,
108         // if so, the overall result goes to that level
109         if (validationMessage.getValidationResult().ordinal() > validationResult.ordinal()) {
110             validationResult = validationMessage.getValidationResult();
111         }
112     }
113
114     /*
115      * (non-Javadoc)
116      *
117      * @see java.lang.Object#toString()
118      */
119     @Override
120     public String toString() {
121         final StringBuilder builder = new StringBuilder();
122
123         switch (validationResult) {
124             case VALID:
125
126                 builder.append("***validation of model successful***");
127                 return builder.toString();
128             case OBSERVATION:
129
130                 builder.append("\n***observations noted during validation of model***\n");
131                 break;
132             case WARNING:
133
134                 builder.append("\n***warnings issued during validation of model***\n");
135                 break;
136             case INVALID:
137                 builder.append("\n***validation of model failed***\n");
138                 break;
139             default:
140                 break;
141         }
142
143         for (final PfValidationMessage message : messageList) {
144             builder.append(message);
145             builder.append("\n");
146         }
147
148         builder.append("********************************");
149         return builder.toString();
150     }
151 }