Add validation methods for PAP REST API
[policy/models.git] / models-tosca / src / main / java / org / onap / policy / models / tosca / authorative / concepts / ToscaPolicyIdentifier.java
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP Policy Models
4  * ================================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.models.tosca.authorative.concepts;
22
23 import lombok.Data;
24 import lombok.NoArgsConstructor;
25 import lombok.NonNull;
26 import org.apache.commons.lang3.ObjectUtils;
27 import org.onap.policy.common.parameters.BeanValidationResult;
28 import org.onap.policy.common.parameters.ValidationResult;
29
30 /**
31  * Identifies a policy. Both the name and version must be non-null.
32  */
33 @Data
34 @NoArgsConstructor
35 public class ToscaPolicyIdentifier implements Comparable<ToscaPolicyIdentifier> {
36
37     @NonNull
38     private String name;
39
40     @NonNull
41     private String version;
42
43
44     public ToscaPolicyIdentifier(@NonNull String name, @NonNull String version) {
45         this.name = name;
46         this.version = version;
47     }
48
49     public ToscaPolicyIdentifier(ToscaPolicyIdentifier source) {
50         this.name = source.name;
51         this.version = source.version;
52     }
53
54     /**
55      * Validates that appropriate fields are populated for an incoming call to the PAP
56      * REST API.
57      *
58      * @return the validation result
59      */
60     public ValidationResult validatePapRest() {
61         BeanValidationResult result = new BeanValidationResult("group", this);
62
63         result.validateNotNull("name", name);
64         result.validateNotNull("version", version);
65
66         return result;
67     }
68
69     @Override
70     public int compareTo(ToscaPolicyIdentifier other) {
71         if (this == other) {
72             return 0;
73         }
74
75         if (other == null) {
76             return 1;
77         }
78
79         int result = ObjectUtils.compare(getName(), other.getName());
80         if (result != 0) {
81             return result;
82         }
83
84         return ObjectUtils.compare(getVersion(), other.getVersion());
85     }
86 }