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

package org.openecomp.sdc.vendorsoftwareproduct.services;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.openecomp.core.utilities.json.JsonUtil;
import org.openecomp.sdc.generator.datatypes.tosca.*;
import org.openecomp.sdc.logging.api.Logger;
import org.openecomp.sdc.logging.api.LoggerFactory;
import org.openecomp.sdc.vendorlicense.dao.types.FeatureGroupEntity;
import org.openecomp.sdc.vendorlicense.dao.types.FeatureGroupModel;
import org.openecomp.sdc.vendorlicense.facade.VendorLicenseFacade;
import org.openecomp.sdc.vendorlicense.facade.VendorLicenseFacadeFactory;
import org.openecomp.sdc.vendorsoftwareproduct.dao.*;
import org.openecomp.sdc.vendorsoftwareproduct.dao.type.*;
import org.openecomp.sdc.vendorsoftwareproduct.types.composition.*;
import org.openecomp.sdc.vendorsoftwareproduct.types.questionnaire.component.compute.Compute;
import org.openecomp.sdc.vendorsoftwareproduct.types.questionnaire.component.image.ImageDetails;
import org.openecomp.sdc.versioning.dao.types.Version;

import java.util.*;


public class ManualVspDataCollectionService {

  private static final VendorSoftwareProductInfoDao vendorSoftwareProductInfoDao =
      VendorSoftwareProductInfoDaoFactory.getInstance().createInterface();
  private static final DeploymentFlavorDao deploymentFlavorDao =
      DeploymentFlavorDaoFactory.getInstance().createInterface();
  private static final ComputeDao computeDao =
      ComputeDaoFactory.getInstance().createInterface();
  private static final ImageDao imageDao =
      ImageDaoFactory.getInstance().createInterface();
  private static final ComponentDao componentDao =
      ComponentDaoFactory.getInstance().createInterface();

  private static final NicDao nicDao =
      NicDaoFactory.getInstance().createInterface();
  private static final VendorLicenseFacade vendorLicenseFacade =
      VendorLicenseFacadeFactory.getInstance().createInterface();

  private final Logger log = LoggerFactory.getLogger(this.getClass().getName());

  /**
   * Gets vendor name for the vsp.
   *
   * @param vspId   the vsp id
   * @param version the version
   * @return the release vendor name
   */
  public Optional<String> getReleaseVendor(String vspId, Version version) {
    String vendorName = null;
    VspDetails vspDetails = vendorSoftwareProductInfoDao.get(new VspDetails(vspId, version));
    if (Objects.nonNull(vspDetails)) {
      vendorName = vspDetails.getVendorName();
    }
    return Optional.ofNullable(vendorName);
  }

  /**
   * Gets the deployment flavor data for manually onboarded VSPs.
   *
   * @param vspId   the vsp id
   * @param version the version
   * @return the allowed flavors
   */
  public Map<String, DeploymentFlavorModel> getAllowedFlavors(String vspId, Version version) {
    Map<String, DeploymentFlavorModel> allowedFlavors = new HashMap<>();
    Collection<DeploymentFlavorEntity> deploymentFlavorEntities =
        deploymentFlavorDao.list(new DeploymentFlavorEntity(vspId, version, null));
    if (CollectionUtils.isNotEmpty(deploymentFlavorEntities)) {
      for (DeploymentFlavorEntity deploymentFlavorEntity : deploymentFlavorEntities) {
        DeploymentFlavor deploymentFlavorCompositionData =
            deploymentFlavorEntity.getDeploymentFlavorCompositionData();

        VspDetails vspDetails = vendorSoftwareProductInfoDao.get(new VspDetails(vspId, version));
        String vspVlmId;
        Version vlmVersion;
        if (Objects.nonNull(vspDetails)) {
          vspVlmId = vspDetails.getVendorId();
          vlmVersion = vspDetails.getVlmVersion();
          if (StringUtils.isNotEmpty(vspVlmId)) {
            DeploymentFlavorModel deploymentFlavorModel = new DeploymentFlavorModel();
            String featureGroupId = deploymentFlavorCompositionData.getFeatureGroupId();
            if (Objects.isNull(featureGroupId)) {
              //No feature group associated with deployment flavor. So excluding this deployment
              // flavor for Tosca model
              continue;
            }
            //Gather and set License flavor info
            LicenseFlavor licenseFlavor = getLicenseFlavor(featureGroupId);
            deploymentFlavorModel.setLicense_flavor(licenseFlavor);
            //Get sp_part_number
            Optional<String> partNumber = getPartNumber(vspVlmId, vlmVersion, featureGroupId);
            partNumber.ifPresent(deploymentFlavorModel::setSp_part_number);
            //Gather and set Vendor Info
            String vendorModel = deploymentFlavorCompositionData.getModel();
            Optional<VendorInfo> vendorInfo = getVendorInfo(vspVlmId, vendorModel, vlmVersion,
                featureGroupId);
            vendorInfo.ifPresent(deploymentFlavorModel::setVendor_info);
            //Gather and set Compute info
            List<ComponentComputeAssociation> componentComputeAssociations =
                deploymentFlavorCompositionData.getComponentComputeAssociations();
            if (CollectionUtils.isNotEmpty(componentComputeAssociations)) {
              for (ComponentComputeAssociation componentComputeAssociation :
                  componentComputeAssociations) {
                String componentId = componentComputeAssociation.getComponentId();
                String computeFlavorId = componentComputeAssociation.getComputeFlavorId();
                Optional<ComputeFlavor> computeFlavor =
                    getComputeFlavor(vspId, version, componentId, computeFlavorId);
                computeFlavor.ifPresent(deploymentFlavorModel::setCompute_flavor);
              }
            }
            partNumber.ifPresent(spPartNumber -> allowedFlavors.put(spPartNumber,
                deploymentFlavorModel));
          }
        }
      }
    }
    return allowedFlavors;
  }

