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