2 * Copyright © 2018 European Support Limited
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.openecomp.sdc.vendorsoftwareproduct.services;
19 import org.apache.commons.collections4.CollectionUtils;
20 import org.apache.commons.lang.StringUtils;
21 import org.openecomp.core.utilities.json.JsonUtil;
22 import org.openecomp.sdc.generator.datatypes.tosca.*;
23 import org.openecomp.sdc.logging.api.Logger;
24 import org.openecomp.sdc.logging.api.LoggerFactory;
25 import org.openecomp.sdc.vendorlicense.dao.types.FeatureGroupEntity;
26 import org.openecomp.sdc.vendorlicense.dao.types.FeatureGroupModel;
27 import org.openecomp.sdc.vendorlicense.facade.VendorLicenseFacade;
28 import org.openecomp.sdc.vendorlicense.facade.VendorLicenseFacadeFactory;
29 import org.openecomp.sdc.vendorsoftwareproduct.dao.*;
30 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.*;
31 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.*;
32 import org.openecomp.sdc.vendorsoftwareproduct.types.questionnaire.component.compute.Compute;
33 import org.openecomp.sdc.vendorsoftwareproduct.types.questionnaire.component.image.ImageDetails;
34 import org.openecomp.sdc.versioning.dao.types.Version;
39 public class ManualVspDataCollectionService {
41 private static final VendorSoftwareProductInfoDao vendorSoftwareProductInfoDao =
42 VendorSoftwareProductInfoDaoFactory.getInstance().createInterface();
43 private static final DeploymentFlavorDao deploymentFlavorDao =
44 DeploymentFlavorDaoFactory.getInstance().createInterface();
45 private static final ComputeDao computeDao =
46 ComputeDaoFactory.getInstance().createInterface();
47 private static final ImageDao imageDao =
48 ImageDaoFactory.getInstance().createInterface();
49 private static final ComponentDao componentDao =
50 ComponentDaoFactory.getInstance().createInterface();
52 private static final NicDao nicDao =
53 NicDaoFactory.getInstance().createInterface();
54 private static final VendorLicenseFacade vendorLicenseFacade =
55 VendorLicenseFacadeFactory.getInstance().createInterface();
57 private final Logger log = LoggerFactory.getLogger(this.getClass().getName());
60 * Gets vendor name for the vsp.
62 * @param vspId the vsp id
63 * @param version the version
64 * @return the release vendor name
66 public Optional<String> getReleaseVendor(String vspId, Version version) {
67 String vendorName = null;
68 VspDetails vspDetails = vendorSoftwareProductInfoDao.get(new VspDetails(vspId, version));
69 if (Objects.nonNull(vspDetails)) {
70 vendorName = vspDetails.getVendorName();
72 return Optional.ofNullable(vendorName);
76 * Gets the deployment flavor data for manually onboarded VSPs.
78 * @param vspId the vsp id
79 * @param version the version
80 * @return the allowed flavors
82 public Map<String, DeploymentFlavorModel> getAllowedFlavors(String vspId, Version version) {
83 Map<String, DeploymentFlavorModel> allowedFlavors = new HashMap<>();
84 Collection<DeploymentFlavorEntity> deploymentFlavorEntities =
85 deploymentFlavorDao.list(new DeploymentFlavorEntity(vspId, version, null));
86 if (CollectionUtils.isNotEmpty(deploymentFlavorEntities)) {
87 for (DeploymentFlavorEntity deploymentFlavorEntity : deploymentFlavorEntities) {
88 DeploymentFlavor deploymentFlavorCompositionData =
89 deploymentFlavorEntity.getDeploymentFlavorCompositionData();
91 VspDetails vspDetails = vendorSoftwareProductInfoDao.get(new VspDetails(vspId, version));
94 if (Objects.nonNull(vspDetails)) {
95 vspVlmId = vspDetails.getVendorId();
96 vlmVersion = vspDetails.getVlmVersion();
97 if (StringUtils.isNotEmpty(vspVlmId)) {
98 DeploymentFlavorModel deploymentFlavorModel = new DeploymentFlavorModel();
99 String featureGroupId = deploymentFlavorCompositionData.getFeatureGroupId();
100 if (Objects.isNull(featureGroupId)) {
101 //No feature group associated with deployment flavor. So excluding this deployment
102 // flavor for Tosca model
105 //Gather and set License flavor info
106 LicenseFlavor licenseFlavor = getLicenseFlavor(featureGroupId);
107 deploymentFlavorModel.setLicense_flavor(licenseFlavor);
109 Optional<String> partNumber = getPartNumber(vspVlmId, vlmVersion, featureGroupId);
110 partNumber.ifPresent(deploymentFlavorModel::setSp_part_number);
111 //Gather and set Vendor Info
112 String vendorModel = deploymentFlavorCompositionData.getModel();
113 Optional<VendorInfo> vendorInfo = getVendorInfo(vspVlmId, vendorModel, vlmVersion,
115 vendorInfo.ifPresent(deploymentFlavorModel::setVendor_info);
116 //Gather and set Compute info
117 List<ComponentComputeAssociation> componentComputeAssociations =
118 deploymentFlavorCompositionData.getComponentComputeAssociations();
119 if (CollectionUtils.isNotEmpty(componentComputeAssociations)) {
120 for (ComponentComputeAssociation componentComputeAssociation :
121 componentComputeAssociations) {
122 String componentId = componentComputeAssociation.getComponentId();
123 String computeFlavorId = componentComputeAssociation.getComputeFlavorId();
124 Optional<ComputeFlavor> computeFlavor =
125 getComputeFlavor(vspId, version, componentId, computeFlavorId);
126 computeFlavor.ifPresent(deploymentFlavorModel::setCompute_flavor);
129 partNumber.ifPresent(spPartNumber -> allowedFlavors.put(spPartNumber,
130 deploymentFlavorModel));
135 return allowedFlavors;
139 * Gets the component image data for manually onboarded VSPs.
141 * @param vspId the vsp id
142 * @param version the version
143 * @return the vsp component images
145 public Map<String, List<MultiFlavorVfcImage>> getVspComponentImages(String vspId,
147 Map<String, List<MultiFlavorVfcImage>> vspComponentImages = new HashMap<>();
148 Collection<DeploymentFlavorEntity> deploymentFlavorEntities =
149 deploymentFlavorDao.list(new DeploymentFlavorEntity(vspId, version, null));
150 for (DeploymentFlavorEntity deploymentFlavorEntity : deploymentFlavorEntities) {
151 DeploymentFlavor deploymentFlavorCompositionData =
152 deploymentFlavorEntity.getDeploymentFlavorCompositionData();
154 List<ComponentComputeAssociation> componentComputeAssociations =
155 deploymentFlavorCompositionData.getComponentComputeAssociations();
156 if (CollectionUtils.isNotEmpty(componentComputeAssociations)) {
157 for (ComponentComputeAssociation componentComputeAssociation :
158 componentComputeAssociations) {
159 String componentId = componentComputeAssociation.getComponentId();
160 List<MultiFlavorVfcImage> componentImages =
161 getComponentImages(vspId, version, componentId);
162 if (CollectionUtils.isNotEmpty(componentImages)) {
163 vspComponentImages.put(componentId, componentImages);
168 return vspComponentImages;
172 * Gets the component data for manually onboarded VSPs.
174 * @param vspId the vsp id
175 * @param version the version
176 * @return the vsp components
178 public Map<String, String> getVspComponents(String vspId, Version version) {
179 Map<String, String> componentIdNameMap = new HashMap<>();
180 Collection<DeploymentFlavorEntity> deploymentFlavorEntities =
181 deploymentFlavorDao.list(new DeploymentFlavorEntity(vspId, version, null));
182 for (DeploymentFlavorEntity deploymentFlavorEntity : deploymentFlavorEntities) {
183 DeploymentFlavor deploymentFlavorCompositionData =
184 deploymentFlavorEntity.getDeploymentFlavorCompositionData();
186 List<ComponentComputeAssociation> componentComputeAssociations =
187 deploymentFlavorCompositionData.getComponentComputeAssociations();
188 if (CollectionUtils.isNotEmpty(componentComputeAssociations)) {
189 for (ComponentComputeAssociation componentComputeAssociation :
190 componentComputeAssociations) {
191 String componentId = componentComputeAssociation.getComponentId();
192 Optional<String> componentName = getComponentName(vspId, version, componentId);
193 componentName.ifPresent(name -> componentIdNameMap.put(componentId, name));
197 return componentIdNameMap;
201 * Gets the NIC data for manually onboarded VSPs.
203 * @param vspId the vsp id
204 * @param version the version
205 * @return the vsp component nics
207 public Map<String, List<Nic>> getVspComponentNics(String vspId, Version version) {
208 Map<String, List<Nic>> vspComponentNics = new HashMap<>();
209 Collection<DeploymentFlavorEntity> deploymentFlavorEntities =
210 deploymentFlavorDao.list(new DeploymentFlavorEntity(vspId, version, null));
211 if (CollectionUtils.isNotEmpty(deploymentFlavorEntities)) {
212 for (DeploymentFlavorEntity deploymentFlavorEntity : deploymentFlavorEntities) {
213 DeploymentFlavor deploymentFlavorCompositionData =
214 deploymentFlavorEntity.getDeploymentFlavorCompositionData();
215 if (Objects.nonNull(deploymentFlavorCompositionData)) {
216 List<ComponentComputeAssociation> componentComputeAssociations =
217 deploymentFlavorCompositionData.getComponentComputeAssociations();
218 if (CollectionUtils.isNotEmpty(componentComputeAssociations)) {
219 for (ComponentComputeAssociation componentComputeAssociation :
220 componentComputeAssociations) {
221 String componentId = componentComputeAssociation.getComponentId();
222 List<Nic> componentNics = getComponentNics(vspId, version, componentId);
223 if (CollectionUtils.isNotEmpty(componentNics)) {
224 vspComponentNics.put(componentId, componentNics);
231 return vspComponentNics;
234 private List<Nic> getComponentNics(String vspId, Version version, String componentId) {
235 List<Nic> componentNics = new ArrayList<>();
236 Collection<NicEntity> nics = nicDao.list(new NicEntity(vspId, version, componentId, null));
237 if (Objects.nonNull(nics)) {
238 for (NicEntity nic : nics) {
239 String nicId = nic.getId();
240 NicEntity nicEntity = nicDao.get(new NicEntity(vspId, version, componentId, nicId));
241 if (Objects.nonNull(nicEntity)
242 && Objects.nonNull(nicEntity.getCompositionData())) {
243 componentNics.add(nicEntity.getNicCompositionData());
247 return componentNics;
250 private LicenseFlavor getLicenseFlavor(String featureGroupId) {
251 LicenseFlavor licenseFlavor = new LicenseFlavor();
252 licenseFlavor.setFeature_group_uuid(featureGroupId);
253 return licenseFlavor;
256 private Optional<String> getPartNumber(String vlmId, Version version,
257 String featureGroupId) {
258 FeatureGroupModel featureGroup = getFeatureGroup(vlmId, version, featureGroupId);
259 if (Objects.nonNull(featureGroup)) {
260 return Optional.ofNullable(featureGroup.getFeatureGroup().getPartNumber());
262 return Optional.empty();
265 private Optional<VendorInfo> getVendorInfo(String vlmId, String vendorModel, Version version,
266 String featureGroupId) {
267 VendorInfo vendorInfo = null;
268 FeatureGroupModel featureGroup = getFeatureGroup(vlmId, version, featureGroupId);
269 if (Objects.nonNull(featureGroup)) {
270 //Process Feature group to get Manufacturer ref no.
271 String manufacturerReferenceNumber = featureGroup.getEntityManufacturerReferenceNumber();
272 vendorInfo = new VendorInfo();
273 vendorInfo.setVendor_model(vendorModel);
274 if (Objects.nonNull(manufacturerReferenceNumber)) {
275 vendorInfo.setManufacturer_reference_number(manufacturerReferenceNumber);
278 return Optional.ofNullable(vendorInfo);
281 private Optional<ComputeFlavor> getComputeFlavor(String vspId, Version version,
282 String componentId, String computeFlavorId) {
283 ComputeFlavor computeFlavor = null;
284 ComputeEntity computeQuestionnaire;
286 computeQuestionnaire = computeDao.getQuestionnaireData(vspId, version, componentId,
288 } catch (Exception ex) {
289 log.warn("Failed to get QuestionnaireData from computeDao," +
290 " initializing computeQuestionnaire to null", ex);
291 computeQuestionnaire = null;
293 if (Objects.nonNull(computeQuestionnaire)) {
294 String computeQuestionnaireData = computeQuestionnaire.getQuestionnaireData();
295 if (Objects.nonNull(computeQuestionnaireData)) {
298 compute = JsonUtil.json2Object(computeQuestionnaireData, Compute.class);
299 } catch (Exception ex) {
300 log.warn("Failed to convert json value to compute object," +
301 "initializing compute to null", ex);
304 if (compute != null && Objects.nonNull(compute.getVmSizing())) {
305 computeFlavor = new ComputeFlavor();
306 if (Objects.nonNull(compute.getVmSizing().getNumOfCPUs())) {
307 computeFlavor.setNum_cpus(compute.getVmSizing().getNumOfCPUs());
309 if (Objects.nonNull(compute.getVmSizing().getFileSystemSizeGB())) {
310 computeFlavor.setDisk_size(compute.getVmSizing().getFileSystemSizeGB() + " GB");
312 if (Objects.nonNull(compute.getVmSizing().getMemoryRAM())) {
313 computeFlavor.setMem_size(compute.getVmSizing().getMemoryRAM());
318 return Optional.ofNullable(computeFlavor);
321 private FeatureGroupModel getFeatureGroup(String vlmId, Version version, String featureGroupId) {
322 FeatureGroupEntity fgInput = new FeatureGroupEntity();
323 fgInput.setVendorLicenseModelId(vlmId);
324 fgInput.setVersion(version);
325 fgInput.setId(featureGroupId);
326 return vendorLicenseFacade.getFeatureGroupModel(fgInput);
329 private Optional<String> getComponentName(String vspId, Version version, String componentId) {
331 ComponentEntity componentEntity =
332 componentDao.get(new ComponentEntity(vspId, version, componentId));
333 if (Objects.nonNull(componentEntity)
334 && Objects.nonNull(componentEntity.getComponentCompositionData())) {
335 ComponentData componentCompositionData = componentEntity.getComponentCompositionData();
336 return Optional.ofNullable(componentCompositionData.getDisplayName());
338 return Optional.empty();
341 private List<MultiFlavorVfcImage> getComponentImages(String vspId, Version version,
342 String componentId) {
343 List<MultiFlavorVfcImage> multiFlavorVfcImages = new ArrayList<>();
344 MultiFlavorVfcImage multiFlavorVfcImage;
345 Collection<ImageEntity> componentImages =
346 imageDao.list(new ImageEntity(vspId, version, componentId, null));
347 if (Objects.nonNull(componentImages)) {
348 for (ImageEntity componentImage : componentImages) {
349 ImageEntity imageEntity = imageDao.get(componentImage);
350 ImageEntity imageQuestionnaireDataEntity = imageDao.getQuestionnaireData(vspId, version,
351 componentId, componentImage.getId());
352 Image imageCompositionData = imageEntity.getImageCompositionData();
353 if (Objects.nonNull(imageEntity)
354 && Objects.nonNull(imageQuestionnaireDataEntity)
355 && Objects.nonNull(imageCompositionData)) {
356 ImageDetails imageDetails;
358 imageDetails = JsonUtil.json2Object(imageQuestionnaireDataEntity
359 .getQuestionnaireData(), ImageDetails.class);
360 } catch (Exception ex) {
361 log.warn("Failed to convert json value to ImageDetails object," +
362 "initializing imageDetails to null", ex);
365 if (Objects.nonNull(imageDetails)
366 && Objects.nonNull(imageDetails.getVersion())) {
367 //Image version is used as a key for the image block
368 //So excluding the population if questionnaire data is absent or invalid
369 multiFlavorVfcImage = new MultiFlavorVfcImage();
370 multiFlavorVfcImage.setSoftware_version(imageDetails.getVersion());
371 if (Objects.nonNull(imageCompositionData.getFileName())) {
372 multiFlavorVfcImage.setFile_name(imageCompositionData.getFileName());
374 if (Objects.nonNull(imageDetails.getMd5())) {
375 multiFlavorVfcImage.setFile_hash(imageDetails.getMd5());
377 multiFlavorVfcImage.setFile_hash_type("md5");
378 multiFlavorVfcImages.add(multiFlavorVfcImage);
383 return multiFlavorVfcImages;