2 * Copyright © 2016-2017 European Support Limited
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.openecomp.sdc.vendorsoftwareproduct.impl;
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;
48 import java.util.ArrayList;
49 import java.util.Collection;
50 import java.util.Iterator;
51 import java.util.List;
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;
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;
72 public Collection<DeploymentFlavorEntity> listDeploymentFlavors(String vspId, Version version) {
73 return deploymentFlavorDao.list(new DeploymentFlavorEntity(vspId, version, null));
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);
89 validateDeploymentFlavor(deploymentFlavorEntity, deploymentFlavorEntity.getVersion());
90 createDeploymentFlavor =
91 compositionEntityDataManager.createDeploymentFlavor(deploymentFlavorEntity);
93 return createDeploymentFlavor;
96 private void validateDeploymentFlavor(DeploymentFlavorEntity deploymentFlavorEntity,
98 //Validation for unique model.
99 Collection<DeploymentFlavorEntity> listDeploymentFlavors =
100 listDeploymentFlavors(deploymentFlavorEntity.getVspId(),
102 isDeploymentFlavorModelDuplicate(deploymentFlavorEntity, listDeploymentFlavors);
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(),
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);
118 validateComponentComputeAssociation(deploymentFlavorEntity, version);
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);
138 private List<String> getFeatureGroupListForVsp(String vspId, Version version) {
139 final VspDetails vspDetails = vspInfoDao.get(new VspDetails(vspId, version));
140 return vspDetails.getFeatureGroups();
143 private boolean isEmpty(Collection coll) {
144 return coll == null || coll.isEmpty();
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)) {
162 private void validateComponentComputeAssociation(DeploymentFlavorEntity deploymentFlavorEntity,
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);
173 for (Integer vfcCount : frequencyMapping.values()) {
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);
187 private void validateComponentComputeAssocoationList(
188 DeploymentFlavorEntity deploymentFlavorEntity,
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());
211 private void validateComponentComputeAssociationFlavour(
212 DeploymentFlavorEntity deploymentFlavorEntity,
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);
235 public CompositionEntityResponse<DeploymentFlavor> getDeploymentFlavor(String vspId,
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,
253 response.setData(deploymentFlavor);
257 private DeploymentFlavorEntity getValidatedDeploymentFlavor(String vspId, Version version, String
258 deploymentFlavorId) {
259 DeploymentFlavorEntity retrieved = deploymentFlavorDao.get(new DeploymentFlavorEntity(vspId,
260 version, deploymentFlavorId));
262 .validateEntityExistence(retrieved, new DeploymentFlavorEntity(vspId, version,
263 deploymentFlavorId), VspDetails.ENTITY_TYPE);
268 public CompositionEntityResponse<DeploymentFlavor> getDeploymentFlavorSchema(String vspId,
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,
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);
297 if (deploymentFlavorEntity != null) {
298 deploymentFlavorDao.delete(new DeploymentFlavorEntity(vspId, version, deploymentFlavorId));
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);
317 DeploymentFlavorEntity retrieved =
318 getValidatedDeploymentFlavor(deploymentFlavorEntity.getVspId(),
319 deploymentFlavorEntity.getVersion(),
320 deploymentFlavorEntity.getId());
323 Collection<DeploymentFlavorEntity> listDeploymentFlavors = listDeploymentFlavors(
324 deploymentFlavorEntity.getVspId(), deploymentFlavorEntity.getVersion());
325 listDeploymentFlavors.remove(retrieved);
326 isDeploymentFlavorModelDuplicate(deploymentFlavorEntity, listDeploymentFlavors);
328 validateComponentComputeAssociation(deploymentFlavorEntity,
329 deploymentFlavorEntity.getVersion());
331 DeploymentFlavorCompositionSchemaInput schemaInput = new
332 DeploymentFlavorCompositionSchemaInput();
333 schemaInput.setManual(vspInfoDao.isManual(deploymentFlavorEntity.getVspId(),
334 deploymentFlavorEntity.getVersion()));
335 schemaInput.setDeploymentFlavor(retrieved.getDeploymentFlavorCompositionData());
337 List<String> featureGroups =
338 getFeatureGroupListForVsp(deploymentFlavorEntity.getVspId(),
339 deploymentFlavorEntity.getVersion());
340 schemaInput.setFeatureGroupIds(featureGroups);
342 CompositionEntityValidationData validationData = compositionEntityDataManager
343 .validateEntity(deploymentFlavorEntity, SchemaTemplateContext.composition, schemaInput);
344 if (CollectionUtils.isEmpty(validationData.getErrors())) {
345 deploymentFlavorDao.update(deploymentFlavorEntity);
347 return validationData;