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