2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.sdc.vendorsoftwareproduct.impl;
23 import org.apache.commons.collections4.CollectionUtils;
24 import org.openecomp.core.utilities.json.JsonUtil;
25 import org.openecomp.sdc.common.errors.CoreException;
26 import org.openecomp.sdc.common.errors.ErrorCategory;
27 import org.openecomp.sdc.common.errors.ErrorCode;
28 import org.openecomp.sdc.datatypes.error.ErrorLevel;
29 import org.openecomp.sdc.logging.context.impl.MdcDataErrorMessage;
30 import org.openecomp.sdc.logging.types.LoggerConstants;
31 import org.openecomp.sdc.logging.types.LoggerErrorCode;
32 import org.openecomp.sdc.logging.types.LoggerTragetServiceName;
33 import org.openecomp.sdc.vendorsoftwareproduct.ComponentManager;
34 import org.openecomp.sdc.vendorsoftwareproduct.NicManager;
35 import org.openecomp.sdc.vendorsoftwareproduct.dao.ComponentDao;
36 import org.openecomp.sdc.vendorsoftwareproduct.dao.VendorSoftwareProductInfoDao;
37 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentEntity;
38 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspDetails;
39 import org.openecomp.sdc.vendorsoftwareproduct.errors.CompositionEditNotAllowedErrorBuilder;
40 import org.openecomp.sdc.vendorsoftwareproduct.errors.VendorSoftwareProductErrorCodes;
41 import org.openecomp.sdc.vendorsoftwareproduct.CompositionEntityDataManager;
42 import org.openecomp.sdc.vendorsoftwareproduct.services.schemagenerator.SchemaGenerator;
43 import org.openecomp.sdc.vendorsoftwareproduct.types.CompositionEntityResponse;
44 import org.openecomp.sdc.vendorsoftwareproduct.types.QuestionnaireResponse;
45 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.ComponentData;
46 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.CompositionEntityType;
47 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.CompositionEntityValidationData;
48 import org.openecomp.sdc.vendorsoftwareproduct.types.schemagenerator.ComponentCompositionSchemaInput;
49 import org.openecomp.sdc.vendorsoftwareproduct.types.schemagenerator.ComponentQuestionnaireSchemaInput;
50 import org.openecomp.sdc.vendorsoftwareproduct.types.schemagenerator.SchemaTemplateContext;
51 import org.openecomp.sdc.vendorsoftwareproduct.types.schemagenerator.SchemaTemplateInput;
52 import org.openecomp.sdc.versioning.VersioningUtil;
53 import org.openecomp.sdc.versioning.dao.types.Version;
55 import java.util.Collection;
56 import java.util.List;
58 import java.util.stream.Collectors;
60 import static org.openecomp.sdc.tosca.datatypes.ToscaNodeType.COMPUTE_TYPE_PREFIX;
62 public class ComponentManagerImpl implements ComponentManager {
63 private final ComponentDao componentDao;
64 private final CompositionEntityDataManager compositionEntityDataManager;
65 private final NicManager nicManager;
66 private final VendorSoftwareProductInfoDao vspInfoDao;
68 public ComponentManagerImpl(ComponentDao componentDao,
69 CompositionEntityDataManager compositionEntityDataManager,
70 NicManager nicManager,
71 VendorSoftwareProductInfoDao vspInfoDao) {
72 this.componentDao = componentDao;
73 this.compositionEntityDataManager = compositionEntityDataManager;
74 this.nicManager = nicManager;
75 this.vspInfoDao = vspInfoDao;
79 public Collection<ComponentEntity> listComponents(String vspId, Version version) {
80 return componentDao.list(new ComponentEntity(vspId, version, null));
84 public void deleteComponents(String vspId, Version version) {
85 if (!vspInfoDao.isManual(vspId, version)) {
86 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
87 LoggerTragetServiceName.DELETE_COMPONENT, ErrorLevel.ERROR.name(),
88 LoggerErrorCode.PERMISSION_ERROR.getErrorCode(), "Can't delete component");
89 throw new CoreException(
90 new CompositionEditNotAllowedErrorBuilder(vspId, version).build());
95 public ComponentEntity createComponent(ComponentEntity component) {
96 final String vfcAddNotAllowedInHeatOnboardingMsg =
97 "VFCs cannot be added for VSPs onboarded with HEAT.";
99 ComponentEntity createdComponent;
100 if (!vspInfoDao.isManual(component.getVspId(), component.getVersion())) {
101 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
102 LoggerTragetServiceName.CREATE_COMPONENT, ErrorLevel.ERROR.name(),
103 LoggerErrorCode.PERMISSION_ERROR.getErrorCode(), "Can't create component");
104 throw new CoreException(
105 new ErrorCode.ErrorCodeBuilder().withCategory(ErrorCategory.APPLICATION)
106 .withId(VendorSoftwareProductErrorCodes.VFC_ADD_NOT_ALLOWED_IN_HEAT_ONBOARDING)
107 .withMessage(vfcAddNotAllowedInHeatOnboardingMsg).build());
109 validateComponentManual(component);
110 updateComponentName(component);
111 createdComponent = compositionEntityDataManager.createComponent(component);
113 return createdComponent;
116 private void updateComponentName(ComponentEntity component) {
117 ComponentData data = component.getComponentCompositionData();
118 data.setName(COMPUTE_TYPE_PREFIX + data.getDisplayName());
119 component.setComponentCompositionData(data);
122 private void validateComponentManual(ComponentEntity component) {
123 final String vspVfcCountExceedMsg = "Creation of only one VFC per "
126 final String vspVfcDuplicateNameMsg = "VFC with specified name "
127 + "already present in given VSP.";
129 Collection<ComponentEntity> vspComponentList =
130 listComponents(component.getVspId(), component.getVersion());
131 if (!vspComponentList.isEmpty()) //1707 release only supports 1 VFC in VSP (manual creation)
133 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
134 LoggerTragetServiceName.CREATE_COMPONENT, ErrorLevel.ERROR.name(),
135 LoggerErrorCode.PERMISSION_ERROR.getErrorCode(), "Can't create component: "
136 + "vsp component count exceed");
137 throw new CoreException(
138 new ErrorCode.ErrorCodeBuilder().withCategory(ErrorCategory.APPLICATION)
139 .withId(VendorSoftwareProductErrorCodes.VSP_VFC_COUNT_EXCEED)
140 .withMessage(vspVfcCountExceedMsg).build());
142 if (!isVfcNameUnique(vspComponentList,
143 component.getComponentCompositionData().getDisplayName())) {
144 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
145 LoggerTragetServiceName.CREATE_COMPONENT, ErrorLevel.ERROR.name(),
146 LoggerErrorCode.PERMISSION_ERROR.getErrorCode(), "Can't create component: "
147 + "vsp component duplicate name");
148 throw new CoreException(
149 new ErrorCode.ErrorCodeBuilder().withCategory(ErrorCategory.APPLICATION)
150 .withId(VendorSoftwareProductErrorCodes.VSP_VFC_DUPLICATE_NAME)
151 .withMessage(vspVfcDuplicateNameMsg).build());
155 private boolean isVfcNameUnique(Collection<ComponentEntity> component, String displayName) {
156 for (ComponentEntity comp : component) {
157 if (comp.getComponentCompositionData().getDisplayName().equalsIgnoreCase(displayName)) {
165 public CompositionEntityValidationData updateComponent(ComponentEntity component) {
166 ComponentEntity retrieved =
167 getValidatedComponent(component.getVspId(), component.getVersion(), component.getId());
169 boolean isManual = vspInfoDao.isManual(component.getVspId(), component.getVersion());
171 validateComponentUpdateManual(retrieved);
174 ComponentCompositionSchemaInput schemaInput = new ComponentCompositionSchemaInput();
175 schemaInput.setManual(isManual);
176 schemaInput.setComponent(retrieved.getComponentCompositionData());
178 CompositionEntityValidationData validationData = compositionEntityDataManager
179 .validateEntity(component, SchemaTemplateContext.composition, schemaInput);
180 if (CollectionUtils.isEmpty(validationData.getErrors())) {
182 updateComponentName(component);
184 componentDao.update(component);
186 return validationData;
189 private void validateComponentUpdateManual(ComponentEntity component) {
190 Collection<ComponentEntity> vspComponentList =
191 listComponents(component.getVspId(), component.getVersion());
192 //VFC name should be unique within VSP
193 //Removing VFC with same ID from list to avoid self compare
194 for (ComponentEntity ce : vspComponentList) {
195 if (ce.getId().equals(component.getId())) {
196 vspComponentList.remove(ce);
200 if (!isVfcNameUnique(vspComponentList, component.getComponentCompositionData()
201 .getDisplayName())) {
202 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
203 LoggerTragetServiceName.UPDATE_COMPONENT, ErrorLevel.ERROR.name(),
204 LoggerErrorCode.PERMISSION_ERROR.getErrorCode(), "Component with same name already " +
205 "exists for specified VSP");
206 throw new CoreException(
207 new ErrorCode.ErrorCodeBuilder().withCategory(ErrorCategory.APPLICATION)
208 .withId(VendorSoftwareProductErrorCodes.VSP_VFC_DUPLICATE_NAME)
209 .withMessage("VFC with specified name already present in given VSP.").build());
215 public CompositionEntityResponse<ComponentData> getComponent(String vspId, Version version,
216 String componentId) {
217 ComponentEntity componentEntity = getValidatedComponent(vspId, version, componentId);
218 ComponentData component = componentEntity.getComponentCompositionData();
220 ComponentCompositionSchemaInput schemaInput = new ComponentCompositionSchemaInput();
221 schemaInput.setManual(vspInfoDao.isManual(vspId, version));
222 schemaInput.setComponent(component);
224 CompositionEntityResponse<ComponentData> response = new CompositionEntityResponse<>();
225 response.setId(componentId);
226 response.setData(component);
227 response.setSchema(getComponentCompositionSchema(schemaInput));
232 public void deleteComponent(String vspId, Version version, String componentId) {
233 if (!vspInfoDao.isManual(vspId, version)) {
234 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
235 LoggerTragetServiceName.DELETE_COMPONENT, ErrorLevel.ERROR.name(),
236 LoggerErrorCode.PERMISSION_ERROR.getErrorCode(), "Can't delete component");
237 throw new CoreException(
238 new CompositionEditNotAllowedErrorBuilder(vspId, version).build());
243 public QuestionnaireResponse getQuestionnaire(String vspId, Version version,
244 String componentId) {
245 QuestionnaireResponse questionnaireResponse = new QuestionnaireResponse();
246 ComponentEntity component = componentDao.getQuestionnaireData(vspId, version, componentId);
248 .validateEntityExistence(component, new ComponentEntity(vspId, version, componentId),
249 VspDetails.ENTITY_TYPE);
251 questionnaireResponse.setData(component.getQuestionnaireData());
252 List<String> nicNames = nicManager.listNics(vspId, version, componentId).stream()
253 .map(nic -> nic.getNicCompositionData().getName()).collect(Collectors.toList());
254 questionnaireResponse.setSchema(getComponentQuestionnaireSchema(
255 new ComponentQuestionnaireSchemaInput(nicNames, questionnaireResponse.getData() == null
257 : JsonUtil.json2Object(questionnaireResponse.getData(), Map.class))));
258 return questionnaireResponse;
262 public void updateQuestionnaire(String vspId, Version version, String componentId,
263 String questionnaireData) {
264 validateComponentExistence(vspId, version, componentId);
266 componentDao.updateQuestionnaireData(vspId, version, componentId, questionnaireData);
270 public void validateComponentExistence(String vspId, Version version, String componentId) {
271 getValidatedComponent(vspId, version, componentId);
274 private ComponentEntity getValidatedComponent(String vspId, Version version, String componentId) {
275 ComponentEntity retrieved = componentDao.get(new ComponentEntity(vspId, version, componentId));
277 .validateEntityExistence(retrieved, new ComponentEntity(vspId, version, componentId),
278 VspDetails.ENTITY_TYPE);
282 protected String getComponentCompositionSchema(ComponentCompositionSchemaInput schemaInput) {
283 return SchemaGenerator
284 .generate(SchemaTemplateContext.composition, CompositionEntityType.component, schemaInput);
287 protected String getComponentQuestionnaireSchema(SchemaTemplateInput schemaInput) {
288 return SchemaGenerator
289 .generate(SchemaTemplateContext.questionnaire, CompositionEntityType.component,