  /**
   * Gets the component image data for manually onboarded VSPs.
   *
   * @param vspId   the vsp id
   * @param version the version
   * @return the vsp component images
   */
  public Map<String, List<MultiFlavorVfcImage>> getVspComponentImages(String vspId,
                                                                      Version version) {
    Map<String, List<MultiFlavorVfcImage>> vspComponentImages = new HashMap<>();
    Collection<DeploymentFlavorEntity> deploymentFlavorEntities =
        deploymentFlavorDao.list(new DeploymentFlavorEntity(vspId, version, null));
    for (DeploymentFlavorEntity deploymentFlavorEntity : deploymentFlavorEntities) {
      DeploymentFlavor deploymentFlavorCompositionData =
          deploymentFlavorEntity.getDeploymentFlavorCompositionData();

      List<ComponentComputeAssociation> componentComputeAssociations =
          deploymentFlavorCompositionData.getComponentComputeAssociations();
      if (CollectionUtils.isNotEmpty(componentComputeAssociations)) {
        for (ComponentComputeAssociation componentComputeAssociation :
            componentComputeAssociations) {
          String componentId = componentComputeAssociation.getComponentId();
          List<MultiFlavorVfcImage> componentImages =
              getComponentImages(vspId, version, componentId);
          if (CollectionUtils.isNotEmpty(componentImages)) {
            vspComponentImages.put(componentId, componentImages);
          }
        }
      }
    }
    return vspComponentImages;
  }

  /**
   * Gets the component data for manually onboarded VSPs.
   *
   * @param vspId   the vsp id
   * @param version the version
   * @return the vsp components
   */
  public Map<String, String> getVspComponents(String vspId, Version version) {
    Map<String, String> componentIdNameMap = new HashMap<>();
    Collection<DeploymentFlavorEntity> deploymentFlavorEntities =
        deploymentFlavorDao.list(new DeploymentFlavorEntity(vspId, version, null));
    for (DeploymentFlavorEntity deploymentFlavorEntity : deploymentFlavorEntities) {
      DeploymentFlavor deploymentFlavorCompositionData =
          deploymentFlavorEntity.getDeploymentFlavorCompositionData();

      List<ComponentComputeAssociation> componentComputeAssociations =
          deploymentFlavorCompositionData.getComponentComputeAssociations();
      if (CollectionUtils.isNotEmpty(componentComputeAssociations)) {
        for (ComponentComputeAssociation componentComputeAssociation :
            componentComputeAssociations) {
          String componentId = componentComputeAssociation.getComponentId();
          Optional<String> componentName = getComponentName(vspId, version, componentId);
          componentName.ifPresent(name -> componentIdNameMap.put(componentId, name));
        }
      }
    }
    return componentIdNameMap;
  }

