6e67e38c1586fa1ffbf142b35b353ccb4ea54095
[sdc.git] /
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.distribution.engine;
22
23 import java.util.ArrayList;
24 import java.util.Collection;
25 import java.util.List;
26 import java.util.Map;
27
28 import javax.annotation.PostConstruct;
29
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;
44
45 import fj.data.Either;
46
47 @Component("serviceDistributionArtifactsBuilder")
48 public class ServiceDistributionArtifactsBuilder {
49
50         private int defaultArtifactInstallTimeout = 60;
51
52         private static Logger logger = LoggerFactory.getLogger(ServiceDistributionArtifactsBuilder.class.getName());
53
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";
57
58         final static String RESOURCE_INSTANCE_ARTIFACT_URL = BASE_ARTIFACT_URL + "resourceInstances/%s/artifacts/%s";
59
60         @javax.annotation.Resource
61         ServiceOperation serviceOperation;
62
63         @javax.annotation.Resource
64         ResourceOperation resourceOperation;
65
66         @javax.annotation.Resource
67         InterfaceLifecycleOperation interfaceLifecycleOperation;
68
69         @javax.annotation.Resource
70         IArtifactOperation artifactOperation;
71
72         /*
73          * @javax.annotation.Resource private InformationDeployedArtifactsBusinessLogic informationDeployedArtifactsBusinessLogic;
74          */
75
76         @PostConstruct
77         private void init() {
78                 defaultArtifactInstallTimeout = ConfigurationManager.getConfigurationManager().getConfiguration().getDefaultHeatArtifactTimeoutMinutes();
79         }
80
81         public ServiceOperation getServiceOperation() {
82                 return serviceOperation;
83         }
84
85         public void setServiceOperation(ServiceOperation serviceOperation) {
86                 this.serviceOperation = serviceOperation;
87         }
88
89         public ResourceOperation getResourceOperation() {
90                 return resourceOperation;
91         }
92
93         public void setResourceOperation(ResourceOperation resourceOperation) {
94                 this.resourceOperation = resourceOperation;
95         }
96
97         public InterfaceLifecycleOperation getInterfaceLifecycleOperation() {
98                 return interfaceLifecycleOperation;
99         }
100
101         public void setInterfaceLifecycleOperation(InterfaceLifecycleOperation interfaceLifecycleOperation) {
102                 this.interfaceLifecycleOperation = interfaceLifecycleOperation;
103         }
104
105         public INotificationData buildResourceInstanceForDistribution(Service service, String distributionId) {
106                 INotificationData notificationData = new NotificationDataImpl();
107
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());
115
116                 logger.debug("Before returning notification data object {}", notificationData);
117
118                 return notificationData;
119
120         }
121
122         public INotificationData buildServiceForDistribution(INotificationData notificationData, Service service) {
123
124                 notificationData.setServiceArtifacts(convertServiceArtifactsToArtifactInfo(service));
125
126                 logger.debug("Before returning notification data object {}", notificationData);
127
128                 return notificationData;
129
130         }
131
132         private List<ArtifactInfoImpl> convertServiceArtifactsToArtifactInfo(Service service) {
133
134                 Map<String, ArtifactDefinition> serviceArtifactsMap = service.getDeploymentArtifacts();
135                 List<ArtifactDefinition> ret = new ArrayList<ArtifactDefinition>();
136
137                 for (ArtifactDefinition artifactDef : serviceArtifactsMap.values()) {
138                         if (artifactDef.checkEsIdExist()) {
139                                 ret.add(artifactDef);
140                         }
141                 }
142                 List<ArtifactInfoImpl> artifacts = ArtifactInfoImpl.convertServiceArtifactToArtifactInfoImpl(service, ret);
143                 return artifacts;
144         }
145
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());
157                                 } else {
158                                         resourceInvariantUUID = responseResult.left().value();
159                                 }
160                                 JsonContainerResourceInstance jsonContainer = new JsonContainerResourceInstance(resourceInstance, resourceInvariantUUID, resourceType, artifacts);
161                                 ret.add(jsonContainer);
162                         }
163
164                 }
165                 return ret;
166         }
167
168         private List<ArtifactDefinition> getArtifactsWithPayload(ComponentInstance resourceInstance) {
169                 List<ArtifactDefinition> ret = new ArrayList<ArtifactDefinition>();
170
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());
177                 }
178
179                 for (ArtifactDefinition artifactDef : deployableArtifacts) {
180                         if (artifactDef.checkEsIdExist()) {
181                                 ret.add(artifactDef);
182                         }
183                 }
184
185                 return ret;
186         }
187
188         /**
189          * build the url for resource intance artifact
190          * 
191          * @param service
192          * @param resourceData
193          * @param artifactName
194          * @return
195          */
196         public static String buildResourceInstanceArtifactUrl(Service service, ComponentInstance resourceInstance, String artifactName) {
197
198                 String url = String.format(RESOURCE_INSTANCE_ARTIFACT_URL, service.getSystemName(), service.getVersion(), resourceInstance.getNormalizedName(), artifactName);
199
200                 logger.debug("After building artifact url {}", url);
201
202                 return url;
203         }
204
205         /**
206          * build the url for resource intance artifact
207          * 
208          * @param service
209          * @param resourceData
210          * @param artifactName
211          * @return
212          */
213         public static String buildServiceArtifactUrl(Service service, String artifactName) {
214
215                 String url = String.format(SERVICE_ARTIFACT_URL, service.getSystemName(), service.getVersion(), artifactName);
216
217                 logger.debug("After building artifact url {}", url);
218
219                 return url;
220
221         }
222
223         /**
224          * Retrieve all deployment artifacts of all resources under a given service
225          * 
226          * @param resourceArtifactsResult
227          * @param service
228          * @param deConfiguration
229          * @return
230          */
231         public Either<Boolean, StorageOperationStatus> isServiceContainsDeploymentArtifacts(Service service) {
232
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);
237                         return result;
238                 }
239
240                 List<ComponentInstance> resourceInstances = service.getComponentInstances();
241
242                 if (resourceInstances != null) {
243                         for (ComponentInstance resourceInstance : resourceInstances) {
244
245                                 Map<String, ArtifactDefinition> deploymentArtifactsMapper = resourceInstance.getDeploymentArtifacts();
246                                 // List<ArtifactDefinition> informationDeployedArtifacts =
247                                 // informationDeployedArtifactsBusinessLogic.getInformationalDeployedArtifactsForResourceInstance(resourceInstance);
248
249                                 boolean isDeployableArtifactFound = isContainsPayload(deploymentArtifactsMapper.values());// ||
250                                                                                                                                                                                                                         // isContainsPayload(informationDeployedArtifacts);
251                                 if (isDeployableArtifactFound) {
252                                         result = Either.left(true);
253                                         break;
254                                 }
255
256                         }
257
258                 }
259
260                 return result;
261         }
262
263         private boolean isContainsPayload(Collection<ArtifactDefinition> collection) {
264                 boolean payLoadFound = collection != null && collection.stream().anyMatch(p -> p.checkEsIdExist());
265                 return payLoadFound;
266         }
267
268 }