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.core.utilities.json.JsonUtil;
21 import org.openecomp.sdc.common.errors.CoreException;
22 import org.openecomp.sdc.common.errors.ErrorCode;
23 import org.openecomp.sdc.vendorsoftwareproduct.ComponentManager;
24 import org.openecomp.sdc.vendorsoftwareproduct.CompositionEntityDataManager;
25 import org.openecomp.sdc.vendorsoftwareproduct.NicManager;
26 import org.openecomp.sdc.vendorsoftwareproduct.dao.ComponentDao;
27 import org.openecomp.sdc.vendorsoftwareproduct.dao.VendorSoftwareProductInfoDao;
28 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentEntity;
29 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspDetails;
30 import org.openecomp.sdc.vendorsoftwareproduct.errors.CompositionEditNotAllowedErrorBuilder;
31 import org.openecomp.sdc.vendorsoftwareproduct.errors.VendorSoftwareProductErrorCodes;
32 import org.openecomp.sdc.vendorsoftwareproduct.services.schemagenerator.SchemaGenerator;
33 import org.openecomp.sdc.vendorsoftwareproduct.types.CompositionEntityResponse;
34 import org.openecomp.sdc.vendorsoftwareproduct.types.QuestionnaireResponse;
35 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.ComponentData;
36 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.CompositionEntityType;
37 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.CompositionEntityValidationData;
38 import org.openecomp.sdc.vendorsoftwareproduct.types.schemagenerator.ComponentCompositionSchemaInput;
39 import org.openecomp.sdc.vendorsoftwareproduct.types.schemagenerator.ComponentQuestionnaireSchemaInput;
40 import org.openecomp.sdc.vendorsoftwareproduct.types.schemagenerator.SchemaTemplateContext;
41 import org.openecomp.sdc.vendorsoftwareproduct.types.schemagenerator.SchemaTemplateInput;
42 import org.openecomp.sdc.versioning.VersioningUtil;
43 import org.openecomp.sdc.versioning.dao.types.Version;
45 import java.util.Collection;
46 import java.util.List;
48 import java.util.stream.Collectors;
50 import static org.openecomp.sdc.tosca.datatypes.ToscaNodeType.COMPUTE_TYPE_PREFIX;
52 public class ComponentManagerImpl implements ComponentManager {
53 private final ComponentDao componentDao;
54 private final CompositionEntityDataManager compositionEntityDataManager;
55 private final NicManager nicManager;
56 private final VendorSoftwareProductInfoDao vspInfoDao;
58 public ComponentManagerImpl(ComponentDao componentDao,
59 CompositionEntityDataManager compositionEntityDataManager,
60 NicManager nicManager,
61 VendorSoftwareProductInfoDao vspInfoDao) {
62 this.componentDao = componentDao;
63 this.compositionEntityDataManager = compositionEntityDataManager;
64 this.nicManager = nicManager;
65 this.vspInfoDao = vspInfoDao;
69 public Collection<ComponentEntity> listComponents(String vspId, Version version) {
70 return componentDao.list(new ComponentEntity(vspId, version, null));
74 public void deleteComponents(String vspId, Version version) {
75 if (!vspInfoDao.isManual(vspId, version)) {
76 throw new CoreException(
77 new CompositionEditNotAllowedErrorBuilder(vspId, version).build());
82 public ComponentEntity createComponent(ComponentEntity component) {
83 final String vfcAddNotAllowedInHeatOnboardingMsg =
84 "VFCs cannot be added for VSPs onboarded with HEAT.";
86 ComponentEntity createdComponent;
87 if (!vspInfoDao.isManual(component.getVspId(), component.getVersion())) {
88 throw new CoreException(
89 new ErrorCode.ErrorCodeBuilder()
90 .withId(VendorSoftwareProductErrorCodes.VFC_ADD_NOT_ALLOWED_IN_HEAT_ONBOARDING)
91 .withMessage(vfcAddNotAllowedInHeatOnboardingMsg).build());
93 validateComponentManual(component);
94 updateComponentName(component);
95 createdComponent = compositionEntityDataManager.createComponent(component);
97 return createdComponent;
100 private void updateComponentName(ComponentEntity component) {
101 ComponentData data = component.getComponentCompositionData();
102 data.setName(COMPUTE_TYPE_PREFIX + data.getDisplayName());
103 component.setComponentCompositionData(data);
106 private void validateComponentManual(ComponentEntity component) {
107 final String vspVfcCountExceedMsg = "Creation of only one VFC per "
110 final String vspVfcDuplicateNameMsg = "VFC with specified name "
111 + "already present in given VSP.";
113 Collection<ComponentEntity> vspComponentList =
114 listComponents(component.getVspId(), component.getVersion());
115 if (!vspComponentList.isEmpty()) {
116 //1707 release only supports 1 VFC in VSP (manual creation)
117 throw new CoreException(
118 new ErrorCode.ErrorCodeBuilder()
119 .withId(VendorSoftwareProductErrorCodes.VSP_VFC_COUNT_EXCEED)
120 .withMessage(vspVfcCountExceedMsg).build());
122 if (!isVfcNameUnique(vspComponentList,
123 component.getComponentCompositionData().getDisplayName())) {
124 throw new CoreException(
125 new ErrorCode.ErrorCodeBuilder()
126 .withId(VendorSoftwareProductErrorCodes.VSP_VFC_DUPLICATE_NAME)
127 .withMessage(vspVfcDuplicateNameMsg).build());
131 private boolean isVfcNameUnique(Collection<ComponentEntity> component, String displayName) {
132 for (ComponentEntity comp : component) {
133 if (comp.getComponentCompositionData().getDisplayName().equalsIgnoreCase(displayName)) {
141 public CompositionEntityValidationData updateComponent(ComponentEntity component) {
142 ComponentEntity retrieved =
143 getValidatedComponent(component.getVspId(), component.getVersion(), component.getId());
145 boolean isManual = vspInfoDao.isManual(component.getVspId(), component.getVersion());
147 validateComponentUpdateManual(retrieved);
150 ComponentCompositionSchemaInput schemaInput = new ComponentCompositionSchemaInput();
151 schemaInput.setManual(isManual);
152 schemaInput.setComponent(retrieved.getComponentCompositionData());
154 CompositionEntityValidationData validationData = compositionEntityDataManager
155 .validateEntity(component, SchemaTemplateContext.composition, schemaInput);
156 if (CollectionUtils.isEmpty(validationData.getErrors())) {
158 updateComponentName(component);
160 componentDao.update(component);
162 return validationData;
165 private void validateComponentUpdateManual(ComponentEntity component) {
166 Collection<ComponentEntity> vspComponentList =
167 listComponents(component.getVspId(), component.getVersion());
168 //VFC name should be unique within VSP
169 //Removing VFC with same ID from list to avoid self compare
170 for (ComponentEntity ce : vspComponentList) {
171 if (ce.getId().equals(component.getId())) {
172 vspComponentList.remove(ce);
176 if (!isVfcNameUnique(vspComponentList, component.getComponentCompositionData()
177 .getDisplayName())) {
178 throw new CoreException(
179 new ErrorCode.ErrorCodeBuilder()
180 .withId(VendorSoftwareProductErrorCodes.VSP_VFC_DUPLICATE_NAME)
181 .withMessage("VFC with specified name already present in given VSP.").build());
187 public CompositionEntityResponse<ComponentData> getComponent(String vspId, Version version,
188 String componentId) {
189 ComponentEntity componentEntity = getValidatedComponent(vspId, version, componentId);
190 ComponentData component = componentEntity.getComponentCompositionData();
192 ComponentCompositionSchemaInput schemaInput = new ComponentCompositionSchemaInput();
193 schemaInput.setManual(vspInfoDao.isManual(vspId, version));
194 schemaInput.setComponent(component);
196 CompositionEntityResponse<ComponentData> response = new CompositionEntityResponse<>();
197 response.setId(componentId);
198 response.setData(component);
199 response.setSchema(getComponentCompositionSchema(schemaInput));
204 public void deleteComponent(String vspId, Version version, String componentId) {
205 if (!vspInfoDao.isManual(vspId, version)) {
206 throw new CoreException(
207 new CompositionEditNotAllowedErrorBuilder(vspId, version).build());
212 public QuestionnaireResponse getQuestionnaire(String vspId, Version version,
213 String componentId) {
214 QuestionnaireResponse questionnaireResponse = new QuestionnaireResponse();
215 ComponentEntity component = componentDao.getQuestionnaireData(vspId, version, componentId);
217 .validateEntityExistence(component, new ComponentEntity(vspId, version, componentId),
218 VspDetails.ENTITY_TYPE);
220 questionnaireResponse.setData(component.getQuestionnaireData());
221 List<String> nicNames = nicManager.listNics(vspId, version, componentId).stream()
222 .map(nic -> nic.getNicCompositionData().getName()).collect(Collectors.toList());
223 questionnaireResponse.setSchema(getComponentQuestionnaireSchema(
224 new ComponentQuestionnaireSchemaInput(nicNames, questionnaireResponse.getData() == null
226 : JsonUtil.json2Object(questionnaireResponse.getData(), Map.class))));
227 return questionnaireResponse;
231 public void updateQuestionnaire(String vspId, Version version, String componentId,
232 String questionnaireData) {
233 validateComponentExistence(vspId, version, componentId);
235 componentDao.updateQuestionnaireData(vspId, version, componentId, questionnaireData);
239 public void validateComponentExistence(String vspId, Version version, String componentId) {
240 getValidatedComponent(vspId, version, componentId);
243 private ComponentEntity getValidatedComponent(String vspId, Version version, String componentId) {
244 ComponentEntity retrieved = componentDao.get(new ComponentEntity(vspId, version, componentId));
246 .validateEntityExistence(retrieved, new ComponentEntity(vspId, version, componentId),
247 VspDetails.ENTITY_TYPE);
251 protected String getComponentCompositionSchema(ComponentCompositionSchemaInput schemaInput) {
252 return SchemaGenerator
253 .generate(SchemaTemplateContext.composition, CompositionEntityType.component, schemaInput);
256 protected String getComponentQuestionnaireSchema(SchemaTemplateInput schemaInput) {
257 return SchemaGenerator
258 .generate(SchemaTemplateContext.questionnaire, CompositionEntityType.component,