Merge "Add Legacy Op Policy Persistence"
[policy/models.git] / models-base / src / main / java / org / onap / policy / models / base / PfKeyUse.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.List;
24
25 import javax.ws.rs.core.Response;
26
27 import lombok.EqualsAndHashCode;
28 import lombok.NonNull;
29 import lombok.ToString;
30
31 import org.onap.policy.common.utils.validation.Assertions;
32 import org.onap.policy.models.base.PfValidationResult.ValidationResult;
33
34 /**
35  * This class records a usage of a key in the system. When the list of keys being used by a concept
36  * is built using the getKeys() method of the {@link PfConcept} class, an instance of this class is
37  * created for every key occurrence. The list of keys returned by the getKeys() method is a list of
38  * {@link PfKeyUse} objects.
39  *
40  * <p>Validation checks that each key is valid.
41  */
42 @EqualsAndHashCode(callSuper = false)
43 @ToString
44 public class PfKeyUse extends PfKey {
45     private static final long serialVersionUID = 2007147220109881705L;
46
47     private PfKey usedKey;
48
49     /**
50      * The Default Constructor creates this concept with a null key.
51      */
52     public PfKeyUse() {
53         this(new PfConceptKey());
54     }
55
56     /**
57      * This constructor creates an instance of this class, and holds a reference to a used key.
58      *
59      * @param usedKey a used key
60      */
61     public PfKeyUse(@NonNull final PfKey usedKey) {
62         this.usedKey = usedKey;
63     }
64
65     /**
66      * Copy constructor.
67      *
68      * @param copyConcept the concept to copy from
69      */
70     public PfKeyUse(@NonNull final PfKeyUse copyConcept) {
71         super(copyConcept);
72     }
73
74     @Override
75     public PfKey getKey() {
76         return usedKey;
77     }
78
79     @Override
80     public List<PfKey> getKeys() {
81         return usedKey.getKeys();
82     }
83
84     @Override
85     public String getId() {
86         return usedKey.getId();
87     }
88
89     @Override
90     public boolean isNullKey() {
91         return usedKey.isNullKey();
92     }
93
94     /**
95      * Sets the key.
96      *
97      * @param key the key
98      */
99     public void setKey(@NonNull final PfKey key) {
100         this.usedKey = key;
101     }
102
103     @Override
104     public PfKey.Compatibility getCompatibility(@NonNull final PfKey otherKey) {
105         return usedKey.getCompatibility(otherKey);
106     }
107
108     @Override
109     public boolean isCompatible(@NonNull final PfKey otherKey) {
110         return usedKey.isCompatible(otherKey);
111     }
112
113     @Override
114     public boolean isNewerThan(@NonNull final PfKey otherKey) {
115         return usedKey.isCompatible(otherKey);
116     }
117
118     @Override
119     public int getMajorVersion() {
120         return usedKey.getMajorVersion();
121     }
122
123     @Override
124     public int getMinorVersion() {
125         return usedKey.getMinorVersion();
126     }
127
128     @Override
129     public int getPatchVersion() {
130         return usedKey.getPatchVersion();
131     }
132
133     @Override
134     public void clean() {
135         usedKey.clean();
136     }
137
138     @Override
139     public PfValidationResult validate(@NonNull final PfValidationResult result) {
140         if (usedKey.isNullKey()) {
141             result.addValidationMessage(new PfValidationMessage(usedKey, this.getClass(), ValidationResult.INVALID,
142                     "usedKey is a null key"));
143         }
144         return usedKey.validate(result);
145     }
146
147     @Override
148     public int compareTo(final PfConcept otherObj) {
149         Assertions.argumentNotNull(otherObj, "comparison object may not be null");
150
151         if (this == otherObj) {
152             return 0;
153         }
154         if (getClass() != otherObj.getClass()) {
155             return this.hashCode() - otherObj.hashCode();
156         }
157
158         final PfKeyUse other = (PfKeyUse) otherObj;
159
160         return usedKey.compareTo(other.usedKey);
161     }
162
163     @Override
164     public PfConcept copyTo(@NonNull final PfConcept target) {
165         final Object copyObject = target;
166         Assertions.instanceOf(copyObject, PfKeyUse.class);
167
168         final PfKeyUse copy = ((PfKeyUse) copyObject);
169         try {
170             copy.usedKey = usedKey.getClass().newInstance();
171         } catch (final Exception e) {
172             throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR,
173                     "error copying concept key: " + e.getMessage(), e);
174         }
175         usedKey.copyTo(copy.usedKey);
176
177         return copy;
178     }
179 }