  /**
   * Gets the NIC data for manually onboarded VSPs.
   *
   * @param vspId   the vsp id
   * @param version the version
   * @return the vsp component nics
   */
  public Map<String, List<Nic>> getVspComponentNics(String vspId, Version version) {
    Map<String, List<Nic>> vspComponentNics = new HashMap<>();
    Collection<DeploymentFlavorEntity> deploymentFlavorEntities =
        deploymentFlavorDao.list(new DeploymentFlavorEntity(vspId, version, null));
    if (CollectionUtils.isNotEmpty(deploymentFlavorEntities)) {
      for (DeploymentFlavorEntity deploymentFlavorEntity : deploymentFlavorEntities) {
        DeploymentFlavor deploymentFlavorCompositionData =
            deploymentFlavorEntity.getDeploymentFlavorCompositionData();
        if (Objects.nonNull(deploymentFlavorCompositionData)) {
          List<ComponentComputeAssociation> componentComputeAssociations =
              deploymentFlavorCompositionData.getComponentComputeAssociations();
          if (CollectionUtils.isNotEmpty(componentComputeAssociations)) {
            for (ComponentComputeAssociation componentComputeAssociation :
                componentComputeAssociations) {
              String componentId = componentComputeAssociation.getComponentId();
              List<Nic> componentNics = getComponentNics(vspId, version, componentId);
              if (CollectionUtils.isNotEmpty(componentNics)) {
                vspComponentNics.put(componentId, componentNics);
              }
            }
          }
        }
      }
    }
    return vspComponentNics;
  }

  private List<Nic> getComponentNics(String vspId, Version version, String componentId) {
    List<Nic> componentNics = new ArrayList<>();
    Collection<NicEntity> nics = nicDao.list(new NicEntity(vspId, version, componentId, null));
    if (Objects.nonNull(nics)) {
      for (NicEntity nic : nics) {
        String nicId = nic.getId();
        NicEntity nicEntity = nicDao.get(new NicEntity(vspId, version, componentId, nicId));
        if (Objects.nonNull(nicEntity)
            && Objects.nonNull(nicEntity.getCompositionData())) {
          componentNics.add(nicEntity.getNicCompositionData());
        }
      }
    }
    return componentNics;
  }

  private LicenseFlavor getLicenseFlavor(String featureGroupId) {
    LicenseFlavor licenseFlavor = new LicenseFlavor();
    licenseFlavor.setFeature_group_uuid(featureGroupId);
    return licenseFlavor;
  }

  private Optional<String> getPartNumber(String vlmId, Version version,
                                         String featureGroupId) {
    FeatureGroupModel featureGroup = getFeatureGroup(vlmId, version, featureGroupId);
    if (Objects.nonNull(featureGroup)) {
      return Optional.ofNullable(featureGroup.getFeatureGroup().getPartNumber());
    }
    return Optional.empty();
  }

  private Optional<VendorInfo> getVendorInfo(String vlmId, String vendorModel, Version version,
                                             String featureGroupId) {
    VendorInfo vendorInfo = null;
    FeatureGroupModel featureGroup = getFeatureGroup(vlmId, version, featureGroupId);
    if (Objects.nonNull(featureGroup)) {
      //Process Feature group to get Manufacturer ref no.
      String manufacturerReferenceNumber = featureGroup.getEntityManufacturerReferenceNumber();
      vendorInfo = new VendorInfo();
      vendorInfo.setVendor_model(vendorModel);
      if (Objects.nonNull(manufacturerReferenceNumber)) {
        vendorInfo.setManufacturer_reference_number(manufacturerReferenceNumber);
      }
    }
    return Optional.ofNullable(vendorInfo);
  }

