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.services.composition.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;
67 private static final String VSP_ID = "VSP id";
68 private static final String VSP_ID_COMPONENT_ID = "VSP id, component id";
70 public ComponentManagerImpl(ComponentDao componentDao,
71 CompositionEntityDataManager compositionEntityDataManager,
72 NicManager nicManager,
73 VendorSoftwareProductInfoDao vspInfoDao) {
74 this.componentDao = componentDao;
75 this.compositionEntityDataManager = compositionEntityDataManager;
76 this.nicManager = nicManager;
77 this.vspInfoDao = vspInfoDao;
81 public Collection<ComponentEntity> listComponents(String vspId, Version version) {
82 return componentDao.list(new ComponentEntity(vspId, version, null));
86 public void deleteComponents(String vspId, Version version) {
87 if (!vspInfoDao.isManual(vspId, version)) {
88 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
89 LoggerTragetServiceName.DELETE_COMPONENT, ErrorLevel.ERROR.name(),
90 LoggerErrorCode.PERMISSION_ERROR.getErrorCode(), "Can't delete component");
91 throw new CoreException(
92 new CompositionEditNotAllowedErrorBuilder(vspId, version).build());
97 public ComponentEntity createComponent(ComponentEntity component) {
98 final String vfcAddNotAllowedInHeatOnboardingMsg =
99 "VFCs cannot be added for VSPs onboarded with HEAT.";
101 ComponentEntity createdComponent;
102 if (!vspInfoDao.isManual(component.getVspId(), component.getVersion())) {
103 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
104 LoggerTragetServiceName.CREATE_COMPONENT, ErrorLevel.ERROR.name(),
105 LoggerErrorCode.PERMISSION_ERROR.getErrorCode(), "Can't create component");
106 throw new CoreException(
107 new ErrorCode.ErrorCodeBuilder().withCategory(ErrorCategory.APPLICATION)
108 .withId(VendorSoftwareProductErrorCodes.VFC_ADD_NOT_ALLOWED_IN_HEAT_ONBOARDING)
109 .withMessage(vfcAddNotAllowedInHeatOnboardingMsg).build());
111 validateComponentManual(component);
112 updateComponentName(component);
113 createdComponent = compositionEntityDataManager.createComponent(component);
115 return createdComponent;
118 private void updateComponentName(ComponentEntity component) {
119 ComponentData data = component.getComponentCompositionData();
120 data.setName(COMPUTE_TYPE_PREFIX + data.getDisplayName());
121 component.setComponentCompositionData(data);
124 private void validateComponentManual(ComponentEntity component) {
125 final String vspVfcCountExceedMsg = "Creation of only one VFC per "
128 final String vspVfcDuplicateNameMsg = "VFC with specified name "
129 + "already present in given VSP.";
131 Collection<ComponentEntity> vspComponentList =
132 listComponents(component.getVspId(), component.getVersion());
133 if (!vspComponentList.isEmpty()) //1707 release only supports 1 VFC in VSP (manual creation)
135 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
136 LoggerTragetServiceName.CREATE_COMPONENT, ErrorLevel.ERROR.name(),
137 LoggerErrorCode.PERMISSION_ERROR.getErrorCode(), "Can't create component: "
138 + "vsp component count exceed");
139 throw new CoreException(
140 new ErrorCode.ErrorCodeBuilder().withCategory(ErrorCategory.APPLICATION)
141 .withId(VendorSoftwareProductErrorCodes.VSP_VFC_COUNT_EXCEED)
142 .withMessage(vspVfcCountExceedMsg).build());
144 if (!isVfcNameUnique(vspComponentList,
145 component.getComponentCompositionData().getDisplayName())) {
146 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
147 LoggerTragetServiceName.CREATE_COMPONENT, ErrorLevel.ERROR.name(),
148 LoggerErrorCode.PERMISSION_ERROR.getErrorCode(), "Can't create component: "
149 + "vsp component duplicate name");
150 throw new CoreException(
151 new ErrorCode.ErrorCodeBuilder().withCategory(ErrorCategory.APPLICATION)
152 .withId(VendorSoftwareProductErrorCodes.VSP_VFC_DUPLICATE_NAME)
153 .withMessage(vspVfcDuplicateNameMsg).build());
157 private boolean isVfcNameUnique(Collection<ComponentEntity> component, String displayName) {
158 for (ComponentEntity comp : component) {
159 if (comp.getComponentCompositionData().getDisplayName().equalsIgnoreCase(displayName)) {
167 public CompositionEntityValidationData updateComponent(ComponentEntity component) {
168 ComponentEntity retrieved =
169 getValidatedComponent(component.getVspId(), component.getVersion(), component.getId());
171 boolean isManual = vspInfoDao.isManual(component.getVspId(), component.getVersion());
173 validateComponentUpdateManual(retrieved);
176 ComponentCompositionSchemaInput schemaInput = new ComponentCompositionSchemaInput();
177 schemaInput.setManual(isManual);
178 schemaInput.setComponent(retrieved.getComponentCompositionData());
180 CompositionEntityValidationData validationData = compositionEntityDataManager
181 .validateEntity(component, SchemaTemplateContext.composition, schemaInput);
182 if (CollectionUtils.isEmpty(validationData.getErrors())) {
184 updateComponentName(component);
186 componentDao.update(component);
188 return validationData;
191 private void validateComponentUpdateManual(ComponentEntity component) {
192 Collection<ComponentEntity> vspComponentList =
193 listComponents(component.getVspId(), component.getVersion());
194 //VFC name should be unique within VSP
195 //Removing VFC with same ID from list to avoid self compare
196 for (ComponentEntity ce : vspComponentList) {
197 if (ce.getId().equals(component.getId())) {
198 vspComponentList.remove(ce);
202 if (!isVfcNameUnique(vspComponentList, component.getComponentCompositionData()
203 .getDisplayName())) {
204 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
205 LoggerTragetServiceName.UPDATE_COMPONENT, ErrorLevel.ERROR.name(),
206 LoggerErrorCode.PERMISSION_ERROR.getErrorCode(), "Component with same name already " +
207 "exists for specified VSP");
208 throw new CoreException(
209 new ErrorCode.ErrorCodeBuilder().withCategory(ErrorCategory.APPLICATION)
210 .withId(VendorSoftwareProductErrorCodes.VSP_VFC_DUPLICATE_NAME)
211 .withMessage("VFC with specified name already present in given VSP.").build());
217 public CompositionEntityResponse<ComponentData> getComponent(String vspId, Version version,
218 String componentId) {
219 ComponentEntity componentEntity = getValidatedComponent(vspId, version, componentId);
220 ComponentData component = componentEntity.getComponentCompositionData();
222 ComponentCompositionSchemaInput schemaInput = new ComponentCompositionSchemaInput();
223 schemaInput.setManual(vspInfoDao.isManual(vspId, version));
224 schemaInput.setComponent(component);
226 CompositionEntityResponse<ComponentData> response = new CompositionEntityResponse<>();
227 response.setId(componentId);
228 response.setData(component);
229 response.setSchema(getComponentCompositionSchema(schemaInput));
234 public void deleteComponent(String vspId, Version version, String componentId) {
235 if (!vspInfoDao.isManual(vspId, version)) {
236 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
237 LoggerTragetServiceName.DELETE_COMPONENT, ErrorLevel.ERROR.name(),
238 LoggerErrorCode.PERMISSION_ERROR.getErrorCode(), "Can't delete component");
239 throw new CoreException(
240 new CompositionEditNotAllowedErrorBuilder(vspId, version).build());
245 public QuestionnaireResponse getQuestionnaire(String vspId, Version version,
246 String componentId) {
247 QuestionnaireResponse questionnaireResponse = new QuestionnaireResponse();
248 ComponentEntity component = componentDao.getQuestionnaireData(vspId, version, componentId);
250 .validateEntityExistence(component, new ComponentEntity(vspId, version, componentId),
251 VspDetails.ENTITY_TYPE);
253 questionnaireResponse.setData(component.getQuestionnaireData());
254 List<String> nicNames = nicManager.listNics(vspId, version, componentId).stream()
255 .map(nic -> nic.getNicCompositionData().getName()).collect(Collectors.toList());
256 questionnaireResponse.setSchema(getComponentQuestionnaireSchema(
257 new ComponentQuestionnaireSchemaInput(nicNames, questionnaireResponse.getData() == null
259 : JsonUtil.json2Object(questionnaireResponse.getData(), Map.class))));
260 return questionnaireResponse;
264 public void updateQuestionnaire(String vspId, Version version, String componentId,
265 String questionnaireData) {
266 validateComponentExistence(vspId, version, componentId);
268 componentDao.updateQuestionnaireData(vspId, version, componentId, questionnaireData);
272 public void validateComponentExistence(String vspId, Version version, String componentId) {
273 getValidatedComponent(vspId, version, componentId);
276 private ComponentEntity getValidatedComponent(String vspId, Version version, String componentId) {
277 ComponentEntity retrieved = componentDao.get(new ComponentEntity(vspId, version, componentId));
279 .validateEntityExistence(retrieved, new ComponentEntity(vspId, version, componentId),
280 VspDetails.ENTITY_TYPE);
284 protected String getComponentCompositionSchema(ComponentCompositionSchemaInput schemaInput) {
285 return SchemaGenerator
286 .generate(SchemaTemplateContext.composition, CompositionEntityType.component, schemaInput);
289 protected String getComponentQuestionnaireSchema(SchemaTemplateInput schemaInput) {
290 return SchemaGenerator
291 .generate(SchemaTemplateContext.questionnaire, CompositionEntityType.component,