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