[sdc] update code of sdc
[sdc.git] / common-be / src / main / java / org / openecomp / sdc / be / datatypes / tosca / ToscaDataDefinition.java
1 package org.openecomp.sdc.be.datatypes.tosca;
2
3 import java.util.HashMap;
4 import java.util.List;
5 import java.util.Map;
6 import java.util.Map.Entry;
7 import java.util.stream.Collectors;
8
9 import org.codehaus.jackson.annotate.JsonCreator;
10 import org.codehaus.jackson.annotate.JsonIgnore;
11 import org.codehaus.jackson.annotate.JsonValue;
12 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
13 import org.openecomp.sdc.be.datatypes.enums.JsonPresentationFields;
14
15 import fj.data.Either;
16
17 public abstract class ToscaDataDefinition {
18         
19         protected Map<String, Object> toscaPresentation;
20
21         
22         public ToscaDataDefinition(){
23                 toscaPresentation = new HashMap<String, Object>();
24         }
25         @JsonCreator
26         public ToscaDataDefinition(Map<String, Object> art){
27                 toscaPresentation = art;
28         }
29         @JsonValue
30         public Object getToscaPresentationValue(JsonPresentationFields name) {
31                 if (toscaPresentation != null && toscaPresentation.containsKey(name.getPresentation())) {
32                         return toscaPresentation.get(name.getPresentation());
33                 }
34                 return null;
35         }
36         
37         public void setToscaPresentationValue(JsonPresentationFields name, Object value) {
38                 if (toscaPresentation == null && value !=null) {
39                         toscaPresentation = new HashMap<String, Object>();                      
40                 }
41                 toscaPresentation.put(name.getPresentation(), value);
42                 
43         }
44         public void setOwnerIdIfEmpty(String ownerId){
45                 if (  getOwnerId() == null ){
46                         setOwnerId(ownerId);
47                 }
48         }
49         public void setOwnerId(String ownerId){
50                 setToscaPresentationValue(JsonPresentationFields.OWNER_ID, ownerId);
51         }
52
53         public String getOwnerId(){
54                 return (String) getToscaPresentationValue(JsonPresentationFields.OWNER_ID);
55         }
56         
57         
58         //default merge function for merging data maps - implement where needed and use mergeDataMaps method where applicable instead of map1.putAll(map2) 
59         public <T extends ToscaDataDefinition> T mergeFunction(T other, boolean allowDefaultValueOverride){
60                 other.setOwnerId(getOwnerId());
61                 return other;
62         }
63         
64         public static <T extends ToscaDataDefinition> Either<Map<String, T>, String> mergeDataMaps(Map<String, T> map1, Map<String, T> map2){
65                 return mergeDataMaps(map1, map2, false);
66         }
67         
68         //return Either.right(item key) if an illegal merge was attempted (overriding data type is forbidden)
69         public static <T extends ToscaDataDefinition> Either<Map<String, T>, String> mergeDataMaps(Map<String, T> map1, Map<String, T> map2, boolean allowDefaultValueOverride){
70                 for(Entry<String, T> entry : map2.entrySet()){
71                         map1.merge(entry.getKey(), entry.getValue(), (item1, item2) -> item1.mergeFunction(item2, allowDefaultValueOverride));
72                     //validate merge success
73                     if(!map1.containsKey(entry.getKey()))
74                         return Either.right(entry.getKey());
75                 }
76                 return Either.left(map1);
77         }
78         
79         public static <T extends ToscaDataDefinition> Map<String, T> listToMapByName(List<T> dataList) {
80                 return null == dataList? new HashMap<>() : dataList.stream()
81                 .collect(Collectors.toMap(p -> (String)p.getToscaPresentationValue(JsonPresentationFields.NAME), p -> p));
82         }
83 }