4637231de6ad2891cd2957eca632a743289064d6
[sdc.git] / common-be / src / main / java / org / openecomp / sdc / be / datatypes / tosca / ToscaDataDefinition.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
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  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.sdc.be.datatypes.tosca;
22
23 import com.fasterxml.jackson.annotation.JsonCreator;
24 import com.fasterxml.jackson.annotation.JsonValue;
25 import fj.data.Either;
26 import org.openecomp.sdc.be.datatypes.enums.JsonPresentationFields;
27
28 import java.util.HashMap;
29 import java.util.List;
30 import java.util.Map;
31 import java.util.Map.Entry;
32 import java.util.Set;
33 import java.util.stream.Collectors;
34
35 public abstract class ToscaDataDefinition {
36
37     protected Map<String, Object> toscaPresentation;
38
39     public ToscaDataDefinition() {
40         toscaPresentation = new HashMap<>();
41     }
42
43     @JsonCreator
44     public ToscaDataDefinition(Map<String, Object> art) {
45         toscaPresentation = art;
46     }
47
48     @JsonValue
49     public Object getToscaPresentationValue(JsonPresentationFields name) {
50         if (toscaPresentation != null && toscaPresentation.containsKey(name.getPresentation())) {
51             return toscaPresentation.get(name.getPresentation());
52         }
53         return null;
54     }
55
56     public void setToscaPresentationValue(JsonPresentationFields name, Object value) {
57         if(name !=null) {
58             if (toscaPresentation == null) {
59                 toscaPresentation = new HashMap<>();
60             }
61             toscaPresentation.put(name.getPresentation(), value);
62         }
63
64     }
65
66     public void setOwnerIdIfEmpty(String ownerId) {
67         if (getOwnerId() == null) {
68             setOwnerId(ownerId);
69         }
70     }
71
72     public String getType() {
73         return (String) getToscaPresentationValue(JsonPresentationFields.TYPE);
74     }
75
76     public String getVersion() {
77         return (String) getToscaPresentationValue(JsonPresentationFields.VERSION);
78     }
79
80     public void setOwnerId(String ownerId) {
81         setToscaPresentationValue(JsonPresentationFields.OWNER_ID, ownerId);
82     }
83
84     public String getOwnerId() {
85         return (String) getToscaPresentationValue(JsonPresentationFields.OWNER_ID);
86     }
87
88     // default merge function for merging data maps - implement where needed and use mergeDataMaps method where applicable instead of map1.putAll(map2)
89     public <T extends ToscaDataDefinition> T mergeFunction(T other, boolean allowDefaultValueOverride) {
90         other.setOwnerId(getOwnerId());
91         return other;
92     }
93
94     public static <T extends ToscaDataDefinition> Either<Map<String, T>, String> mergeDataMaps(Map<String, T> map1, Map<String, T> map2) {
95         return mergeDataMaps(map1, map2, false);
96     }
97
98     // return Either.right(item key) if an illegal merge was attempted (overriding data type is forbidden)
99     public static <T extends ToscaDataDefinition> Either<Map<String, T>, String> mergeDataMaps(Map<String, T> map1, Map<String, T> map2, boolean allowDefaultValueOverride) {
100         for (Entry<String, T> entry : map2.entrySet()) {
101             map1.merge(entry.getKey(), entry.getValue(), (item1, item2) -> item1.mergeFunction(item2, allowDefaultValueOverride));
102             // validate merge success
103             if (!map1.containsKey(entry.getKey())) {
104                 return Either.right(entry.getKey());
105             }
106         }
107         return Either.left(map1);
108     }
109
110     public static <T extends ToscaDataDefinition> Map<String, T> listToMapByName(List<T> dataList) {
111         return null == dataList ? new HashMap<>() : dataList.stream()
112                 .collect(Collectors.toMap(p -> (String) p.getToscaPresentationValue(JsonPresentationFields.NAME), p -> p));
113     }
114
115     public boolean findUidMatch(String uid) {
116         return uid.equals(getToscaPresentationValue(JsonPresentationFields.UNIQUE_ID));
117
118     }
119
120     public <T extends ToscaDataDefinition> T removeByOwnerId(Set<String> ownerIdList) {
121         return (T) this;
122     }
123
124     public static <T extends ToscaDataDefinition> T removeAndCollectByOwnerId(T complexStructure, Set<String> ownerIdList) {
125         return complexStructure.removeByOwnerId(ownerIdList);
126     }
127
128     public <T extends ToscaDataDefinition> T updateIfExist(T other, boolean allowDefaultValueOverride) {
129         return other;
130     }
131
132     public boolean isEmpty() {
133         return false;
134     }
135
136 }