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