Sync Integ to Master
[sdc.git] / common-be / src / main / java / org / openecomp / sdc / be / datatypes / elements / ListDataDefinition.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 org.openecomp.sdc.be.datatypes.enums.JsonPresentationFields;
25 import org.openecomp.sdc.be.datatypes.tosca.ToscaDataDefinition;
26
27 import java.io.Serializable;
28 import java.util.ArrayList;
29 import java.util.List;
30 import java.util.Map;
31 import java.util.Optional;
32 import java.util.Set;
33 import java.util.stream.Collectors;
34
35 public class ListDataDefinition<T extends ToscaDataDefinition> extends ToscaDataDefinition  implements Serializable {
36
37         protected List<T> listToscaDataDefinition;
38
39         public ListDataDefinition(ListDataDefinition<T> cdt) {
40                 super();
41                 listToscaDataDefinition = cdt.listToscaDataDefinition;
42
43         }
44
45         public ListDataDefinition(List<T> listToscaDataDefinition) {
46                 super();
47                 this.listToscaDataDefinition = listToscaDataDefinition;
48         }
49
50         @JsonCreator
51         public ListDataDefinition() {
52                 super();
53                 this.listToscaDataDefinition = new ArrayList<>();
54         }
55
56         public List<T> getListToscaDataDefinition() {
57                 return listToscaDataDefinition;
58         }
59
60         public void add(T value) {
61                 if (listToscaDataDefinition == null) {
62                         listToscaDataDefinition = new ArrayList<T>();
63                 }
64                 listToscaDataDefinition.add(value);
65         }
66
67         public void delete(T value) {
68                 if (listToscaDataDefinition != null) {
69                         listToscaDataDefinition.remove(value);
70                 }
71         }
72
73         @Override
74         public void setOwnerIdIfEmpty(String ownerId) {
75                 if (listToscaDataDefinition != null) {
76                         listToscaDataDefinition.forEach(e -> e.setOwnerIdIfEmpty(ownerId));
77                 }
78         }
79
80         @Override
81         public <S extends ToscaDataDefinition> S mergeFunction(S other, boolean allowDefaultValueOverride) {
82                 Map<String, T> mapByName = listToMapByName(listToscaDataDefinition);
83                 List<T> otherList = ((ListDataDefinition) other).getListToscaDataDefinition();
84                 for (T item : otherList) {
85                         mapByName.merge((String) item.getToscaPresentationValue(JsonPresentationFields.NAME), item, (thisItem, otherItem) -> thisItem.mergeFunction(otherItem, allowDefaultValueOverride));
86                 }
87                 ((ListDataDefinition) other).listToscaDataDefinition = mapByName.values().stream().collect(Collectors.toList());
88                 return other;
89         }
90
91         @Override
92         public boolean findUidMatch(String uid) {
93                 return listToscaDataDefinition.stream().anyMatch(p -> p.findUidMatch(uid));
94         }
95
96         @Override
97         public <T extends ToscaDataDefinition> T removeByOwnerId(Set<String> ownerIdList) {
98                 List<T> collect1 = (List<T>) listToscaDataDefinition.stream().filter(e -> ownerIdList.contains(e.getOwnerId())).collect(Collectors.toList());
99                 ListDataDefinition listDef = new ListDataDefinition(collect1);
100
101                 listToscaDataDefinition.removeIf(e -> ownerIdList.contains(e.getOwnerId()));
102                 return (T) listDef;
103         }
104
105         @Override
106         public <T extends ToscaDataDefinition> T updateIfExist(T other, boolean allowDefaultValueOverride) {
107                 
108                 List<T> list = ((ListDataDefinition)other).getListToscaDataDefinition();
109                 list.forEach(e -> {
110                         String nameFromPrev = (String)e.getToscaPresentationValue(JsonPresentationFields.NAME);
111                         if ( nameFromPrev != null ){
112                                 Optional<T> findAny = (Optional<T>) listToscaDataDefinition.stream().filter(o->nameFromPrev.equals(e.getToscaPresentationValue(JsonPresentationFields.NAME))).findAny();
113                                 if ( findAny.isPresent() ){
114                                         e.mergeFunction(findAny.get(), allowDefaultValueOverride);
115                                 }
116                         }
117                 });
118                 return other;
119         }
120         @Override
121         public boolean isEmpty(){
122                 return listToscaDataDefinition == null || listToscaDataDefinition.isEmpty();
123         }
124 }