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.distribution.engine;
23 import java.util.ArrayList;
24 import java.util.Collection;
25 import java.util.List;
28 import javax.annotation.PostConstruct;
30 import org.openecomp.sdc.be.config.ConfigurationManager;
31 import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
32 import org.openecomp.sdc.be.model.ArtifactDefinition;
33 import org.openecomp.sdc.be.model.ComponentInstance;
34 import org.openecomp.sdc.be.model.Resource;
35 import org.openecomp.sdc.be.model.Service;
36 import org.openecomp.sdc.be.model.operations.api.IArtifactOperation;
37 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
38 import org.openecomp.sdc.be.model.operations.impl.InterfaceLifecycleOperation;
39 import org.openecomp.sdc.be.model.operations.impl.ResourceOperation;
40 import org.openecomp.sdc.be.model.operations.impl.ServiceOperation;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43 import org.springframework.stereotype.Component;
45 import fj.data.Either;
47 @Component("serviceDistributionArtifactsBuilder")
48 public class ServiceDistributionArtifactsBuilder {
50 private int defaultArtifactInstallTimeout = 60;
52 private static Logger logger = LoggerFactory.getLogger(ServiceDistributionArtifactsBuilder.class.getName());
54 final static String BASE_ARTIFACT_URL = "/asdc/v1/catalog/services/%s/%s/";
55 final static String RESOURCE_ARTIFACT_URL = BASE_ARTIFACT_URL + "resources/%s/%s/artifacts/%s";
56 final static String SERVICE_ARTIFACT_URL = BASE_ARTIFACT_URL + "artifacts/%s";
58 final static String RESOURCE_INSTANCE_ARTIFACT_URL = BASE_ARTIFACT_URL + "resourceInstances/%s/artifacts/%s";
60 @javax.annotation.Resource
61 ServiceOperation serviceOperation;
63 @javax.annotation.Resource
64 ResourceOperation resourceOperation;
66 @javax.annotation.Resource
67 InterfaceLifecycleOperation interfaceLifecycleOperation;
69 @javax.annotation.Resource
70 IArtifactOperation artifactOperation;
73 * @javax.annotation.Resource private InformationDeployedArtifactsBusinessLogic informationDeployedArtifactsBusinessLogic;
78 defaultArtifactInstallTimeout = ConfigurationManager.getConfigurationManager().getConfiguration().getDefaultHeatArtifactTimeoutMinutes();
81 public ServiceOperation getServiceOperation() {
82 return serviceOperation;
85 public void setServiceOperation(ServiceOperation serviceOperation) {
86 this.serviceOperation = serviceOperation;
89 public ResourceOperation getResourceOperation() {
90 return resourceOperation;
93 public void setResourceOperation(ResourceOperation resourceOperation) {
94 this.resourceOperation = resourceOperation;
97 public InterfaceLifecycleOperation getInterfaceLifecycleOperation() {
98 return interfaceLifecycleOperation;
101 public void setInterfaceLifecycleOperation(InterfaceLifecycleOperation interfaceLifecycleOperation) {
102 this.interfaceLifecycleOperation = interfaceLifecycleOperation;
105 public INotificationData buildResourceInstanceForDistribution(Service service, String distributionId) {
106 INotificationData notificationData = new NotificationDataImpl();
108 notificationData.setResources(convertRIToJsonContanier(service));
109 notificationData.setServiceName(service.getName());
110 notificationData.setServiceVersion(service.getVersion());
111 notificationData.setDistributionID(distributionId);
112 notificationData.setServiceUUID(service.getUUID());
113 notificationData.setServiceDescription(service.getDescription());
114 notificationData.setServiceInvariantUUID(service.getInvariantUUID());
116 logger.debug("Before returning notification data object {}", notificationData);
118 return notificationData;
122 public INotificationData buildServiceForDistribution(INotificationData notificationData, Service service) {
124 notificationData.setServiceArtifacts(convertServiceArtifactsToArtifactInfo(service));
126 logger.debug("Before returning notification data object {}", notificationData);
128 return notificationData;
132 private List<ArtifactInfoImpl> convertServiceArtifactsToArtifactInfo(Service service) {
134 Map<String, ArtifactDefinition> serviceArtifactsMap = service.getDeploymentArtifacts();
135 List<ArtifactDefinition> ret = new ArrayList<ArtifactDefinition>();
137 for (ArtifactDefinition artifactDef : serviceArtifactsMap.values()) {
138 if (artifactDef.checkEsIdExist()) {
139 ret.add(artifactDef);
142 List<ArtifactInfoImpl> artifacts = ArtifactInfoImpl.convertServiceArtifactToArtifactInfoImpl(service, ret);
146 private List<JsonContainerResourceInstance> convertRIToJsonContanier(Service service) {
147 List<JsonContainerResourceInstance> ret = new ArrayList<JsonContainerResourceInstance>();
148 if (service.getComponentInstances() != null) {
149 for (ComponentInstance resourceInstance : service.getComponentInstances()) {
150 String resourceType = "VF";
151 List<ArtifactDefinition> artifactsDefList = getArtifactsWithPayload(resourceInstance);
152 List<ArtifactInfoImpl> artifacts = ArtifactInfoImpl.convertToArtifactInfoImpl(service, resourceInstance, artifactsDefList);
153 Either<String, StorageOperationStatus> responseResult = resourceOperation.getInvariantUUID(NodeTypeEnum.Resource, resourceInstance.getComponentUid(), false);
154 String resourceInvariantUUID = null;
155 if (responseResult.isRight()) {
156 logger.debug("Resource {} Invariant UUID retrieving failed", resourceInstance.getComponentUid());
158 resourceInvariantUUID = responseResult.left().value();
160 JsonContainerResourceInstance jsonContainer = new JsonContainerResourceInstance(resourceInstance, resourceInvariantUUID, resourceType, artifacts);
161 ret.add(jsonContainer);
168 private List<ArtifactDefinition> getArtifactsWithPayload(ComponentInstance resourceInstance) {
169 List<ArtifactDefinition> ret = new ArrayList<ArtifactDefinition>();
171 // List<ArtifactDefinition> informationDeployedArtifacts =
172 // informationDeployedArtifactsBusinessLogic.getInformationalDeployedArtifactsForResourceInstance(resourceInstance);
173 List<ArtifactDefinition> deployableArtifacts = new ArrayList<ArtifactDefinition>();
174 // deployableArtifacts.addAll(informationDeployedArtifacts);
175 if (resourceInstance.getDeploymentArtifacts() != null) {
176 deployableArtifacts.addAll(resourceInstance.getDeploymentArtifacts().values());
179 for (ArtifactDefinition artifactDef : deployableArtifacts) {
180 if (artifactDef.checkEsIdExist()) {
181 ret.add(artifactDef);
189 * build the url for resource intance artifact
192 * @param resourceData
193 * @param artifactName
196 public static String buildResourceInstanceArtifactUrl(Service service, ComponentInstance resourceInstance, String artifactName) {
198 String url = String.format(RESOURCE_INSTANCE_ARTIFACT_URL, service.getSystemName(), service.getVersion(), resourceInstance.getNormalizedName(), artifactName);
200 logger.debug("After building artifact url {}", url);
206 * build the url for resource intance artifact
209 * @param resourceData
210 * @param artifactName
213 public static String buildServiceArtifactUrl(Service service, String artifactName) {
215 String url = String.format(SERVICE_ARTIFACT_URL, service.getSystemName(), service.getVersion(), artifactName);
217 logger.debug("After building artifact url {}", url);
224 * Retrieve all deployment artifacts of all resources under a given service
226 * @param resourceArtifactsResult
228 * @param deConfiguration
231 public Either<Boolean, StorageOperationStatus> isServiceContainsDeploymentArtifacts(Service service) {
233 Either<Boolean, StorageOperationStatus> result = Either.left(false);
234 Map<String, ArtifactDefinition> serviseArtifactsMap = service.getDeploymentArtifacts();
235 if (serviseArtifactsMap != null && !serviseArtifactsMap.isEmpty()) {
236 result = Either.left(true);
240 List<ComponentInstance> resourceInstances = service.getComponentInstances();
242 if (resourceInstances != null) {
243 for (ComponentInstance resourceInstance : resourceInstances) {
245 Map<String, ArtifactDefinition> deploymentArtifactsMapper = resourceInstance.getDeploymentArtifacts();
246 // List<ArtifactDefinition> informationDeployedArtifacts =
247 // informationDeployedArtifactsBusinessLogic.getInformationalDeployedArtifactsForResourceInstance(resourceInstance);
249 boolean isDeployableArtifactFound = isContainsPayload(deploymentArtifactsMapper.values());// ||
250 // isContainsPayload(informationDeployedArtifacts);
251 if (isDeployableArtifactFound) {
252 result = Either.left(true);
263 private boolean isContainsPayload(Collection<ArtifactDefinition> collection) {
264 boolean payLoadFound = collection != null && collection.stream().anyMatch(p -> p.checkEsIdExist());