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