Added oparent to sdc main
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / components / merge / utils / ComponentInstanceBuildingBlocks.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.sdc.be.components.merge.utils;
22
23 import org.openecomp.sdc.be.datatypes.elements.GroupDataDefinition;
24 import org.openecomp.sdc.be.model.ComponentInstance;
25 import org.openecomp.sdc.be.model.GroupDefinition;
26
27 import java.util.ArrayList;
28 import java.util.HashMap;
29 import java.util.List;
30 import java.util.Map;
31 import java.util.stream.Collectors;
32 import java.util.stream.Stream;
33
34 import static org.openecomp.sdc.be.dao.utils.MapUtil.mergeMaps;
35 import static org.openecomp.sdc.be.dao.utils.MapUtil.toMap;
36
37 public class ComponentInstanceBuildingBlocks {
38
39     private Map<String, GroupDefinition> groups = new HashMap<>();
40     private Map<String, ComponentInstance> vfcInstances = new HashMap<>();
41
42     private ComponentInstanceBuildingBlocks(){}
43
44     private ComponentInstanceBuildingBlocks(List<GroupDefinition> groups, List<ComponentInstance> vfcInstances) {
45         this.groups = groups == null ? new HashMap<>() : toMap(groups, GroupDataDefinition::getUniqueId);
46         this.vfcInstances = vfcInstances == null ? new HashMap<>() : toMap(vfcInstances, ComponentInstance::getUniqueId);
47     }
48
49     private ComponentInstanceBuildingBlocks(Map<String, GroupDefinition> groups,  Map<String, ComponentInstance> vfcInstances) {
50         this.groups = groups == null ? new HashMap<>() : groups;
51         this.vfcInstances = vfcInstances == null ? new HashMap<>() : vfcInstances;
52     }
53
54     static ComponentInstanceBuildingBlocks of(List<GroupDefinition> groups, List<ComponentInstance> instances) {
55         return new ComponentInstanceBuildingBlocks(groups, instances);
56     }
57
58     static ComponentInstanceBuildingBlocks empty() {
59         return new ComponentInstanceBuildingBlocks();
60     }
61
62
63     @SuppressWarnings("unchecked")
64     static ComponentInstanceBuildingBlocks merge(ComponentInstanceBuildingBlocks first, ComponentInstanceBuildingBlocks second) {
65         return new ComponentInstanceBuildingBlocks(mergeMaps(first.groups, second.groups),
66                                                    mergeMaps(first.vfcInstances, second.vfcInstances));
67     }
68
69     public List<GroupDefinition> getGroups() {
70         return new ArrayList<>(groups.values());
71     }
72
73     public List<ComponentInstance> getVfcInstances() {
74         return new ArrayList<>(vfcInstances.values());
75     }
76
77     public List<CapabilityOwner> getCapabilitiesOwners() {
78         Stream<CapabilityOwner> groupsStream = groups.values().stream()
79                 .map(grp -> new CapabilityOwner(grp.getUniqueId(), grp.getInvariantName(), grp.getCapabilities()));
80
81         Stream<CapabilityOwner> instanceStream = vfcInstances.values().stream()
82                 .map(instance -> new CapabilityOwner(instance.getUniqueId(), instance.getName(), instance.getCapabilities()));
83
84         return Stream.concat(groupsStream, instanceStream).collect(Collectors.toList());
85     }
86     
87 }