summaryrefslogtreecommitdiffstats
path: root/openecomp-be/lib/openecomp-sdc-translator-lib/openecomp-sdc-translator-core/src/main/java/org/openecomp/sdc/translator/datatypes/heattotosca/unifiedmodel/consolidation/EntityConsolidationData.java
blob: 3987c540546ce00db032f05b7566db698e24f01a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/*
 * Copyright © 2016-2018 European Support Limited
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.openecomp.sdc.translator.datatypes.heattotosca.unifiedmodel.consolidation;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import org.apache.commons.collections4.MapUtils;
import org.onap.sdc.tosca.datatypes.model.RequirementAssignment;
import org.openecomp.sdc.translator.services.heattotosca.ConsolidationDataUtil;

/**
 * The type Entity consolidation data.
 */
public class EntityConsolidationData {

    private String nodeTemplateId;

    //groups that point to this entity node template
    private List<String> groupIds;

    // key - node template id which has connection to this entity
    // value - List of Requirement assignment data which connect to this entity
    private Map<String, List<RequirementAssignmentData>> nodesConnectedIn;

    // key - node template id which connected from this entity
    // List of Requirement assignment data which connect to the key node template id
    private Map<String, List<RequirementAssignmentData>> nodesConnectedOut;

    //key - node template id which include get attribute function from this entity
    //value - List of getAttr data
    private Map<String, List<GetAttrFuncData>> nodesGetAttrIn;

    //key - node template id which is pointed by this entity using get attribute function
    //value - List of getAttr data
    private Map<String, List<GetAttrFuncData>> nodesGetAttrOut;

    //List of getAttr data
    private List<GetAttrFuncData> outputParametersGetAttrIn;

    public String getNodeTemplateId() {
        return nodeTemplateId;
    }

    public void setNodeTemplateId(String nodeTemplateId) {
        this.nodeTemplateId = nodeTemplateId;
    }

    /**
     * Gets group ids point to me.
     *
     * @return the group ids point to me
     */
    public List<String> getGroupIds() {
        return groupIds;
    }

    /**
     * Sets group ids point to me.
     *
     * @param groupIds the group ids point to me
     */
    public void setGroupIds(List<String> groupIds) {
        this.groupIds = groupIds;
    }


    /**
     * Sets node connected to me.
     *
     * @param nodesConnectedIn the node connected to me
     */
    public void setNodesConnectedIn(Map<String, List<RequirementAssignmentData>> nodesConnectedIn) {
        this.nodesConnectedIn = nodesConnectedIn;
    }

    /**
     * Add node connected to me.
     *
     * @param nodeTemplateId        the node template id which has connection to me
     * @param requirementId         the requirement id
     * @param requirementAssignment the requirement assignment
     */
    public void addNodesConnectedIn(String nodeTemplateId, String requirementId,
                                           RequirementAssignment requirementAssignment) {

        if (this.nodesConnectedIn == null) {
            this.nodesConnectedIn = new HashMap<>();
        }

        this.nodesConnectedIn.computeIfAbsent(nodeTemplateId, k -> new ArrayList<>());
        this.nodesConnectedIn.get(nodeTemplateId).add(
                new RequirementAssignmentData(requirementId, requirementAssignment));
    }

    /**
     * Gets node connected to me.
     *
     * @return the node connected to me
     */
    public Map<String, List<RequirementAssignmentData>> getNodesConnectedIn() {
        return nodesConnectedIn;
    }


    /**
     * Gets node connected from me.
     *
     * @return the node connected from me
     */
    public Map<String, List<RequirementAssignmentData>> getNodesConnectedOut() {
        return nodesConnectedOut;
    }

    /**
     * Sets node connected from me.
     *
     * @param nodesConnectedOut the node connected from me
     */
    public void setNodesConnectedOut(Map<String, List<RequirementAssignmentData>> nodesConnectedOut) {
        this.nodesConnectedOut = nodesConnectedOut;
    }

    /**
     * Add nodeConnectedOut.
     *
     * @param nodeTemplateId        the node template id which is connected from me
     * @param requirementId         the requirement id
     * @param requirementAssignment the requirement assignment
     */
    public void addNodesConnectedOut(String nodeTemplateId, String requirementId,
                                            RequirementAssignment requirementAssignment) {

        if (this.nodesConnectedOut == null) {
            this.nodesConnectedOut = new HashMap<>();
        }

        this.nodesConnectedOut.computeIfAbsent(nodeTemplateId, k -> new ArrayList<>());
        this.nodesConnectedOut.get(nodeTemplateId).add(
                new RequirementAssignmentData(requirementId, requirementAssignment));
    }

    /**
     * Gets nodes get attr in.
     *
     * @return the get attr in
     */
    public Map<String, List<GetAttrFuncData>> getNodesGetAttrIn() {
        return nodesGetAttrIn;
    }

