2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 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.impl;
23 import org.apache.commons.collections.MapUtils;
24 import org.openecomp.sdc.be.components.ArtifactsResolver;
25 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
26 import org.openecomp.sdc.be.model.*;
29 import java.util.stream.Collectors;
31 @org.springframework.stereotype.Component("artifact-resolver")
32 public class ArtifactResolverImpl implements ArtifactsResolver {
35 public ArtifactDefinition findArtifactOnComponent(Component component, ComponentTypeEnum componentType, String artifactId) {
36 List<ArtifactDefinition> allComponentsArtifacts = getAllComponentsArtifacts(component, componentType);
37 return findById(allComponentsArtifacts, artifactId);
42 public ArtifactDefinition findArtifactOnComponentInstance(ComponentInstance componentInstance, String artifactId) {
43 List<ArtifactDefinition> allInstanceArtifacts = getAllInstanceArtifacts(componentInstance);
44 return findById(allInstanceArtifacts, artifactId);
47 private ArtifactDefinition findById(List<ArtifactDefinition> artifacts, String artifactId) {
48 return artifacts.stream()
49 .filter(artifact -> artifact.getUniqueId().equals(artifactId))
50 .findFirst().orElse(null);
53 private List<ArtifactDefinition> getAllComponentsArtifacts(Component component, ComponentTypeEnum componentType) {
54 Map<String, ArtifactDefinition> deploymentArtifacts = Optional.ofNullable(component.getDeploymentArtifacts()).orElse(Collections.emptyMap());
55 Map<String, ArtifactDefinition> artifacts = Optional.ofNullable(component.getArtifacts()).orElse(Collections.emptyMap());
56 Map<String, ArtifactDefinition> interfaceArtifacts= Collections.emptyMap();
57 Map<String, InterfaceDefinition> interfaces = component.getInterfaces();
58 if (MapUtils.isNotEmpty(interfaces)) {
59 interfaceArtifacts = interfaces.values().stream()
60 .flatMap(inte -> inte.getOperationsMap().values().stream())
61 .map(operation -> operation.getImplementationArtifact()).filter(Objects::nonNull)
62 .collect(Collectors.toMap(artifactDefinition -> artifactDefinition.getUniqueId(),
63 artifactDefinition -> artifactDefinition, (a1, a2) -> a1));
66 Map<String, ArtifactDefinition> serviceApiArtifacts = Collections.emptyMap();
67 if (componentType.equals(ComponentTypeEnum.SERVICE)) {
68 serviceApiArtifacts = Optional.ofNullable(((Service) component).getServiceApiArtifacts()).orElse(Collections.emptyMap());
71 return appendAllArtifacts(deploymentArtifacts, artifacts, interfaceArtifacts, serviceApiArtifacts);
74 private List<ArtifactDefinition> getAllInstanceArtifacts(ComponentInstance instance) {
75 Map<String, ArtifactDefinition> deploymentArtifacts = Optional.ofNullable(instance.getDeploymentArtifacts()).orElse(Collections.emptyMap());
76 Map<String, ArtifactDefinition> artifacts = Optional.ofNullable(instance.getArtifacts()).orElse(Collections.emptyMap());
77 return appendAllArtifacts(deploymentArtifacts, artifacts);
81 private final List<ArtifactDefinition> appendAllArtifacts(Map<String, ArtifactDefinition>... artifacts) {
82 List<ArtifactDefinition> allArtifacts = new ArrayList<>();
83 Arrays.stream(artifacts).forEach(a -> allArtifacts.addAll(a.values()));