1 package org.onap.sdc.ci.tests.devObjects;
3 import com.fasterxml.jackson.annotation.JsonCreator;
4 import com.fasterxml.jackson.annotation.JsonValue;
7 import java.util.HashMap;
10 import java.util.Map.Entry;
11 import java.util.stream.Collectors;
13 public abstract class ToscaDataDefinition {
15 protected Map<String, Object> toscaPresentation;
18 public ToscaDataDefinition(){
19 toscaPresentation = new HashMap<String, Object>();
22 public ToscaDataDefinition(Map<String, Object> art){
23 toscaPresentation = art;
26 public Object getToscaPresentationValue(JsonPresentationFields name) {
27 if (toscaPresentation != null && toscaPresentation.containsKey(name.getPresentation())) {
28 return toscaPresentation.get(name.getPresentation());
33 public void setToscaPresentationValue(JsonPresentationFields name, Object value) {
34 if (toscaPresentation == null && value !=null) {
35 toscaPresentation = new HashMap<String, Object>();
37 toscaPresentation.put(name.getPresentation(), value);
40 public void setOwnerIdIfEmpty(String ownerId){
41 if ( getOwnerId() == null ){
45 public void setOwnerId(String ownerId){
46 setToscaPresentationValue(JsonPresentationFields.OWNER_ID, ownerId);
49 public String getOwnerId(){
50 return (String) getToscaPresentationValue(JsonPresentationFields.OWNER_ID);
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());
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);
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());
72 return Either.left(map1);
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));