Sync Integ to Master
[sdc.git] / common-be / src / main / java / org / openecomp / sdc / be / datatypes / elements / MapDataDefinition.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.elements;
22
23
24
25 import com.fasterxml.jackson.annotation.JsonCreator;
26 import com.fasterxml.jackson.annotation.JsonValue;
27 import org.openecomp.sdc.be.datatypes.tosca.ToscaDataDefinition;
28
29 import java.util.HashMap;
30 import java.util.Map;
31 import java.util.Set;
32 import java.util.stream.Collectors;
33
34 public  class MapDataDefinition <T extends ToscaDataDefinition>  extends ToscaDataDefinition  {
35         
36         protected Map<String, T > mapToscaDataDefinition;
37         
38         public MapDataDefinition(MapDataDefinition<T> cdt) {
39                 super();
40                 mapToscaDataDefinition = cdt.mapToscaDataDefinition;    
41                 
42         }
43         @JsonCreator
44         public MapDataDefinition(Map<String, T > mapToscaDataDefinition) {
45                 super();
46                 this.mapToscaDataDefinition = mapToscaDataDefinition;   
47         }
48
49         public MapDataDefinition() {
50                 super();
51         }
52         @JsonValue
53         public Map<String, T > getMapToscaDataDefinition() {
54                 return mapToscaDataDefinition;
55         }
56         
57         public void put(String key, T value){
58                 if(mapToscaDataDefinition == null){
59                         mapToscaDataDefinition = new HashMap<String, T>();
60                 }
61                 mapToscaDataDefinition.put(key, value);
62         }
63         
64         public void delete(String key){
65                 if(mapToscaDataDefinition != null && mapToscaDataDefinition.containsKey(key)){
66                         mapToscaDataDefinition.remove(key);
67                 }
68         }
69         
70         public T findByKey(String key){
71                 T value = null;
72                 if(mapToscaDataDefinition != null && mapToscaDataDefinition.containsKey(key)){
73                         value = mapToscaDataDefinition.get(key);
74                 }
75                 return value;
76         }
77         @Override
78         public void setOwnerIdIfEmpty(String ownerId) {
79                 if ( mapToscaDataDefinition != null ){
80                         mapToscaDataDefinition.entrySet().forEach(e -> e.getValue().setOwnerIdIfEmpty(ownerId));
81                 }
82         }
83
84
85         public String findKeyByItemUidMatch(String uid){
86                 if(null == mapToscaDataDefinition || uid == null)
87                         return null;
88                 Map.Entry<String, T> entry = mapToscaDataDefinition.entrySet().stream().filter(e ->
89                                 e.getValue().findUidMatch(uid))
90                                 .findAny().orElse(null);
91                 if(null == entry)
92                         return null;
93                 return entry.getKey();
94         }
95         @Override
96         public <T extends ToscaDataDefinition>  T removeByOwnerId(Set<String> ownerIdList) {
97                 Map<String, T > collect = (Map<String, T >)mapToscaDataDefinition.entrySet().stream().filter(e -> ownerIdList.contains(e.getValue().getOwnerId())).collect(Collectors.toMap(Map.Entry::getKey, (Map.Entry::getValue)));
98                 
99                 MapDataDefinition collectMap = new MapDataDefinition<>(collect);
100                 
101                 mapToscaDataDefinition.entrySet().removeIf(e -> ownerIdList.contains(e.getValue().getOwnerId()));
102                 
103                 return (T) collectMap;
104         }
105
106         @Override
107         public <T extends ToscaDataDefinition> T updateIfExist(T other, boolean allowDefaultValueOverride) {
108                 
109                 Map<String, T > map = ((MapDataDefinition)other).getMapToscaDataDefinition();
110                 
111                 map.entrySet().forEach(e ->{
112                         String key = e.getKey();
113                         if ( mapToscaDataDefinition.containsKey(key) ){
114                                 e.getValue().mergeFunction(mapToscaDataDefinition.get(key), allowDefaultValueOverride);
115                         }
116                 });
117                 return other;
118         }
119         @Override
120         public boolean isEmpty(){
121                 return mapToscaDataDefinition == null || mapToscaDataDefinition.isEmpty();
122         }
123 }