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