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