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