Java 17 Upgrade
[policy/models.git] / models-tosca / src / main / java / org / onap / policy / models / tosca / authorative / concepts / ToscaEntity.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP Policy Model
4  * ================================================================================
5  * Copyright (C) 2019-2023 Nordix Foundation.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  * SPDX-License-Identifier: Apache-2.0
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.models.tosca.authorative.concepts;
24
25 import com.google.gson.annotations.SerializedName;
26 import jakarta.ws.rs.core.Response;
27 import java.util.LinkedHashMap;
28 import java.util.List;
29 import java.util.Map;
30 import java.util.Map.Entry;
31 import lombok.Data;
32 import lombok.NoArgsConstructor;
33 import lombok.NonNull;
34 import org.onap.policy.models.base.PfKey;
35 import org.onap.policy.models.base.PfModelRuntimeException;
36 import org.onap.policy.models.base.PfNameVersion;
37
38 /**
39  * Class to represent TOSCA data type matching input/output from/to client.
40  *
41  * @author Chenfei Gao (cgao@research.att.com)
42  */
43 @Data
44 @NoArgsConstructor
45 public class ToscaEntity implements PfNameVersion {
46     private String name = PfKey.NULL_KEY_NAME;
47     private String version = PfKey.NULL_KEY_VERSION;
48
49     @SerializedName("derived_from")
50     private String derivedFrom;
51
52     private Map<String, Object> metadata;
53     private String description;
54
55     /**
56      * Copy Constructor.
57      *
58      * @param copyObject object to copy from
59      */
60     public ToscaEntity(@NonNull ToscaEntity copyObject) {
61         this.name = copyObject.name;
62         this.version = copyObject.version;
63         this.derivedFrom = copyObject.derivedFrom;
64         this.description = copyObject.description;
65
66         if (copyObject.metadata != null) {
67             metadata = new LinkedHashMap<>();
68             for (final Entry<String, Object> metadataEntry : copyObject.metadata.entrySet()) {
69                 metadata.put(metadataEntry.getKey(), metadataEntry.getValue());
70             }
71         }
72     }
73
74     /**
75      * Get a key for this entity.
76      *
77      * @return a ToscaEntityKey for this entry
78      */
79     public ToscaEntityKey getKey() {
80         return new ToscaEntityKey(name, version);
81     }
82
83     @Override
84     public String getDefinedName() {
85         return (PfKey.NULL_KEY_NAME.equals(name) ? null : name);
86     }
87
88     @Override
89     public String getDefinedVersion() {
90         return (PfKey.NULL_KEY_VERSION.equals(version) ? null : version);
91     }
92
93     /**
94      * Convert a list of maps of TOSCA entities into a regular map.
95      *
96      * @param listOfMapsOfEntities the incoming list of maps of entities
97      * @return The entities on a regular map
98      * @throws PfModelRuntimeException on duplicate entity entries
99      */
100     public static <T extends ToscaEntity> Map<ToscaEntityKey, T> getEntityListMapAsMap(
101             List<Map<String, T>> listOfMapsOfEntities) {
102         // Declare the return map
103         Map<ToscaEntityKey, T> entityMap = new LinkedHashMap<>();
104
105         if (listOfMapsOfEntities == null) {
106             return entityMap;
107         }
108
109         for (Map<String, T> mapOfEntities : listOfMapsOfEntities) {
110             for (T entityEntry : mapOfEntities.values()) {
111                 if (entityMap.containsKey(entityEntry.getKey())) {
112                     throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR,
113                         "list of map of entities contains more than one entity with key " + entityEntry.getKey());
114                 }
115                 entityMap.put(entityEntry.getKey(), entityEntry);
116             }
117         }
118
119         return entityMap;
120     }
121
122     /**
123      * Convert a map of TOSCA entities into a regular map.
124      *
125      * @param mapOfEntities the incoming list of maps of entities
126      * @return The entities on a regular map
127      * @throws PfModelRuntimeException on duplicate entity entries
128      */
129     public static <T extends ToscaEntity> Map<ToscaEntityKey, T> getEntityMapAsMap(Map<String, T> mapOfEntities) {
130         // Declare the return map
131         Map<ToscaEntityKey, T> entityMap = new LinkedHashMap<>();
132
133         if (mapOfEntities == null) {
134             return entityMap;
135         }
136
137         for (T entityEntry : mapOfEntities.values()) {
138             if (entityMap.containsKey(entityEntry.getKey())) {
139                 throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR,
140                     "list of map of entities contains more than one entity with key " + entityEntry.getKey());
141             }
142
143             entityMap.put(entityEntry.getKey(), entityEntry);
144         }
145
146         return entityMap;
147     }
148
149     /**
150      * Method that should be specialised to return the type of the entity if the entity has a type.
151      *
152      * @return the type of the entity or null if it has no type
153      */
154     public String getType() {
155         return null;
156     }
157
158     /**
159      * Method that should be specialised to return the type version of the entity if the entity has a type.
160      *
161      * @return the type of the entity or null if it has no type
162      */
163     public String getTypeVersion() {
164         return null;
165     }
166 }