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