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.MdcDataDebugMessage;
30 import org.openecomp.sdc.logging.context.impl.MdcDataErrorMessage;
31 import org.openecomp.sdc.logging.types.LoggerConstants;
32 import org.openecomp.sdc.logging.types.LoggerErrorCode;
33 import org.openecomp.sdc.logging.types.LoggerTragetServiceName;
34 import org.openecomp.sdc.vendorsoftwareproduct.ComponentManager;
35 import org.openecomp.sdc.vendorsoftwareproduct.NicManager;
36 import org.openecomp.sdc.vendorsoftwareproduct.dao.ComponentDao;
37 import org.openecomp.sdc.vendorsoftwareproduct.dao.VendorSoftwareProductInfoDao;
38 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentEntity;
39 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspDetails;
40 import org.openecomp.sdc.vendorsoftwareproduct.errors.CompositionEditNotAllowedErrorBuilder;
41 import org.openecomp.sdc.vendorsoftwareproduct.errors.VendorSoftwareProductErrorCodes;
42 import org.openecomp.sdc.vendorsoftwareproduct.services.composition.CompositionEntityDataManager;
43 import org.openecomp.sdc.vendorsoftwareproduct.services.schemagenerator.SchemaGenerator;
44 import org.openecomp.sdc.vendorsoftwareproduct.types.CompositionEntityResponse;
45 import org.openecomp.sdc.vendorsoftwareproduct.types.QuestionnaireResponse;
46 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.ComponentData;
47 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.CompositionEntityType;
48 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.CompositionEntityValidationData;
49 import org.openecomp.sdc.vendorsoftwareproduct.types.schemagenerator.ComponentCompositionSchemaInput;
50 import org.openecomp.sdc.vendorsoftwareproduct.types.schemagenerator.ComponentQuestionnaireSchemaInput;
51 import org.openecomp.sdc.vendorsoftwareproduct.types.schemagenerator.SchemaTemplateContext;
52 import org.openecomp.sdc.vendorsoftwareproduct.types.schemagenerator.SchemaTemplateInput;
53 import org.openecomp.sdc.versioning.VersioningUtil;
54 import org.openecomp.sdc.versioning.dao.types.Version;
56 import java.util.Collection;
57 import java.util.List;
59 import java.util.stream.Collectors;
61 import static org.openecomp.sdc.tosca.datatypes.ToscaNodeType.COMPUTE_TYPE_PREFIX;
63 public class ComponentManagerImpl implements ComponentManager {
64 private static final MdcDataDebugMessage mdcDataDebugMessage = new MdcDataDebugMessage();
65 private final ComponentDao componentDao;
66 private final CompositionEntityDataManager compositionEntityDataManager;
67 private final NicManager nicManager;
68 private final VendorSoftwareProductInfoDao vspInfoDao;
69 private static final String VSP_ID = "VSP id";
70 private static final String VSP_ID_COMPONENT_ID = "VSP id, component id";
72 public ComponentManagerImpl(ComponentDao componentDao,
73 CompositionEntityDataManager compositionEntityDataManager,
74 NicManager nicManager,
75 VendorSoftwareProductInfoDao vspInfoDao) {
76 this.componentDao = componentDao;
77 this.compositionEntityDataManager = compositionEntityDataManager;
78 this.nicManager = nicManager;
79 this.vspInfoDao = vspInfoDao;
83 public Collection<ComponentEntity> listComponents(String vspId, Version version) {
84 mdcDataDebugMessage.debugEntryMessage(VSP_ID, vspId);
85 mdcDataDebugMessage.debugExitMessage(VSP_ID, vspId);
86 return componentDao.list(new ComponentEntity(vspId, version, null));
90 public void deleteComponents(String vspId, Version version) {
91 mdcDataDebugMessage.debugEntryMessage(VSP_ID, vspId);
92 if (!vspInfoDao.isManual(vspId, version)) {
93 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
94 LoggerTragetServiceName.DELETE_COMPONENT, ErrorLevel.ERROR.name(),
95 LoggerErrorCode.PERMISSION_ERROR.getErrorCode(), "Can't delete component");
96 throw new CoreException(
97 new CompositionEditNotAllowedErrorBuilder(vspId, version).build());
100 mdcDataDebugMessage.debugExitMessage(VSP_ID, vspId);
104 public ComponentEntity createComponent(ComponentEntity component) {
105 mdcDataDebugMessage.debugEntryMessage(VSP_ID, component.getId());
107 final String vfcAddNotAllowedInHeatOnboardingMsg =
108 "VFCs cannot be added for VSPs onboarded with HEAT.";
110 ComponentEntity createdComponent;
111 if (!vspInfoDao.isManual(component.getVspId(), component.getVersion())) {
112 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
113 LoggerTragetServiceName.CREATE_COMPONENT, ErrorLevel.ERROR.name(),
114 LoggerErrorCode.PERMISSION_ERROR.getErrorCode(), "Can't create component");
115 throw new CoreException(
116 new ErrorCode.ErrorCodeBuilder().withCategory(ErrorCategory.APPLICATION)
117 .withId(VendorSoftwareProductErrorCodes.VFC_ADD_NOT_ALLOWED_IN_HEAT_ONBOARDING)
118 .withMessage(vfcAddNotAllowedInHeatOnboardingMsg).build());
120 validateComponentManual(component);
121 updateComponentName(component);
122 createdComponent = compositionEntityDataManager.createComponent(component);
125 mdcDataDebugMessage.debugExitMessage(VSP_ID, component.getId());
127 return createdComponent;
130 private void updateComponentName(ComponentEntity component) {
131 ComponentData data = component.getComponentCompositionData();
132 data.setName(COMPUTE_TYPE_PREFIX + data.getDisplayName());
133 component.setComponentCompositionData(data);
136 private void validateComponentManual(ComponentEntity component) {
137 final String vspVfcCountExceedMsg = "Creation of only one VFC per "
140 final String vspVfcDuplicateNameMsg = "VFC with specified name "
141 + "already present in given VSP.";
143 Collection<ComponentEntity> vspComponentList =
144 listComponents(component.getVspId(), component.getVersion());
145 if (!vspComponentList.isEmpty()) //1707 release only supports 1 VFC in VSP (manual creation)
147 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
148 LoggerTragetServiceName.CREATE_COMPONENT, ErrorLevel.ERROR.name(),
149 LoggerErrorCode.PERMISSION_ERROR.getErrorCode(), "Can't create component: "
150 + "vsp component count exceed");
151 throw new CoreException(
152 new ErrorCode.ErrorCodeBuilder().withCategory(ErrorCategory.APPLICATION)
153 .withId(VendorSoftwareProductErrorCodes.VSP_VFC_COUNT_EXCEED)
154 .withMessage(vspVfcCountExceedMsg).build());
156 if (!isVfcNameUnique(vspComponentList,
157 component.getComponentCompositionData().getDisplayName())) {
158 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
159 LoggerTragetServiceName.CREATE_COMPONENT, ErrorLevel.ERROR.name(),
160 LoggerErrorCode.PERMISSION_ERROR.getErrorCode(), "Can't create component: "
161 + "vsp component duplicate name");
162 throw new CoreException(
163 new ErrorCode.ErrorCodeBuilder().withCategory(ErrorCategory.APPLICATION)
164 .withId(VendorSoftwareProductErrorCodes.VSP_VFC_DUPLICATE_NAME)
165 .withMessage(vspVfcDuplicateNameMsg).build());
169 private boolean isVfcNameUnique(Collection<ComponentEntity> component, String displayName) {
170 for (ComponentEntity comp : component) {
171 if (comp.getComponentCompositionData().getDisplayName().equalsIgnoreCase(displayName)) {
179 public CompositionEntityValidationData updateComponent(ComponentEntity component) {
180 mdcDataDebugMessage.debugEntryMessage(VSP_ID_COMPONENT_ID, component
181 .getVspId(), component.getId());
182 ComponentEntity retrieved =
183 getValidatedComponent(component.getVspId(), component.getVersion(), component.getId());
185 boolean isManual = vspInfoDao.isManual(component.getVspId(), component.getVersion());
187 validateComponentUpdateManual(retrieved);
190 ComponentCompositionSchemaInput schemaInput = new ComponentCompositionSchemaInput();
191 schemaInput.setManual(isManual);
192 schemaInput.setComponent(retrieved.getComponentCompositionData());
194 CompositionEntityValidationData validationData = compositionEntityDataManager
195 .validateEntity(component, SchemaTemplateContext.composition, schemaInput);
196 if (CollectionUtils.isEmpty(validationData.getErrors())) {
198 updateComponentName(component);
200 componentDao.update(component);
202 mdcDataDebugMessage.debugExitMessage(VSP_ID_COMPONENT_ID, component.getVspId(),
205 return validationData;
208 private void validateComponentUpdateManual(ComponentEntity component) {
209 Collection<ComponentEntity> vspComponentList =
210 listComponents(component.getVspId(), component.getVersion());
211 //VFC name should be unique within VSP
212 //Removing VFC with same ID from list to avoid self compare
213 for (ComponentEntity ce : vspComponentList) {
214 if (ce.getId().equals(component.getId())) {
215 vspComponentList.remove(ce);
219 if (!isVfcNameUnique(vspComponentList, component.getComponentCompositionData()
220 .getDisplayName())) {
221 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
222 LoggerTragetServiceName.UPDATE_COMPONENT, ErrorLevel.ERROR.name(),
223 LoggerErrorCode.PERMISSION_ERROR.getErrorCode(), "Component with same name already " +
224 "exists for specified VSP");
225 throw new CoreException(
226 new ErrorCode.ErrorCodeBuilder().withCategory(ErrorCategory.APPLICATION)
227 .withId(VendorSoftwareProductErrorCodes.VSP_VFC_DUPLICATE_NAME)
228 .withMessage("VFC with specified name already present in given VSP.").build());
234 public CompositionEntityResponse<ComponentData> getComponent(String vspId, Version version,
235 String componentId) {
236 mdcDataDebugMessage.debugEntryMessage(VSP_ID_COMPONENT_ID, vspId, componentId);
237 ComponentEntity componentEntity = getValidatedComponent(vspId, version, componentId);
238 ComponentData component = componentEntity.getComponentCompositionData();
240 ComponentCompositionSchemaInput schemaInput = new ComponentCompositionSchemaInput();
241 schemaInput.setManual(vspInfoDao.isManual(vspId, version));
242 schemaInput.setComponent(component);
244 CompositionEntityResponse<ComponentData> response = new CompositionEntityResponse<>();
245 response.setId(componentId);
246 response.setData(component);
247 response.setSchema(getComponentCompositionSchema(schemaInput));
248 mdcDataDebugMessage.debugExitMessage(VSP_ID_COMPONENT_ID, vspId, componentId);
254 public void deleteComponent(String vspId, Version version, String componentId) {
255 mdcDataDebugMessage.debugEntryMessage(VSP_ID_COMPONENT_ID, vspId, componentId);
257 if (!vspInfoDao.isManual(vspId, version)) {
258 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
259 LoggerTragetServiceName.DELETE_COMPONENT, ErrorLevel.ERROR.name(),
260 LoggerErrorCode.PERMISSION_ERROR.getErrorCode(), "Can't delete component");
261 throw new CoreException(
262 new CompositionEditNotAllowedErrorBuilder(vspId, version).build());
265 mdcDataDebugMessage.debugExitMessage(VSP_ID_COMPONENT_ID, vspId, componentId);
269 public QuestionnaireResponse getQuestionnaire(String vspId, Version version,
270 String componentId) {
271 mdcDataDebugMessage.debugEntryMessage(VSP_ID_COMPONENT_ID, vspId, componentId);
273 QuestionnaireResponse questionnaireResponse = new QuestionnaireResponse();
274 ComponentEntity component = componentDao.getQuestionnaireData(vspId, version, componentId);
276 .validateEntityExistence(component, new ComponentEntity(vspId, version, componentId),
277 VspDetails.ENTITY_TYPE);
279 questionnaireResponse.setData(component.getQuestionnaireData());
280 List<String> nicNames = nicManager.listNics(vspId, version, componentId).stream()
281 .map(nic -> nic.getNicCompositionData().getName()).collect(Collectors.toList());
282 questionnaireResponse.setSchema(getComponentQuestionnaireSchema(
283 new ComponentQuestionnaireSchemaInput(nicNames, questionnaireResponse.getData() == null
285 : JsonUtil.json2Object(questionnaireResponse.getData(), Map.class))));
287 mdcDataDebugMessage.debugExitMessage(VSP_ID_COMPONENT_ID, vspId, componentId);
288 return questionnaireResponse;
292 public void updateQuestionnaire(String vspId, Version version, String componentId,
293 String questionnaireData) {
294 mdcDataDebugMessage.debugEntryMessage(VSP_ID_COMPONENT_ID, vspId, componentId);
295 validateComponentExistence(vspId, version, componentId);
297 componentDao.updateQuestionnaireData(vspId, version, componentId, questionnaireData);
299 mdcDataDebugMessage.debugExitMessage(VSP_ID_COMPONENT_ID, vspId, componentId);
303 public void validateComponentExistence(String vspId, Version version, String componentId) {
304 getValidatedComponent(vspId, version, componentId);
307 private ComponentEntity getValidatedComponent(String vspId, Version version, String componentId) {
308 ComponentEntity retrieved = componentDao.get(new ComponentEntity(vspId, version, componentId));
310 .validateEntityExistence(retrieved, new ComponentEntity(vspId, version, componentId),
311 VspDetails.ENTITY_TYPE);
315 protected String getComponentCompositionSchema(ComponentCompositionSchemaInput schemaInput) {
316 return SchemaGenerator
317 .generate(SchemaTemplateContext.composition, CompositionEntityType.component, schemaInput);
320 protected String getComponentQuestionnaireSchema(SchemaTemplateInput schemaInput) {
321 return SchemaGenerator
322 .generate(SchemaTemplateContext.questionnaire, CompositionEntityType.component,