Merge "Use common LocalDateTimeTypeAdaptor for SO"
[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-2020 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 io.swagger.annotations.ApiModelProperty;
27 import java.util.LinkedHashMap;
28 import java.util.List;
29 import java.util.Map;
30 import java.util.Map.Entry;
31 import javax.ws.rs.core.Response;
32 import lombok.Data;
33 import lombok.NoArgsConstructor;
34 import lombok.NonNull;
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;
47     private String version;
48
49     @ApiModelProperty(name = "derived_from")
50     @SerializedName("derived_from")
51     private String derivedFrom;
52
53     private Map<String, String> metadata;
54     private String description;
55
56     /**
57      * Copy Constructor.
58      *
59      * @param copyObject object to copy from
60      */
61     public ToscaEntity(@NonNull ToscaEntity copyObject) {
62         this.name = copyObject.name;
63         this.version = copyObject.version;
64         this.derivedFrom = copyObject.derivedFrom;
65         this.description = copyObject.description;
66
67         if (copyObject.metadata != null) {
68             metadata = new LinkedHashMap<>();
69             for (final Entry<String, String> metadataEntry : copyObject.metadata.entrySet()) {
70                 metadata.put(metadataEntry.getKey(), metadataEntry.getValue());
71             }
72         }
73     }
74
75     /**
76      * Get a key for this entity.
77      *
78      * @return a ToscaEntityKey for this entry
79      */
80     public ToscaEntityKey getKey() {
81         return new ToscaEntityKey(name, version);
82     }
83
84     /**
85      * Convert a list of maps of TOSCA entities into a regular map.
86      *
87      * @param listOfMapsOfEntities the incoming list of maps of entities
88      * @return The entities on a regular map
89      * @throws PfModelRuntimeException on duplicate entity entries
90      */
91     public static <T extends ToscaEntity> Map<ToscaEntityKey, T> getEntityListMapAsMap(
92             List<Map<String, T>> listOfMapsOfEntities) {
93         // Declare the return map
94         Map<ToscaEntityKey, T> entityMap = new LinkedHashMap<>();
95
96         for (Map<String, T> mapOfEntities : listOfMapsOfEntities) {
97             for (T entityEntry : mapOfEntities.values()) {
98                 if (entityMap.containsKey(entityEntry.getKey())) {
99                     throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR,
100                             "list of map of entities contains more than one entity with key " + entityEntry.getKey());
101                 }
102
103                 entityMap.put(entityEntry.getKey(), entityEntry);
104             }
105         }
106
107         return entityMap;
108     }
109
110     /**
111      * Convert a map of TOSCA entities into a regular map.
112      *
113      * @param mapOfEntities the incoming list of maps of entities
114      * @return The entities on a regular map
115      * @throws PfModelRuntimeException on duplicate entity entries
116      */
117     public static <T extends ToscaEntity> Map<ToscaEntityKey, T> getEntityMapAsMap(Map<String, T> mapOfEntities) {
118         // Declare the return map
119         Map<ToscaEntityKey, T> entityMap = new LinkedHashMap<>();
120
121         for (T entityEntry : mapOfEntities.values()) {
122             if (entityMap.containsKey(entityEntry.getKey())) {
123                 throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR,
124                         "list of map of entities contains more than one entity with key " + entityEntry.getKey());
125             }
126
127             entityMap.put(entityEntry.getKey(), entityEntry);
128         }
129
130         return entityMap;
131     }
132
133 }