14d9b31f468e6b525bcaa1c5bcfef804da6fdcb0
[sdc.git] /
1 /*
2  * Copyright © 2016-2017 European Support Limited
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package org.openecomp.sdc.vendorsoftwareproduct.impl;
18
19 import org.apache.commons.collections4.CollectionUtils;
20 import org.openecomp.sdc.common.errors.CoreException;
21 import org.openecomp.sdc.common.errors.ErrorCode;
22 import org.openecomp.sdc.datatypes.error.ErrorLevel;
23 import org.openecomp.sdc.logging.context.impl.MdcDataDebugMessage;
24 import org.openecomp.sdc.logging.context.impl.MdcDataErrorMessage;
25 import org.openecomp.sdc.logging.types.LoggerConstants;
26 import org.openecomp.sdc.logging.types.LoggerErrorCode;
27 import org.openecomp.sdc.logging.types.LoggerTragetServiceName;
28 import org.openecomp.sdc.vendorsoftwareproduct.DeploymentFlavorManager;
29 import org.openecomp.sdc.vendorsoftwareproduct.dao.ComputeDao;
30 import org.openecomp.sdc.vendorsoftwareproduct.dao.DeploymentFlavorDao;
31 import org.openecomp.sdc.vendorsoftwareproduct.dao.VendorSoftwareProductInfoDao;
32 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComputeEntity;
33 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.DeploymentFlavorEntity;
34 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspDetails;
35 import org.openecomp.sdc.vendorsoftwareproduct.errors.DeploymentFlavorErrorBuilder;
36 import org.openecomp.sdc.vendorsoftwareproduct.errors.NotSupportedHeatOnboardMethodErrorBuilder;
37 import org.openecomp.sdc.vendorsoftwareproduct.services.composition.CompositionEntityDataManager;
38 import org.openecomp.sdc.vendorsoftwareproduct.services.schemagenerator.SchemaGenerator;
39 import org.openecomp.sdc.vendorsoftwareproduct.types.CompositionEntityResponse;
40 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.ComponentComputeAssociation;
41 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.CompositionEntityType;
42 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.CompositionEntityValidationData;
43 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.DeploymentFlavor;
44 import org.openecomp.sdc.vendorsoftwareproduct.types.schemagenerator.DeploymentFlavorCompositionSchemaInput;
45 import org.openecomp.sdc.vendorsoftwareproduct.types.schemagenerator.SchemaTemplateContext;
46 import org.openecomp.sdc.versioning.VersioningUtil;
47 import org.openecomp.sdc.versioning.dao.types.Version;
48
49 import java.util.ArrayList;
50 import java.util.Collection;
51 import java.util.Iterator;
52 import java.util.List;
53 import java.util.Map;
54
55 public class DeploymentFlavorManagerImpl implements DeploymentFlavorManager {
56
57   private static final MdcDataDebugMessage MDC_DATA_DEBUG_MESSAGE = new MdcDataDebugMessage();
58   private final VendorSoftwareProductInfoDao vspInfoDao;
59   private final DeploymentFlavorDao deploymentFlavorDao;
60   private final CompositionEntityDataManager compositionEntityDataManager;
61   private final ComputeDao computeDao;
62   private static final String VSP_ID_DEPLOYMENT_FLAVOR_ID = "VSP id, deployment flavor id";
63
64   public DeploymentFlavorManagerImpl(VendorSoftwareProductInfoDao vspInfoDao,
65                                      DeploymentFlavorDao deploymentFlavorDao,
66                                      CompositionEntityDataManager compositionEntityDataManager,
67                                      ComputeDao computeDao) {
68     this.vspInfoDao = vspInfoDao;
69     this.deploymentFlavorDao = deploymentFlavorDao;
70     this.compositionEntityDataManager = compositionEntityDataManager;
71     this.computeDao = computeDao;
72
73   }
74
75   @Override
76   public Collection<DeploymentFlavorEntity> listDeploymentFlavors(String vspId, Version version) {
77     MDC_DATA_DEBUG_MESSAGE.debugEntryMessage("VSP id", vspId);
78     MDC_DATA_DEBUG_MESSAGE.debugExitMessage("VSP id", vspId);
79     return deploymentFlavorDao.list(new DeploymentFlavorEntity(vspId, version, null));
80   }
81
82   @Override
83   public DeploymentFlavorEntity createDeploymentFlavor(
84       DeploymentFlavorEntity deploymentFlavorEntity) {
85     DeploymentFlavorEntity createDeploymentFlavor;
86     MDC_DATA_DEBUG_MESSAGE.debugEntryMessage("VSP id ", deploymentFlavorEntity.getVspId());
87
88     if (!vspInfoDao.isManual(deploymentFlavorEntity.getVspId(),
89         deploymentFlavorEntity.getVersion())) {
90       ErrorCode deploymentFlavorErrorBuilder = DeploymentFlavorErrorBuilder
91           .getAddDeploymentNotSupportedHeatOnboardErrorBuilder();
92       MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
93           LoggerTragetServiceName.CREATE_DEPLOYMENT_FLAVOR, ErrorLevel.ERROR.name(),
94           LoggerErrorCode.PERMISSION_ERROR.getErrorCode(), deploymentFlavorErrorBuilder.message());
95       throw new CoreException(deploymentFlavorErrorBuilder);
96     } else {
97       validateDeploymentFlavor(deploymentFlavorEntity, deploymentFlavorEntity.getVersion());
98       createDeploymentFlavor =
99           compositionEntityDataManager.createDeploymentFlavor(deploymentFlavorEntity);
100     }
101     return createDeploymentFlavor;
102   }
103
104   private void validateDeploymentFlavor(DeploymentFlavorEntity deploymentFlavorEntity,
105                                         Version version) {
106     //Validation for unique model.
107     Collection<DeploymentFlavorEntity> listDeploymentFlavors =
108         listDeploymentFlavors(deploymentFlavorEntity.getVspId(),
109             version);
110     isDeploymentFlavorModelDuplicate(deploymentFlavorEntity, listDeploymentFlavors);
111
112     List<String> featureGroups =
113         getFeatureGroupListForVsp(deploymentFlavorEntity.getVspId(), version);
114     String featureGroup = deploymentFlavorEntity.getDeploymentFlavorCompositionData()
115         .getFeatureGroupId();
116       if (featureGroup != null && featureGroup.trim().length() > 0
117               && isEmpty(featureGroups) || (!(validFeatureGroup(featureGroups, featureGroup)))) {
118         ErrorCode deploymentFlavorErrorBuilder = DeploymentFlavorErrorBuilder
119             .getFeatureGroupNotexistErrorBuilder(featureGroup, deploymentFlavorEntity.getVspId(),
120                 version);
121         MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
122             LoggerTragetServiceName.CREATE_DEPLOYMENT_FLAVOR, ErrorLevel.ERROR.name(),
123             LoggerErrorCode.DATA_ERROR.getErrorCode(), deploymentFlavorErrorBuilder.message());
124         throw new CoreException(deploymentFlavorErrorBuilder);
125       }
126
127     validateComponentComputeAssociation(deploymentFlavorEntity, version);
128   }
129
130   private void isDeploymentFlavorModelDuplicate(DeploymentFlavorEntity deploymentFlavorEntity,
131                     Collection<DeploymentFlavorEntity> listDeploymentFlavors) {
132     listDeploymentFlavors.forEach(deploymentFlavor -> {
133       if (deploymentFlavorEntity.getDeploymentFlavorCompositionData().getModel().equalsIgnoreCase(
134           deploymentFlavor.getDeploymentFlavorCompositionData().getModel())) {
135         ErrorCode deploymentFlavorModelErrorBuilder = DeploymentFlavorErrorBuilder
136             .getDuplicateDeploymentFlavorModelErrorBuilder(
137                 deploymentFlavorEntity.getDeploymentFlavorCompositionData().getModel(),
138                 deploymentFlavorEntity.getVspId());
139         MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
140             LoggerTragetServiceName.CREATE_DEPLOYMENT_FLAVOR, ErrorLevel.ERROR.name(),
141             LoggerErrorCode.DATA_ERROR.getErrorCode(), deploymentFlavorModelErrorBuilder.message());
142         throw new CoreException(deploymentFlavorModelErrorBuilder);
143       }
144     });
145   }
146
147   private List<String> getFeatureGroupListForVsp(String vspId, Version version) {
148     final VspDetails vspDetails = vspInfoDao.get(new VspDetails(vspId, version));
149     return vspDetails.getFeatureGroups();
150   }
151
152   private boolean isEmpty(Collection coll) {
153     return coll == null || coll.isEmpty();
154   }
155
156   private boolean validFeatureGroup(List<String> featureGroups, String featureGroupId) {
157     Iterator<String> iterator = featureGroups.iterator();
158     boolean valid = false;
159     while (iterator.hasNext()) {
160       String fgId = iterator.next().trim();
161       if (fgId.equals(featureGroupId)) {
162         valid = true;
163         break;
164       } else {
165         valid = false;
166       }
167     }
168     return valid;
169   }
170
171   private void validateComponentComputeAssociation(DeploymentFlavorEntity deploymentFlavorEntity,
172                                                    Version version) {
173     List<ComponentComputeAssociation> componentComputeAssociationList = deploymentFlavorEntity
174         .getDeploymentFlavorCompositionData().getComponentComputeAssociations();
175     List<String> vfcList = new ArrayList<>();
176     if (!isEmpty(componentComputeAssociationList)) {
177       componentComputeAssociationList.forEach(componentComputeAssociation ->
178         validateComponentComputeAssocoationList(deploymentFlavorEntity,
179                 version, vfcList, componentComputeAssociation));
180       Map<String, Integer> frequencyMapping = CollectionUtils.getCardinalityMap(vfcList);
181
182       for (Integer vfcCount : frequencyMapping.values()) {
183         if (vfcCount != 1) {
184           ErrorCode duplicateVfcAssociationErrorBuilder = DeploymentFlavorErrorBuilder
185               .getDuplicateVfcAssociationErrorBuilder();
186           MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
187               LoggerTragetServiceName.CREATE_DEPLOYMENT_FLAVOR, ErrorLevel.ERROR.name(),
188               LoggerErrorCode.DATA_ERROR.getErrorCode(),
189               duplicateVfcAssociationErrorBuilder.message());
190           throw new CoreException(duplicateVfcAssociationErrorBuilder);
191         }
192       }
193     }
194   }
195
196   private void validateComponentComputeAssocoationList(
197               DeploymentFlavorEntity deploymentFlavorEntity,
198               Version version,
199               List<String> vfcList,
200               ComponentComputeAssociation componentComputeAssociation) {
201     if ((componentComputeAssociation.getComponentId() == null || componentComputeAssociation
202         .getComponentId().trim().length() == 0)
203             && (componentComputeAssociation
204             .getComputeFlavorId() != null && componentComputeAssociation
205             .getComputeFlavorId().trim().length() > 0)) {
206       ErrorCode invalidAssociationErrorBuilder = DeploymentFlavorErrorBuilder
207           .getInvalidAssociationErrorBuilder();
208       MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
209           LoggerTragetServiceName.CREATE_DEPLOYMENT_FLAVOR, ErrorLevel.ERROR.name(),
210           LoggerErrorCode.DATA_ERROR.getErrorCode(), invalidAssociationErrorBuilder.message());
211       throw new CoreException(invalidAssociationErrorBuilder);
212     } else if (componentComputeAssociation.getComponentId() != null
213             && componentComputeAssociation.getComponentId().trim().length() > 0) {
214       validateComponentComputeAssociationFlavour(deploymentFlavorEntity,
215               version, componentComputeAssociation);
216       vfcList.add(componentComputeAssociation.getComponentId());
217     }
218   }
219
220   private void validateComponentComputeAssociationFlavour(
221           DeploymentFlavorEntity deploymentFlavorEntity,
222                           Version version,
223                           ComponentComputeAssociation componentComputeAssociation) {
224     if (componentComputeAssociation
225         .getComputeFlavorId() != null && componentComputeAssociation
226         .getComputeFlavorId().trim().length() > 0) {
227       ComputeEntity computeFlavor = computeDao.get(new ComputeEntity(deploymentFlavorEntity
228           .getVspId(), version, componentComputeAssociation.getComponentId(),
229           componentComputeAssociation.getComputeFlavorId()));
230       if (computeFlavor == null) {
231         ErrorCode invalidComputeIdErrorBuilder = DeploymentFlavorErrorBuilder
232             .getInvalidComputeIdErrorBuilder(componentComputeAssociation.getComputeFlavorId(),
233                 componentComputeAssociation.getComponentId());
234         MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
235             LoggerTragetServiceName.CREATE_DEPLOYMENT_FLAVOR, ErrorLevel.ERROR.name(),
236             LoggerErrorCode.DATA_ERROR.getErrorCode(),
237             invalidComputeIdErrorBuilder.message());
238         throw new CoreException(invalidComputeIdErrorBuilder);
239       }
240     }
241   }
242
243   @Override
244   public CompositionEntityResponse<DeploymentFlavor> getDeploymentFlavor(String vspId,
245                                                         Version version,
246                                                         String deploymentFlavorId) {
247     MDC_DATA_DEBUG_MESSAGE
248         .debugEntryMessage(VSP_ID_DEPLOYMENT_FLAVOR_ID, vspId, deploymentFlavorId);
249
250     DeploymentFlavorEntity deploymentFlavorEntity =
251         getValidatedDeploymentFlavor(vspId, version, deploymentFlavorId);
252     DeploymentFlavor deploymentFlavor = deploymentFlavorEntity.getDeploymentFlavorCompositionData();
253     DeploymentFlavorCompositionSchemaInput schemaInput = new
254         DeploymentFlavorCompositionSchemaInput();
255     schemaInput.setManual(vspInfoDao.isManual(vspId, version));
256     schemaInput.setDeploymentFlavor(deploymentFlavor);
257     List<String> featureGroups =
258         getFeatureGroupListForVsp(vspId, version);
259     schemaInput.setFeatureGroupIds(featureGroups);
260     CompositionEntityResponse<DeploymentFlavor> response = new CompositionEntityResponse<>();
261     response.setId(deploymentFlavorId);
262     response.setSchema(SchemaGenerator
263         .generate(SchemaTemplateContext.composition, CompositionEntityType.deployment,
264             schemaInput));
265     response.setData(deploymentFlavor);
266     MDC_DATA_DEBUG_MESSAGE
267         .debugExitMessage("VSP id, deployment flavor id ", vspId, deploymentFlavorId);
268
269     return response;
270   }
271
272   private DeploymentFlavorEntity getValidatedDeploymentFlavor(String vspId, Version version, String
273       deploymentFlavorId) {
274     DeploymentFlavorEntity retrieved = deploymentFlavorDao.get(new DeploymentFlavorEntity(vspId,
275         version, deploymentFlavorId));
276     VersioningUtil
277         .validateEntityExistence(retrieved, new DeploymentFlavorEntity(vspId, version,
278             deploymentFlavorId), VspDetails.ENTITY_TYPE);
279     return retrieved;
280   }
281
282   @Override
283   public CompositionEntityResponse<DeploymentFlavor> getDeploymentFlavorSchema(String vspId,
284                                                                                Version version) {
285     DeploymentFlavorCompositionSchemaInput schemaInput =
286         new DeploymentFlavorCompositionSchemaInput();
287     schemaInput.setManual(vspInfoDao.isManual(vspId, version));
288     List<String> featureGroups =
289         getFeatureGroupListForVsp(vspId, version);
290     schemaInput.setFeatureGroupIds(featureGroups);
291     CompositionEntityResponse<DeploymentFlavor> response = new CompositionEntityResponse<>();
292     response.setSchema(SchemaGenerator
293         .generate(SchemaTemplateContext.composition, CompositionEntityType.deployment,
294             schemaInput));
295     return response;
296   }
297
298   @Override
299   public void deleteDeploymentFlavor(String vspId, Version version, String deploymentFlavorId) {
300     MDC_DATA_DEBUG_MESSAGE
301         .debugEntryMessage(VSP_ID_DEPLOYMENT_FLAVOR_ID, vspId, deploymentFlavorId);
302     DeploymentFlavorEntity deploymentFlavorEntity =
303         getValidatedDeploymentFlavor(vspId, version, deploymentFlavorId);
304     if (!vspInfoDao.isManual(vspId, version)) {
305       final ErrorCode deleteDeploymentFlavorErrorBuilder =
306           NotSupportedHeatOnboardMethodErrorBuilder
307               .getDelDeploymentFlavorNotSupportedHeatOnboardMethodErrorBuilder();
308       MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
309           LoggerTragetServiceName.DELETE_DEPLOYMENT_FLAVOR, ErrorLevel.ERROR.name(),
310           LoggerErrorCode.PERMISSION_ERROR.getErrorCode(),
311           deleteDeploymentFlavorErrorBuilder.message());
312       throw new CoreException(deleteDeploymentFlavorErrorBuilder);
313     }
314     if (deploymentFlavorEntity != null) {
315       deploymentFlavorDao.delete(new DeploymentFlavorEntity(vspId, version, deploymentFlavorId));
316
317     }
318     MDC_DATA_DEBUG_MESSAGE
319         .debugExitMessage(VSP_ID_DEPLOYMENT_FLAVOR_ID, vspId, deploymentFlavorId);
320   }
321
322   @Override
323   public CompositionEntityValidationData updateDeploymentFlavor(
324       DeploymentFlavorEntity deploymentFlavorEntity) {
325     MDC_DATA_DEBUG_MESSAGE.debugEntryMessage("VSP id, deploymentFlavor id", deploymentFlavorEntity
326         .getVspId(), deploymentFlavorEntity.getId());
327
328     if (!vspInfoDao.isManual(deploymentFlavorEntity.getVspId(),
329         deploymentFlavorEntity.getVersion())) {
330       final ErrorCode updateDeploymentFlavorErrorBuilder =
331           NotSupportedHeatOnboardMethodErrorBuilder
332               .getUpdateDfNotSupportedHeatOnboardMethodErrorBuilder();
333       MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
334           LoggerTragetServiceName.UPDATE_DEPLOYMENT_FLAVOR, ErrorLevel.ERROR.name(),
335           LoggerErrorCode.PERMISSION_ERROR.getErrorCode(),
336           updateDeploymentFlavorErrorBuilder.message());
337       throw new CoreException(updateDeploymentFlavorErrorBuilder);
338     }
339     DeploymentFlavorEntity retrieved =
340         getValidatedDeploymentFlavor(deploymentFlavorEntity.getVspId(),
341             deploymentFlavorEntity.getVersion(),
342             deploymentFlavorEntity.getId());
343
344
345     Collection<DeploymentFlavorEntity> listDeploymentFlavors = listDeploymentFlavors(
346             deploymentFlavorEntity.getVspId(), deploymentFlavorEntity.getVersion());
347     listDeploymentFlavors.remove(retrieved);
348     isDeploymentFlavorModelDuplicate(deploymentFlavorEntity, listDeploymentFlavors);
349
350     validateComponentComputeAssociation(deploymentFlavorEntity,
351         deploymentFlavorEntity.getVersion());
352
353     DeploymentFlavorCompositionSchemaInput schemaInput = new
354         DeploymentFlavorCompositionSchemaInput();
355     schemaInput.setManual(vspInfoDao.isManual(deploymentFlavorEntity.getVspId(),
356         deploymentFlavorEntity.getVersion()));
357     schemaInput.setDeploymentFlavor(retrieved.getDeploymentFlavorCompositionData());
358
359     List<String> featureGroups =
360         getFeatureGroupListForVsp(deploymentFlavorEntity.getVspId(),
361             deploymentFlavorEntity.getVersion());
362     schemaInput.setFeatureGroupIds(featureGroups);
363
364     CompositionEntityValidationData validationData = compositionEntityDataManager
365         .validateEntity(deploymentFlavorEntity, SchemaTemplateContext.composition, schemaInput);
366     if (CollectionUtils.isEmpty(validationData.getErrors())) {
367       deploymentFlavorDao.update(deploymentFlavorEntity);
368     }
369
370     MDC_DATA_DEBUG_MESSAGE.debugExitMessage("VSP id, deploymentFlavor id",
371         deploymentFlavorEntity.getVspId(), deploymentFlavorEntity.getId());
372     return validationData;
373   }
374
375 }