[SDC] rebase 1710 code
[sdc.git] / common-be / src / main / java / org / openecomp / sdc / be / datatypes / tosca / ToscaDataDefinition.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.sdc.be.datatypes.tosca;
22
23 import java.util.HashMap;
24 import java.util.List;
25 import java.util.Map;
26 import java.util.Map.Entry;
27 import java.util.stream.Collectors;
28
29 import org.codehaus.jackson.annotate.JsonCreator;
30 import org.codehaus.jackson.annotate.JsonIgnore;
31 import org.codehaus.jackson.annotate.JsonValue;
32 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
33 import org.openecomp.sdc.be.datatypes.enums.JsonPresentationFields;
34
35 import fj.data.Either;
36
37 public abstract class ToscaDataDefinition {
38         
39         protected Map<String, Object> toscaPresentation;
40
41         
42         public ToscaDataDefinition(){
43                 toscaPresentation = new HashMap<String, Object>();
44         }
45         @JsonCreator
46         public ToscaDataDefinition(Map<String, Object> art){
47                 toscaPresentation = art;
48         }
49         @JsonValue
50         public Object getToscaPresentationValue(JsonPresentationFields name) {
51                 if (toscaPresentation != null && toscaPresentation.containsKey(name.getPresentation())) {
52                         return toscaPresentation.get(name.getPresentation());
53                 }
54                 return null;
55         }
56         
57         public void setToscaPresentationValue(JsonPresentationFields name, Object value) {
58                 if (toscaPresentation == null && value !=null) {
59                         toscaPresentation = new HashMap<String, Object>();                      
60                 }
61                 toscaPresentation.put(name.getPresentation(), value);
62                 
63         }
64         public void setOwnerIdIfEmpty(String ownerId){
65                 if (  getOwnerId() == null ){
66                         setOwnerId(ownerId);
67                 }
68         }
69         public void setOwnerId(String ownerId){
70                 setToscaPresentationValue(JsonPresentationFields.OWNER_ID, ownerId);
71         }
72
73         public String getOwnerId(){
74                 return (String) getToscaPresentationValue(JsonPresentationFields.OWNER_ID);
75         }
76         
77         
78         //default merge function for merging data maps - implement where needed and use mergeDataMaps method where applicable instead of map1.putAll(map2) 
79         public <T extends ToscaDataDefinition> T mergeFunction(T other, boolean allowDefaultValueOverride){
80                 other.setOwnerId(getOwnerId());
81                 return other;
82         }
83         
84         public static <T extends ToscaDataDefinition> Either<Map<String, T>, String> mergeDataMaps(Map<String, T> map1, Map<String, T> map2){
85                 return mergeDataMaps(map1, map2, false);
86         }
87         
88         //return Either.right(item key) if an illegal merge was attempted (overriding data type is forbidden)
89         public static <T extends ToscaDataDefinition> Either<Map<String, T>, String> mergeDataMaps(Map<String, T> map1, Map<String, T> map2, boolean allowDefaultValueOverride){
90                 for(Entry<String, T> entry : map2.entrySet()){
91                         map1.merge(entry.getKey(), entry.getValue(), (item1, item2) -> item1.mergeFunction(item2, allowDefaultValueOverride));
92                     //validate merge success
93                     if(!map1.containsKey(entry.getKey()))
94                         return Either.right(entry.getKey());
95                 }
96                 return Either.left(map1);
97         }
98         
99         public static <T extends ToscaDataDefinition> Map<String, T> listToMapByName(List<T> dataList) {
100                 return null == dataList? new HashMap<>() : dataList.stream()
101                 .collect(Collectors.toMap(p -> (String)p.getToscaPresentationValue(JsonPresentationFields.NAME), p -> p));
102         }
103
104         public boolean findUidMatch(String uid){
105                 return uid.equals(getToscaPresentationValue(JsonPresentationFields.UNIQUE_ID));
106         }
107 }