Revert "Delete workflow artifact
[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 java.util.*;
24
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;
31
32 import java.util.*;
33 @org.springframework.stereotype.Component("artifact-resolver")
34 public class ArtifactResolverImpl implements ArtifactsResolver {
35
36     @Override
37     public ArtifactDefinition findArtifactOnComponent(Component component, ComponentTypeEnum componentType, String artifactId) {
38         List<ArtifactDefinition> allComponentsArtifacts = getAllComponentsArtifacts(component, componentType);
39         return findById(allComponentsArtifacts, artifactId);
40
41     }
42
43     @Override
44     public ArtifactDefinition findArtifactOnComponentInstance(ComponentInstance componentInstance, String artifactId) {
45         List<ArtifactDefinition> allInstanceArtifacts = getAllInstanceArtifacts(componentInstance);
46         return findById(allInstanceArtifacts, artifactId);
47     }
48
49     private ArtifactDefinition findById(List<ArtifactDefinition> artifacts, String artifactId) {
50         return artifacts.stream()
51                         .filter(artifact -> artifact.getUniqueId().equals(artifactId))
52                         .findFirst().orElse(null);
53     }
54
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());
61         }
62         return appendAllArtifacts(deploymentArtifacts, artifacts, serviceApiArtifacts);
63     }
64
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);
69     }
70
71     @SafeVarargs
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()));
75         return allArtifacts;
76     }
77
78 }