Catalog alignment
[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.ArtifactDefinition;
27 import org.openecomp.sdc.be.model.Component;
28 import org.openecomp.sdc.be.model.ComponentInstance;
29 import org.openecomp.sdc.be.model.InterfaceDefinition;
30 import org.openecomp.sdc.be.model.Service;
31
32 import java.util.ArrayList;
33 import java.util.Arrays;
34 import java.util.Collections;
35 import java.util.List;
36 import java.util.Map;
37 import java.util.Objects;
38 import java.util.Optional;
39 import java.util.stream.Collectors;
40
41 @org.springframework.stereotype.Component("artifact-resolver")
42 public class ArtifactResolverImpl implements ArtifactsResolver {
43
44     @Override
45     public ArtifactDefinition findArtifactOnComponent(Component component, ComponentTypeEnum componentType, String artifactId) {
46         List<ArtifactDefinition> allComponentsArtifacts = getAllComponentsArtifacts(component, componentType);
47         return findById(allComponentsArtifacts, artifactId);
48
49     }
50
51     @Override
52     public ArtifactDefinition findArtifactOnComponentInstance(ComponentInstance componentInstance, String artifactId) {
53         List<ArtifactDefinition> allInstanceArtifacts = getAllInstanceArtifacts(componentInstance);
54         return findById(allInstanceArtifacts, artifactId);
55     }
56
57     private ArtifactDefinition findById(List<ArtifactDefinition> artifacts, String artifactId) {
58         return artifacts.stream()
59                         .filter(artifact -> artifact.getUniqueId().equals(artifactId))
60                         .findFirst().orElse(null);
61     }
62
63     private List<ArtifactDefinition> getAllComponentsArtifacts(Component component, ComponentTypeEnum componentType) {
64         Map<String, ArtifactDefinition> deploymentArtifacts = Optional.ofNullable(component.getDeploymentArtifacts()).orElse(Collections.emptyMap());
65         Map<String, ArtifactDefinition> artifacts = Optional.ofNullable(component.getArtifacts()).orElse(Collections.emptyMap());
66         Map<String, ArtifactDefinition> interfaceArtifacts= Collections.emptyMap();
67         Map<String, InterfaceDefinition> interfaces = component.getInterfaces();
68         if (MapUtils.isNotEmpty(interfaces)) {
69             interfaceArtifacts = interfaces.values().stream()
70                     .flatMap(inte -> inte.getOperationsMap().values().stream())
71                     .map(operation -> operation.getImplementationArtifact()).filter(Objects::nonNull)
72                     .collect(Collectors.toMap(artifactDefinition -> artifactDefinition.getUniqueId(),
73                             artifactDefinition -> artifactDefinition, (a1, a2) -> a1));
74         }
75
76         Map<String, ArtifactDefinition> serviceApiArtifacts = Collections.emptyMap();
77         if (componentType == ComponentTypeEnum.SERVICE) {
78             serviceApiArtifacts = Optional.ofNullable(((Service) component).getServiceApiArtifacts()).orElse(Collections.emptyMap());
79         }
80
81         return appendAllArtifacts(deploymentArtifacts, artifacts, interfaceArtifacts, serviceApiArtifacts);
82     }
83
84     private List<ArtifactDefinition> getAllInstanceArtifacts(ComponentInstance instance) {
85         Map<String, ArtifactDefinition> deploymentArtifacts = Optional.ofNullable(instance.getDeploymentArtifacts()).orElse(Collections.emptyMap());
86         Map<String, ArtifactDefinition> artifacts = Optional.ofNullable(instance.getArtifacts()).orElse(Collections.emptyMap());
87         return appendAllArtifacts(deploymentArtifacts, artifacts);
88     }
89
90     @SafeVarargs
91     private final List<ArtifactDefinition> appendAllArtifacts(Map<String, ArtifactDefinition>... artifacts) {
92         List<ArtifactDefinition> allArtifacts = new ArrayList<>();
93         Arrays.stream(artifacts).forEach(a -> allArtifacts.addAll(a.values()));
94         return allArtifacts;
95     }
96
97 }