1c272770b3e6313a66381dd63be0576417db0157
[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 import org.openecomp.sdc.translator.services.heattotosca.ConsolidationDataUtil;
35
36 /**
37  * The type Entity consolidation data.
38  */
39 public class EntityConsolidationData {
40
41     private String nodeTemplateId;
42
43     //groups that point to this entity node template
44     private List<String> groupIds;
45
46     // key - node template id which has connection to this entity
47     // value - List of Requirement assignment data which connect to this entity
48     private Multimap<String, RequirementAssignmentData> nodesConnectedIn;
49
50     // key - node template id which connected from this entity
51     // List of Requirement assignment data which connect to the key node template id
52     private Multimap<String, RequirementAssignmentData> nodesConnectedOut;
53
54     //key - node template id which include get attribute function from this entity
55     //value - List of getAttr data
56     private Map<String, List<GetAttrFuncData>> nodesGetAttrIn;
57
58     //key - node template id which is pointed by this entity using get attribute function
59     //value - List of getAttr data
60     private Map<String, List<GetAttrFuncData>> nodesGetAttrOut;
61
62     //List of getAttr data
63     private List<GetAttrFuncData> outputParametersGetAttrIn;
64
65     public String getNodeTemplateId() {
66         return nodeTemplateId;
67     }
68
69     public void setNodeTemplateId(String nodeTemplateId) {
70         this.nodeTemplateId = nodeTemplateId;
71     }
72
73     /**
74      * Gets group ids point to me.
75      *
76      * @return the group ids point to me
77      */
78     public List<String> getGroupIds() {
79         return groupIds;
80     }
81
82     /**
83      * Sets group ids point to me.
84      *
85      * @param groupIds the group ids point to me
86      */
87     public void setGroupIds(List<String> groupIds) {
88         this.groupIds = groupIds;
89     }
90
91
92     /**
93      * Sets node connected to me.
94      *
95      * @param nodesConnectedIn the node connected to me
96      */
97     public void setNodesConnectedIn(Multimap<String, RequirementAssignmentData> nodesConnectedIn) {
98         this.nodesConnectedIn = nodesConnectedIn;
99     }
100
101     /**
102      * Add node connected to me.
103      *
104      * @param nodeTemplateId        the node template id which has connection to me
105      * @param requirementId         the requirement id
106      * @param requirementAssignment the requirement assignment
107      */
108     public void addNodesConnectedIn(String nodeTemplateId, String requirementId,
109                                            RequirementAssignment requirementAssignment) {
110
111         if (this.nodesConnectedIn == null) {
112             this.nodesConnectedIn = ArrayListMultimap.create();
113         }
114
115         this.nodesConnectedIn.get(nodeTemplateId).add(
116                 new RequirementAssignmentData(requirementId, requirementAssignment));
117     }
118
119     /**
120      * Gets node connected to me.
121      *
122      * @return the node connected to me
123      */
124     public Multimap<String, RequirementAssignmentData> getNodesConnectedIn() {
125         return nodesConnectedIn;
126     }
127
128
129     /**
130      * Gets node connected from me.
131      *
132      * @return the node connected from me
133      */
134     public Multimap<String, RequirementAssignmentData> getNodesConnectedOut() {
135         return nodesConnectedOut;
136     }
137
138     /**
139      * Sets node connected from me.
140      *
141      * @param nodesConnectedOut the node connected from me
142      */
143     public void setNodesConnectedOut(Multimap<String, RequirementAssignmentData> nodesConnectedOut) {
144         this.nodesConnectedOut = nodesConnectedOut;
145     }
146
147     /**
148      * Add nodeConnectedOut.
149      *
150      * @param nodeTemplateId        the node template id which is connected from me
151      * @param requirementId         the requirement id
152      * @param requirementAssignment the requirement assignment
153      */
154     public void addNodesConnectedOut(String nodeTemplateId, String requirementId,
155                                             RequirementAssignment requirementAssignment) {
156
157         if (this.nodesConnectedOut == null) {
158             this.nodesConnectedOut = ArrayListMultimap.create();
159         }
160
161         this.nodesConnectedOut.get(nodeTemplateId).add(
162                 new RequirementAssignmentData(requirementId, requirementAssignment));
163     }
164
165     /**
166      * Gets nodes get attr in.
167      *
168      * @return the get attr in
169      */
170     public Map<String, List<GetAttrFuncData>> getNodesGetAttrIn() {
171         return nodesGetAttrIn;
172     }
173
174     /**
175      * Sets nodes get attr in.
176      *
177      * @param nodesGetAttrIn the get attr in
178      */
179     public void setNodesGetAttrIn(Map<String, List<GetAttrFuncData>> nodesGetAttrIn) {
180         this.nodesGetAttrIn = nodesGetAttrIn;
181     }
182
183     /**
184      * Add nodes get attr in data.
185      *
186      * @param nodeTemplateId  the node template id
187      * @param getAttrFuncData get attr data
188      */
189     public void addNodesGetAttrIn(String nodeTemplateId, GetAttrFuncData getAttrFuncData) {
190
191         if (nodesGetAttrIn == null) {
192             nodesGetAttrIn = new HashMap<>();
193         }
194
195         this.nodesGetAttrIn.putIfAbsent(nodeTemplateId, new ArrayList<>());
196         this.nodesGetAttrIn.get(nodeTemplateId).add(getAttrFuncData);
197     }
198
199     /**
200      * Gets output parameters get attr from me.
201      *
202      * @return the get attr from me
203      */
204     public List<GetAttrFuncData> getOutputParametersGetAttrIn() {
205         return outputParametersGetAttrIn;
206     }
207
208     /**
209      * Sets output parameters get attr from me.
210      *
211      * @param outputParametersGetAttrIn the output parameters get attr from me
212      */
213     public void setOutputParametersGetAttrIn(List<GetAttrFuncData> outputParametersGetAttrIn) {
214         this.outputParametersGetAttrIn = outputParametersGetAttrIn;
215     }
216
217     /**
218      * Add output parameters get attr data.
219      *
220      * @param getAttrFuncData get attr data
221      */
222     public void addOutputParamGetAttrIn(GetAttrFuncData getAttrFuncData) {
223
224         if (outputParametersGetAttrIn == null) {
225             outputParametersGetAttrIn = new ArrayList<>();
226         }
227
228         this.outputParametersGetAttrIn.add(getAttrFuncData);
229     }
230
231     /**
232      * Gets nodes get attr out.
233      *
234      * @return the get attr out
235      */
236     public Map<String, List<GetAttrFuncData>> getNodesGetAttrOut() {
237         return nodesGetAttrOut;
238     }
239
240     /**
241      * Sets nodes get attr out.
242      *
243      * @param nodesGetAttrOut the get attr out
244      */
245     public void setNodesGetAttrOut(Map<String, List<GetAttrFuncData>> nodesGetAttrOut) {
246         this.nodesGetAttrOut = nodesGetAttrOut;
247     }
248
249     /**
250      * Add nodes get attr out data.
251      *
252      * @param nodeTemplateId  the node template id
253      * @param getAttrFuncData get attr data
254      */
255     public void addNodesGetAttrOut(String nodeTemplateId, GetAttrFuncData getAttrFuncData) {
256
257         if (nodesGetAttrOut == null) {
258             nodesGetAttrOut = new HashMap<>();
259         }
260
261         this.nodesGetAttrOut.putIfAbsent(nodeTemplateId, new ArrayList<>());
262         this.nodesGetAttrOut.get(nodeTemplateId).add(getAttrFuncData);
263     }
264
265     public void removeParamNameFromAttrFuncList(String paramName) {
266
267         if (outputParametersGetAttrIn == null) {
268             return;
269         }
270
271         outputParametersGetAttrIn.removeIf(outputParameters -> paramName.equals(outputParameters.getFieldName()));
272     }
273
274     /**
275      * Per port type compare get attr of entity with given consolidation data list.
276      *
277      * @param entityConsolidationDataList consolidation data list
278      * @param portTypeToIds               the port type to ids
279      * @return true in case get attr list same for all port types.
280      *         otherwise return false
281      */
282     public boolean isGetAttrOutFromEntityLegal(Collection<? extends EntityConsolidationData>
283                     entityConsolidationDataList, Map<String, List<String>> portTypeToIds) {
284         if (CollectionUtils.isEmpty(entityConsolidationDataList)
285                 || MapUtils.isEmpty(portTypeToIds)) {
286             return true;
287         }
288
289         for (String portType : portTypeToIds.keySet()) {
290             Set<GetAttrFuncData> startingGetAttrFunc =
291                     getEntityGetAttrFuncAsSet(portType);
292
293             for (EntityConsolidationData entity : entityConsolidationDataList) {
294                 Set<GetAttrFuncData> currentGetAttrFuncData =
295                         entity.getEntityGetAttrFuncAsSet(portType);
296                 if (!(startingGetAttrFunc.equals(currentGetAttrFuncData))) {
297                     return false;
298                 }
299             }
300         }
301         return true;
302     }
303
304     private Set<GetAttrFuncData> getEntityGetAttrFuncAsSet(String portType) {
305         if (MapUtils.isEmpty(nodesGetAttrOut)) {
306             return new HashSet<>();
307         }
308
309         return nodesGetAttrOut.entrySet().stream()
310                               .filter(entry -> portType.equals(ConsolidationDataUtil.getPortType(entry.getKey())))
311                               .flatMap(entry -> entry.getValue().stream())
312                               .collect(Collectors.toSet());
313     }
314
315     /**
316      * Add group id information to consolidation data.
317      *
318      * @param groupId       Group id of which compute node is a part
319      */
320
321     void addGroupId(String groupId) {
322         if (groupIds == null) {
323             groupIds = new ArrayList<>();
324         }
325         groupIds.add(groupId);
326     }
327 }