2d62e36eaf3dd1a5fdc52ec43ccb76b0005d0e14
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2022,2024 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     @SuppressWarnings("squid:S1948")
54     private Map<String, Object> properties;
55
56     /**
57      * Copy constructor.
58      *
59      * @param copyConcept the concept to copy from
60      */
61     public DocToscaWithTypeAndStringProperties(final DocToscaWithTypeAndStringProperties<T> copyConcept) {
62         super(copyConcept);
63         this.type = copyConcept.type;
64         this.typeVersion = copyConcept.typeVersion;
65         this.properties = (copyConcept.properties != null ? new LinkedHashMap<>(copyConcept.properties) : null);
66     }
67
68     @Override
69     public T toAuthorative() {
70         var tosca = super.toAuthorative();
71
72         tosca.setType(type);
73         tosca.setTypeVersion(typeVersion);
74
75         tosca.setProperties(PfUtils.mapMap(properties, x -> x));
76
77         return tosca;
78     }
79
80     @Override
81     public void fromAuthorative(T authorativeConcept) {
82         super.fromAuthorative(authorativeConcept);
83         if (authorativeConcept.getType() == null) {
84             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST,
85                     "Type not specified, the type of this TOSCA entity must be specified in the type field");
86         }
87         var key = DocUtil.createDocConceptKey(authorativeConcept.getType(), authorativeConcept.getTypeVersion());
88         type = key.getName();
89         typeVersion = key.getVersion();
90
91         if (PfKey.NULL_KEY_VERSION.equals(typeVersion)) {
92             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST,
93                     "Version not specified, the version of this TOSCA entity must be specified"
94                             + " in the type_version field");
95         }
96
97         properties = PfUtils.mapMap(authorativeConcept.getProperties(), x -> x);
98     }
99
100     public DocConceptKey getTypeDocConceptKey() {
101         return new DocConceptKey(type, typeVersion);
102     }
103 }