639bcfbfcd2b72cad3db696db40c350658aaa16a
[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.HashMap;
24 import java.util.HashSet;
25 import java.util.List;
26 import java.util.Map;
27 import java.util.Set;
28
29 import org.apache.commons.collections4.MapUtils;
30 import org.onap.sdc.tosca.datatypes.model.RequirementAssignment;
31
32 public class ComputeTemplateConsolidationData extends EntityConsolidationData {
33     // key - volume node template id
34     // value - List of requirement id and the requirement assignment on the
35     // compute node which connect to this volume
36     private Multimap<String, RequirementAssignmentData> volumes;
37
38     // key - port type (port id excluding index),
39     // value - List of connected port node template ids, with this port type
40     private Map<String, List<String>> ports;
41
42     public Multimap<String, RequirementAssignmentData> getVolumes() {
43         return volumes;
44     }
45
46     public void setVolumes(Multimap<String, RequirementAssignmentData> volumes) {
47         this.volumes = volumes;
48     }
49
50     public Map<String, List<String>> getPorts() {
51         return ports;
52     }
53
54     public void setPorts(Map<String, List<String>> ports) {
55         this.ports = ports;
56     }
57
58     public void addPort(String portType, String portNodeTemplateId) {
59         if (this.ports == null) {
60             this.ports = new HashMap<>();
61         }
62         this.ports.putIfAbsent(portType, new ArrayList<>());
63         this.ports.get(portType).add(portNodeTemplateId);
64     }
65
66     public void addVolume(String requirementId, RequirementAssignment requirementAssignment) {
67         if (this.volumes == null) {
68             this.volumes = ArrayListMultimap.create();
69         }
70         this.volumes.put(requirementAssignment.getNode(), new RequirementAssignmentData(requirementId,
71                 requirementAssignment));
72     }
73
74     /**
75      * Collect all ports of each type from compute.
76      *
77      * @param portTypeToIds will be populated with all port of each type
78      */
79     public void collectAllPortsOfEachTypeFromCompute(Map<String, List<String>> portTypeToIds) {
80         if (MapUtils.isNotEmpty(ports)) {
81             for (Map.Entry<String, List<String>> portTypeToIdEntry : ports.entrySet()) {
82                 portTypeToIds.putIfAbsent(portTypeToIdEntry.getKey(), new ArrayList<>());
83                 portTypeToIds.get(portTypeToIdEntry.getKey()).addAll(portTypeToIdEntry.getValue());
84             }
85         }
86     }
87
88     /**
89      * Is number of port from each compute type legal.
90      *
91      * @return the boolean
92      */
93     public boolean isNumberOfPortFromEachTypeLegal() {
94         Map<String, List<String>> currPortsMap = getPorts();
95         return MapUtils.isEmpty(currPortsMap) || currPortsMap.values().stream()
96                                                              .allMatch(portList -> portList.size() == 1);
97     }
98
99     public Set<String> getPortsIds() {
100         return MapUtils.isEmpty(getPorts()) ? new HashSet<>() : getPorts().keySet();
101     }
102
103     public int getNumberOfPorts() {
104         return getPortsIds().size();
105     }
106 }