1 package org.openecomp.sdc.be.datatypes.tosca;
3 import java.util.HashMap;
6 import java.util.Map.Entry;
7 import java.util.stream.Collectors;
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;
15 import fj.data.Either;
17 public abstract class ToscaDataDefinition {
19 protected Map<String, Object> toscaPresentation;
22 public ToscaDataDefinition(){
23 toscaPresentation = new HashMap<String, Object>();
26 public ToscaDataDefinition(Map<String, Object> art){
27 toscaPresentation = art;
30 public Object getToscaPresentationValue(JsonPresentationFields name) {
31 if (toscaPresentation != null && toscaPresentation.containsKey(name.getPresentation())) {
32 return toscaPresentation.get(name.getPresentation());
37 public void setToscaPresentationValue(JsonPresentationFields name, Object value) {
38 if (toscaPresentation == null && value !=null) {
39 toscaPresentation = new HashMap<String, Object>();
41 toscaPresentation.put(name.getPresentation(), value);
44 public void setOwnerIdIfEmpty(String ownerId){
45 if ( getOwnerId() == null ){
49 public void setOwnerId(String ownerId){
50 setToscaPresentationValue(JsonPresentationFields.OWNER_ID, ownerId);
53 public String getOwnerId(){
54 return (String) getToscaPresentationValue(JsonPresentationFields.OWNER_ID);
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());
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);
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());
76 return Either.left(map1);
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));