re base code
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / components / impl / ArtifactResolverImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
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.impl;
22
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.*;
27
28 import java.util.*;
29 import java.util.stream.Collectors;
30
31 @org.springframework.stereotype.Component("artifact-resolver")
32 public class ArtifactResolverImpl implements ArtifactsResolver {
33
34     @Override
35     public ArtifactDefinition findArtifactOnComponent(Component component, ComponentTypeEnum componentType, String artifactId) {
36         List<ArtifactDefinition> allComponentsArtifacts = getAllComponentsArtifacts(component, componentType);
37         return findById(allComponentsArtifacts, artifactId);
38
39     }
40
41     @Override
42     public ArtifactDefinition findArtifactOnComponentInstance(ComponentInstance componentInstance, String artifactId) {
43         List<ArtifactDefinition> allInstanceArtifacts = getAllInstanceArtifacts(componentInstance);
44         return findById(allInstanceArtifacts, artifactId);
45     }
46
47     private ArtifactDefinition findById(List<ArtifactDefinition> artifacts, String artifactId) {
48         return artifacts.stream()
49                         .filter(artifact -> artifact.getUniqueId().equals(artifactId))
50                         .findFirst().orElse(null);
51     }
52
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         if (componentType == ComponentTypeEnum.RESOURCE) {
58             Map<String, InterfaceDefinition> interfaces = ((Resource) component).getInterfaces();
59             if (MapUtils.isNotEmpty(interfaces)) {
60                 interfaceArtifacts = interfaces.values().stream()
61                         .flatMap(inte -> inte.getOperationsMap().values().stream())
62                         .map(operation -> operation.getImplementationArtifact())
63                         .collect(Collectors.toMap(artifactDefinition -> artifactDefinition.getUniqueId(), artifactDefinition -> artifactDefinition));
64             }
65         }
66
67         Map<String, ArtifactDefinition> serviceApiArtifacts = Collections.emptyMap();
68         if (componentType.equals(ComponentTypeEnum.SERVICE)) {
69             serviceApiArtifacts = Optional.ofNullable(((Service) component).getServiceApiArtifacts()).orElse(Collections.emptyMap());
70         }
71         return appendAllArtifacts(deploymentArtifacts, artifacts, interfaceArtifacts, serviceApiArtifacts);
72     }
73
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);
78     }
79
80     @SafeVarargs
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()));
84         return allArtifacts;
85     }
86
87 }