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