aboutsummaryrefslogtreecommitdiffstats
path: root/catalog-be/src/main/java/org/openecomp/sdc/be/components/distribution/engine/ServiceDistributionArtifactsBuilder.java
blob: 0330a756c65b7696ed294fd9c64462e0c8d1fe83 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/*-
 * ============LICENSE_START=======================================================
 * SDC
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.openecomp.sdc.be.components.distribution.engine;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

import javax.annotation.PostConstruct;

import org.openecomp.sdc.be.config.ConfigurationManager;
import org.openecomp.sdc.be.model.ArtifactDefinition;
import org.openecomp.sdc.be.model.ComponentInstance;
import org.openecomp.sdc.be.model.ComponentParametersView;
import org.openecomp.sdc.be.model.Resource;
import org.openecomp.sdc.be.model.Service;
import org.openecomp.sdc.be.model.category.CategoryDefinition;
import org.openecomp.sdc.be.model.category.SubCategoryDefinition;
import org.openecomp.sdc.be.model.jsontitan.operations.ToscaOperationFacade;
import org.openecomp.sdc.be.model.operations.api.IArtifactOperation;
import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
import org.openecomp.sdc.be.model.operations.impl.InterfaceLifecycleOperation;
import org.openecomp.sdc.common.api.ArtifactTypeEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import fj.data.Either;

@Component("serviceDistributionArtifactsBuilder")
public class ServiceDistributionArtifactsBuilder {

	private int defaultArtifactInstallTimeout = 60;

	private static Logger logger = LoggerFactory.getLogger(ServiceDistributionArtifactsBuilder.class.getName());

	final static String BASE_ARTIFACT_URL = "/sdc/v1/catalog/services/%s/%s/";
	final static String RESOURCE_ARTIFACT_URL = BASE_ARTIFACT_URL + "resources/%s/%s/artifacts/%s";
	final static String SERVICE_ARTIFACT_URL = BASE_ARTIFACT_URL + "artifacts/%s";

	final static String RESOURCE_INSTANCE_ARTIFACT_URL = BASE_ARTIFACT_URL + "resourceInstances/%s/artifacts/%s";

	@javax.annotation.Resource
	InterfaceLifecycleOperation interfaceLifecycleOperation;

	@javax.annotation.Resource
	IArtifactOperation artifactOperation;
	
	@Autowired
	ToscaOperationFacade toscaOperationFacade;

	/*
	 * @javax.annotation.Resource private
	 * InformationDeployedArtifactsBusinessLogic
	 * informationDeployedArtifactsBusinessLogic;
	 */

	@PostConstruct
	private void init() {
		defaultArtifactInstallTimeout = ConfigurationManager.getConfigurationManager().getConfiguration()
				.getDefaultHeatArtifactTimeoutMinutes();
	}

	public InterfaceLifecycleOperation getInterfaceLifecycleOperation() {
		return interfaceLifecycleOperation;
	}

	public void setInterfaceLifecycleOperation(InterfaceLifecycleOperation interfaceLifecycleOperation) {
		this.interfaceLifecycleOperation = interfaceLifecycleOperation;
	}

	public INotificationData buildResourceInstanceForDistribution(Service service, String distributionId) {
		INotificationData notificationData = new NotificationDataImpl();

		notificationData.setResources(convertRIToJsonContanier(service));
		notificationData.setServiceName(service.getName());
		notificationData.setServiceVersion(service.getVersion());
		notificationData.setDistributionID(distributionId);
		notificationData.setServiceUUID(service.getUUID());
		notificationData.setServiceDescription(service.getDescription());
		notificationData.setServiceInvariantUUID(service.getInvariantUUID());
		String workloadContext= ConfigurationManager.getConfigurationManager().getConfiguration().getWorkloadContext();
		if(workloadContext!=null){
			notificationData.setWorkloadContext(workloadContext);
		}
		logger.debug("Before returning notification data object {}", notificationData);

		return notificationData;

	}

	public INotificationData buildServiceForDistribution(INotificationData notificationData, Service service) {

		notificationData.setServiceArtifacts(convertServiceArtifactsToArtifactInfo(service));

		logger.debug("Before returning notification data object {}", notificationData);

		return notificationData;

	}

	private List<ArtifactInfoImpl> convertServiceArtifactsToArtifactInfo(Service service) {
		
		Map<String, ArtifactDefinition> serviceArtifactsMap = service.getDeploymentArtifacts();
		List<ArtifactDefinition> extractedServiceArtifacts = serviceArtifactsMap.values().stream()
				//filters all artifacts with existing EsId
				.filter(artifactDef -> artifactDef.checkEsIdExist())
				//collects all filtered artifacts with existing EsId to List
				.collect(Collectors.toList());
		
		Optional<ArtifactDefinition> toscaTemplateArtifactOptl = exrtactToscaTemplateArtifact(service);
		if(toscaTemplateArtifactOptl.isPresent()){
			extractedServiceArtifacts.add(toscaTemplateArtifactOptl.get());
		}
		
		Optional<ArtifactDefinition> toscaCsarArtifactOptl = exrtactToscaCsarArtifact(service);
		if(toscaCsarArtifactOptl.isPresent()){
			extractedServiceArtifacts.add(toscaCsarArtifactOptl.get());
		}
		
		List<ArtifactInfoImpl> artifacts = ArtifactInfoImpl.convertServiceArtifactToArtifactInfoImpl(service, extractedServiceArtifacts);
		return artifacts;
	}

	private Optional<ArtifactDefinition> exrtactToscaTemplateArtifact(Service service) {
		return service.getToscaArtifacts().values().stream()
				//filters TOSCA_TEMPLATE artifact
				.filter(e -> e.getArtifactType().equals(ArtifactTypeEnum.TOSCA_TEMPLATE.getType())).findAny();
	}
	
	private Optional<ArtifactDefinition> exrtactToscaCsarArtifact(Service service) {
		return service.getToscaArtifacts().values().stream()
				//filters TOSCA_CSAR artifact
				.filter(e -> e.getArtifactType().equals(ArtifactTypeEnum.TOSCA_CSAR.getType())).findAny();
	}

	private List<JsonContainerResourceInstance> convertRIToJsonContanier(Service service) {
		List<JsonContainerResourceInstance> ret = new ArrayList<JsonContainerResourceInstance>();
		if (service.getComponentInstances() != null) {
			for (ComponentInstance resourceInstance : service.getComponentInstances()) {
				String resoucreType = resourceInstance.getOriginType().getValue();
				List<ArtifactDefinition> artifactsDefList = getArtifactsWithPayload(resourceInstance);
				List<ArtifactInfoImpl> artifacts = ArtifactInfoImpl.convertToArtifactInfoImpl(service, resourceInstance,
						artifactsDefList);

				String resourceInvariantUUID = null;
				String resourceCategory = null;
				String resourceSubcategory = null;

				ComponentParametersView componentParametersView = new ComponentParametersView();
				componentParametersView.disableAll();
				componentParametersView.setIgnoreCategories(false);
				Either<Resource, StorageOperationStatus> componentResponse = toscaOperationFacade
						.getToscaElement(resourceInstance.getComponentUid(), componentParametersView);

				if (componentResponse.isRight()) {
					logger.debug("Resource {} Invariant UUID & Categories retrieving failed", resourceInstance.getComponentUid());
				} else {
					Resource resource = componentResponse.left().value();
					resourceInvariantUUID = resource.getInvariantUUID();

					List<CategoryDefinition> categories = resource.getCategories();

					if (categories != null) {
						CategoryDefinition categoryDefinition = categories.get(0);

						if (categoryDefinition != null) {
							resourceCategory = categoryDefinition.getName();
							List<SubCategoryDefinition> subcategories = categoryDefinition.getSubcategories();
							if (null != subcategories) {
								SubCategoryDefinition subCategoryDefinition = subcategories.get(0);

								if (subCategoryDefinition != null) {
									resourceSubcategory = subCategoryDefinition.getName();
								}
							}
						}
					}
				}

				JsonContainerResourceInstance jsonContainer = new JsonContainerResourceInstance(resourceInstance, resoucreType,
						rebuildArtifactswith120TimeoutInsteadOf60(artifacts)/*TODO used to send artifacts, the function is a fix to the short timeout bug in distribution*/);
				jsonContainer.setResourceInvariantUUID(resourceInvariantUUID);
				jsonContainer.setCategory(resourceCategory);
				jsonContainer.setSubcategory(resourceSubcategory);
				ret.add(jsonContainer);
			}
		}
		return ret;
	}

	private List<ArtifactInfoImpl> rebuildArtifactswith120TimeoutInsteadOf60(List<ArtifactInfoImpl> artifacts) {
		for(ArtifactInfoImpl artifact : artifacts){
			if(artifact.getArtifactTimeout().equals(60)){
				artifact.setArtifactTimeout(120);
			}
		}
		return artifacts;
	}

	private List<ArtifactDefinition> getArtifactsWithPayload(ComponentInstance resourceInstance) {
		List<ArtifactDefinition> ret = new ArrayList<ArtifactDefinition>();

		// List<ArtifactDefinition> informationDeployedArtifacts =
		// informationDeployedArtifactsBusinessLogic.getInformationalDeployedArtifactsForResourceInstance(resourceInstance);
		List<ArtifactDefinition> deployableArtifacts = new ArrayList<ArtifactDefinition>();
		// deployableArtifacts.addAll(informationDeployedArtifacts);
		if (resourceInstance.getDeploymentArtifacts() != null) {
			deployableArtifacts.addAll(resourceInstance.getDeploymentArtifacts().values());
		}

		for (ArtifactDefinition artifactDef : deployableArtifacts) {
			if (artifactDef.checkEsIdExist()) {
				ret.add(artifactDef);
			}
		}

		return ret;
	}

	/**
	 * build the url for resource intance artifact
	 * 
	 * @param service
	 * @param resourceData
	 * @param artifactName
	 * @return
	 */
	public static String buildResourceInstanceArtifactUrl(Service service, ComponentInstance resourceInstance,
			String artifactName) {

		String url = String.format(RESOURCE_INSTANCE_ARTIFACT_URL, service.getSystemName(), service.getVersion(),
				resourceInstance.getNormalizedName(), artifactName);

		logger.debug("After building artifact url {}", url);

		return url;
	}

	/**
	 * build the url for resource intance artifact
	 * 
	 * @param service
	 * @param resourceData
	 * @param artifactName
	 * @return
	 */
	public static String buildServiceArtifactUrl(Service service, String artifactName) {

		String url = String.format(SERVICE_ARTIFACT_URL, service.getSystemName(), service.getVersion(), artifactName);

		logger.debug("After building artifact url {}", url);

		return url;

	}

	/**
	 * Retrieve all deployment artifacts of all resources under a given service
	 * 
	 * @param resourceArtifactsResult
	 * @param service
	 * @param deConfiguration
	 * @return
	 */
	public Either<Boolean, StorageOperationStatus> isServiceContainsDeploymentArtifacts(Service service) {

		Either<Boolean, StorageOperationStatus> result = Either.left(false);
		Map<String, ArtifactDefinition> serviseArtifactsMap = service.getDeploymentArtifacts();
		if (serviseArtifactsMap != null && !serviseArtifactsMap.isEmpty()) {
			result = Either.left(true);
			return result;
		}

		List<ComponentInstance> resourceInstances = service.getComponentInstances();

		if (resourceInstances != null) {
			for (ComponentInstance resourceInstance : resourceInstances) {

				Map<String, ArtifactDefinition> deploymentArtifactsMapper = resourceInstance.getDeploymentArtifacts();
				// List<ArtifactDefinition> informationDeployedArtifacts =
				// informationDeployedArtifactsBusinessLogic.getInformationalDeployedArtifactsForResourceInstance(resourceInstance);

				boolean isDeployableArtifactFound = isContainsPayload(deploymentArtifactsMapper.values());// ||
																											// isContainsPayload(informationDeployedArtifacts);
				if (isDeployableArtifactFound) {
					result = Either.left(true);
					break;
				}

			}

		}

		return result;
	}

	private boolean isContainsPayload(Collection<ArtifactDefinition> collection) {
		boolean payLoadFound = collection != null && collection.stream().anyMatch(p -> p.checkEsIdExist());
		return payLoadFound;
	}

}