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