c339f721e40ccad1541e54323d491ed6c3f3b2fc
[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-2022 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.PfKey;
36 import org.onap.policy.models.base.PfModelRuntimeException;
37 import org.onap.policy.models.base.PfNameVersion;
38
39 /**
40  * Class to represent TOSCA data type matching input/output from/to client.
41  *
42  * @author Chenfei Gao (cgao@research.att.com)
43  */
44 @Data
45 @NoArgsConstructor
46 public class ToscaEntity implements PfNameVersion {
47     private String name = PfKey.NULL_KEY_NAME;
48     private String version = PfKey.NULL_KEY_VERSION;
49
50     @ApiModelProperty(name = "derived_from")
51     @SerializedName("derived_from")
52     private String derivedFrom;
53
54     private Map<String, Object> metadata;
55     private String description;
56
57     /**
58      * Copy Constructor.
59      *
60      * @param copyObject object to copy from
61      */
62     public ToscaEntity(@NonNull ToscaEntity copyObject) {
63         this.name = copyObject.name;
64         this.version = copyObject.version;
65         this.derivedFrom = copyObject.derivedFrom;
66         this.description = copyObject.description;
67
68         if (copyObject.metadata != null) {
69             metadata = new LinkedHashMap<>();
70             for (final Entry<String, Object> metadataEntry : copyObject.metadata.entrySet()) {
71                 metadata.put(metadataEntry.getKey(), metadataEntry.getValue());
72             }
73         }
74     }
75
76     /**
77      * Get a key for this entity.
78      *
79      * @return a ToscaEntityKey for this entry
80      */
81     public ToscaEntityKey getKey() {
82         return new ToscaEntityKey(name, version);
83     }
84
85     @Override
86     public String getDefinedName() {
87         return (PfKey.NULL_KEY_NAME.equals(name) ? null : name);
88     }
89
90     @Override
91     public String getDefinedVersion() {
92         return (PfKey.NULL_KEY_VERSION.equals(version) ? null : version);
93     }
94
95     /**
96      * Convert a list of maps of TOSCA entities into a regular map.
97      *
98      * @param listOfMapsOfEntities the incoming list of maps of entities
99      * @return The entities on a regular map
100      * @throws PfModelRuntimeException on duplicate entity entries
101      */
102     public static <T extends ToscaEntity> Map<ToscaEntityKey, T> getEntityListMapAsMap(
103             List<Map<String, T>> listOfMapsOfEntities) {
104         // Declare the return map
105         Map<ToscaEntityKey, T> entityMap = new LinkedHashMap<>();
106
107         if (listOfMapsOfEntities == null) {
108             return entityMap;
109         }
110
111         for (Map<String, T> mapOfEntities : listOfMapsOfEntities) {
112             for (T entityEntry : mapOfEntities.values()) {
113                 if (entityMap.containsKey(entityEntry.getKey())) {
114                     throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR,
115                         "list of map of entities contains more than one entity with key " + entityEntry.getKey());
116                 }
117                 entityMap.put(entityEntry.getKey(), entityEntry);
118             }
119         }
120
121         return entityMap;
122     }
123
124     /**
125      * Convert a map of TOSCA entities into a regular map.
126      *
127      * @param mapOfEntities the incoming list of maps of entities
128      * @return The entities on a regular map
129      * @throws PfModelRuntimeException on duplicate entity entries
130      */
131     public static <T extends ToscaEntity> Map<ToscaEntityKey, T> getEntityMapAsMap(Map<String, T> mapOfEntities) {
132         // Declare the return map
133         Map<ToscaEntityKey, T> entityMap = new LinkedHashMap<>();
134
135         if (mapOfEntities == null) {
136             return entityMap;
137         }
138
139         for (T entityEntry : mapOfEntities.values()) {
140             if (entityMap.containsKey(entityEntry.getKey())) {
141                 throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR,
142                     "list of map of entities contains more than one entity with key " + entityEntry.getKey());
143             }
144
145             entityMap.put(entityEntry.getKey(), entityEntry);
146         }
147
148         return entityMap;
149     }
150
151     /**
152      * Method that should be specialised to return the type of the entity if the entity has a type.
153      *
154      * @return the type of the entity or null if it has no type
155      */
156     public String getType() {
157         return null;
158     }
159
160     /**
161      * Method that should be specialised to return the type version of the entity if the entity has a type.
162      *
163      * @return the type of the entity or null if it has no type
164      */
165     public String getTypeVersion() {
166         return null;
167     }
168 }