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;
27 import java.util.Optional;
28 import java.util.stream.Collectors;
30 import javax.annotation.PostConstruct;
32 import org.openecomp.sdc.be.config.ConfigurationManager;
33 import org.openecomp.sdc.be.model.ArtifactDefinition;
34 import org.openecomp.sdc.be.model.ComponentInstance;
35 import org.openecomp.sdc.be.model.ComponentParametersView;
36 import org.openecomp.sdc.be.model.Resource;
37 import org.openecomp.sdc.be.model.Service;
38 import org.openecomp.sdc.be.model.category.CategoryDefinition;
39 import org.openecomp.sdc.be.model.category.SubCategoryDefinition;
40 import org.openecomp.sdc.be.model.jsontitan.operations.ToscaOperationFacade;
41 import org.openecomp.sdc.be.model.operations.api.IArtifactOperation;
42 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
43 import org.openecomp.sdc.be.model.operations.impl.InterfaceLifecycleOperation;
44 import org.openecomp.sdc.common.api.ArtifactTypeEnum;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
47 import org.springframework.beans.factory.annotation.Autowired;
48 import org.springframework.stereotype.Component;
50 import fj.data.Either;
52 @Component("serviceDistributionArtifactsBuilder")
53 public class ServiceDistributionArtifactsBuilder {
55 private int defaultArtifactInstallTimeout = 60;
57 private static Logger logger = LoggerFactory.getLogger(ServiceDistributionArtifactsBuilder.class.getName());
59 final static String BASE_ARTIFACT_URL = "/sdc/v1/catalog/services/%s/%s/";
60 final static String RESOURCE_ARTIFACT_URL = BASE_ARTIFACT_URL + "resources/%s/%s/artifacts/%s";
61 final static String SERVICE_ARTIFACT_URL = BASE_ARTIFACT_URL + "artifacts/%s";
63 final static String RESOURCE_INSTANCE_ARTIFACT_URL = BASE_ARTIFACT_URL + "resourceInstances/%s/artifacts/%s";
65 @javax.annotation.Resource
66 InterfaceLifecycleOperation interfaceLifecycleOperation;
68 @javax.annotation.Resource
69 IArtifactOperation artifactOperation;
72 ToscaOperationFacade toscaOperationFacade;
75 * @javax.annotation.Resource private
76 * InformationDeployedArtifactsBusinessLogic
77 * informationDeployedArtifactsBusinessLogic;
82 defaultArtifactInstallTimeout = ConfigurationManager.getConfigurationManager().getConfiguration()
83 .getDefaultHeatArtifactTimeoutMinutes();
86 public InterfaceLifecycleOperation getInterfaceLifecycleOperation() {
87 return interfaceLifecycleOperation;
90 public void setInterfaceLifecycleOperation(InterfaceLifecycleOperation interfaceLifecycleOperation) {
91 this.interfaceLifecycleOperation = interfaceLifecycleOperation;
94 public INotificationData buildResourceInstanceForDistribution(Service service, String distributionId) {
95 INotificationData notificationData = new NotificationDataImpl();
97 notificationData.setResources(convertRIToJsonContanier(service));
98 notificationData.setServiceName(service.getName());
99 notificationData.setServiceVersion(service.getVersion());
100 notificationData.setDistributionID(distributionId);
101 notificationData.setServiceUUID(service.getUUID());
102 notificationData.setServiceDescription(service.getDescription());
103 notificationData.setServiceInvariantUUID(service.getInvariantUUID());
105 logger.debug("Before returning notification data object {}", notificationData);
107 return notificationData;
111 public INotificationData buildServiceForDistribution(INotificationData notificationData, Service service) {
113 notificationData.setServiceArtifacts(convertServiceArtifactsToArtifactInfo(service));
115 logger.debug("Before returning notification data object {}", notificationData);
117 return notificationData;
121 private List<ArtifactInfoImpl> convertServiceArtifactsToArtifactInfo(Service service) {
123 Map<String, ArtifactDefinition> serviceArtifactsMap = service.getDeploymentArtifacts();
124 List<ArtifactDefinition> extractedServiceArtifacts = serviceArtifactsMap.values().stream()
125 //filters all artifacts with existing EsId
126 .filter(artifactDef -> artifactDef.checkEsIdExist())
127 //collects all filtered artifacts with existing EsId to List
128 .collect(Collectors.toList());
130 Optional<ArtifactDefinition> toscaTemplateArtifactOptl = exrtactToscaTemplateArtifact(service);
131 if(toscaTemplateArtifactOptl.isPresent()){
132 extractedServiceArtifacts.add(toscaTemplateArtifactOptl.get());
135 Optional<ArtifactDefinition> toscaCsarArtifactOptl = exrtactToscaCsarArtifact(service);
136 if(toscaCsarArtifactOptl.isPresent()){
137 extractedServiceArtifacts.add(toscaCsarArtifactOptl.get());
140 List<ArtifactInfoImpl> artifacts = ArtifactInfoImpl.convertServiceArtifactToArtifactInfoImpl(service, extractedServiceArtifacts);
144 private Optional<ArtifactDefinition> exrtactToscaTemplateArtifact(Service service) {
145 return service.getToscaArtifacts().values().stream()
146 //filters TOSCA_TEMPLATE artifact
147 .filter(e -> e.getArtifactType().equals(ArtifactTypeEnum.TOSCA_TEMPLATE.getType())).findAny();
150 private Optional<ArtifactDefinition> exrtactToscaCsarArtifact(Service service) {
151 return service.getToscaArtifacts().values().stream()
152 //filters TOSCA_CSAR artifact
153 .filter(e -> e.getArtifactType().equals(ArtifactTypeEnum.TOSCA_CSAR.getType())).findAny();
156 private List<JsonContainerResourceInstance> convertRIToJsonContanier(Service service) {
157 List<JsonContainerResourceInstance> ret = new ArrayList<JsonContainerResourceInstance>();
158 if (service.getComponentInstances() != null) {
159 for (ComponentInstance resourceInstance : service.getComponentInstances()) {
160 String resoucreType = resourceInstance.getOriginType().getValue();
161 List<ArtifactDefinition> artifactsDefList = getArtifactsWithPayload(resourceInstance);
162 List<ArtifactInfoImpl> artifacts = ArtifactInfoImpl.convertToArtifactInfoImpl(service, resourceInstance,
165 String resourceInvariantUUID = null;
166 String resourceCategory = null;
167 String resourceSubcategory = null;
169 ComponentParametersView componentParametersView = new ComponentParametersView();
170 componentParametersView.disableAll();
171 componentParametersView.setIgnoreCategories(false);
172 Either<Resource, StorageOperationStatus> componentResponse = toscaOperationFacade
173 .getToscaElement(resourceInstance.getComponentUid(), componentParametersView);
175 if (componentResponse.isRight()) {
176 logger.debug("Resource {} Invariant UUID & Categories retrieving failed", resourceInstance.getComponentUid());
178 Resource resource = componentResponse.left().value();
179 resourceInvariantUUID = resource.getInvariantUUID();
181 List<CategoryDefinition> categories = resource.getCategories();
183 if (categories != null) {
184 CategoryDefinition categoryDefinition = categories.get(0);
186 if (categoryDefinition != null) {
187 resourceCategory = categoryDefinition.getName();
188 List<SubCategoryDefinition> subcategories = categoryDefinition.getSubcategories();
189 if (null != subcategories) {
190 SubCategoryDefinition subCategoryDefinition = subcategories.get(0);
192 if (subCategoryDefinition != null) {
193 resourceSubcategory = subCategoryDefinition.getName();
200 JsonContainerResourceInstance jsonContainer = new JsonContainerResourceInstance(resourceInstance, resoucreType,
202 jsonContainer.setResourceInvariantUUID(resourceInvariantUUID);
203 jsonContainer.setCategory(resourceCategory);
204 jsonContainer.setSubcategory(resourceSubcategory);
205 ret.add(jsonContainer);
211 private List<ArtifactDefinition> getArtifactsWithPayload(ComponentInstance resourceInstance) {
212 List<ArtifactDefinition> ret = new ArrayList<ArtifactDefinition>();
214 // List<ArtifactDefinition> informationDeployedArtifacts =
215 // informationDeployedArtifactsBusinessLogic.getInformationalDeployedArtifactsForResourceInstance(resourceInstance);
216 List<ArtifactDefinition> deployableArtifacts = new ArrayList<ArtifactDefinition>();
217 // deployableArtifacts.addAll(informationDeployedArtifacts);
218 if (resourceInstance.getDeploymentArtifacts() != null) {
219 deployableArtifacts.addAll(resourceInstance.getDeploymentArtifacts().values());
222 for (ArtifactDefinition artifactDef : deployableArtifacts) {
223 if (artifactDef.checkEsIdExist()) {
224 ret.add(artifactDef);
232 * build the url for resource intance artifact
235 * @param resourceData
236 * @param artifactName
239 public static String buildResourceInstanceArtifactUrl(Service service, ComponentInstance resourceInstance,
240 String artifactName) {
242 String url = String.format(RESOURCE_INSTANCE_ARTIFACT_URL, service.getSystemName(), service.getVersion(),
243 resourceInstance.getNormalizedName(), artifactName);
245 logger.debug("After building artifact url {}", url);
251 * build the url for resource intance artifact
254 * @param resourceData
255 * @param artifactName
258 public static String buildServiceArtifactUrl(Service service, String artifactName) {
260 String url = String.format(SERVICE_ARTIFACT_URL, service.getSystemName(), service.getVersion(), artifactName);
262 logger.debug("After building artifact url {}", url);
269 * Retrieve all deployment artifacts of all resources under a given service
271 * @param resourceArtifactsResult
273 * @param deConfiguration
276 public Either<Boolean, StorageOperationStatus> isServiceContainsDeploymentArtifacts(Service service) {
278 Either<Boolean, StorageOperationStatus> result = Either.left(false);
279 Map<String, ArtifactDefinition> serviseArtifactsMap = service.getDeploymentArtifacts();
280 if (serviseArtifactsMap != null && !serviseArtifactsMap.isEmpty()) {
281 result = Either.left(true);
285 List<ComponentInstance> resourceInstances = service.getComponentInstances();
287 if (resourceInstances != null) {
288 for (ComponentInstance resourceInstance : resourceInstances) {
290 Map<String, ArtifactDefinition> deploymentArtifactsMapper = resourceInstance.getDeploymentArtifacts();
291 // List<ArtifactDefinition> informationDeployedArtifacts =
292 // informationDeployedArtifactsBusinessLogic.getInformationalDeployedArtifactsForResourceInstance(resourceInstance);
294 boolean isDeployableArtifactFound = isContainsPayload(deploymentArtifactsMapper.values());// ||
295 // isContainsPayload(informationDeployedArtifacts);
296 if (isDeployableArtifactFound) {
297 result = Either.left(true);
308 private boolean isContainsPayload(Collection<ArtifactDefinition> collection) {
309 boolean payLoadFound = collection != null && collection.stream().anyMatch(p -> p.checkEsIdExist());