Merge "CDS Actor service-provider implemntation"
[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 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
32 import javax.ws.rs.core.Response;
33
34 import lombok.Data;
35 import lombok.NoArgsConstructor;
36 import lombok.NonNull;
37
38 import org.onap.policy.models.base.PfModelRuntimeException;
39 import org.onap.policy.models.base.PfNameVersion;
40
41 /**
42  * Class to represent TOSCA data type matching input/output from/to client.
43  *
44  * @author Chenfei Gao (cgao@research.att.com)
45  */
46 @Data
47 @NoArgsConstructor
48 public class ToscaEntity implements PfNameVersion {
49     private String name;
50
51     private String version;
52
53     @ApiModelProperty(name = "derived_from")
54     @SerializedName("derived_from")
55     private String derivedFrom;
56
57     private Map<String, String> metadata;
58
59     private String description;
60
61     /**
62      * Copy Constructor.
63      *
64      * @param copyObject object to copy from
65      */
66     public ToscaEntity(@NonNull ToscaEntity copyObject) {
67         this.name = copyObject.name;
68         this.version = copyObject.version;
69         this.derivedFrom = copyObject.derivedFrom;
70         this.description = copyObject.description;
71
72         if (copyObject.metadata != null) {
73             metadata = new LinkedHashMap<>();
74             for (final Entry<String, String> metadataEntry : copyObject.metadata.entrySet()) {
75                 metadata.put(metadataEntry.getKey(), metadataEntry.getValue());
76             }
77         }
78     }
79
80     /**
81      * Get a key for this entity.
82      *
83      * @return a ToscaEntityKey for this entry
84      */
85     public ToscaEntityKey getKey() {
86         return new ToscaEntityKey(name, version);
87     }
88
89     /**
90      * Convert a list of maps of TOSCA entities into a regular map.
91      *
92      * @param listOfMapsOfEntities the incoming list of maps of entities
93      * @return The entities on a regular map
94      * @throws PfModelException on duplicate entity entries
95      */
96     public static <T extends ToscaEntity> Map<ToscaEntityKey, T> getEntityListMapAsMap(
97             List<Map<String, T>> listOfMapsOfEntities) {
98         // Declare the return map
99         Map<ToscaEntityKey, T> entityMap = new LinkedHashMap<>();
100
101         for (Map<String, T> mapOfEntities : listOfMapsOfEntities) {
102             for (T entityEntry : mapOfEntities.values()) {
103                 if (entityMap.containsKey(entityEntry.getKey())) {
104                     throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR,
105                             "list of map of entities contains more than one entity with key " + entityEntry.getKey());
106                 }
107
108                 entityMap.put(entityEntry.getKey(), entityEntry);
109             }
110         }
111
112         return entityMap;
113     }
114
115     /**
116      * Convert a map of TOSCA entities into a regular map.
117      *
118      * @param mapOfEntities the incoming list of maps of entities
119      * @return The entities on a regular map
120      * @throws PfModelException on duplicate entity entries
121      */
122     public static <T extends ToscaEntity> Map<ToscaEntityKey, T> getEntityMapAsMap(Map<String, T> mapOfEntities) {
123         // Declare the return map
124         Map<ToscaEntityKey, T> entityMap = new LinkedHashMap<>();
125
126         for (T entityEntry : mapOfEntities.values()) {
127             if (entityMap.containsKey(entityEntry.getKey())) {
128                 throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR,
129                         "list of map of entities contains more than one entity with key " + entityEntry.getKey());
130             }
131
132             entityMap.put(entityEntry.getKey(), entityEntry);
133         }
134
135         return entityMap;
136     }
137
138 }