55b7364c90377101d5c9cb0bc9d44b80f161c01b
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2022 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.clamp.models.acm.document.concepts;
22
23 import com.google.gson.annotations.SerializedName;
24 import jakarta.ws.rs.core.Response;
25 import java.io.Serial;
26 import java.util.LinkedHashMap;
27 import java.util.Map;
28 import lombok.Data;
29 import lombok.EqualsAndHashCode;
30 import lombok.NoArgsConstructor;
31 import lombok.ToString;
32 import org.onap.policy.clamp.models.acm.document.base.DocConceptKey;
33 import org.onap.policy.clamp.models.acm.document.base.DocUtil;
34 import org.onap.policy.models.base.PfKey;
35 import org.onap.policy.models.base.PfModelRuntimeException;
36 import org.onap.policy.models.base.PfUtils;
37 import org.onap.policy.models.tosca.authorative.concepts.ToscaWithTypeAndObjectProperties;
38
39 @Data
40 @EqualsAndHashCode(callSuper = true)
41 @NoArgsConstructor
42 @ToString
43 public class DocToscaWithTypeAndStringProperties<T extends ToscaWithTypeAndObjectProperties> extends DocToscaEntity<T> {
44
45     @Serial
46     private static final long serialVersionUID = 1L;
47
48     private String type;
49
50     @SerializedName("type_version")
51     private String typeVersion;
52
53     private Map<String, Object> properties;
54
55     /**
56      * Copy constructor.
57      *
58      * @param copyConcept the concept to copy from
59      */
60     public DocToscaWithTypeAndStringProperties(final DocToscaWithTypeAndStringProperties<T> copyConcept) {
61         super(copyConcept);
62         this.type = copyConcept.type;
63         this.typeVersion = copyConcept.typeVersion;
64         this.properties = (copyConcept.properties != null ? new LinkedHashMap<>(copyConcept.properties) : null);
65     }
66
67     @Override
68     public T toAuthorative() {
69         var tosca = super.toAuthorative();
70
71         tosca.setType(type);
72         tosca.setTypeVersion(typeVersion);
73
74         tosca.setProperties(PfUtils.mapMap(properties, x -> x));
75
76         return tosca;
77     }
78
79     @Override
80     public void fromAuthorative(T authorativeConcept) {
81         super.fromAuthorative(authorativeConcept);
82         if (authorativeConcept.getType() == null) {
83             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST,
84                     "Type not specified, the type of this TOSCA entity must be specified in the type field");
85         }
86         var key = DocUtil.createDocConceptKey(authorativeConcept.getType(), authorativeConcept.getTypeVersion());
87         type = key.getName();
88         typeVersion = key.getVersion();
89
90         if (PfKey.NULL_KEY_VERSION.equals(typeVersion)) {
91             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST,
92                     "Version not specified, the version of this TOSCA entity must be specified"
93                             + " in the type_version field");
94         }
95
96         properties = PfUtils.mapMap(authorativeConcept.getProperties(), x -> x);
97     }
98
99     public DocConceptKey getTypeDocConceptKey() {
100         return new DocConceptKey(type, typeVersion);
101     }
102 }