[SDC-29] rebase continue work to align source
[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         public String getType(){
59                 return (String) getToscaPresentationValue(JsonPresentationFields.TYPE);
60         }
61         
62         public String getName(){
63                 return (String) getToscaPresentationValue(JsonPresentationFields.NAME);
64         }
65         
66         //default merge function for merging data maps - implement where needed and use mergeDataMaps method where applicable instead of map1.putAll(map2) 
67         public <T extends ToscaDataDefinition> T mergeFunction(T other, boolean allowDefaultValueOverride){
68                 other.setOwnerId(getOwnerId());
69                 return other;
70         }
71         
72         public static <T extends ToscaDataDefinition> Either<Map<String, T>, String> mergeDataMaps(Map<String, T> map1, Map<String, T> map2){
73                 return mergeDataMaps(map1, map2, false);
74         }
75         
76         //return Either.right(item key) if an illegal merge was attempted (overriding data type is forbidden)
77         public static <T extends ToscaDataDefinition> Either<Map<String, T>, String> mergeDataMaps(Map<String, T> map1, Map<String, T> map2, boolean allowDefaultValueOverride){
78                 for(Entry<String, T> entry : map2.entrySet()){
79                         map1.merge(entry.getKey(), entry.getValue(), (item1, item2) -> item1.mergeFunction(item2, allowDefaultValueOverride));
80                     //validate merge success
81                     if(!map1.containsKey(entry.getKey()))
82                         return Either.right(entry.getKey());
83                 }
84                 return Either.left(map1);
85         }
86         
87         public static <T extends ToscaDataDefinition> Map<String, T> listToMapByName(List<T> dataList) {
88                 return null == dataList? new HashMap<>() : dataList.stream()
89                 .collect(Collectors.toMap(p -> p.getName(), p -> p));
90         }
91 }