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