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