push addional code
[sdc.git] / openecomp-be / backend / openecomp-sdc-vendor-software-product-manager / src / main / java / org / openecomp / sdc / vendorsoftwareproduct / impl / VendorSoftwareProductManagerImpl.java
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.vendorsoftwareproduct.impl;
22
23 import static org.openecomp.sdc.vendorsoftwareproduct.VendorSoftwareProductConstants.CSAR;
24 import static org.openecomp.sdc.vendorsoftwareproduct.VendorSoftwareProductConstants.GENERAL_COMPONENT_ID;
25 import static org.openecomp.sdc.vendorsoftwareproduct.VendorSoftwareProductConstants.UPLOAD_RAW_DATA;
26 import static org.openecomp.sdc.vendorsoftwareproduct.VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE;
27 import static org.openecomp.sdc.vendorsoftwareproduct.VendorSoftwareProductConstants.VSP_PACKAGE_ZIP;
28
29 import org.apache.commons.collections4.CollectionUtils;
30 import org.apache.commons.collections4.MapUtils;
31 import org.openecomp.core.enrichment.EnrichmentArtifactsServiceFactory;
32 import org.openecomp.core.enrichment.api.EnrichmentManager;
33 import org.openecomp.core.enrichment.enrichmentartifacts.EnrichmentArtifactsService;
34 import org.openecomp.core.enrichment.factory.EnrichmentManagerFactory;
35 import org.openecomp.core.enrichment.types.ComponentArtifactType;
36 import org.openecomp.core.model.dao.EnrichedServiceModelDao;
37 import org.openecomp.core.model.dao.EnrichedServiceModelDaoFactory;
38 import org.openecomp.core.model.dao.ServiceModelDao;
39 import org.openecomp.core.model.dao.ServiceModelDaoFactory;
40 import org.openecomp.core.model.types.ServiceElement;
41 import org.openecomp.core.util.UniqueValueUtil;
42 import org.openecomp.core.utilities.CommonMethods;
43 import org.openecomp.core.utilities.file.FileContentHandler;
44 import org.openecomp.core.utilities.file.FileUtils;
45 import org.openecomp.core.utilities.json.JsonSchemaDataGenerator;
46 import org.openecomp.core.utilities.json.JsonUtil;
47 import org.openecomp.core.validation.api.ValidationManager;
48 import org.openecomp.core.validation.errors.Messages;
49 import org.openecomp.core.validation.types.MessageContainerUtil;
50 import org.openecomp.sdc.common.errors.CoreException;
51 import org.openecomp.sdc.common.errors.ErrorCategory;
52 import org.openecomp.sdc.common.errors.ErrorCode;
53 import org.openecomp.sdc.common.errors.ValidationErrorBuilder;
54 import org.openecomp.sdc.common.utils.AsdcCommon;
55 import org.openecomp.sdc.datatypes.error.ErrorLevel;
56 import org.openecomp.sdc.datatypes.error.ErrorMessage;
57 import org.openecomp.sdc.enrichment.impl.tosca.ComponentInfo;
58 import org.openecomp.sdc.heat.datatypes.structure.HeatStructureTree;
59 import org.openecomp.sdc.heat.datatypes.structure.ValidationStructureList;
60 import org.openecomp.sdc.heat.services.tree.HeatTreeManager;
61 import org.openecomp.sdc.heat.services.tree.HeatTreeManagerUtil;
62 import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel;
63 import org.openecomp.sdc.tosca.services.impl.ToscaFileOutputServiceCsarImpl;
64 import org.openecomp.sdc.validation.utils.ValidationManagerUtil;
65 import org.openecomp.sdc.vendorlicense.VendorLicenseArtifactServiceFactory;
66 import org.openecomp.sdc.vendorlicense.facade.VendorLicenseFacade;
67 import org.openecomp.sdc.vendorlicense.facade.VendorLicenseFacadeFactory;
68 import org.openecomp.sdc.vendorlicense.licenseartifacts.VendorLicenseArtifactsService;
69 import org.openecomp.sdc.vendorsoftwareproduct.VendorSoftwareProductConstants;
70 import org.openecomp.sdc.vendorsoftwareproduct.VendorSoftwareProductManager;
71 import org.openecomp.sdc.vendorsoftwareproduct.dao.ComponentArtifactDao;
72 import org.openecomp.sdc.vendorsoftwareproduct.dao.ComponentArtifactDaoFactory;
73 import org.openecomp.sdc.vendorsoftwareproduct.dao.VendorSoftwareProductDao;
74 import org.openecomp.sdc.vendorsoftwareproduct.dao.VendorSoftwareProductDaoFactory;
75 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentArtifactEntity;
76 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentEntity;
77 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.NetworkEntity;
78 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.PackageInfo;
79 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ProcessArtifactEntity;
80 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.UploadDataEntity;
81 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspDetails;
82 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspQuestionnaireEntity;
83 import org.openecomp.sdc.vendorsoftwareproduct.errors.CreatePackageForNonFinalVendorSoftwareProductErrorBuilder;
84 import org.openecomp.sdc.vendorsoftwareproduct.errors.FileCreationErrorBuilder;
85 import org.openecomp.sdc.vendorsoftwareproduct.errors.MibUploadErrorBuilder;
86 import org.openecomp.sdc.vendorsoftwareproduct.errors.PackageInvalidErrorBuilder;
87 import org.openecomp.sdc.vendorsoftwareproduct.errors.PackageNotFoundErrorBuilder;
88 import org.openecomp.sdc.vendorsoftwareproduct.errors.TranslationFileCreationErrorBuilder;
89 import org.openecomp.sdc.vendorsoftwareproduct.errors.UploadInvalidErrorBuilder;
90 import org.openecomp.sdc.vendorsoftwareproduct.errors.VendorSoftwareProductErrorCodes;
91 import org.openecomp.sdc.vendorsoftwareproduct.errors.VendorSoftwareProductInvalidErrorBuilder;
92 import org.openecomp.sdc.vendorsoftwareproduct.errors.VendorSoftwareProductNotFoundErrorBuilder;
93 import org.openecomp.sdc.vendorsoftwareproduct.services.CompositionDataExtractor;
94 import org.openecomp.sdc.vendorsoftwareproduct.services.CompositionEntityDataManager;
95 import org.openecomp.sdc.vendorsoftwareproduct.services.SchemaGenerator;
96 import org.openecomp.sdc.vendorsoftwareproduct.types.CompositionEntityResponse;
97 import org.openecomp.sdc.vendorsoftwareproduct.types.CompositionEntityValidationData;
98 import org.openecomp.sdc.vendorsoftwareproduct.types.QuestionnaireResponse;
99 import org.openecomp.sdc.vendorsoftwareproduct.types.QuestionnaireValidationResult;
100 import org.openecomp.sdc.vendorsoftwareproduct.types.UploadFileResponse;
101 import org.openecomp.sdc.vendorsoftwareproduct.types.ValidationResponse;
102 import org.openecomp.sdc.vendorsoftwareproduct.types.VersionedVendorSoftwareProductInfo;
103 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.Component;
104 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.ComponentData;
105 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.CompositionData;
106 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.CompositionEntityId;
107 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.CompositionEntityType;
108 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.Network;
109 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.Nic;
110 import org.openecomp.sdc.vendorsoftwareproduct.types.schemagenerator.ComponentCompositionSchemaInput;
111 import org.openecomp.sdc.vendorsoftwareproduct.types.schemagenerator.ComponentQuestionnaireSchemaInput;
112 import org.openecomp.sdc.vendorsoftwareproduct.types.schemagenerator.MibUploadStatus;
113 import org.openecomp.sdc.vendorsoftwareproduct.types.schemagenerator.NetworkCompositionSchemaInput;
114 import org.openecomp.sdc.vendorsoftwareproduct.types.schemagenerator.NicCompositionSchemaInput;
115 import org.openecomp.sdc.vendorsoftwareproduct.types.schemagenerator.SchemaTemplateContext;
116 import org.openecomp.sdc.vendorsoftwareproduct.types.schemagenerator.SchemaTemplateInput;
117 import org.openecomp.sdc.vendorsoftwareproduct.util.CompilationUtil;
118 import org.openecomp.sdc.vendorsoftwareproduct.util.VendorSoftwareProductUtils;
119 import org.openecomp.sdc.versioning.VersioningManager;
120 import org.openecomp.sdc.versioning.VersioningManagerFactory;
121 import org.openecomp.sdc.versioning.VersioningUtil;
122 import org.openecomp.sdc.versioning.dao.types.Version;
123 import org.openecomp.sdc.versioning.dao.types.VersionStatus;
124 import org.openecomp.sdc.versioning.errors.RequestedVersionInvalidErrorBuilder;
125 import org.openecomp.sdc.versioning.types.VersionInfo;
126 import org.openecomp.sdc.versioning.types.VersionableEntityAction;
127
128 import java.io.File;
129 import java.io.FileOutputStream;
130 import java.io.IOException;
131 import java.io.InputStream;
132 import java.nio.ByteBuffer;
133 import java.util.ArrayList;
134 import java.util.Collection;
135 import java.util.Collections;
136 import java.util.Comparator;
137 import java.util.HashMap;
138 import java.util.List;
139 import java.util.Map;
140 import java.util.Objects;
141 import java.util.stream.Collectors;
142
143 /**
144  * The type Vendor software product manager.
145  */
146 public class VendorSoftwareProductManagerImpl implements VendorSoftwareProductManager {
147
148   private static final String VSP_COMPOSITION_EDIT_NOT_ALLOWED_MSG =
149       "Composition entities may not be created / deleted for Vendor Software Product whose "
150           + "entities were uploaded";
151
152   private static final VersioningManager versioningManager =
153       VersioningManagerFactory.getInstance().createInterface();
154   private static final VendorSoftwareProductDao vendorSoftwareProductDao =
155       VendorSoftwareProductDaoFactory.getInstance().createInterface();
156   private static final VendorLicenseFacade vendorLicenseFacade =
157       VendorLicenseFacadeFactory.getInstance().createInterface();
158   private static final ComponentArtifactDao componentArtifactDao =
159       ComponentArtifactDaoFactory.getInstance().createInterface();
160   private static final ServiceModelDao<ToscaServiceModel, ServiceElement> serviceModelDao =
161       ServiceModelDaoFactory.getInstance().createInterface();
162   private static final EnrichedServiceModelDao<ToscaServiceModel, ServiceElement>
163       enrichedServiceModelDao = EnrichedServiceModelDaoFactory.getInstance().createInterface();
164   private static VendorLicenseArtifactsService licenseArtifactsService =
165       VendorLicenseArtifactServiceFactory.getInstance().createInterface();
166   private static EnrichmentArtifactsService enrichmentArtifactsService =
167       EnrichmentArtifactsServiceFactory.getInstance().createInterface();
168
169
170   /**
171    * Instantiates a new Vendor software product manager.
172    */
173   public VendorSoftwareProductManagerImpl() {
174     vendorSoftwareProductDao.registerVersioning(VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE);
175     serviceModelDao.registerVersioning(VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE);
176     enrichedServiceModelDao.registerVersioning(VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE);
177     componentArtifactDao.registerVersioning(VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE);
178   }
179
180   private static List<ErrorCode> validateCompletedVendorSoftwareProduct(VspDetails vspDetails,
181                                                                         UploadDataEntity uploadData,
182                                                                         Object serviceModel) {
183     List<ErrorCode> errros = new ArrayList<>();
184
185     if (vspDetails.getName() == null) {
186       errros.add(createMissingMandatoryFieldError("name"));
187     }
188     if (vspDetails.getDescription() == null) {
189       errros.add(createMissingMandatoryFieldError("description"));
190     }
191     if (vspDetails.getVendorId() == null) {
192       errros.add(createMissingMandatoryFieldError("vendor Id"));
193     }
194     if (vspDetails.getVlmVersion() == null) {
195       errros.add(createMissingMandatoryFieldError(
196           "licensing version (in the format of: {integer}.{integer})"));
197     }
198     if (vspDetails.getCategory() == null) {
199       errros.add(createMissingMandatoryFieldError("category"));
200     }
201     if (vspDetails.getSubCategory() == null) {
202       errros.add(createMissingMandatoryFieldError("sub category"));
203     }
204     if (vspDetails.getLicenseAgreement() == null) {
205       errros.add(createMissingMandatoryFieldError("license agreement"));
206     }
207     if (CollectionUtils.isEmpty(vspDetails.getFeatureGroups())) {
208       errros.add(createMissingMandatoryFieldError("feature groups"));
209     }
210     if (uploadData == null || uploadData.getContentData() == null || serviceModel == null) {
211       errros.add(
212           new VendorSoftwareProductInvalidErrorBuilder(vspDetails.getId(), vspDetails.getVersion())
213               .build());
214     }
215
216     return errros.isEmpty() ? null : errros;
217   }
218
219   private static ErrorCode createMissingMandatoryFieldError(String fieldName) {
220     return new ValidationErrorBuilder("must be supplied", fieldName).build();
221   }
222
223   private static String getVspQuestionnaireSchema(SchemaTemplateInput schemaInput) {
224     return SchemaGenerator
225         .generate(SchemaTemplateContext.questionnaire, CompositionEntityType.vsp, schemaInput);
226   }
227
228   private static String getComponentQuestionnaireSchema(SchemaTemplateInput schemaInput) {
229     return SchemaGenerator
230         .generate(SchemaTemplateContext.questionnaire, CompositionEntityType.component,
231             schemaInput);
232   }
233
234   private static String getNicQuestionnaireSchema(SchemaTemplateInput schemaInput) {
235     return SchemaGenerator
236         .generate(SchemaTemplateContext.questionnaire, CompositionEntityType.nic, schemaInput);
237   }
238
239   private static void sortVspListByModificationTimeDescOrder(
240       List<VersionedVendorSoftwareProductInfo> vendorLicenseModels) {
241     Collections.sort(vendorLicenseModels, new Comparator<VersionedVendorSoftwareProductInfo>() {
242       @Override
243       public int compare(VersionedVendorSoftwareProductInfo o1,
244                          VersionedVendorSoftwareProductInfo o2) {
245         return o2.getVspDetails().getWritetimeMicroSeconds()
246             .compareTo(o1.getVspDetails().getWritetimeMicroSeconds());
247       }
248     });
249   }
250
251   private boolean isManual(String vspId, Version version) {
252     return false;
253   }
254
255   @Override
256   public Version checkout(String vendorSoftwareProductId, String user) {
257     Version newVersion = versioningManager
258         .checkout(VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE, vendorSoftwareProductId, user);
259     vendorSoftwareProductDao.updateVspLatestModificationTime(vendorSoftwareProductId, newVersion);
260     return newVersion;
261   }
262
263   @Override
264   public Version undoCheckout(String vendorSoftwareProductId, String user) {
265     Version newVersion = versioningManager
266         .undoCheckout(VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE, vendorSoftwareProductId, user);
267     vendorSoftwareProductDao.updateVspLatestModificationTime(vendorSoftwareProductId, newVersion);
268     return newVersion;
269   }
270
271   @Override
272   public Version checkin(String vendorSoftwareProductId, String user) {
273     Version newVersion = versioningManager
274         .checkin(VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE, vendorSoftwareProductId, user, null);
275     vendorSoftwareProductDao.updateVspLatestModificationTime(vendorSoftwareProductId, newVersion);
276     return newVersion;
277   }
278
279   @Override
280   public ValidationResponse submit(String vendorSoftwareProductId, String user) throws IOException {
281     VspDetails vspDetails = getVspDetails(vendorSoftwareProductId, null, user).getVspDetails();
282     UploadDataEntity uploadData = vendorSoftwareProductDao
283         .getUploadData(new UploadDataEntity(vendorSoftwareProductId, vspDetails.getVersion()));
284     ToscaServiceModel serviceModel =
285         serviceModelDao.getServiceModel(vendorSoftwareProductId, vspDetails.getVersion());
286     Version newVersion = null;
287
288     ValidationResponse validationResponse = new ValidationResponse();
289     validationResponse
290         .setVspErrors(validateCompletedVendorSoftwareProduct(vspDetails, uploadData, serviceModel));
291     validationResponse.setLicensingDataErrors(validateLicensingData(vspDetails));
292     validationResponse.setUploadDataErrors(validateUploadData(uploadData));
293     validationResponse.setQuestionnaireValidationResult(
294         validateQuestionnaire(vspDetails.getId(), vspDetails.getVersion()));
295     validationResponse.setCompilationErrors(
296         compile(vendorSoftwareProductId, vspDetails.getVersion(), serviceModel));
297
298     if (validationResponse.isValid()) {
299       newVersion = versioningManager
300           .submit(VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE, vendorSoftwareProductId, user, null);
301     }
302     //vendorSoftwareProductDao.updateVspLatestModificationTime(vendorSoftwareProductId, newVersion);
303     return validationResponse;
304   }
305
306   private Map<String, List<ErrorMessage>> compile(String vendorSoftwareProductId, Version version,
307                                                   ToscaServiceModel serviceModel) {
308     Collection<ComponentEntity> components = listComponents(vendorSoftwareProductId, version);
309     if (serviceModel == null) {
310       return null;
311     }
312     if (CollectionUtils.isEmpty(components)) {
313       enrichedServiceModelDao.storeServiceModel(vendorSoftwareProductId, version, serviceModel);
314       return null;
315     }
316     EnrichmentManager<ToscaServiceModel> enrichmentManager =
317         EnrichmentManagerFactory.getInstance().createInterface();
318     enrichmentManager.initInput(vendorSoftwareProductId, version);
319     enrichmentManager.addModel(serviceModel);
320
321     ComponentInfo componentInfo = new ComponentInfo();
322     Map<String, List<ErrorMessage>> compileErrors = new HashMap<>();
323     CompilationUtil.addMonitoringInfo(componentInfo, compileErrors);
324     for (ComponentEntity componentEntity : components) {
325       ComponentInfo currentEntityComponentInfo = new ComponentInfo();
326       currentEntityComponentInfo.setCeilometerInfo(componentInfo.getCeilometerInfo());
327       CompilationUtil
328           .addMibInfo(vendorSoftwareProductId, version, componentEntity, currentEntityComponentInfo,
329               compileErrors);
330       enrichmentManager.addEntityInput(componentEntity.getComponentCompositionData().getName(),
331           currentEntityComponentInfo);
332
333     }
334     Map<String, List<ErrorMessage>> enrichErrors;
335     enrichErrors = enrichmentManager.enrich();
336     enrichedServiceModelDao
337         .storeServiceModel(vendorSoftwareProductId, version, enrichmentManager.getModel());
338     if (enrichErrors != null) {
339       compileErrors.putAll(enrichErrors);
340     }
341
342     vendorSoftwareProductDao.updateVspLatestModificationTime(vendorSoftwareProductId, version);
343
344     return compileErrors;
345   }
346
347   private Collection<ErrorCode> validateLicensingData(VspDetails vspDetails) {
348     if (vspDetails.getVendorId() == null || vspDetails.getVlmVersion() == null
349         || vspDetails.getLicenseAgreement() == null
350         || CollectionUtils.isEmpty(vspDetails.getFeatureGroups())) {
351       return null;
352     }
353     return vendorLicenseFacade
354         .validateLicensingData(vspDetails.getVendorId(), vspDetails.getVlmVersion(),
355             vspDetails.getLicenseAgreement(), vspDetails.getFeatureGroups());
356   }
357
358   @Override
359   public VspDetails createNewVsp(VspDetails vspDetails, String user) {
360     UniqueValueUtil.validateUniqueValue(
361         VendorSoftwareProductConstants.UniqueValues.VENDOR_SOFTWARE_PRODUCT_NAME,
362         vspDetails.getName());
363     vspDetails.setId(CommonMethods.nextUuId());
364
365     //        vspDetails.setLastModificationTime(new Date());
366
367     Version version = versioningManager
368         .create(VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE, vspDetails.getId(), user);
369     vspDetails.setVersion(version);
370
371     //        vspDetails.setLastModificationTime(new Date());
372
373     vendorSoftwareProductDao.createVendorSoftwareProductInfo(vspDetails);
374     vendorSoftwareProductDao.updateQuestionnaire(vspDetails.getId(), version,
375         new JsonSchemaDataGenerator(getVspQuestionnaireSchema(null)).generateData());
376     UniqueValueUtil
377         .createUniqueValue(VendorSoftwareProductConstants.UniqueValues.VENDOR_SOFTWARE_PRODUCT_NAME,
378             vspDetails.getName());
379
380     return vspDetails;
381   }
382
383   @Override
384   public List<VersionedVendorSoftwareProductInfo> getVspList(String versionFilter, String user) {
385     Map<String, VersionInfo> idToVersionsInfo = versioningManager
386         .listEntitiesVersionInfo(VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE, user,
387             VersionableEntityAction.Read);
388
389     List<VersionedVendorSoftwareProductInfo> vsps = new ArrayList<>();
390     for (Map.Entry<String, VersionInfo> entry : idToVersionsInfo.entrySet()) {
391       VersionInfo versionInfo = entry.getValue();
392       if (versionFilter != null && versionFilter.equals(VersionStatus.Final.name())) {
393         if (versionInfo.getLatestFinalVersion() == null) {
394           continue;
395         }
396         versionInfo.setActiveVersion(versionInfo.getLatestFinalVersion());
397         versionInfo.setStatus(VersionStatus.Final);
398         versionInfo.setLockingUser(null);
399       }
400
401       VspDetails vsp = vendorSoftwareProductDao.getVendorSoftwareProductInfo(
402           new VspDetails(entry.getKey(), entry.getValue().getActiveVersion()));
403       if (vsp != null) {
404         vsp.setValidationDataStructure(null);
405         vsps.add(new VersionedVendorSoftwareProductInfo(vsp, entry.getValue()));
406       }
407     }
408
409     sortVspListByModificationTimeDescOrder(vsps);
410     return vsps;
411   }
412
413   @Override
414   public void updateVsp(VspDetails vspDetails, String user) {
415     Version activeVersion =
416         getVersionInfo(vspDetails.getId(), VersionableEntityAction.Write, user).getActiveVersion();
417     vspDetails.setVersion(activeVersion);
418     //        vspDetails.setLastModificationTime(new Date());
419
420     VspDetails retrieved = vendorSoftwareProductDao.getVendorSoftwareProductInfo(vspDetails);
421     vspDetails.setValidationData(retrieved.getValidationData());
422     UniqueValueUtil
423         .updateUniqueValue(VendorSoftwareProductConstants.UniqueValues.VENDOR_SOFTWARE_PRODUCT_NAME,
424             retrieved.getName(), vspDetails.getName());
425     vendorSoftwareProductDao.updateVendorSoftwareProductInfo(vspDetails);
426
427     vendorSoftwareProductDao.updateVspLatestModificationTime(vspDetails.getId(), activeVersion);
428   }
429
430   @Override
431   public VersionedVendorSoftwareProductInfo getVspDetails(String vspId, Version version,
432                                                           String user) {
433     VersionInfo versionInfo = getVersionInfo(vspId, VersionableEntityAction.Read, user);
434     if (version == null) {
435       version = versionInfo.getActiveVersion();
436     } else {
437       if (!versionInfo.getViewableVersions().contains(version)) {
438         throw new CoreException(new RequestedVersionInvalidErrorBuilder().build());
439       }
440     }
441
442     VspDetails vendorSoftwareProductInfo =
443         vendorSoftwareProductDao.getVendorSoftwareProductInfo(new VspDetails(vspId, version));
444     if (vendorSoftwareProductInfo == null) {
445       throw new CoreException(new VendorSoftwareProductNotFoundErrorBuilder(vspId).build());
446     }
447     return new VersionedVendorSoftwareProductInfo(vendorSoftwareProductInfo, versionInfo);
448   }
449
450   @Override
451   public void deleteVsp(String vspId, String user) {
452     throw new UnsupportedOperationException("Unsupported operation for 1607 release.");
453   }
454
455   @Override
456   public UploadFileResponse uploadFile(String vspId, InputStream heatFileToUpload, String user) {
457     Version activeVersion =
458         getVersionInfo(vspId, VersionableEntityAction.Write, user).getActiveVersion();
459     UploadFileResponse uploadFileResponse = new UploadFileResponse();
460
461     if (heatFileToUpload == null) {
462       uploadFileResponse.addStructureError(AsdcCommon.UPLOAD_FILE,
463           new ErrorMessage(ErrorLevel.ERROR,
464               Messages.NO_ZIP_FILE_WAS_UPLOADED_OR_ZIP_NOT_EXIST.getErrorMessage()));
465       return uploadFileResponse;
466     }
467
468     InputStream uploadedFileData;
469     FileContentHandler fileContentMap;
470     Map<String, List<ErrorMessage>> errors = new HashMap<>();
471     try {
472       fileContentMap = getContent(heatFileToUpload, errors);
473       if (!errors.isEmpty()) {
474         return addStructureErrorsToResponse(uploadFileResponse, errors);
475       }
476
477       uploadedFileData = fileContentMap.getFileContent(UPLOAD_RAW_DATA);
478       fileContentMap.remove(UPLOAD_RAW_DATA);
479
480       ValidationManagerUtil.handleMissingManifest(fileContentMap, errors);
481       if (!errors.isEmpty()) {
482         return addStructureErrorsToResponse(uploadFileResponse, errors);
483       }
484
485     } catch (CoreException ce) {
486       ErrorMessage.ErrorMessageUtil.addMessage(AsdcCommon.UPLOAD_FILE, errors)
487           .add(new ErrorMessage(ErrorLevel.ERROR, ce.getMessage()));
488       return addStructureErrorsToResponse(uploadFileResponse, errors);
489     }
490
491     HeatStructureTree tree = createAndValidateHeatTree(uploadFileResponse, fileContentMap);
492
493     deleteUploadDataAndContent(vspId, activeVersion);
494     saveHotData(vspId, activeVersion, uploadedFileData, fileContentMap, tree);
495
496     vendorSoftwareProductDao.updateVspLatestModificationTime(vspId, activeVersion);
497
498     ToscaServiceModel toscaServiceModel =
499         VendorSoftwareProductUtils.loadAndTranslateTemplateData(fileContentMap)
500             .getToscaServiceModel();
501     if (toscaServiceModel != null) {
502       serviceModelDao.storeServiceModel(vspId, activeVersion, toscaServiceModel);
503       saveCompositionData(vspId, activeVersion,
504           CompositionDataExtractor.extractServiceCompositionData(toscaServiceModel));
505     }
506
507     return uploadFileResponse;
508   }
509
510   private UploadFileResponse addStructureErrorsToResponse(UploadFileResponse uploadFileResponse,
511                                                           Map<String, List<ErrorMessage>> errors) {
512     uploadFileResponse.addStructureErrors(errors);
513     return uploadFileResponse;
514   }
515
516   private HeatStructureTree createAndValidateHeatTree(UploadFileResponse uploadFileResponse,
517                                                       FileContentHandler fileContentMap) {
518     VendorSoftwareProductUtils.addFileNamesToUploadFileResponse(fileContentMap, uploadFileResponse);
519     Map<String, List<ErrorMessage>> validationErrors =
520         ValidationManagerUtil.initValidationManager(fileContentMap).validate();
521     uploadFileResponse.getErrors().putAll(validationErrors);
522
523     HeatTreeManager heatTreeManager = HeatTreeManagerUtil.initHeatTreeManager(fileContentMap);
524     heatTreeManager.createTree();
525     heatTreeManager.addErrors(validationErrors);
526     return heatTreeManager.getTree();
527   }
528
529   private void saveHotData(String vspId, Version activeVersion, InputStream uploadedFileData,
530                            FileContentHandler fileContentMap, HeatStructureTree tree) {
531     Map<String, Object> manifestAsMap = (Map<String, Object>) JsonUtil
532         .json2Object(fileContentMap.getFileContent(AsdcCommon.MANIFEST_NAME), Map.class);
533
534     UploadDataEntity uploadData = new UploadDataEntity(vspId, activeVersion);
535     uploadData.setPackageName((String) manifestAsMap.get("name"));
536     uploadData.setPackageVersion((String) manifestAsMap.get("version"));
537     uploadData.setContentData(ByteBuffer.wrap(FileUtils.toByteArray(uploadedFileData)));
538     uploadData.setValidationDataStructure(new ValidationStructureList(tree));
539     vendorSoftwareProductDao.updateUploadData(uploadData);
540   }
541
542   private FileContentHandler getContent(InputStream heatFileToUpload,
543                                         Map<String, List<ErrorMessage>> errors) {
544     FileContentHandler contentMap = null;
545     byte[] uploadedFileData;
546     try {
547       uploadedFileData = FileUtils.toByteArray(heatFileToUpload);
548       VendorSoftwareProductUtils.validateRawZipData(uploadedFileData, errors);
549       contentMap = VendorSoftwareProductUtils.loadUploadFileContent(uploadedFileData);
550       VendorSoftwareProductUtils.validateContentZipData(contentMap, errors);
551       contentMap.addFile(UPLOAD_RAW_DATA, uploadedFileData);
552     } catch (IOException e0) {
553       ErrorMessage.ErrorMessageUtil.addMessage(AsdcCommon.UPLOAD_FILE, errors)
554           .add(new ErrorMessage(ErrorLevel.ERROR, Messages.INVALID_ZIP_FILE.getErrorMessage()));
555     }
556     return contentMap;
557   }
558
559   private void validateMibZipContent(String vspId, Version version, byte[] uploadedFileData,
560                                      Map<String, List<ErrorMessage>> errors) {
561     FileContentHandler contentMap;
562     try {
563       contentMap = VendorSoftwareProductUtils.loadUploadFileContent(uploadedFileData);
564       VendorSoftwareProductUtils.validateContentZipData(contentMap, errors);
565     } catch (IOException e0) {
566       throw new CoreException(
567           new MibUploadErrorBuilder(vspId, version, Messages.INVALID_ZIP_FILE.getErrorMessage())
568               .build());
569     }
570   }
571
572   @Override
573   public List<PackageInfo> listPackages(String category, String subCategory) {
574     return vendorSoftwareProductDao.listPackages(category, subCategory);
575   }
576
577   @Override
578   public File getTranslatedFile(String vspId, Version version, String user) {
579     VersionInfo versionInfo = getVersionInfo(vspId, VersionableEntityAction.Read, user);
580     if (version == null) {
581       if (versionInfo.getLatestFinalVersion() == null) {
582         throw new CoreException(new PackageNotFoundErrorBuilder(vspId).build());
583       }
584       version = versionInfo.getLatestFinalVersion();
585     } else {
586       if (!version.isFinal() || !versionInfo.getViewableVersions().contains(version)) {
587         throw new CoreException(new RequestedVersionInvalidErrorBuilder().build());
588       }
589     }
590
591     PackageInfo packageInfo =
592         vendorSoftwareProductDao.getPackageInfo(new PackageInfo(vspId, version));
593     if (packageInfo == null) {
594       throw new CoreException(new PackageNotFoundErrorBuilder(vspId, version).build());
595     }
596
597     ByteBuffer translatedFileBuffer = packageInfo.getTranslatedFile();
598     if (translatedFileBuffer == null) {
599       throw new CoreException(new PackageInvalidErrorBuilder(vspId, version).build());
600     }
601
602     File translatedFile = new File(VSP_PACKAGE_ZIP);
603
604     try {
605       FileOutputStream fos = new FileOutputStream(translatedFile);
606       fos.write(translatedFileBuffer.array());
607       fos.close();
608     } catch (IOException e0) {
609       throw new CoreException(new TranslationFileCreationErrorBuilder(vspId, version).build(), e0);
610     }
611
612     return translatedFile;
613   }
614
615   @Override
616   public File getLatestHeatPackage(String vspId,
617                                    String user) { //todo remove the writing to file system..
618     VersionInfo versionInfo = getVersionInfo(vspId, VersionableEntityAction.Read, user);
619     Version version = versionInfo.getActiveVersion();
620
621     UploadDataEntity uploadData =
622         vendorSoftwareProductDao.getUploadData(new UploadDataEntity(vspId, version));
623
624     ByteBuffer contentData = uploadData.getContentData();
625     if (contentData == null) {
626       return null;
627     }
628
629     File heatPkgFile = new File(String.format("heats-for-%s.zip", vspId));
630
631     try {
632       FileOutputStream fos = new FileOutputStream(heatPkgFile);
633       fos.write(contentData.array());
634       fos.close();
635     } catch (IOException e0) {
636       throw new CoreException(new FileCreationErrorBuilder(vspId).build(), e0);
637     }
638     return heatPkgFile;
639   }
640
641   @Override
642   public PackageInfo createPackage(String vspId, String user) throws IOException {
643     VersionInfo versionInfo = getVersionInfo(vspId, VersionableEntityAction.Read, user);
644     Version activeVersion = versionInfo.getActiveVersion();
645     if (!activeVersion.isFinal()) {
646       throw new CoreException(
647           new CreatePackageForNonFinalVendorSoftwareProductErrorBuilder(vspId, activeVersion)
648               .build());
649     }
650
651     ToscaServiceModel toscaServiceModel =
652         enrichedServiceModelDao.getServiceModel(vspId, activeVersion);
653     VspDetails vspDetails =
654         vendorSoftwareProductDao.getVendorSoftwareProductInfo(new VspDetails(vspId, activeVersion));
655     Version vlmVersion = vspDetails.getVlmVersion();
656
657     PackageInfo packageInfo = createPackageInfo(vspId, vspDetails);
658
659     ToscaFileOutputServiceCsarImpl toscaServiceTemplateServiceCsar =
660         new ToscaFileOutputServiceCsarImpl();
661     FileContentHandler licenseArtifacts = licenseArtifactsService
662         .createLicenseArtifacts(vspDetails.getId(), vspDetails.getVendorId(), vlmVersion,
663             vspDetails.getFeatureGroups(), user);
664     //todo add tosca validation here
665     packageInfo.setTranslatedFile(ByteBuffer.wrap(
666         toscaServiceTemplateServiceCsar.createOutputFile(toscaServiceModel, licenseArtifacts)));
667
668     vendorSoftwareProductDao.insertPackageDetails(packageInfo);
669
670     vendorSoftwareProductDao.updateVspLatestModificationTime(vspId, vspDetails.getVersion());
671
672     return packageInfo;
673   }
674
675   private PackageInfo createPackageInfo(String vspId, VspDetails vspDetails) {
676     PackageInfo packageInfo = new PackageInfo();
677     packageInfo.setVspId(vspId);
678     packageInfo.setVersion(vspDetails.getVersion());
679     packageInfo.setDisplayName(vspDetails.getPackageName());
680     packageInfo.setVspName(vspDetails.getName());
681     packageInfo.setVspDescription(vspDetails.getDescription());
682     packageInfo.setCategory(vspDetails.getCategory());
683     packageInfo.setSubCategory(vspDetails.getSubCategory());
684     packageInfo.setVendorName(vspDetails.getVendorName());
685     packageInfo.setPackageType(CSAR);
686     packageInfo.setVendorRelease("1.0"); //todo TBD
687     return packageInfo;
688   }
689
690   @Override
691   public QuestionnaireResponse getVspQuestionnaire(String vspId, Version version, String user) {
692     version = VersioningUtil
693         .resolveVersion(version, getVersionInfo(vspId, VersionableEntityAction.Read, user));
694
695     QuestionnaireResponse questionnaireResponse = new QuestionnaireResponse();
696     questionnaireResponse.setData(getVspQuestionnaire(vspId, version).getQuestionnaireData());
697     questionnaireResponse.setSchema(getVspQuestionnaireSchema(null));
698
699     return questionnaireResponse;
700   }
701
702   private VspQuestionnaireEntity getVspQuestionnaire(String vspId, Version version) {
703     VspQuestionnaireEntity retrieved = vendorSoftwareProductDao.getQuestionnaire(vspId, version);
704     VersioningUtil.validateEntityExistence(retrieved, new VspQuestionnaireEntity(vspId, version),
705         VspDetails.ENTITY_TYPE);
706     return retrieved;
707   }
708
709   @Override
710   public void updateVspQuestionnaire(String vspId, String questionnaireData, String user) {
711     Version activeVersion =
712         getVersionInfo(vspId, VersionableEntityAction.Write, user).getActiveVersion();
713
714     vendorSoftwareProductDao.updateVspLatestModificationTime(vspId, activeVersion);
715
716     vendorSoftwareProductDao.updateQuestionnaire(vspId, activeVersion, questionnaireData);
717   }
718
719   @Override
720   public Collection<NetworkEntity> listNetworks(String vspId, Version version, String user) {
721     version = VersioningUtil
722         .resolveVersion(version, getVersionInfo(vspId, VersionableEntityAction.Read, user));
723     return listNetworks(vspId, version);
724   }
725
726   private Collection<NetworkEntity> listNetworks(String vspId, Version version) {
727     return vendorSoftwareProductDao.listNetworks(vspId, version);
728   }
729
730   @Override
731   public NetworkEntity createNetwork(NetworkEntity network, String user) {
732     Version activeVersion =
733         getVersionInfo(network.getVspId(), VersionableEntityAction.Write, user).getActiveVersion();
734     network.setVersion(activeVersion);
735     if (!isManual(network.getVspId(), activeVersion)) {
736       throw new CoreException(
737           new ErrorCode.ErrorCodeBuilder().withCategory(ErrorCategory.APPLICATION)
738               .withId(VendorSoftwareProductErrorCodes.VSP_COMPOSITION_EDIT_NOT_ALLOWED)
739               .withMessage(VSP_COMPOSITION_EDIT_NOT_ALLOWED_MSG).build());
740     }
741
742     vendorSoftwareProductDao.updateVspLatestModificationTime(network.getVspId(), activeVersion);
743     return null;
744   }
745
746   private NetworkEntity createNetwork(NetworkEntity network) {
747     network.setId(CommonMethods.nextUuId());
748     vendorSoftwareProductDao.createNetwork(network);
749
750     return network;
751   }
752
753   @Override
754   public CompositionEntityValidationData updateNetwork(NetworkEntity network, String user) {
755     Version activeVersion =
756         getVersionInfo(network.getVspId(), VersionableEntityAction.Write, user).getActiveVersion();
757     network.setVersion(activeVersion);
758     NetworkEntity retrieved = getNetwork(network.getVspId(), activeVersion, network.getId());
759
760     NetworkCompositionSchemaInput schemaInput = new NetworkCompositionSchemaInput();
761     schemaInput.setManual(isManual(network.getVspId(), activeVersion));
762     schemaInput.setNetwork(retrieved.getNetworkCompositionData());
763
764     CompositionEntityValidationData validationData = CompositionEntityDataManager
765         .validateEntity(network, SchemaTemplateContext.composition, schemaInput);
766     if (CollectionUtils.isEmpty(validationData.getErrors())) {
767       vendorSoftwareProductDao.updateNetwork(network);
768     }
769
770     vendorSoftwareProductDao.updateVspLatestModificationTime(network.getVspId(), activeVersion);
771
772     return validationData;
773   }
774
775   @Override
776   public CompositionEntityResponse<Network> getNetwork(String vspId, Version version,
777                                                        String networkId, String user) {
778     version = VersioningUtil
779         .resolveVersion(version, getVersionInfo(vspId, VersionableEntityAction.Read, user));
780     NetworkEntity networkEntity = getNetwork(vspId, version, networkId);
781     Network network = networkEntity.getNetworkCompositionData();
782
783     NetworkCompositionSchemaInput schemaInput = new NetworkCompositionSchemaInput();
784     schemaInput.setManual(isManual(vspId, version));
785     schemaInput.setNetwork(network);
786
787     CompositionEntityResponse<Network> response = new CompositionEntityResponse<>();
788     response.setId(networkId);
789     response.setData(network);
790     response.setSchema(SchemaGenerator
791         .generate(SchemaTemplateContext.composition, CompositionEntityType.network, schemaInput));
792
793     return response;
794   }
795
796   private NetworkEntity getNetwork(String vspId, Version version, String networkId) {
797     NetworkEntity retrieved = vendorSoftwareProductDao.getNetwork(vspId, version, networkId);
798     VersioningUtil.validateEntityExistence(retrieved, new NetworkEntity(vspId, version, networkId),
799         VspDetails.ENTITY_TYPE);
800     return retrieved;
801   }
802
803   @Override
804   public void deleteNetwork(String vspId, String networkId, String user) {
805     Version activeVersion =
806         getVersionInfo(vspId, VersionableEntityAction.Write, user).getActiveVersion();
807     if (!isManual(vspId, activeVersion)) {
808       throw new CoreException(
809           new ErrorCode.ErrorCodeBuilder().withCategory(ErrorCategory.APPLICATION)
810               .withId(VendorSoftwareProductErrorCodes.VSP_COMPOSITION_EDIT_NOT_ALLOWED)
811               .withMessage(VSP_COMPOSITION_EDIT_NOT_ALLOWED_MSG).build());
812     }
813
814     vendorSoftwareProductDao.updateVspLatestModificationTime(vspId, activeVersion);
815   }
816
817   @Override
818   public QuestionnaireResponse getComponentQuestionnaire(String vspId, Version version,
819                                                          String componentId, String user) {
820     version = VersioningUtil
821         .resolveVersion(version, getVersionInfo(vspId, VersionableEntityAction.Read, user));
822
823     QuestionnaireResponse questionnaireResponse = new QuestionnaireResponse();
824     questionnaireResponse.setData(getComponent(vspId, version, componentId).getQuestionnaireData());
825     List<String> nicNames = listNics(vspId, version, componentId).stream()
826         .map(nic -> nic.getNicCompositionData().getName()).collect(Collectors.toList());
827     questionnaireResponse.setSchema(getComponentQuestionnaireSchema(
828         new ComponentQuestionnaireSchemaInput(nicNames,
829             JsonUtil.json2Object(questionnaireResponse.getData(), Map.class))));
830
831     return questionnaireResponse;
832   }
833
834   @Override
835   public void updateComponentQuestionnaire(String vspId, String componentId,
836                                            String questionnaireData, String user) {
837     Version activeVersion =
838         getVersionInfo(vspId, VersionableEntityAction.Write, user).getActiveVersion();
839     getComponent(vspId, activeVersion, componentId);
840
841     vendorSoftwareProductDao
842         .updateComponentQuestionnaire(vspId, activeVersion, componentId, questionnaireData);
843
844     vendorSoftwareProductDao.updateVspLatestModificationTime(vspId, activeVersion);
845   }
846
847   @Override
848   public Collection<ComponentEntity> listComponents(String vspId, Version version, String user) {
849     version = VersioningUtil
850         .resolveVersion(version, getVersionInfo(vspId, VersionableEntityAction.Read, user));
851     return listComponents(vspId, version);
852   }
853
854   private Collection<ComponentEntity> listComponents(String vspId, Version version) {
855     return vendorSoftwareProductDao.listComponents(vspId, version);
856   }
857
858   @Override
859   public void deleteComponents(String vspId, String user) {
860     Version activeVersion =
861         getVersionInfo(vspId, VersionableEntityAction.Write, user).getActiveVersion();
862     if (!isManual(vspId, activeVersion)) {
863       throw new CoreException(
864           new ErrorCode.ErrorCodeBuilder().withCategory(ErrorCategory.APPLICATION)
865               .withId(VendorSoftwareProductErrorCodes.VSP_COMPOSITION_EDIT_NOT_ALLOWED)
866               .withMessage(VSP_COMPOSITION_EDIT_NOT_ALLOWED_MSG).build());
867     }
868
869     vendorSoftwareProductDao.updateVspLatestModificationTime(vspId, activeVersion);
870   }
871
872   @Override
873   public ComponentEntity createComponent(ComponentEntity component, String user) {
874     Version activeVersion =
875         getVersionInfo(component.getVspId(), VersionableEntityAction.Write, user)
876             .getActiveVersion();
877     component.setVersion(activeVersion);
878
879     if (!isManual(component.getVspId(), activeVersion)) {
880       throw new CoreException(
881           new ErrorCode.ErrorCodeBuilder().withCategory(ErrorCategory.APPLICATION)
882               .withId(VendorSoftwareProductErrorCodes.VSP_COMPOSITION_EDIT_NOT_ALLOWED)
883               .withMessage(VSP_COMPOSITION_EDIT_NOT_ALLOWED_MSG).build());
884
885     }
886
887     /*        ComponentCompositionSchemaInput schemaInput = new ComponentCompositionSchemaInput();
888         schemaInput.setManual(true);
889         CompositionEntityValidationData validationData = CompositionEntityDataManager
890         .validateEntity(component, SchemaTemplateContext.composition, schemaInput);
891         if (CollectionUtils.isEmpty(validationData.getErrors())) {
892             return createComponent(component);
893         }
894         return validationData;*/
895
896     vendorSoftwareProductDao.updateVspLatestModificationTime(component.getVspId(), activeVersion);
897
898     return null;
899   }
900
901   private ComponentEntity createComponent(ComponentEntity component) {
902     component.setId(CommonMethods.nextUuId());
903     component.setQuestionnaireData(
904         new JsonSchemaDataGenerator(getComponentQuestionnaireSchema(null)).generateData());
905
906     vendorSoftwareProductDao.createComponent(component);
907
908     return component;
909   }
910
911   @Override
912   public CompositionEntityResponse<ComponentData> getComponent(String vspId, Version version,
913                                                                String componentId, String user) {
914     version = VersioningUtil
915         .resolveVersion(version, getVersionInfo(vspId, VersionableEntityAction.Read, user));
916     ComponentEntity componentEntity = getComponent(vspId, version, componentId);
917     ComponentData component = componentEntity.getComponentCompositionData();
918
919     ComponentCompositionSchemaInput schemaInput = new ComponentCompositionSchemaInput();
920     schemaInput.setManual(isManual(vspId, version));
921     schemaInput.setComponent(component);
922
923     CompositionEntityResponse<ComponentData> response = new CompositionEntityResponse<>();
924     response.setId(componentId);
925     response.setData(component);
926     response.setSchema(SchemaGenerator
927         .generate(SchemaTemplateContext.composition, CompositionEntityType.component, schemaInput));
928
929     return response;
930   }
931
932   private ComponentEntity getComponent(String vspId, Version version, String componentId) {
933     ComponentEntity retrieved = vendorSoftwareProductDao.getComponent(vspId, version, componentId);
934     VersioningUtil
935         .validateEntityExistence(retrieved, new ComponentEntity(vspId, version, componentId),
936             VspDetails.ENTITY_TYPE);
937     return retrieved;
938   }
939
940   @Override
941   public CompositionEntityValidationData updateComponent(ComponentEntity component, String user) {
942     Version activeVersion =
943         getVersionInfo(component.getVspId(), VersionableEntityAction.Write, user)
944             .getActiveVersion();
945     component.setVersion(activeVersion);
946     ComponentEntity retrieved =
947         getComponent(component.getVspId(), activeVersion, component.getId());
948
949     ComponentCompositionSchemaInput schemaInput = new ComponentCompositionSchemaInput();
950     schemaInput.setManual(isManual(component.getVspId(), activeVersion));
951     schemaInput.setComponent(retrieved.getComponentCompositionData());
952
953     CompositionEntityValidationData validationData = CompositionEntityDataManager
954         .validateEntity(component, SchemaTemplateContext.composition, schemaInput);
955     if (CollectionUtils.isEmpty(validationData.getErrors())) {
956       vendorSoftwareProductDao.updateComponent(component);
957     }
958
959     vendorSoftwareProductDao.updateVspLatestModificationTime(component.getVspId(), activeVersion);
960
961     return validationData;
962   }
963
964   @Override
965   public void deleteComponent(String vspId, String componentId, String user) {
966     Version activeVersion =
967         getVersionInfo(vspId, VersionableEntityAction.Write, user).getActiveVersion();
968     if (!isManual(vspId, activeVersion)) {
969       throw new CoreException(
970           new ErrorCode.ErrorCodeBuilder().withCategory(ErrorCategory.APPLICATION)
971               .withId(VendorSoftwareProductErrorCodes.VSP_COMPOSITION_EDIT_NOT_ALLOWED)
972               .withMessage(VSP_COMPOSITION_EDIT_NOT_ALLOWED_MSG).build());
973     }
974
975     vendorSoftwareProductDao.updateVspLatestModificationTime(vspId, activeVersion);
976   }
977
978   @Override
979   public Collection<org.openecomp.sdc.vendorsoftwareproduct.dao.type.ProcessEntity> listProcesses(
980       String vspId, Version version, String componentId,
981       String user) {
982     version = VersioningUtil
983         .resolveVersion(version, getVersionInfo(vspId, VersionableEntityAction.Read, user));
984     if (!GENERAL_COMPONENT_ID.equals(componentId)) {
985       getComponent(vspId, version, componentId);
986     }
987     return vendorSoftwareProductDao.listProcesses(vspId, version, componentId);
988   }
989
990   @Override
991   public void deleteProcesses(String vspId, String componentId, String user) {
992     Version activeVersion =
993         getVersionInfo(vspId, VersionableEntityAction.Write, user).getActiveVersion();
994     if (!GENERAL_COMPONENT_ID.equals(componentId)) {
995       getComponent(vspId, activeVersion, componentId);
996     }
997
998     Collection<org.openecomp.sdc.vendorsoftwareproduct.dao.type.ProcessEntity> processes =
999         vendorSoftwareProductDao.listProcesses(vspId, activeVersion, componentId);
1000     for (org.openecomp.sdc.vendorsoftwareproduct.dao.type.ProcessEntity process : processes) {
1001       UniqueValueUtil.deleteUniqueValue(VendorSoftwareProductConstants.UniqueValues.PROCESS_NAME,
1002           process.getVspId(), process.getVersion().toString(), process.getComponentId(),
1003           process.getName());
1004     }
1005
1006     vendorSoftwareProductDao.deleteProcesses(vspId, activeVersion, componentId);
1007
1008     vendorSoftwareProductDao.updateVspLatestModificationTime(vspId, activeVersion);
1009   }
1010
1011   @Override
1012   public org.openecomp.sdc.vendorsoftwareproduct.dao.type.ProcessEntity createProcess(
1013       org.openecomp.sdc.vendorsoftwareproduct.dao.type.ProcessEntity process, String user) {
1014     Version activeVersion =
1015         getVersionInfo(process.getVspId(), VersionableEntityAction.Write, user).getActiveVersion();
1016     process.setVersion(activeVersion);
1017     UniqueValueUtil.validateUniqueValue(VendorSoftwareProductConstants.UniqueValues.PROCESS_NAME,
1018         process.getVspId(), process.getVersion().toString(), process.getComponentId(),
1019         process.getName());
1020     process.setId(CommonMethods.nextUuId());
1021     if (!GENERAL_COMPONENT_ID.equals(process.getComponentId())) {
1022       getComponent(process.getVspId(), activeVersion, process.getComponentId());
1023     }
1024
1025     vendorSoftwareProductDao.createProcess(process);
1026     UniqueValueUtil.createUniqueValue(VendorSoftwareProductConstants.UniqueValues.PROCESS_NAME,
1027         process.getVspId(), process.getVersion().toString(), process.getComponentId(),
1028         process.getName());
1029
1030     vendorSoftwareProductDao.updateVspLatestModificationTime(process.getVspId(), activeVersion);
1031     return process;
1032   }
1033
1034   @Override
1035   public org.openecomp.sdc.vendorsoftwareproduct.dao.type.ProcessEntity getProcess(String vspId,
1036                                                                                  Version version,
1037                                                                                  String componentId,
1038                                                                                  String processId,
1039                                                                                  String user) {
1040     version = VersioningUtil
1041         .resolveVersion(version, getVersionInfo(vspId, VersionableEntityAction.Read, user));
1042     org.openecomp.sdc.vendorsoftwareproduct.dao.type.ProcessEntity retrieved =
1043         vendorSoftwareProductDao.getProcess(vspId, version, componentId, processId);
1044     validateProcessExistence(vspId, version, componentId, processId, retrieved);
1045     return retrieved;
1046   }
1047
1048   @Override
1049   public void updateProcess(org.openecomp.sdc.vendorsoftwareproduct.dao.type.ProcessEntity process,
1050                             String user) {
1051     Version activeVersion =
1052         getVersionInfo(process.getVspId(), VersionableEntityAction.Write, user).getActiveVersion();
1053     process.setVersion(activeVersion);
1054
1055     org.openecomp.sdc.vendorsoftwareproduct.dao.type.ProcessEntity retrieved =
1056         vendorSoftwareProductDao
1057             .getProcess(process.getVspId(), activeVersion, process.getComponentId(),
1058                 process.getId());
1059     validateProcessExistence(process.getVspId(), activeVersion, process.getComponentId(),
1060         process.getId(), retrieved);
1061
1062     UniqueValueUtil.updateUniqueValue(VendorSoftwareProductConstants.UniqueValues.PROCESS_NAME,
1063         retrieved.getName(), process.getName(), process.getVspId(), process.getVersion().toString(),
1064         process.getComponentId());
1065     vendorSoftwareProductDao.updateProcess(process);
1066
1067     vendorSoftwareProductDao.updateVspLatestModificationTime(process.getVspId(), activeVersion);
1068   }
1069
1070   @Override
1071   public void deleteProcess(String vspId, String componentId, String processId, String user) {
1072     Version activeVersion =
1073         getVersionInfo(vspId, VersionableEntityAction.Write, user).getActiveVersion();
1074     org.openecomp.sdc.vendorsoftwareproduct.dao.type.ProcessEntity retrieved =
1075         vendorSoftwareProductDao.getProcess(vspId, activeVersion, componentId, processId);
1076     validateProcessExistence(vspId, activeVersion, componentId, processId, retrieved);
1077
1078     vendorSoftwareProductDao.deleteProcess(vspId, activeVersion, componentId, processId);
1079     UniqueValueUtil.deleteUniqueValue(VendorSoftwareProductConstants.UniqueValues.PROCESS_NAME,
1080         retrieved.getVspId(), retrieved.getVersion().toString(), retrieved.getComponentId(),
1081         retrieved.getName());
1082
1083     vendorSoftwareProductDao.updateVspLatestModificationTime(vspId, activeVersion);
1084   }
1085
1086   @Override
1087   public File getProcessArtifact(String vspId, Version version, String componentId,
1088                                  String processId, String user) {
1089     version = VersioningUtil
1090         .resolveVersion(version, getVersionInfo(vspId, VersionableEntityAction.Read, user));
1091     ProcessArtifactEntity retrieved =
1092         vendorSoftwareProductDao.getProcessArtifact(vspId, version, componentId, processId);
1093     validateProcessArtifactExistence(vspId, version, componentId, processId, retrieved);
1094
1095     File file = new File(String
1096         .format("%s_%s_%s_%s", vspId, version.toString().replace('.', '_'), componentId,
1097             processId));
1098     try {
1099       FileOutputStream fos = new FileOutputStream(file);
1100       fos.write(retrieved.getArtifact().array());
1101       fos.close();
1102     } catch (IOException e0) {
1103       throw new CoreException(new UploadInvalidErrorBuilder().build());
1104     }
1105
1106     return file;
1107   }
1108
1109   @Override
1110   public void deleteProcessArtifact(String vspId, String componentId, String processId,
1111                                     String user) {
1112     Version activeVersion =
1113         getVersionInfo(vspId, VersionableEntityAction.Write, user).getActiveVersion();
1114     ProcessArtifactEntity retrieved =
1115         vendorSoftwareProductDao.getProcessArtifact(vspId, activeVersion, componentId, processId);
1116     validateProcessArtifactExistence(vspId, activeVersion, componentId, processId, retrieved);
1117
1118     vendorSoftwareProductDao.deleteProcessArtifact(vspId, activeVersion, componentId, processId);
1119
1120     vendorSoftwareProductDao.updateVspLatestModificationTime(vspId, activeVersion);
1121   }
1122
1123   @Override
1124   public void uploadProcessArtifact(InputStream artifactFile, String artifactFileName, String vspId,
1125                                     String componentId, String processId, String user) {
1126     Version activeVersion =
1127         getVersionInfo(vspId, VersionableEntityAction.Write, user).getActiveVersion();
1128     org.openecomp.sdc.vendorsoftwareproduct.dao.type.ProcessEntity retrieved =
1129         vendorSoftwareProductDao.getProcess(vspId, activeVersion, componentId, processId);
1130     validateProcessExistence(vspId, activeVersion, componentId, processId, retrieved);
1131
1132     if (artifactFile == null) {
1133       throw new CoreException(new UploadInvalidErrorBuilder().build());
1134     }
1135
1136     byte[] artifact;
1137     try {
1138       artifact = FileUtils.toByteArray(artifactFile);
1139     } catch (RuntimeException e0) {
1140       throw new CoreException(new UploadInvalidErrorBuilder().build());
1141     }
1142
1143     vendorSoftwareProductDao
1144         .uploadProcessArtifact(vspId, activeVersion, componentId, processId, artifact,
1145             artifactFileName);
1146
1147     vendorSoftwareProductDao.updateVspLatestModificationTime(vspId, activeVersion);
1148   }
1149
1150   @Override
1151   public Collection<org.openecomp.sdc.vendorsoftwareproduct.dao.type.NicEntity> listNics(
1152       String vspId, Version version, String componentId,
1153       String user) {
1154     version = VersioningUtil
1155         .resolveVersion(version, getVersionInfo(vspId, VersionableEntityAction.Read, user));
1156     Collection<org.openecomp.sdc.vendorsoftwareproduct.dao.type.NicEntity> nics =
1157         listNics(vspId, version, componentId);
1158
1159     Map<String, String> networksNameById = listNetworksNameById(vspId, version);
1160     nics.stream().forEach(nicEntity -> {
1161       Nic nic = nicEntity.getNicCompositionData();
1162       nic.setNetworkName(networksNameById.get(nic.getNetworkId()));
1163       nicEntity.setNicCompositionData(nic);
1164     });
1165     return nics;
1166   }
1167
1168   private Collection<org.openecomp.sdc.vendorsoftwareproduct.dao.type.NicEntity> listNics(
1169       String vspId, Version version, String componentId) {
1170     getComponent(vspId, version, componentId);
1171
1172     return vendorSoftwareProductDao.listNics(vspId, version, componentId);
1173   }
1174
1175   private Map<String, String> listNetworksNameById(String vspId, Version version) {
1176     Collection<NetworkEntity> networks = listNetworks(vspId, version);
1177     return networks.stream().collect(Collectors.toMap(NetworkEntity::getId,
1178         networkEntity -> networkEntity.getNetworkCompositionData().getName()));
1179   }
1180
1181   @Override
1182   public org.openecomp.sdc.vendorsoftwareproduct.dao.type.NicEntity createNic(
1183       org.openecomp.sdc.vendorsoftwareproduct.dao.type.NicEntity nic, String user) {
1184     Version activeVersion =
1185         getVersionInfo(nic.getVspId(), VersionableEntityAction.Write, user).getActiveVersion();
1186     nic.setVersion(activeVersion);
1187     if (!isManual(nic.getVspId(), activeVersion)) {
1188       throw new CoreException(
1189           new ErrorCode.ErrorCodeBuilder().withCategory(ErrorCategory.APPLICATION)
1190               .withId(VendorSoftwareProductErrorCodes.VSP_COMPOSITION_EDIT_NOT_ALLOWED)
1191               .withMessage(VSP_COMPOSITION_EDIT_NOT_ALLOWED_MSG).build());
1192     }
1193
1194     vendorSoftwareProductDao.updateVspLatestModificationTime(nic.getVspId(), activeVersion);
1195
1196     return null;
1197   }
1198
1199   private org.openecomp.sdc.vendorsoftwareproduct.dao.type.NicEntity createNic(
1200       org.openecomp.sdc.vendorsoftwareproduct.dao.type.NicEntity nic) {
1201     nic.setId(CommonMethods.nextUuId());
1202     nic.setQuestionnaireData(
1203         new JsonSchemaDataGenerator(getNicQuestionnaireSchema(null)).generateData());
1204
1205     vendorSoftwareProductDao.createNic(nic);
1206
1207     return nic;
1208   }
1209
1210   @Override
1211   public CompositionEntityResponse<Nic> getNic(String vspId, Version version, String componentId,
1212                                                String nicId, String user) {
1213     version = VersioningUtil
1214         .resolveVersion(version, getVersionInfo(vspId, VersionableEntityAction.Read, user));
1215     org.openecomp.sdc.vendorsoftwareproduct.dao.type.NicEntity
1216         nicEntity = getNic(vspId, version, componentId, nicId);
1217     Nic nic = nicEntity.getNicCompositionData();
1218
1219     NicCompositionSchemaInput schemaInput = new NicCompositionSchemaInput();
1220     schemaInput.setManual(isManual(vspId, version));
1221     schemaInput.setNic(nic);
1222     Map<String, String> networksNameById = listNetworksNameById(vspId, version);
1223     nic.setNetworkName(networksNameById.get(nic.getNetworkId()));
1224     schemaInput.setNetworkIds(networksNameById.keySet());
1225
1226     CompositionEntityResponse<Nic> response = new CompositionEntityResponse<>();
1227     response.setId(nicId);
1228     response.setData(nic);
1229     response.setSchema(SchemaGenerator
1230         .generate(SchemaTemplateContext.composition, CompositionEntityType.nic, schemaInput));
1231
1232     return response;
1233   }
1234
1235   private org.openecomp.sdc.vendorsoftwareproduct.dao.type.NicEntity getNic(String vspId,
1236                                                                             Version version,
1237                                                                             String componentId,
1238                                                                             String nicId) {
1239     getComponent(vspId, version, componentId);
1240     org.openecomp.sdc.vendorsoftwareproduct.dao.type.NicEntity
1241         retrieved = vendorSoftwareProductDao.getNic(vspId, version, componentId, nicId);
1242     VersioningUtil
1243         .validateEntityExistence(retrieved,
1244             new org.openecomp.sdc.vendorsoftwareproduct.dao.type.NicEntity(vspId, version,
1245                 componentId, nicId),
1246             VspDetails.ENTITY_TYPE);
1247     return retrieved;
1248   }
1249
1250   @Override
1251   public void deleteNic(String vspId, String componentId, String nicId, String user) {
1252     Version activeVersion =
1253         getVersionInfo(vspId, VersionableEntityAction.Write, user).getActiveVersion();
1254     if (!isManual(vspId, activeVersion)) {
1255       throw new CoreException(
1256           new ErrorCode.ErrorCodeBuilder().withCategory(ErrorCategory.APPLICATION)
1257               .withId(VendorSoftwareProductErrorCodes.VSP_COMPOSITION_EDIT_NOT_ALLOWED)
1258               .withMessage(VSP_COMPOSITION_EDIT_NOT_ALLOWED_MSG).build());
1259     }
1260
1261     vendorSoftwareProductDao.updateVspLatestModificationTime(vspId, activeVersion);
1262   }
1263
1264   @Override
1265   public CompositionEntityValidationData updateNic(
1266       org.openecomp.sdc.vendorsoftwareproduct.dao.type.NicEntity nic, String user) {
1267     Version activeVersion =
1268         getVersionInfo(nic.getVspId(), VersionableEntityAction.Write, user).getActiveVersion();
1269     nic.setVersion(activeVersion);
1270     org.openecomp.sdc.vendorsoftwareproduct.dao.type.NicEntity
1271         retrieved = getNic(nic.getVspId(), activeVersion, nic.getComponentId(), nic.getId());
1272
1273     NicCompositionSchemaInput schemaInput = new NicCompositionSchemaInput();
1274     schemaInput.setManual(isManual(nic.getVspId(), activeVersion));
1275     schemaInput.setNic(retrieved.getNicCompositionData());
1276
1277     CompositionEntityValidationData validationData = CompositionEntityDataManager
1278         .validateEntity(nic, SchemaTemplateContext.composition, schemaInput);
1279     if (CollectionUtils.isEmpty(validationData.getErrors())) {
1280       vendorSoftwareProductDao.updateNic(nic);
1281     }
1282
1283     vendorSoftwareProductDao.updateVspLatestModificationTime(nic.getVspId(), activeVersion);
1284     return validationData;
1285   }
1286
1287   @Override
1288   public QuestionnaireResponse getNicQuestionnaire(String vspId, Version version,
1289                                                    String componentId, String nicId, String user) {
1290     version = VersioningUtil
1291         .resolveVersion(version, getVersionInfo(vspId, VersionableEntityAction.Read, user));
1292
1293     QuestionnaireResponse questionnaireResponse = new QuestionnaireResponse();
1294     questionnaireResponse
1295         .setData(getNic(vspId, version, componentId, nicId).getQuestionnaireData());
1296     questionnaireResponse.setSchema(getNicQuestionnaireSchema(null));
1297
1298     return questionnaireResponse;
1299   }
1300
1301   @Override
1302   public void updateNicQuestionnaire(String vspId, String componentId, String nicId,
1303                                      String questionnaireData, String user) {
1304     Version activeVersion =
1305         getVersionInfo(vspId, VersionableEntityAction.Write, user).getActiveVersion();
1306     getNic(vspId, activeVersion, componentId, nicId);
1307
1308     vendorSoftwareProductDao
1309         .updateNicQuestionnaire(vspId, activeVersion, componentId, nicId, questionnaireData);
1310
1311     vendorSoftwareProductDao.updateVspLatestModificationTime(vspId, activeVersion);
1312   }
1313
1314   @Override
1315   public void deleteComponentMib(String vspId, String componentId, boolean isTrap, String user) {
1316     Version activeVersion =
1317         getVersionInfo(vspId, VersionableEntityAction.Write, user).getActiveVersion();
1318     ComponentArtifactEntity componentArtifactEntity =
1319         setValuesForComponentArtifactEntityUpload(vspId, activeVersion, null, componentId, null,
1320             isTrap, null);
1321     ComponentArtifactEntity retrieved =
1322         componentArtifactDao.getArtifactByType(componentArtifactEntity);
1323
1324     componentArtifactDao.delete(retrieved);
1325
1326     vendorSoftwareProductDao.updateVspLatestModificationTime(vspId, activeVersion);
1327   }
1328
1329   @Override
1330   public void uploadComponentMib(InputStream object, String filename, String vspId,
1331                                  String componentId, boolean isTrap, String user) {
1332     Version activeVersion =
1333         getVersionInfo(vspId, VersionableEntityAction.Write, user).getActiveVersion();
1334     ComponentArtifactEntity componentArtifactEntity;
1335
1336
1337     if (object == null) {
1338       throw new CoreException(new MibUploadErrorBuilder(
1339           Messages.NO_ZIP_FILE_WAS_UPLOADED_OR_ZIP_NOT_EXIST.getErrorMessage()).build());
1340     } else {
1341       byte[] uploadedFileData;
1342       Map<String, List<ErrorMessage>> errors = new HashMap<>();
1343       try {
1344         uploadedFileData = FileUtils.toByteArray(object);
1345         validateMibZipContent(vspId, activeVersion, uploadedFileData, errors);
1346         if (MapUtils.isNotEmpty(errors)) {
1347           throw new CoreException(
1348               new MibUploadErrorBuilder(errors.values().iterator().next().get(0).getMessage())
1349                   .build());
1350         }
1351
1352         createArtifactInDatabase(vspId, activeVersion, filename, componentId, isTrap,
1353             uploadedFileData);
1354
1355       } catch (Exception e0) {
1356         throw new CoreException(new MibUploadErrorBuilder(e0.getMessage()).build());
1357       }
1358     }
1359
1360     vendorSoftwareProductDao.updateVspLatestModificationTime(vspId, activeVersion);
1361   }
1362
1363   private void createArtifactInDatabase(String vspId, Version activeVersion, String filename,
1364                                         String componentId, boolean isTrap,
1365                                         byte[] uploadedFileData) {
1366     ComponentArtifactEntity componentArtifactEntity;
1367
1368     String artifactId = CommonMethods.nextUuId();
1369     componentArtifactEntity =
1370         setValuesForComponentArtifactEntityUpload(vspId, activeVersion, filename, componentId,
1371             artifactId, isTrap, uploadedFileData);
1372     componentArtifactDao.update(componentArtifactEntity);
1373   }
1374
1375   @Override
1376   public MibUploadStatus listMibFilenames(String vspId, String componentId, String user) {
1377     Version activeVersion =
1378         getVersionInfo(vspId, VersionableEntityAction.Read, user).getActiveVersion();
1379     ComponentArtifactEntity current =
1380         new ComponentArtifactEntity(vspId, activeVersion, componentId, null);
1381
1382     return setMibUploadStatusValues(current);
1383
1384   }
1385
1386   private MibUploadStatus setMibUploadStatusValues(
1387       ComponentArtifactEntity componentArtifactEntity) {
1388     MibUploadStatus mibUploadStatus = new MibUploadStatus();
1389
1390     Collection<ComponentArtifactEntity> artifactNames =
1391         componentArtifactDao.getArtifactNamesAndTypesForComponent(componentArtifactEntity);
1392     Map<ComponentArtifactType, String> artifactTypeToFilename =
1393         VendorSoftwareProductUtils.filterNonTrapOrPollArtifacts(artifactNames);
1394
1395     if (MapUtils.isNotEmpty(artifactTypeToFilename)) {
1396       if (artifactTypeToFilename.containsKey(ComponentArtifactType.SNMP_TRAP)) {
1397         mibUploadStatus.setSnmpTrap(artifactTypeToFilename.get(ComponentArtifactType.SNMP_TRAP));
1398       }
1399       if (artifactTypeToFilename.containsKey(ComponentArtifactType.SNMP_POLL)) {
1400         mibUploadStatus.setSnmpPoll(artifactTypeToFilename.get(ComponentArtifactType.SNMP_POLL));
1401       }
1402     }
1403
1404     return mibUploadStatus;
1405   }
1406
1407   private ComponentArtifactEntity setValuesForComponentArtifactEntityUpload(String vspId,
1408                                                                             Version version,
1409                                                                             String filename,
1410                                                                             String componentId,
1411                                                                             String artifactId,
1412                                                                             boolean isTrap,
1413                                                                             byte[]
1414                                                                                 uploadedFileData) {
1415     ComponentArtifactEntity componentArtifactEntity = new ComponentArtifactEntity();
1416
1417     componentArtifactEntity.setVspId(vspId);
1418     componentArtifactEntity.setVersion(version);
1419     componentArtifactEntity.setComponentId(componentId);
1420     componentArtifactEntity.setId(artifactId);
1421     componentArtifactEntity.setType(ComponentArtifactType.getComponentArtifactType(isTrap));
1422     componentArtifactEntity.setArtifactName(filename);
1423
1424     if (Objects.nonNull(uploadedFileData)) {
1425       componentArtifactEntity.setArtifact(ByteBuffer.wrap(uploadedFileData));
1426     }
1427
1428     return componentArtifactEntity;
1429   }
1430
1431   private void validateProcessExistence(String vspId, Version version, String componentId,
1432                        String processId,
1433                        org.openecomp.sdc.vendorsoftwareproduct.dao.type.ProcessEntity retrieved) {
1434     if (retrieved != null) {
1435       return;
1436     }
1437     if (!GENERAL_COMPONENT_ID.equals(componentId)) {
1438       getComponent(vspId, version, componentId);
1439     }
1440     VersioningUtil.validateEntityExistence(retrieved,
1441         new org.openecomp.sdc.vendorsoftwareproduct.dao.type.ProcessEntity(vspId, version,
1442             componentId, processId),
1443         VspDetails.ENTITY_TYPE);//todo retrieved is always null ??
1444   }
1445
1446   private void validateProcessArtifactExistence(String vspId, Version version, String componentId,
1447                                                 String processId, ProcessArtifactEntity retrieved) {
1448     if (retrieved != null) {
1449       VersioningUtil.validateEntityExistence(retrieved.getArtifact(),
1450           new ProcessArtifactEntity(vspId, version, componentId, processId),
1451           VspDetails.ENTITY_TYPE);
1452     } else {
1453       if (!GENERAL_COMPONENT_ID.equals(componentId)) {
1454         getComponent(vspId, version, componentId);
1455       }
1456       VersioningUtil.validateEntityExistence(retrieved,
1457           new org.openecomp.sdc.vendorsoftwareproduct.dao.type.ProcessEntity(vspId, version,
1458               componentId, processId),
1459           VspDetails.ENTITY_TYPE); //todo retrieved is always null ??
1460     }
1461   }
1462
1463   private Map<String, List<ErrorMessage>> validateUploadData(UploadDataEntity uploadData)
1464       throws IOException {
1465     if (uploadData == null || uploadData.getContentData() == null) {
1466       return null;
1467     }
1468
1469     FileContentHandler fileContentMap =
1470         VendorSoftwareProductUtils.loadUploadFileContent(uploadData.getContentData().array());
1471     ValidationManager validationManager =
1472         ValidationManagerUtil.initValidationManager(fileContentMap);
1473     Map<String, List<ErrorMessage>> validationErrors = validationManager.validate();
1474
1475     return
1476         MapUtils.isEmpty(MessageContainerUtil.getMessageByLevel(ErrorLevel.ERROR, validationErrors))
1477             ? null : validationErrors;
1478   }
1479
1480   private VersionInfo getVersionInfo(String vendorSoftwareProductId, VersionableEntityAction action,
1481                                      String user) {
1482     return versioningManager
1483         .getEntityVersionInfo(VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE, vendorSoftwareProductId,
1484             user, action);
1485   }
1486
1487   private void saveCompositionData(String vspId, Version version, CompositionData compositionData) {
1488     Map<String, String> networkIdByName = new HashMap<>();
1489     for (Network network : compositionData.getNetworks()) {
1490
1491       NetworkEntity networkEntity = new NetworkEntity(vspId, version, null);
1492       networkEntity.setNetworkCompositionData(network);
1493
1494       if (network.getName() != null) {
1495         networkIdByName.put(network.getName(), createNetwork(networkEntity).getId());
1496       }
1497     }
1498
1499     for (Component component : compositionData.getComponents()) {
1500       ComponentEntity componentEntity = new ComponentEntity(vspId, version, null);
1501       componentEntity.setComponentCompositionData(component.getData());
1502
1503       String componentId = createComponent(componentEntity).getId();
1504
1505       if (CollectionUtils.isNotEmpty(component.getNics())) {
1506         for (Nic nic : component.getNics()) {
1507           if (nic.getNetworkName() != null) {
1508             nic.setNetworkId(networkIdByName.get(nic.getNetworkName()));
1509             nic.setNetworkName(null);
1510           }
1511
1512           org.openecomp.sdc.vendorsoftwareproduct.dao.type.NicEntity
1513               nicEntity =
1514               new org.openecomp.sdc.vendorsoftwareproduct.dao.type.NicEntity(vspId, version,
1515                   componentId, null);
1516           nicEntity.setNicCompositionData(nic);
1517           createNic(nicEntity);
1518         }
1519       }
1520     }
1521   }
1522
1523   private void deleteUploadDataAndContent(String vspId, Version version) {
1524     vendorSoftwareProductDao.deleteUploadData(vspId, version);
1525   }
1526
1527   private QuestionnaireValidationResult validateQuestionnaire(String vspId, Version version) {
1528     CompositionEntityDataManager compositionEntityDataManager = new CompositionEntityDataManager();
1529     compositionEntityDataManager
1530         .addEntity(vendorSoftwareProductDao.getQuestionnaire(vspId, version), null);
1531
1532     Collection<org.openecomp.sdc.vendorsoftwareproduct.dao.type.NicEntity> nics =
1533         vendorSoftwareProductDao.listNicsByVsp(vspId, version);
1534
1535     Map<String, List<String>> nicNamesByComponent = new HashMap<>();
1536     for (org.openecomp.sdc.vendorsoftwareproduct.dao.type.NicEntity nicEntity : nics) {
1537       compositionEntityDataManager.addEntity(nicEntity, null);
1538
1539       Nic nic = nicEntity.getNicCompositionData();
1540       if (nic != null && nic.getName() != null) {
1541         List<String> nicNames = nicNamesByComponent.get(nicEntity.getComponentId());
1542         if (nicNames == null) {
1543           nicNames = new ArrayList<>();
1544           nicNamesByComponent.put(nicEntity.getComponentId(), nicNames);
1545         }
1546         nicNames.add(nic.getName());
1547       }
1548     }
1549
1550     Collection<ComponentEntity> components =
1551         vendorSoftwareProductDao.listComponentsQuestionnaire(vspId, version);
1552     components.stream().forEach(component -> compositionEntityDataManager.addEntity(component,
1553         new ComponentQuestionnaireSchemaInput(nicNamesByComponent.get(component.getId()),
1554             JsonUtil.json2Object(component.getQuestionnaireData(), Map.class))));
1555
1556     Map<CompositionEntityId, Collection<String>> errorsByEntityId =
1557         compositionEntityDataManager.validateEntitiesQuestionnaire();
1558     if (MapUtils.isNotEmpty(errorsByEntityId)) {
1559       compositionEntityDataManager.buildTrees();
1560       compositionEntityDataManager.addErrorsToTrees(errorsByEntityId);
1561       Collection<CompositionEntityValidationData> roots = compositionEntityDataManager.getTrees();
1562       return new QuestionnaireValidationResult(roots.iterator().next());
1563     }
1564
1565     return null;
1566   }
1567 }