    /**
     * Sets nodes get attr in.
     *
     * @param nodesGetAttrIn the get attr in
     */
    public void setNodesGetAttrIn(Map<String, List<GetAttrFuncData>> nodesGetAttrIn) {
        this.nodesGetAttrIn = nodesGetAttrIn;
    }

    /**
     * Add nodes get attr in data.
     *
     * @param nodeTemplateId  the node template id
     * @param getAttrFuncData get attr data
     */
    public void addNodesGetAttrIn(String nodeTemplateId, GetAttrFuncData getAttrFuncData) {

        if (nodesGetAttrIn == null) {
            nodesGetAttrIn = new HashMap<>();
        }

        this.nodesGetAttrIn.putIfAbsent(nodeTemplateId, new ArrayList<>());
        this.nodesGetAttrIn.get(nodeTemplateId).add(getAttrFuncData);
    }

    /**
     * Gets output parameters get attr from me.
     *
     * @return the get attr from me
     */
    public List<GetAttrFuncData> getOutputParametersGetAttrIn() {
        return outputParametersGetAttrIn;
    }

    /**
     * Sets output parameters get attr from me.
     *
     * @param outputParametersGetAttrIn the output parameters get attr from me
     */
    public void setOutputParametersGetAttrIn(List<GetAttrFuncData> outputParametersGetAttrIn) {
        this.outputParametersGetAttrIn = outputParametersGetAttrIn;
    }

    /**
     * Add output parameters get attr data.
     *
     * @param getAttrFuncData get attr data
     */
    public void addOutputParamGetAttrIn(GetAttrFuncData getAttrFuncData) {

        if (outputParametersGetAttrIn == null) {
            outputParametersGetAttrIn = new ArrayList<>();
        }

        this.outputParametersGetAttrIn.add(getAttrFuncData);
    }

    /**
     * Gets nodes get attr out.
     *
     * @return the get attr out
     */
    public Map<String, List<GetAttrFuncData>> getNodesGetAttrOut() {
        return nodesGetAttrOut;
    }

    /**
     * Sets nodes get attr out.
     *
     * @param nodesGetAttrOut the get attr out
     */
    public void setNodesGetAttrOut(Map<String, List<GetAttrFuncData>> nodesGetAttrOut) {
        this.nodesGetAttrOut = nodesGetAttrOut;
    }

    /**
     * Add nodes get attr out data.
     *
     * @param nodeTemplateId  the node template id
     * @param getAttrFuncData get attr data
     */
    public void addNodesGetAttrOut(String nodeTemplateId, GetAttrFuncData getAttrFuncData) {

        if (nodesGetAttrOut == null) {
            nodesGetAttrOut = new HashMap<>();
        }

        this.nodesGetAttrOut.putIfAbsent(nodeTemplateId, new ArrayList<>());
        this.nodesGetAttrOut.get(nodeTemplateId).add(getAttrFuncData);
    }

    public void removeParamNameFromAttrFuncList(String paramName) {

        if (outputParametersGetAttrIn == null) {
            return;
        }

        outputParametersGetAttrIn.removeIf(outputParameters -> paramName.equals(outputParameters.getFieldName()));
    }

    /**
     * Per port type compare get attr of entity with given consolidation data list.
     *
     * @param entityConsolidationDataList consolidation data list
     * @param portTypeToIds               the port type to ids
     * @return true in case get attr list same for all port types.
     *         otherwise return false
     */
    public boolean isGetAttrOutFromEntityLegal(Collection<? extends EntityConsolidationData>
                    entityConsolidationDataList, Map<String, List<String>> portTypeToIds) {
        for (String portType : portTypeToIds.keySet()) {
            Set<GetAttrFuncData> startingGetAttrFunc =
                    getEntityGetAttrFuncAsSet(portType);

            for (EntityConsolidationData entity : entityConsolidationDataList) {
                Set<GetAttrFuncData> currentGetAttrFuncData =
                        entity.getEntityGetAttrFuncAsSet(portType);
                if (!(startingGetAttrFunc.equals(currentGetAttrFuncData))) {
                    return false;
                }
            }
        }
        return true;
    }

    private Set<GetAttrFuncData> getEntityGetAttrFuncAsSet(String portType) {
        if (MapUtils.isEmpty(nodesGetAttrOut)) {
            return new HashSet<>();
        }

        return nodesGetAttrOut.entrySet().stream()
                              .filter(entry -> portType.equals(ConsolidationDataUtil.getPortType(entry.getKey())))
                              .flatMap(entry -> entry.getValue().stream())
                              .collect(Collectors.toSet());
    }

    /**
     * Add group id information to consolidation data.
     *
     * @param groupId       Group id of which compute node is a part
     */

    void addGroupId(String groupId) {
        if (groupIds == null) {
            groupIds = new ArrayList<>();
        }
        groupIds.add(groupId);
    }
}