Refactor models for common type handling
[policy/models.git] / models-tosca / src / main / java / org / onap / policy / models / tosca / simple / concepts / JpaToscaPolicy.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP Policy Model
4  * ================================================================================
5  * Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2019-2021 Nordix Foundation.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  * SPDX-License-Identifier: Apache-2.0
21  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.policy.models.tosca.simple.concepts;
25
26 import java.util.LinkedHashMap;
27 import java.util.List;
28 import javax.persistence.ElementCollection;
29 import javax.persistence.Entity;
30 import javax.persistence.Inheritance;
31 import javax.persistence.InheritanceType;
32 import javax.persistence.Table;
33 import javax.ws.rs.core.Response;
34 import lombok.Data;
35 import lombok.EqualsAndHashCode;
36 import lombok.NonNull;
37 import org.onap.policy.common.parameters.BeanValidationResult;
38 import org.onap.policy.common.parameters.annotations.NotNull;
39 import org.onap.policy.common.parameters.annotations.Valid;
40 import org.onap.policy.common.utils.coder.CoderException;
41 import org.onap.policy.common.utils.coder.StandardCoder;
42 import org.onap.policy.models.base.PfConcept;
43 import org.onap.policy.models.base.PfConceptKey;
44 import org.onap.policy.models.base.PfKey;
45 import org.onap.policy.models.base.PfModelRuntimeException;
46 import org.onap.policy.models.base.PfUtils;
47 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
48
49 /**
50  * Class to represent the policy in TOSCA definition.
51  *
52  * @author Chenfei Gao (cgao@research.att.com)
53  * @author Liam Fallon (liam.fallon@est.tech)
54  */
55 @Entity
56 @Table(name = "ToscaPolicy")
57 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
58 @Data
59 @EqualsAndHashCode(callSuper = true)
60 public class JpaToscaPolicy extends JpaToscaWithTypeAndStringProperties<ToscaPolicy> {
61     private static final long serialVersionUID = 3265174757061982805L;
62
63     // Tags for metadata
64     private static final String METADATA_POLICY_ID_TAG = "policy-id";
65     private static final String METADATA_POLICY_VERSION_TAG = "policy-version";
66
67     private static final StandardCoder STANDARD_CODER = new StandardCoder();
68
69     @ElementCollection
70     private List<@NotNull @Valid PfConceptKey> targets;
71
72     /**
73      * The Default Constructor creates a {@link JpaToscaPolicy} object with a null key.
74      */
75     public JpaToscaPolicy() {
76         super();
77     }
78
79     /**
80      * The Key Constructor creates a {@link JpaToscaPolicy} object with the given concept key.
81      *
82      * @param key the key
83      */
84     public JpaToscaPolicy(@NonNull final PfConceptKey key) {
85         super(key, new PfConceptKey());
86     }
87
88     /**
89      * The full Constructor creates a {@link JpaToscaPolicy} object with all mandatory fields.
90      *
91      * @param key the key
92      * @param type the type of the policy
93      */
94     public JpaToscaPolicy(@NonNull final PfConceptKey key, @NonNull final PfConceptKey type) {
95         super(key, type);
96     }
97
98     /**
99      * Copy constructor.
100      *
101      * @param copyConcept the concept to copy from
102      */
103     public JpaToscaPolicy(@NonNull final JpaToscaPolicy copyConcept) {
104         super(copyConcept);
105         this.targets = PfUtils.mapList(copyConcept.targets, PfConceptKey::new);
106     }
107
108     /**
109      * Authorative constructor.
110      *
111      * @param authorativeConcept the authorative concept to copy from
112      */
113     public JpaToscaPolicy(final ToscaPolicy authorativeConcept) {
114         super(new PfConceptKey());
115         this.fromAuthorative(authorativeConcept);
116     }
117
118     @Override
119     public ToscaPolicy toAuthorative() {
120         ToscaPolicy toscaPolicy = new ToscaPolicy();
121         super.setToscaEntity(toscaPolicy);
122         super.toAuthorative();
123
124         return toscaPolicy;
125     }
126
127     @Override
128     public void fromAuthorative(@NonNull final ToscaPolicy toscaPolicy) {
129         super.fromAuthorative(toscaPolicy);
130
131         // Add the property metadata if it doesn't exist already
132         if (toscaPolicy.getMetadata() == null) {
133             setMetadata(new LinkedHashMap<>());
134         }
135
136         // Add the policy name and version fields to the metadata
137         getMetadata().put(METADATA_POLICY_ID_TAG, getKey().getName());
138         getMetadata().put(METADATA_POLICY_VERSION_TAG, getKey().getVersion());
139     }
140
141     @Override
142     protected Object deserializePropertyValue(String propValue) {
143         try {
144             return STANDARD_CODER.decode(propValue, Object.class);
145         } catch (CoderException ce) {
146             String errorMessage = "error decoding property JSON value read from database: " + propValue;
147             throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR, errorMessage, ce);
148         }
149     }
150
151     @Override
152     protected String serializePropertyValue(Object propValue) {
153         try {
154             return STANDARD_CODER.encode(propValue);
155         } catch (CoderException ce) {
156             String errorMessage = "error encoding property JSON value for database: " + propValue;
157             throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR, errorMessage, ce);
158         }
159     }
160
161     @Override
162     public List<PfKey> getKeys() {
163         final List<PfKey> keyList = super.getKeys();
164
165         if (targets != null) {
166             keyList.addAll(targets);
167         }
168
169         return keyList;
170     }
171
172     @Override
173     public void clean() {
174         super.clean();
175
176         if (targets != null) {
177             for (PfConceptKey target : targets) {
178                 target.clean();
179             }
180         }
181     }
182
183     @Override
184     public BeanValidationResult validate(String fieldName) {
185         BeanValidationResult result = super.validate(fieldName);
186
187         validateKeyVersionNotNull(result, "key", getKey());
188
189         return result;
190     }
191
192     @Override
193     public int compareTo(final PfConcept otherConcept) {
194         if (this == otherConcept) {
195             return 0;
196         }
197
198         int result = super.compareTo(otherConcept);
199         if (result != 0) {
200             return result;
201         }
202
203         final JpaToscaPolicy other = (JpaToscaPolicy) otherConcept;
204
205         return PfUtils.compareCollections(targets, other.targets);
206     }
207 }