  private Optional<ComputeFlavor> getComputeFlavor(String vspId, Version version,
                                                   String componentId, String computeFlavorId) {
    ComputeFlavor computeFlavor = null;
    ComputeEntity computeQuestionnaire;
    try {
      computeQuestionnaire = computeDao.getQuestionnaireData(vspId, version, componentId,
          computeFlavorId);
    } catch (Exception ex) {
      log.warn("Failed to get QuestionnaireData from computeDao," +
              " initializing computeQuestionnaire to null", ex);
      computeQuestionnaire = null;
    }
    if (Objects.nonNull(computeQuestionnaire)) {
      String computeQuestionnaireData = computeQuestionnaire.getQuestionnaireData();
      if (Objects.nonNull(computeQuestionnaireData)) {
        Compute compute;
        try {
          compute = JsonUtil.json2Object(computeQuestionnaireData, Compute.class);
        } catch (Exception ex) {
          log.warn("Failed to convert json value to compute object," +
                  "initializing compute to null", ex);
          compute = null;
        }
        if (compute != null && Objects.nonNull(compute.getVmSizing())) {
          computeFlavor = new ComputeFlavor();
          if (Objects.nonNull(compute.getVmSizing().getNumOfCPUs())) {
            computeFlavor.setNum_cpus(compute.getVmSizing().getNumOfCPUs());
          }
          if (Objects.nonNull(compute.getVmSizing().getFileSystemSizeGB())) {
            computeFlavor.setDisk_size(compute.getVmSizing().getFileSystemSizeGB() + " GB");
          }
          if (Objects.nonNull(compute.getVmSizing().getMemoryRAM())) {
            computeFlavor.setMem_size(compute.getVmSizing().getMemoryRAM());
          }
        }
      }
    }
    return Optional.ofNullable(computeFlavor);
  }

  private FeatureGroupModel getFeatureGroup(String vlmId, Version version, String featureGroupId) {
    FeatureGroupEntity fgInput = new FeatureGroupEntity();
    fgInput.setVendorLicenseModelId(vlmId);
    fgInput.setVersion(version);
    fgInput.setId(featureGroupId);
    return vendorLicenseFacade.getFeatureGroupModel(fgInput);
  }

  private Optional<String> getComponentName(String vspId, Version version, String componentId) {

    ComponentEntity componentEntity =
        componentDao.get(new ComponentEntity(vspId, version, componentId));
    if (Objects.nonNull(componentEntity)
        && Objects.nonNull(componentEntity.getComponentCompositionData())) {
      ComponentData componentCompositionData = componentEntity.getComponentCompositionData();
      return Optional.ofNullable(componentCompositionData.getDisplayName());
    }
    return Optional.empty();
  }

  private List<MultiFlavorVfcImage> getComponentImages(String vspId, Version version,
                                                       String componentId) {
    List<MultiFlavorVfcImage> multiFlavorVfcImages = new ArrayList<>();
    MultiFlavorVfcImage multiFlavorVfcImage;
    Collection<ImageEntity> componentImages =
        imageDao.list(new ImageEntity(vspId, version, componentId, null));
    if (Objects.nonNull(componentImages)) {
      for (ImageEntity componentImage : componentImages) {
        ImageEntity imageEntity = imageDao.get(componentImage);
        ImageEntity imageQuestionnaireDataEntity = imageDao.getQuestionnaireData(vspId, version,
            componentId, componentImage.getId());
        Image imageCompositionData = imageEntity.getImageCompositionData();
        if (Objects.nonNull(imageEntity)
            && Objects.nonNull(imageQuestionnaireDataEntity)
            && Objects.nonNull(imageCompositionData)) {
          ImageDetails imageDetails;
          try {
            imageDetails = JsonUtil.json2Object(imageQuestionnaireDataEntity
                .getQuestionnaireData(), ImageDetails.class);
          } catch (Exception ex) {
            log.warn("Failed to convert json value to ImageDetails object," +
                    "initializing imageDetails to null", ex);
            imageDetails = null;
          }
          if (Objects.nonNull(imageDetails)
              && Objects.nonNull(imageDetails.getVersion())) {
            //Image version is used as a key for the image block
            //So excluding the population if questionnaire data is absent or invalid
            multiFlavorVfcImage = new MultiFlavorVfcImage();
            multiFlavorVfcImage.setSoftware_version(imageDetails.getVersion());
            if (Objects.nonNull(imageCompositionData.getFileName())) {
              multiFlavorVfcImage.setFile_name(imageCompositionData.getFileName());
            }
            if (Objects.nonNull(imageDetails.getMd5())) {
              multiFlavorVfcImage.setFile_hash(imageDetails.getMd5());
            }
            multiFlavorVfcImage.setFile_hash_type("md5");
            multiFlavorVfcImages.add(multiFlavorVfcImage);
          }
        }
      }
    }
    return multiFlavorVfcImages;
  }
}