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;
25 import org.openecomp.sdc.be.components.ArtifactsResolver;
26 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
27 import org.openecomp.sdc.be.model.ArtifactDefinition;
28 import org.openecomp.sdc.be.model.Component;
29 import org.openecomp.sdc.be.model.ComponentInstance;
30 import org.openecomp.sdc.be.model.Service;
33 @org.springframework.stereotype.Component("artifact-resolver")
34 public class ArtifactResolverImpl implements ArtifactsResolver {
37 public ArtifactDefinition findArtifactOnComponent(Component component, ComponentTypeEnum componentType, String artifactId) {
38 List<ArtifactDefinition> allComponentsArtifacts = getAllComponentsArtifacts(component, componentType);
39 return findById(allComponentsArtifacts, artifactId);
44 public ArtifactDefinition findArtifactOnComponentInstance(ComponentInstance componentInstance, String artifactId) {
45 List<ArtifactDefinition> allInstanceArtifacts = getAllInstanceArtifacts(componentInstance);
46 return findById(allInstanceArtifacts, artifactId);
49 private ArtifactDefinition findById(List<ArtifactDefinition> artifacts, String artifactId) {
50 return artifacts.stream()
51 .filter(artifact -> artifact.getUniqueId().equals(artifactId))
52 .findFirst().orElse(null);
55 private List<ArtifactDefinition> getAllComponentsArtifacts(Component component, ComponentTypeEnum componentType) {
56 Map<String, ArtifactDefinition> deploymentArtifacts = Optional.ofNullable(component.getDeploymentArtifacts()).orElse(Collections.emptyMap());
57 Map<String, ArtifactDefinition> artifacts = Optional.ofNullable(component.getArtifacts()).orElse(Collections.emptyMap());
58 Map<String, ArtifactDefinition> serviceApiArtifacts = Collections.emptyMap();
59 if (componentType.equals(ComponentTypeEnum.SERVICE)) {
60 serviceApiArtifacts = Optional.ofNullable(((Service) component).getServiceApiArtifacts()).orElse(Collections.emptyMap());
62 return appendAllArtifacts(deploymentArtifacts, artifacts, serviceApiArtifacts);
65 private List<ArtifactDefinition> getAllInstanceArtifacts(ComponentInstance instance) {
66 Map<String, ArtifactDefinition> deploymentArtifacts = Optional.ofNullable(instance.getDeploymentArtifacts()).orElse(Collections.emptyMap());
67 Map<String, ArtifactDefinition> artifacts = Optional.ofNullable(instance.getArtifacts()).orElse(Collections.emptyMap());
68 return appendAllArtifacts(deploymentArtifacts, artifacts);
72 private final List<ArtifactDefinition> appendAllArtifacts(Map<String, ArtifactDefinition>... artifacts) {
73 List<ArtifactDefinition> allArtifacts = new ArrayList<>();
74 Arrays.stream(artifacts).forEach(a -> allArtifacts.addAll(a.values()));