2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.be.components.merge.utils;
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;
27 import java.util.ArrayList;
28 import java.util.HashMap;
29 import java.util.List;
31 import java.util.stream.Collectors;
32 import java.util.stream.Stream;
34 import static org.openecomp.sdc.be.dao.utils.MapUtil.mergeMaps;
35 import static org.openecomp.sdc.be.dao.utils.MapUtil.toMap;
37 public class ComponentInstanceBuildingBlocks {
39 private Map<String, GroupDefinition> groups = new HashMap<>();
40 private Map<String, ComponentInstance> vfcInstances = new HashMap<>();
42 private ComponentInstanceBuildingBlocks(){}
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);
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;
54 static ComponentInstanceBuildingBlocks of(List<GroupDefinition> groups, List<ComponentInstance> instances) {
55 return new ComponentInstanceBuildingBlocks(groups, instances);
58 static ComponentInstanceBuildingBlocks empty() {
59 return new ComponentInstanceBuildingBlocks();
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));
69 public List<GroupDefinition> getGroups() {
70 return new ArrayList<>(groups.values());
73 public List<ComponentInstance> getVfcInstances() {
74 return new ArrayList<>(vfcInstances.values());
77 public List<CapabilityOwner> getCapabilitiesOwners() {
78 Stream<CapabilityOwner> groupsStream = groups.values().stream()
79 .map(grp -> new CapabilityOwner(grp.getUniqueId(), grp.getInvariantName(), grp.getCapabilities()));
81 Stream<CapabilityOwner> instanceStream = vfcInstances.values().stream()
82 .map(instance -> new CapabilityOwner(instance.getUniqueId(), instance.getName(), instance.getCapabilities()));
84 return Stream.concat(groupsStream, instanceStream).collect(Collectors.toList());