814011d84653dfd991f95b7eb03b38b6c0c21ba8
[sdc.git] /
1 /*
2  * Copyright © 2016-2018 European Support Limited
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.openecomp.sdc.translator.datatypes.heattotosca.unifiedmodel.consolidation;
18
19 import com.google.common.collect.ArrayListMultimap;
20 import com.google.common.collect.Multimap;
21
22 import java.util.ArrayList;
23 import java.util.Collection;
24 import java.util.HashMap;
25 import java.util.HashSet;
26 import java.util.List;
27 import java.util.Map;
28 import java.util.Set;
29 import java.util.stream.Collectors;
30
31 import org.apache.commons.collections4.CollectionUtils;
32 import org.apache.commons.collections4.MapUtils;
33 import org.onap.sdc.tosca.datatypes.model.RequirementAssignment;
34
35 /**
36  * The type Entity consolidation data.
37  */
38 public class EntityConsolidationData {
39
40     private String nodeTemplateId;
41
42     //groups that point to this entity node template
43     private List<String> groupIds;
44
45     // key - node template id which has connection to this entity
46     // value - List of Requirement assignment data which connect to this entity
47     private Multimap<String, RequirementAssignmentData> nodesConnectedIn;
48
49     // key - node template id which connected from this entity
50     // List of Requirement assignment data which connect to the key node template id
51     private Multimap<String, RequirementAssignmentData> nodesConnectedOut;
52
53     //key - node template id which include get attribute function from this entity
54     //value - List of getAttr data
55     private Map<String, List<GetAttrFuncData>> nodesGetAttrIn;
56
57     //key - node template id which is pointed by this entity using get attribute function
58     //value - List of getAttr data
59     private Map<String, List<GetAttrFuncData>> nodesGetAttrOut;
60
61     //List of getAttr data
62     private List<GetAttrFuncData> outputParametersGetAttrIn;
63
64     public String getNodeTemplateId() {
65         return nodeTemplateId;
66     }
67
68     public void setNodeTemplateId(String nodeTemplateId) {
69         this.nodeTemplateId = nodeTemplateId;
70     }
71
72     /**
73      * Gets group ids point to me.
74      *
75      * @return the group ids point to me
76      */
77     public List<String> getGroupIds() {
78         return groupIds;
79     }
80
81     /**
82      * Sets group ids point to me.
83      *
84      * @param groupIds the group ids point to me
85      */
86     public void setGroupIds(List<String> groupIds) {
87         this.groupIds = groupIds;
88     }
89
90
91     /**
92      * Sets node connected to me.
93      *
94      * @param nodesConnectedIn the node connected to me
95      */
96     public void setNodesConnectedIn(Multimap<String, RequirementAssignmentData> nodesConnectedIn) {
97         this.nodesConnectedIn = nodesConnectedIn;
98     }
99
100     /**
101      * Add node connected to me.
102      *
103      * @param nodeTemplateId        the node template id which has connection to me
104      * @param requirementId         the requirement id
105      * @param requirementAssignment the requirement assignment
106      */
107     public void addNodesConnectedIn(String nodeTemplateId, String requirementId,
108                                     RequirementAssignment requirementAssignment) {
109
110         if (this.nodesConnectedIn == null) {
111             this.nodesConnectedIn = ArrayListMultimap.create();
112         }
113
114         this.nodesConnectedIn.get(nodeTemplateId).add(
115                 new RequirementAssignmentData(requirementId, requirementAssignment));
116     }
117
118     /**
119      * Gets node connected to me.
120      *
121      * @return the node connected to me
122      */
123     public Multimap<String, RequirementAssignmentData> getNodesConnectedIn() {
124         return nodesConnectedIn;
125     }
126
127
128     /**
129      * Gets node connected from me.
130      *
131      * @return the node connected from me
132      */
133     public Multimap<String, RequirementAssignmentData> getNodesConnectedOut() {
134         return nodesConnectedOut;
135     }
136
137     /**
138      * Sets node connected from me.
139      *
140      * @param nodesConnectedOut the node connected from me
141      */
142     public void setNodesConnectedOut(Multimap<String, RequirementAssignmentData> nodesConnectedOut) {
143         this.nodesConnectedOut = nodesConnectedOut;
144     }
145
146     /**
147      * Add nodeConnectedOut.
148      *
149      * @param nodeTemplateId        the node template id which is connected from me
150      * @param requirementId         the requirement id
151      * @param requirementAssignment the requirement assignment
152      */
153     public void addNodesConnectedOut(String nodeTemplateId, String requirementId,
154                                      RequirementAssignment requirementAssignment) {
155
156         if (this.nodesConnectedOut == null) {
157             this.nodesConnectedOut = ArrayListMultimap.create();
158         }
159
160         this.nodesConnectedOut.get(nodeTemplateId).add(
161                 new RequirementAssignmentData(requirementId, requirementAssignment));
162     }
163
164     /**
165      * Gets nodes get attr in.
166      *
167      * @return the get attr in
168      */
169     public Map<String, List<GetAttrFuncData>> getNodesGetAttrIn() {
170         return nodesGetAttrIn;
171     }
172
173     /**
174      * Sets nodes get attr in.
175      *
176      * @param nodesGetAttrIn the get attr in
177      */
178     public void setNodesGetAttrIn(Map<String, List<GetAttrFuncData>> nodesGetAttrIn) {
179         this.nodesGetAttrIn = nodesGetAttrIn;
180     }
181
182     /**
183      * Add nodes get attr in data.
184      *
185      * @param nodeTemplateId  the node template id
186      * @param getAttrFuncData get attr data
187      */
188     public void addNodesGetAttrIn(String nodeTemplateId, GetAttrFuncData getAttrFuncData) {
189
190         if (nodesGetAttrIn == null) {
191             nodesGetAttrIn = new HashMap<>();
192         }
193
194         this.nodesGetAttrIn.putIfAbsent(nodeTemplateId, new ArrayList<>());
195         this.nodesGetAttrIn.get(nodeTemplateId).add(getAttrFuncData);
196     }
197
198     /**
199      * Gets output parameters get attr from me.
200      *
201      * @return the get attr from me
202      */
203     public List<GetAttrFuncData> getOutputParametersGetAttrIn() {
204         return outputParametersGetAttrIn;
205     }
206
207     /**
208      * Sets output parameters get attr from me.
209      *
210      * @param outputParametersGetAttrIn the output parameters get attr from me
211      */
212     public void setOutputParametersGetAttrIn(List<GetAttrFuncData> outputParametersGetAttrIn) {
213         this.outputParametersGetAttrIn = outputParametersGetAttrIn;
214     }
215
216     /**
217      * Add output parameters get attr data.
218      *
219      * @param getAttrFuncData get attr data
220      */
221     public void addOutputParamGetAttrIn(GetAttrFuncData getAttrFuncData) {
222
223         if (outputParametersGetAttrIn == null) {
224             outputParametersGetAttrIn = new ArrayList<>();
225         }
226
227         this.outputParametersGetAttrIn.add(getAttrFuncData);
228     }
229
230     /**
231      * Gets nodes get attr out.
232      *
233      * @return the get attr out
234      */
235     public Map<String, List<GetAttrFuncData>> getNodesGetAttrOut() {
236         return nodesGetAttrOut;
237     }
238
239     /**
240      * Sets nodes get attr out.
241      *
242      * @param nodesGetAttrOut the get attr out
243      */
244     public void setNodesGetAttrOut(Map<String, List<GetAttrFuncData>> nodesGetAttrOut) {
245         this.nodesGetAttrOut = nodesGetAttrOut;
246     }
247
248     /**
249      * Add nodes get attr out data.
250      *
251      * @param nodeTemplateId  the node template id
252      * @param getAttrFuncData get attr data
253      */
254     public void addNodesGetAttrOut(String nodeTemplateId, GetAttrFuncData getAttrFuncData) {
255
256         if (nodesGetAttrOut == null) {
257             nodesGetAttrOut = new HashMap<>();
258         }
259
260         this.nodesGetAttrOut.putIfAbsent(nodeTemplateId, new ArrayList<>());
261         this.nodesGetAttrOut.get(nodeTemplateId).add(getAttrFuncData);
262     }
263
264     public void removeParamNameFromAttrFuncList(String paramName) {
265
266         if (outputParametersGetAttrIn == null) {
267             return;
268         }
269
270         outputParametersGetAttrIn.removeIf(outputParameters -> paramName.equals(outputParameters.getFieldName()));
271     }
272
273     /**
274      * Per port type compare get attr of entity with given consolidation data list.
275      *
276      * @param entityConsolidationDataList consolidation data list
277      * @param portTypeToIds               the port type to ids
278      * @return true in case get attr list same for all port types.
279      *         otherwise return false
280      */
281     public boolean isGetAttrOutFromEntityLegal(Collection<? extends EntityConsolidationData>
282                                                        entityConsolidationDataList, Map<String, List<String>> portTypeToIds) {
283         if (CollectionUtils.isEmpty(entityConsolidationDataList)
284                 || MapUtils.isEmpty(portTypeToIds)) {
285             return true;
286         }
287
288         for (String portType : portTypeToIds.keySet()) {
289             Set<GetAttrFuncData> startingGetAttrFunc =
290                     getEntityGetAttrFuncAsSet(portType, portTypeToIds);
291
292             for (EntityConsolidationData entity : entityConsolidationDataList) {
293                 Set<GetAttrFuncData> currentGetAttrFuncData =
294                         entity.getEntityGetAttrFuncAsSet(portType, portTypeToIds);
295                 if (!(startingGetAttrFunc.equals(currentGetAttrFuncData))) {
296                     return false;
297                 }
298             }
299         }
300         return true;
301     }
302
303     private Set<GetAttrFuncData> getEntityGetAttrFuncAsSet(String portType, Map<String, List<String>> portTypeToIds) {
304         if (MapUtils.isEmpty(nodesGetAttrOut)) {
305             return new HashSet<>();
306         }
307
308         return nodesGetAttrOut.entrySet().stream()
309                 .filter(entry -> portType.equals(getPortTypeFromNodeTemplateId(entry.getKey(),
310                         portTypeToIds)))
311                 .flatMap(entry -> entry.getValue().stream())
312                 .collect(Collectors.toSet());
313     }
314
315     private String getPortTypeFromNodeTemplateId(String portNodeTemplateId, Map<String, List<String>> portTypeToIds) {
316         for (Map.Entry<String, List<String>> portTypeToIdEntry : portTypeToIds.entrySet()) {
317             if (portTypeToIdEntry.getValue().contains(portNodeTemplateId)) {
318                 return portTypeToIdEntry.getKey();
319             }
320         }
321         return "";
322     }
323
324     /**
325      * Add group id information to consolidation data.
326      *
327      * @param groupId       Group id of which compute node is a part
328      */
329
330     void addGroupId(String groupId) {
331         if (groupIds == null) {
332             groupIds = new ArrayList<>();
333         }
334         groupIds.add(groupId);
335     }
336 }