2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019-2020 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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.models.base;
23 import java.util.LinkedList;
24 import java.util.List;
28 * This class records the result of a validation and holds all validation observation messages.
31 public class PfValidationResult {
33 * The ValidationResult enumeration describes the severity of a validation result.
35 public enum ValidationResult {
36 /** No problems or observations were detected during validation. */
39 * Observations were made on a concept (such as blank descriptions) of a nature that will not affect the use of
44 * Warnings were made on a concept (such as defined but unused concepts) of a nature that may affect the use of
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.
55 // The actual verification result
56 private ValidationResult validationResult = ValidationResult.VALID;
58 // Messages collected during the verification process
59 private final List<PfValidationMessage> messageList = new LinkedList<>();
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).
65 * @return true, if the concept is reported as valid and can be used
67 public boolean isValid() {
68 return validationResult != ValidationResult.INVALID;
72 * Check if a validation reported a concept with no errors or warnings, returns true if the model is OK to use.
74 * @return true, if the concept has no warnings or errors
76 public boolean isOk() {
77 return validationResult == ValidationResult.VALID || validationResult == ValidationResult.OBSERVATION;
82 * Append a validation result to another validation result.
84 * @param result2Append the result to append to the current validation result
86 public void append(final PfValidationResult result2Append) {
87 for (PfValidationMessage message : result2Append.getMessageList()) {
88 addValidationMessage(message);
93 * Adds a validation message to the validation result, used by validate() implementations on {@link PfConcept}
94 * subclasses to report validaiton observations.
96 * @param validationMessage the validation message
98 public void addValidationMessage(final PfValidationMessage validationMessage) {
99 messageList.add(validationMessage);
101 // Check if the incoming message has a more sever status than the
102 // current one on the overall validation result,
103 // if so, the overall result goes to that level
104 if (validationMessage.getValidationResult().ordinal() > validationResult.ordinal()) {
105 validationResult = validationMessage.getValidationResult();
112 * @see java.lang.Object#toString()
115 public String toString() {
116 final StringBuilder builder = new StringBuilder();
118 switch (validationResult) {
121 builder.append("***validation of model successful***");
122 return builder.toString();
125 builder.append("\n***observations noted during validation of model***\n");
129 builder.append("\n***warnings issued during validation of model***\n");
132 builder.append("\n***validation of model failed***\n");
138 for (final PfValidationMessage message : messageList) {
139 builder.append(message);
140 builder.append("\n");
143 builder.append("********************************");
144 return builder.toString();