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.mso.asdc.installer.heat;
23 import java.sql.Timestamp;
24 import java.text.SimpleDateFormat;
25 import java.util.ArrayList;
26 import java.util.Date;
27 import java.util.HashMap;
28 import java.util.List;
32 import org.hibernate.exception.ConstraintViolationException;
33 import org.hibernate.exception.LockAcquisitionException;
34 import org.openecomp.sdc.api.notification.IArtifactInfo;
36 import org.openecomp.mso.asdc.client.ASDCConfiguration;
37 import org.openecomp.mso.asdc.client.exceptions.ArtifactInstallerException;
38 import org.openecomp.mso.asdc.installer.ASDCElementInfo;
39 import org.openecomp.mso.asdc.installer.BigDecimalVersion;
40 import org.openecomp.mso.asdc.installer.IVfResourceInstaller;
41 import org.openecomp.mso.asdc.installer.VfModuleArtifact;
42 import org.openecomp.mso.asdc.installer.VfModuleStructure;
43 import org.openecomp.mso.asdc.installer.VfResourceStructure;
44 import org.openecomp.mso.asdc.util.YamlEditor;
45 import org.openecomp.mso.db.catalog.CatalogDatabase;
46 import org.openecomp.mso.db.catalog.beans.HeatEnvironment;
47 import org.openecomp.mso.db.catalog.beans.HeatFiles;
48 import org.openecomp.mso.db.catalog.beans.HeatTemplate;
49 import org.openecomp.mso.db.catalog.beans.HeatTemplateParam;
50 import org.openecomp.mso.db.catalog.beans.NetworkResourceCustomization;
51 import org.openecomp.mso.db.catalog.beans.AllottedResourceCustomization;
52 import org.openecomp.mso.db.catalog.beans.Service;
53 import org.openecomp.mso.db.catalog.beans.ServiceToAllottedResources;
54 import org.openecomp.mso.db.catalog.beans.ServiceToNetworks;
55 import org.openecomp.mso.db.catalog.beans.VfModule;
56 import org.openecomp.mso.db.catalog.beans.VnfResource;
57 import org.openecomp.mso.logger.MessageEnum;
58 import org.openecomp.mso.logger.MsoLogger;
60 public class VfResourceInstaller implements IVfResourceInstaller {
62 private MsoLogger logger;
64 public VfResourceInstaller() {
65 logger = MsoLogger.getMsoLogger(MsoLogger.Catalog.ASDC);
69 public boolean isResourceAlreadyDeployed(VfResourceStructure vfResourceStruct)
70 throws ArtifactInstallerException {
72 boolean status = false;
73 VfResourceStructure vfResourceStructure = (VfResourceStructure)vfResourceStruct;
75 try(CatalogDatabase db = new CatalogDatabase()) {
77 String resourceType = vfResourceStruct.getResourceInstance().getResourceType();
78 String category = vfResourceStruct.getResourceInstance().getCategory();
80 // Check for duplicate VF Module that is not an Allotted Resource
81 if(resourceType.equals("VF") && !category.equalsIgnoreCase("Allotted Resource")){
82 logger.info(MessageEnum.ASDC_CHECK_HEAT_TEMPLATE, "VNFResource",
83 VfResourceInstaller.createVNFName(vfResourceStructure),
84 BigDecimalVersion.castAndCheckNotificationVersionToString(
85 vfResourceStructure.getNotification().getServiceVersion()), "", "");
87 VnfResource vnfResource = db.getVnfResource(
88 VfResourceInstaller.createVNFName(vfResourceStructure),
89 BigDecimalVersion.castAndCheckNotificationVersionToString(
90 vfResourceStructure.getNotification().getServiceVersion()));
92 if (vnfResource != null) {
99 // Check dup for VF Allotted Resource
100 if(resourceType.equals("VF") && category.equalsIgnoreCase("Allotted Resource")){
101 logger.info(MessageEnum.ASDC_CHECK_HEAT_TEMPLATE, "AllottedResource",
102 vfResourceStruct.getResourceInstance().getResourceInstanceName(),
103 BigDecimalVersion.castAndCheckNotificationVersionToString(
104 vfResourceStructure.getNotification().getServiceVersion()), "", "");
106 List<AllottedResourceCustomization> allottedResources = db.getAllAllottedResourcesByServiceModelUuid(vfResourceStruct.getNotification().getServiceUUID());
108 if(allottedResources != null && allottedResources.size() > 0){
109 for(AllottedResourceCustomization allottedResource : allottedResources){
111 String existingAllottedResource = allottedResource.getModelCustomizationUuid();
112 String notificationAllottedResource = vfResourceStruct.getResourceInstance().getResourceCustomizationUUID();
114 if(existingAllottedResource.equals(notificationAllottedResource)){
123 // Check Network for duplicates
124 if(resourceType.equals("VL")){
125 logger.info(MessageEnum.ASDC_CHECK_HEAT_TEMPLATE, "NetworkResource",
126 vfResourceStruct.getResourceInstance().getResourceInstanceName(),
127 BigDecimalVersion.castAndCheckNotificationVersionToString(
128 vfResourceStructure.getNotification().getServiceVersion()), "", "");
130 List<NetworkResourceCustomization> networkResources = db.getAllNetworksByServiceModelUuid(vfResourceStruct.getNotification().getServiceUUID());
132 if(networkResources != null && networkResources.size() > 0){
133 for(NetworkResourceCustomization networkResource : networkResources){
135 String existingNetworkResource = networkResource.getModelCustomizationUuid();
136 String notificationNetworkResource = vfResourceStruct.getResourceInstance().getResourceCustomizationUUID();
138 if(existingNetworkResource.equals(notificationNetworkResource)){
148 logger.info(MessageEnum.ASDC_ARTIFACT_ALREADY_DEPLOYED_DETAIL,
149 vfResourceStructure.getResourceInstance().getResourceInstanceName(),
150 vfResourceStructure.getResourceInstance().getResourceCustomizationUUID(),
151 vfResourceStructure.getNotification().getServiceName(),
152 BigDecimalVersion.castAndCheckNotificationVersionToString(
153 vfResourceStructure.getNotification().getServiceVersion()),
154 vfResourceStructure.getNotification().getServiceUUID(),
155 vfResourceStructure.getResourceInstance().getResourceName(),"", "");
157 logger.info(MessageEnum.ASDC_ARTIFACT_NOT_DEPLOYED_DETAIL,
158 vfResourceStructure.getResourceInstance().getResourceInstanceName(),
159 vfResourceStructure.getResourceInstance().getResourceCustomizationUUID(),
160 vfResourceStructure.getNotification().getServiceName(),
161 BigDecimalVersion.castAndCheckNotificationVersionToString(
162 vfResourceStructure.getNotification().getServiceVersion()),
163 vfResourceStructure.getNotification().getServiceUUID(),
164 vfResourceStructure.getResourceInstance().getResourceName(),"", "");
169 } catch (Exception e) {
170 logger.error(MessageEnum.ASDC_ARTIFACT_CHECK_EXC, "", "", MsoLogger.ErrorCode.SchemaError, "Exception - isResourceAlreadyDeployed");
171 throw new ArtifactInstallerException("Exception caught during checking existence of the VNF Resource.", e);
176 public void installTheResource(VfResourceStructure vfResourceStruct) throws ArtifactInstallerException {
178 // 1. Add the DB object list (Hashed) to be created from the HashMap
180 // The DB objects will be stored in each VfModuleArtifact objects
181 // Those objects could be reused by different VfModule
183 VfResourceStructure vfResourceStructure = (VfResourceStructure)vfResourceStruct;
185 for (VfModuleArtifact vfModuleArtifact : vfResourceStructure.getArtifactsMapByUUID().values()) {
187 switch (vfModuleArtifact.getArtifactInfo().getArtifactType()) {
188 case ASDCConfiguration.HEAT:
189 case ASDCConfiguration.HEAT_VOL:
190 case ASDCConfiguration.HEAT_NESTED:
191 VfResourceInstaller.createHeatTemplateFromArtifact(vfResourceStructure, vfModuleArtifact);
193 case ASDCConfiguration.HEAT_ENV:
194 VfResourceInstaller.createHeatEnvFromArtifact(vfResourceStructure, vfModuleArtifact);
196 case ASDCConfiguration.HEAT_ARTIFACT:
197 VfResourceInstaller.createHeatFileFromArtifact(vfResourceStructure, vfModuleArtifact);
199 case ASDCConfiguration.HEAT_NET:
200 case ASDCConfiguration.OTHER:
201 logger.warn(MessageEnum.ASDC_ARTIFACT_TYPE_NOT_SUPPORT, vfModuleArtifact.getArtifactInfo().getArtifactType()+"(Artifact Name:"+vfModuleArtifact.getArtifactInfo().getArtifactName()+")", "", "", MsoLogger.ErrorCode.DataError, "Artifact type not supported");
209 // in case of deployment failure, use a string that will represent the type of artifact that failed...
210 List<ASDCElementInfo> artifactListForLogging = new ArrayList<>();
212 CatalogDatabase catalogDB = new CatalogDatabase();
213 // 2. Create the VFModules/VNFResource objects by linking them to the
214 // objects created before and store them in Resource/module structure
215 // Opening a DB transaction, starting from here
218 VfResourceInstaller.createService(vfResourceStructure);
220 String resourceType = vfResourceStructure.getResourceInstance().getResourceType();
221 String resourceCategory = vfResourceStructure.getResourceInstance().getCategory();
223 if(resourceType.equals("VF")){
225 if(resourceCategory.equalsIgnoreCase("Allotted Resource")){
226 VfResourceInstaller.createAllottedResourceCustomization(vfResourceStructure);
227 catalogDB.saveAllottedResourceCustomization(vfResourceStructure.getCatalogResourceCustomization());
229 VfResourceInstaller.createVnfResource(vfResourceStructure);
230 catalogDB.saveOrUpdateVnfResource(vfResourceStructure.getCatalogVnfResource());
234 if(resourceType.equals("VL")){
235 VfResourceInstaller.createNetworkResourceCustomization(vfResourceStructure);
236 catalogDB.saveNetworkResourceCustomization(vfResourceStructure.getCatalogNetworkResourceCustomization());
239 // Add this one for logging
240 artifactListForLogging.add(ASDCElementInfo.createElementFromVfResourceStructure(vfResourceStructure));
242 //catalogDB.saveOrUpdateVnfResource(vfResourceStructure.getCatalogVnfResource());
243 catalogDB.saveService(vfResourceStructure.getCatalogService());
245 // Now that the service has been added we can populate the Service_to_AllottedResources table
246 if(resourceType.equals("VF") && resourceCategory.equalsIgnoreCase("Allotted Resource")){
247 catalogDB.saveServiceToAllottedResources(vfResourceStructure.getCatalogServiceToAllottedResources());
250 // Now that the service has been added we can populate the Service_to_Network table
251 if(resourceType.equals("VL")){
252 catalogDB.saveServiceToNetworks(vfResourceStructure.getCatalogServiceToNetworks());
255 for (VfModuleStructure vfModuleStructure : vfResourceStructure.getVfModuleStructure()) {
257 // Here we set the right db structure according to the Catalog
260 // We expect only one MAIN HEAT per VFMODULE
261 // we can also obtain from it the Env ArtifactInfo, that's why
263 // get the Main IArtifactInfo
265 HeatTemplate heatMainTemplate = null;
266 HeatEnvironment heatEnv = null;
268 HeatTemplate heatVolumeTemplate = null;
269 HeatEnvironment heatVolumeEnv = null;
271 if (vfModuleStructure.getArtifactsMap().containsKey(ASDCConfiguration.HEAT)) {
272 IArtifactInfo mainEnvArtifactInfo = vfModuleStructure.getArtifactsMap().get(ASDCConfiguration.HEAT)
273 .get(0).getArtifactInfo().getGeneratedArtifact();
276 heatMainTemplate = (HeatTemplate) vfModuleStructure.getArtifactsMap()
277 .get(ASDCConfiguration.HEAT).get(0).getCatalogObject();
279 // Add this one for logging
280 artifactListForLogging.add(ASDCElementInfo
281 .createElementFromVfArtifactInfo(vfModuleStructure.getArtifactsMap().get(ASDCConfiguration.HEAT).get(0).getArtifactInfo()));
283 catalogDB.saveHeatTemplate(heatMainTemplate, heatMainTemplate.getParameters());
284 // Indicate we have deployed it in the DB
285 vfModuleStructure.getArtifactsMap().get(ASDCConfiguration.HEAT).get(0).incrementDeployedInDB();
289 // We expect only one VOL HEAT per VFMODULE
290 // we can also obtain from it the Env ArtifactInfo, that's why
291 // we get the Volume IArtifactInfo
293 if (vfModuleStructure.getArtifactsMap().containsKey(ASDCConfiguration.HEAT_VOL)) {
294 IArtifactInfo volEnvArtifactInfo = vfModuleStructure.getArtifactsMap().get(ASDCConfiguration.HEAT_VOL).get(0)
295 .getArtifactInfo().getGeneratedArtifact();
297 heatVolumeTemplate = (HeatTemplate) vfModuleStructure.getArtifactsMap()
298 .get(ASDCConfiguration.HEAT_VOL).get(0).getCatalogObject();
300 // Add this one for logging
301 artifactListForLogging.add(ASDCElementInfo.createElementFromVfArtifactInfo(vfModuleStructure.getArtifactsMap().get(ASDCConfiguration.HEAT_VOL).get(0).getArtifactInfo()));
303 catalogDB.saveHeatTemplate(heatVolumeTemplate, heatVolumeTemplate.getParameters());
304 // Indicate we have deployed it in the DB
305 vfModuleStructure.getArtifactsMap().get(ASDCConfiguration.HEAT_VOL).get(0).incrementDeployedInDB();
307 if (volEnvArtifactInfo != null) {
308 heatVolumeEnv = (HeatEnvironment) vfResourceStructure.getArtifactsMapByUUID()
309 .get(volEnvArtifactInfo.getArtifactUUID()).getCatalogObject();
311 // Add this one for logging
312 artifactListForLogging.add(ASDCElementInfo.createElementFromVfArtifactInfo(volEnvArtifactInfo));
314 catalogDB.saveHeatEnvironment(heatVolumeEnv);
315 // Indicate we have deployed it in the DB
316 vfResourceStructure.getArtifactsMapByUUID().get(volEnvArtifactInfo.getArtifactUUID()).incrementDeployedInDB();
322 // Here we expect many HEAT_NESTED template to be there
323 // check first if we really have nested heat templates
324 if (vfModuleStructure.getArtifactsMap().containsKey(ASDCConfiguration.HEAT_NESTED)) {
325 for (VfModuleArtifact heatNestedArtifact : vfModuleStructure.getArtifactsMap()
326 .get(ASDCConfiguration.HEAT_NESTED)) {
328 // Check if this nested is well referenced by the MAIN HEAT
329 String parentArtifactType = VfResourceInstaller.identifyParentOfNestedTemplate(vfModuleStructure,heatNestedArtifact);
330 HeatTemplate heatNestedTemplate = (HeatTemplate) heatNestedArtifact.getCatalogObject();
332 if (parentArtifactType != null) {
334 switch (parentArtifactType) {
335 case ASDCConfiguration.HEAT:
337 // Add this one for logging
338 artifactListForLogging.add(ASDCElementInfo.createElementFromVfArtifactInfo(heatNestedArtifact.getArtifactInfo()));
340 catalogDB.saveNestedHeatTemplate (heatMainTemplate.getId(), heatNestedTemplate, heatNestedTemplate.getTemplateName());
341 // Indicate we have deployed it in the DB
342 heatNestedArtifact.incrementDeployedInDB();
344 case ASDCConfiguration.HEAT_VOL:
346 // Add this one for logging
347 artifactListForLogging.add(ASDCElementInfo.createElementFromVfArtifactInfo(heatNestedArtifact.getArtifactInfo()));
348 catalogDB.saveNestedHeatTemplate (heatVolumeTemplate.getId(), heatNestedTemplate, heatNestedTemplate.getTemplateName());
349 // Indicate we have deployed it in the DB
350 heatNestedArtifact.incrementDeployedInDB();
357 } else { // Assume it belongs to HEAT MAIN
358 // Add this one for logging
359 artifactListForLogging.add(ASDCElementInfo.createElementFromVfArtifactInfo(heatNestedArtifact.getArtifactInfo()));
361 catalogDB.saveNestedHeatTemplate (heatMainTemplate.getId(), heatNestedTemplate, heatNestedTemplate.getTemplateName());
362 // Indicate we have deployed it in the DB
363 heatNestedArtifact.incrementDeployedInDB();
368 if (mainEnvArtifactInfo != null) {
369 heatEnv = (HeatEnvironment) vfResourceStructure.getArtifactsMapByUUID()
370 .get(mainEnvArtifactInfo.getArtifactUUID()).getCatalogObject();
372 // Add this one for logging
373 artifactListForLogging.add(ASDCElementInfo.createElementFromVfArtifactInfo(mainEnvArtifactInfo));
375 catalogDB.saveHeatEnvironment(heatEnv);
376 // Indicate we have deployed it in the DB
377 vfResourceStructure.getArtifactsMapByUUID().get(mainEnvArtifactInfo.getArtifactUUID()).incrementDeployedInDB();
383 // here we expect one VFModule to be there
384 VfResourceInstaller.createVfModule(vfModuleStructure,heatMainTemplate, heatVolumeTemplate, heatEnv, heatVolumeEnv);
385 VfModule vfModule = vfModuleStructure.getCatalogVfModule();
387 // Add this one for logging
388 artifactListForLogging.add(ASDCElementInfo.createElementFromVfModuleStructure(vfModuleStructure));
390 catalogDB.saveOrUpdateVfModule(vfModule);
392 // Here we expect many HEAT_TEMPLATE files to be there
393 if (vfModuleStructure.getArtifactsMap().containsKey(ASDCConfiguration.HEAT_ARTIFACT)) {
394 for (VfModuleArtifact heatArtifact : vfModuleStructure.getArtifactsMap()
395 .get(ASDCConfiguration.HEAT_ARTIFACT)) {
397 HeatFiles heatFile = (HeatFiles) heatArtifact.getCatalogObject();
399 // Add this one for logging
400 artifactListForLogging.add(ASDCElementInfo.createElementFromVfArtifactInfo(heatArtifact.getArtifactInfo()));
403 catalogDB.saveVfModuleToHeatFiles (vfModule.getId(), heatFile);
404 // Indicate we will deploy it in the DB
405 heatArtifact.incrementDeployedInDB();
412 vfResourceStructure.setSuccessfulDeployment();
414 } catch (Exception e) {
416 Throwable dbExceptionToCapture = e;
417 while (!(dbExceptionToCapture instanceof ConstraintViolationException || dbExceptionToCapture instanceof LockAcquisitionException)
418 && (dbExceptionToCapture.getCause() != null)) {
419 dbExceptionToCapture = dbExceptionToCapture.getCause();
422 if (dbExceptionToCapture instanceof ConstraintViolationException || dbExceptionToCapture instanceof LockAcquisitionException) {
423 logger.warn(MessageEnum.ASDC_ARTIFACT_ALREADY_DEPLOYED, vfResourceStructure.getResourceInstance().getResourceName(),
424 vfResourceStructure.getNotification().getServiceVersion(), "", "", MsoLogger.ErrorCode.DataError, "Exception - ASCDC Artifact already deployed", e);
426 String endEvent = "Exception caught during installation of " + vfResourceStructure.getResourceInstance().getResourceName() + ". Transaction rollback.";
427 String elementToLog = (artifactListForLogging.size() > 0 ? artifactListForLogging.get(artifactListForLogging.size()-1).toString() : "No element listed");
428 logger.error(MessageEnum.ASDC_ARTIFACT_INSTALL_EXC, elementToLog, "", "", MsoLogger.ErrorCode.DataError, "Exception caught during installation of " + vfResourceStructure.getResourceInstance().getResourceName() + ". Transaction rollback", e);
429 catalogDB.rollback();
430 throw new ArtifactInstallerException(
431 "Exception caught during installation of " + vfResourceStructure.getResourceInstance().getResourceName() + ". Transaction rollback.", e);
436 // Debug log the whole collection...
437 logger.debug(artifactListForLogging.toString());
442 private static String identifyParentOfNestedTemplate(VfModuleStructure vfModuleStructure,VfModuleArtifact heatNestedArtifact) {
444 if (vfModuleStructure.getArtifactsMap().get(ASDCConfiguration.HEAT) != null
445 && vfModuleStructure.getArtifactsMap().get(ASDCConfiguration.HEAT).get(0).getArtifactInfo().getRelatedArtifacts() != null) {
446 for (IArtifactInfo unknownArtifact : vfModuleStructure.getArtifactsMap().get(ASDCConfiguration.HEAT).get(0)
447 .getArtifactInfo().getRelatedArtifacts()) {
448 if (heatNestedArtifact.getArtifactInfo().getArtifactUUID().equals(unknownArtifact.getArtifactUUID())) {
449 return ASDCConfiguration.HEAT;
455 if (vfModuleStructure.getArtifactsMap().get(ASDCConfiguration.HEAT_VOL) != null
456 && vfModuleStructure.getArtifactsMap().get(ASDCConfiguration.HEAT_VOL).get(0).getArtifactInfo().getRelatedArtifacts() != null) {
457 for (IArtifactInfo unknownArtifact:vfModuleStructure.getArtifactsMap().get(ASDCConfiguration.HEAT_VOL).get(0).getArtifactInfo().getRelatedArtifacts()) {
458 if (heatNestedArtifact.getArtifactInfo().getArtifactUUID().equals(unknownArtifact.getArtifactUUID())) {
459 return ASDCConfiguration.HEAT_VOL;
465 // Does not belong to anything
470 private static void createVnfResource(VfResourceStructure vfResourceStructure) {
471 VnfResource vnfResource = new VnfResource();
473 vnfResource.setAsdcUuid(vfResourceStructure.getResourceInstance().getResourceUUID());
474 vnfResource.setDescription(vfResourceStructure.getNotification().getServiceDescription());
476 vnfResource.setOrchestrationMode("HEAT");
477 // Set the version but Version is stored into ASDC_SERVICE_MODEL_VERSION
478 vnfResource.setVersion(BigDecimalVersion
479 .castAndCheckNotificationVersionToString(vfResourceStructure.getNotification().getServiceVersion()));
480 vnfResource.setVnfType(VfResourceInstaller.createVNFName(vfResourceStructure));
481 vnfResource.setModelVersion(BigDecimalVersion
482 .castAndCheckNotificationVersionToString(vfResourceStructure.getResourceInstance().getResourceVersion()));
484 vnfResource.setModelInvariantUuid(vfResourceStructure.getResourceInstance().getResourceInvariantUUID());
485 vnfResource.setModelCustomizationName(vfResourceStructure.getResourceInstance().getResourceInstanceName());
486 vnfResource.setModelCustomizationUuid(vfResourceStructure.getResourceInstance().getResourceCustomizationUUID());
487 vnfResource.setModelName(vfResourceStructure.getResourceInstance().getResourceName());
488 vnfResource.setServiceModelInvariantUUID(vfResourceStructure.getNotification().getServiceInvariantUUID());
489 //vnfResource.setCreated(getCurrentTimeStamp());
491 vfResourceStructure.setCatalogVnfResource(vnfResource);
494 private static void createNetworkResourceCustomization(VfResourceStructure vfResourceStructure) {
495 NetworkResourceCustomization networkResourceCustomization = new NetworkResourceCustomization();
497 networkResourceCustomization.setModelCustomizationUuid(vfResourceStructure.getResourceInstance().getResourceCustomizationUUID().trim());
498 networkResourceCustomization.setModelName(vfResourceStructure.getResourceInstance().getResourceName().trim());
499 networkResourceCustomization.setModelInstanceName(vfResourceStructure.getResourceInstance().getResourceInstanceName().trim());
500 networkResourceCustomization.setModelInvariantUuid(vfResourceStructure.getResourceInstance().getResourceInvariantUUID().trim());
501 networkResourceCustomization.setModelUuid(vfResourceStructure.getResourceInstance().getResourceUUID().trim());
502 networkResourceCustomization.setModelVersion(vfResourceStructure.getResourceInstance().getResourceVersion().trim());
503 //networkResourceCustomization.setCreated(getCurrentTimeStamp());
505 vfResourceStructure.setCatalogNetworkResourceCustomization(networkResourceCustomization);
507 ServiceToNetworks serviceNetworks = new ServiceToNetworks();
508 serviceNetworks.setNetworkModelCustomizationUuid(networkResourceCustomization.getModelCustomizationUuid());
509 serviceNetworks.setServiceModelUuid(vfResourceStructure.getNotification().getServiceUUID());
511 vfResourceStructure.setCatalogServiceToNetworks(serviceNetworks);
515 private static void createAllottedResourceCustomization(VfResourceStructure vfResourceStructure) {
516 AllottedResourceCustomization resourceCustomization = new AllottedResourceCustomization();
518 resourceCustomization.setModelCustomizationUuid(vfResourceStructure.getResourceInstance().getResourceCustomizationUUID().trim());
519 resourceCustomization.setModelName(vfResourceStructure.getResourceInstance().getResourceName().trim());
520 resourceCustomization.setModelInstanceName(vfResourceStructure.getResourceInstance().getResourceInstanceName().trim());
521 resourceCustomization.setModelInvariantUuid(vfResourceStructure.getResourceInstance().getResourceInvariantUUID().trim());
522 resourceCustomization.setModelUuid(vfResourceStructure.getResourceInstance().getResourceUUID().trim());
523 resourceCustomization.setVersion(vfResourceStructure.getResourceInstance().getResourceVersion().trim());
524 //resourceCustomization.setCreated(getCurrentTimeStamp());
526 vfResourceStructure.setCatalogResourceCustomization(resourceCustomization);
528 ServiceToAllottedResources serviceAllottedResources = new ServiceToAllottedResources();
529 serviceAllottedResources.setArModelCustomizationUuid(resourceCustomization.getModelCustomizationUuid());
530 serviceAllottedResources.setServiceModelUuid(vfResourceStructure.getNotification().getServiceUUID());
532 vfResourceStructure.setCatalogServiceToAllottedResources(serviceAllottedResources);
536 private static void createVfModule(VfModuleStructure vfModuleStructure,HeatTemplate heatMain, HeatTemplate heatVolume,HeatEnvironment heatEnv, HeatEnvironment heatVolumeEnv) {
537 VfModule vfModule = new VfModule();
538 vfModule.setType(createVfModuleName(vfModuleStructure));
539 vfModule.setAsdcUuid(vfModuleStructure.getVfModuleMetadata().getVfModuleModelUUID());
540 vfModule.setDescription(vfModuleStructure.getVfModuleMetadata().getVfModuleModelDescription());
542 if (vfModuleStructure.getVfModuleMetadata().isBase()) {
543 vfModule.setIsBase(1);
545 vfModule.setIsBase(0);
548 vfModule.setModelCustomizationUuid(vfModuleStructure.getVfModuleMetadata().getVfModuleModelCustomizationUUID());
549 vfModule.setModelInvariantUuid(vfModuleStructure.getVfModuleMetadata().getVfModuleModelInvariantUUID());
550 vfModule.setModelName(vfModuleStructure.getVfModuleMetadata().getVfModuleModelName());
552 vfModule.setVersion(BigDecimalVersion.castAndCheckNotificationVersionToString(vfModuleStructure.getParentVfResource().getNotification().getServiceVersion()));
553 vfModule.setModelVersion(BigDecimalVersion.castAndCheckNotificationVersionToString(
554 vfModuleStructure.getVfModuleMetadata().getVfModuleModelVersion()));
556 Map<String,String> map = vfModuleStructure.getVfModuleMetadata().getProperties();
560 if(map.get("vf_module_label") != null){
561 vfModule.setLabel(map.get("vf_module_label"));
563 if(map.get("initial_count") != null && map.get("initial_count").length() > 0){
564 vfModule.setInitialCount(Integer.parseInt(map.get("initial_count")));
566 if(map.get("min_vf_module_instances") != null && map.get("min_vf_module_instances").length() > 0){
567 vfModule.setMinInstances(Integer.parseInt(map.get("min_vf_module_instances")));
569 if(map.get("max_vf_module_instances") != null && map.get("max_vf_module_instances").length() > 0){
570 vfModule.setMaxInstances(Integer.parseInt(map.get("max_vf_module_instances")));
575 vfModuleStructure.setCatalogVfModule(vfModule);
577 VfResourceInstaller.createVfModuleLinks(vfModule, vfModuleStructure.getParentVfResource().getCatalogVnfResource(), heatMain,heatVolume, heatEnv,heatVolumeEnv);
580 private static void createVfModuleLinks(VfModule vfModule, VnfResource vnfResource, HeatTemplate heatMain,
581 HeatTemplate heatVolume, HeatEnvironment heatEnv, HeatEnvironment heatVolumeEnv) {
583 if (heatMain !=null) {
584 vfModule.setTemplateId(heatMain.getId());
586 if (heatEnv != null) {
587 vfModule.setEnvironmentId(heatEnv.getId());
589 if (heatVolume != null) {
590 vfModule.setVolTemplateId(heatVolume.getId());
592 if (heatVolumeEnv != null) {
593 vfModule.setVolEnvironmentId(heatVolumeEnv.getId());
596 vfModule.setVnfResourceId(vnfResource.getId());
600 private static Set<HeatTemplateParam> extractHeatTemplateParameters(String yamlFile) {
602 // Scan the payload downloadResult and extract the HeatTemplate
604 YamlEditor yamlEditor = new YamlEditor(yamlFile.getBytes());
605 return yamlEditor.getParameterList();
610 public static String verifyTheFilePrefixInArtifacts(String filebody, VfResourceStructure vfResourceStructure, List<String> listTypes) {
611 String newFileBody = filebody;
612 for (VfModuleArtifact moduleArtifact:vfResourceStructure.getArtifactsMapByUUID().values()) {
614 if (listTypes.contains(moduleArtifact.getArtifactInfo().getArtifactType())) {
616 newFileBody = verifyTheFilePrefixInString(newFileBody,moduleArtifact.getArtifactInfo().getArtifactName());
622 public static String verifyTheFilePrefixInString(final String body, final String filenameToVerify) {
624 String needlePrefix = "file:///";
625 String prefixedFilenameToVerify = needlePrefix+filenameToVerify;
627 if ((body == null) || (body.length() == 0) || (filenameToVerify == null) || (filenameToVerify.length() == 0)) {
631 StringBuffer sb = new StringBuffer(body.length());
633 int currentIndex = 0;
636 while (currentIndex != -1) {
637 startIndex = currentIndex;
638 currentIndex = body.indexOf(prefixedFilenameToVerify, startIndex);
640 if (currentIndex == -1) {
644 // We append from the startIndex up to currentIndex (start of File Name)
645 sb.append(body.substring(startIndex, currentIndex));
646 sb.append(filenameToVerify);
648 currentIndex += prefixedFilenameToVerify.length();
651 sb.append(body.substring(startIndex));
653 return sb.toString();
656 private static void createHeatTemplateFromArtifact(VfResourceStructure vfResourceStructure,
657 VfModuleArtifact vfModuleArtifact) {
658 HeatTemplate heatTemplate = new HeatTemplate();
660 // TODO Set the label
661 heatTemplate.setAsdcLabel("label");
662 // Use the ResourceName of the ASDC template because the HEAT could be
664 heatTemplate.setAsdcResourceName(vfResourceStructure.getResourceInstance().getResourceName());
665 heatTemplate.setAsdcUuid(vfModuleArtifact.getArtifactInfo().getArtifactUUID());
667 List<String> typeList = new ArrayList<String>();
668 typeList.add(ASDCConfiguration.HEAT_NESTED);
669 typeList.add(ASDCConfiguration.HEAT_ARTIFACT);
671 heatTemplate.setTemplateBody(verifyTheFilePrefixInArtifacts(vfModuleArtifact.getResult(),vfResourceStructure,typeList));
672 heatTemplate.setTemplateName(vfModuleArtifact.getArtifactInfo().getArtifactName());
674 if (vfModuleArtifact.getArtifactInfo().getArtifactTimeout() != null) {
675 heatTemplate.setTimeoutMinutes(vfModuleArtifact.getArtifactInfo().getArtifactTimeout());
677 heatTemplate.setTimeoutMinutes(240);
680 heatTemplate.setDescription(vfModuleArtifact.getArtifactInfo().getArtifactDescription());
681 heatTemplate.setVersion(BigDecimalVersion
682 .castAndCheckNotificationVersionToString(vfModuleArtifact.getArtifactInfo().getArtifactVersion()));
684 if(vfModuleArtifact.getArtifactInfo().getArtifactChecksum() != null){
685 heatTemplate.setArtifactChecksum(vfModuleArtifact.getArtifactInfo().getArtifactChecksum());
687 heatTemplate.setArtifactChecksum("MANUAL_RECORD");
690 Set<HeatTemplateParam> heatParam = VfResourceInstaller
691 .extractHeatTemplateParameters(vfModuleArtifact.getResult());
692 heatTemplate.setParameters(heatParam);
694 vfModuleArtifact.setCatalogObject(heatTemplate);
697 private static void createHeatEnvFromArtifact(VfResourceStructure vfResourceStructure,
698 VfModuleArtifact vfModuleArtifact) {
699 HeatEnvironment heatEnvironment = new HeatEnvironment();
701 heatEnvironment.setName(vfModuleArtifact.getArtifactInfo().getArtifactName());
702 // TODO Set the label
703 heatEnvironment.setAsdcLabel("Label");
705 List<String> typeList = new ArrayList<String>();
706 typeList.add(ASDCConfiguration.HEAT);
707 typeList.add(ASDCConfiguration.HEAT_VOL);
709 heatEnvironment.setEnvironment(verifyTheFilePrefixInArtifacts(vfModuleArtifact.getResult(),vfResourceStructure,typeList));
710 heatEnvironment.setAsdcUuid(vfModuleArtifact.getArtifactInfo().getArtifactUUID());
711 heatEnvironment.setDescription(vfModuleArtifact.getArtifactInfo().getArtifactDescription());
712 heatEnvironment.setVersion(BigDecimalVersion
713 .castAndCheckNotificationVersionToString(vfModuleArtifact.getArtifactInfo().getArtifactVersion()));
714 heatEnvironment.setAsdcResourceName(VfResourceInstaller.createVNFName(vfResourceStructure));
716 if(vfModuleArtifact.getArtifactInfo().getArtifactChecksum() != null){
717 heatEnvironment.setArtifactChecksum(vfModuleArtifact.getArtifactInfo().getArtifactChecksum());
719 heatEnvironment.setArtifactChecksum("MANUAL_RECORD");
722 vfModuleArtifact.setCatalogObject(heatEnvironment);
726 private static void createHeatFileFromArtifact(VfResourceStructure vfResourceStructure,
727 VfModuleArtifact vfModuleArtifact) {
729 HeatFiles heatFile = new HeatFiles();
730 // TODO Set the label
731 heatFile.setAsdcLabel("Label");
732 heatFile.setAsdcUuid(vfModuleArtifact.getArtifactInfo().getArtifactUUID());
733 heatFile.setDescription(vfModuleArtifact.getArtifactInfo().getArtifactDescription());
734 heatFile.setFileBody(vfModuleArtifact.getResult());
735 heatFile.setFileName(vfModuleArtifact.getArtifactInfo().getArtifactName());
736 heatFile.setVersion(BigDecimalVersion
737 .castAndCheckNotificationVersionToString(vfModuleArtifact.getArtifactInfo().getArtifactVersion()));
739 heatFile.setAsdcResourceName(vfResourceStructure.getResourceInstance().getResourceName());
741 if(vfModuleArtifact.getArtifactInfo().getArtifactChecksum() != null){
742 heatFile.setArtifactChecksum(vfModuleArtifact.getArtifactInfo().getArtifactChecksum());
744 heatFile.setArtifactChecksum("MANUAL_RECORD");
747 vfModuleArtifact.setCatalogObject(heatFile);
751 private static void createService(VfResourceStructure vfResourceStructure) {
753 Service service = new Service();
754 service.setDescription(vfResourceStructure.getNotification().getServiceDescription());
755 service.setServiceName(vfResourceStructure.getNotification().getServiceName());
756 service.setServiceNameVersionId(vfResourceStructure.getNotification().getServiceUUID());
757 service.setVersion(vfResourceStructure.getNotification().getServiceVersion());
758 service.setModelInvariantUUID(vfResourceStructure.getNotification().getServiceInvariantUUID());
760 vfResourceStructure.setCatalogService(service);
764 private static String createVNFName(VfResourceStructure vfResourceStructure) {
766 return vfResourceStructure.getNotification().getServiceName() + "/" + vfResourceStructure.getResourceInstance().getResourceInstanceName();
769 private static String createVfModuleName(VfModuleStructure vfModuleStructure) {
771 return createVNFName(vfModuleStructure.getParentVfResource())+"::"+vfModuleStructure.getVfModuleMetadata().getVfModuleModelName();
775 private static Timestamp getCurrentTimeStamp() {
777 return new Timestamp(new Date().getTime());