Add DAO Enabled Tosca Model
[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 void clean() {
115         usedKey.clean();
116     }
117
118     @Override
119     public PfValidationResult validate(@NonNull final PfValidationResult result) {
120         if (usedKey.isNullKey()) {
121             result.addValidationMessage(new PfValidationMessage(usedKey, this.getClass(), ValidationResult.INVALID,
122                     "usedKey is a null key"));
123         }
124         return usedKey.validate(result);
125     }
126
127     @Override
128     public int compareTo(final PfConcept otherObj) {
129         Assertions.argumentNotNull(otherObj, "comparison object may not be null");
130
131         if (this == otherObj) {
132             return 0;
133         }
134         if (getClass() != otherObj.getClass()) {
135             return this.hashCode() - otherObj.hashCode();
136         }
137
138         final PfKeyUse other = (PfKeyUse) otherObj;
139
140         return usedKey.compareTo(other.usedKey);
141     }
142
143     @Override
144     public PfConcept copyTo(@NonNull final PfConcept target) {
145         final Object copyObject = target;
146         Assertions.instanceOf(copyObject, PfKeyUse.class);
147
148         final PfKeyUse copy = ((PfKeyUse) copyObject);
149         try {
150             copy.usedKey = usedKey.getClass().newInstance();
151         } catch (final Exception e) {
152             throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR,
153                     "error copying concept key: " + e.getMessage(), e);
154         }
155         usedKey.copyTo(copy.usedKey);
156
157         return copy;
158     }
159 }