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 ComponentDao componentDao;
66 private CompositionEntityDataManager compositionEntityDataManager;
67 private NicManager nicManager;
68 private VendorSoftwareProductInfoDao vspInfoDao;
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 mdcDataDebugMessage.debugEntryMessage("VSP id", vspId);
83 mdcDataDebugMessage.debugExitMessage("VSP id", vspId);
84 return componentDao.list(new ComponentEntity(vspId, version, null));
88 public void deleteComponents(String vspId, Version version) {
89 mdcDataDebugMessage.debugEntryMessage("VSP id", vspId);
90 if (!vspInfoDao.isManual(vspId, version)) {
91 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
92 LoggerTragetServiceName.DELETE_COMPONENT, ErrorLevel.ERROR.name(),
93 LoggerErrorCode.PERMISSION_ERROR.getErrorCode(), "Can't delete component");
94 throw new CoreException(
95 new CompositionEditNotAllowedErrorBuilder(vspId, version).build());
98 mdcDataDebugMessage.debugExitMessage("VSP id", vspId);
102 public ComponentEntity createComponent(ComponentEntity component) {
103 mdcDataDebugMessage.debugEntryMessage("VSP id", component.getId());
105 if (!isManual(component.getVspId(), component.getVersion())) {
106 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
107 LoggerTragetServiceName.CREATE_COMPONENT, ErrorLevel.ERROR.name(),
108 LoggerErrorCode.PERMISSION_ERROR.getErrorCode(), "Can't create component");
109 throw new CoreException(
110 new CompositionEditNotAllowedErrorBuilder(component.getVspId(), component.getVersion())
114 mdcDataDebugMessage.debugExitMessage("VSP id", component.getId());
119 public ComponentEntity createComponent(ComponentEntity component) {
120 mdcDataDebugMessage.debugEntryMessage("VSP id", component.getId());
122 final String VFC_ADD_NOT_ALLOWED_IN_HEAT_ONBOARDING_MSG =
123 "VFCs cannot be added for VSPs onboarded with HEAT.";
125 ComponentEntity createdComponent;
126 if (!vspInfoDao.isManual(component.getVspId(), component.getVersion())) {
127 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
128 LoggerTragetServiceName.CREATE_COMPONENT, ErrorLevel.ERROR.name(),
129 LoggerErrorCode.PERMISSION_ERROR.getErrorCode(), "Can't create component");
130 throw new CoreException(
131 new ErrorCode.ErrorCodeBuilder().withCategory(ErrorCategory.APPLICATION)
132 .withId(VendorSoftwareProductErrorCodes.VFC_ADD_NOT_ALLOWED_IN_HEAT_ONBOARDING)
133 .withMessage(VFC_ADD_NOT_ALLOWED_IN_HEAT_ONBOARDING_MSG).build());
135 validateComponentManual(component);
136 updateComponentName(component);
137 createdComponent = compositionEntityDataManager.createComponent(component);
140 mdcDataDebugMessage.debugExitMessage("VSP id", component.getId());
142 return createdComponent;
145 private void updateComponentName(ComponentEntity component) {
146 ComponentData data = component.getComponentCompositionData();
147 data.setName(COMPUTE_TYPE_PREFIX + data.getDisplayName());
148 component.setComponentCompositionData(data);
151 private void validateComponentManual(ComponentEntity component) {
152 final String VSP_VFC_COUNT_EXCEED_MSG = "Creation of only one VFC per "
155 final String VSP_VFC_DUPLICATE_NAME_MSG = "VFC with specified name "
156 + "already present in given VSP.";
158 Collection<ComponentEntity> vspComponentList =
159 listComponents(component.getVspId(), component.getVersion());
160 if (vspComponentList.size() >= 1) //1707 release only supports 1 VFC in VSP (manual creation)
162 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
163 LoggerTragetServiceName.CREATE_COMPONENT, ErrorLevel.ERROR.name(),
164 LoggerErrorCode.PERMISSION_ERROR.getErrorCode(), "Can't create component: "
165 + "vsp component count exceed");
166 throw new CoreException(
167 new ErrorCode.ErrorCodeBuilder().withCategory(ErrorCategory.APPLICATION)
168 .withId(VendorSoftwareProductErrorCodes.VSP_VFC_COUNT_EXCEED)
169 .withMessage(VSP_VFC_COUNT_EXCEED_MSG).build());
171 if (!isVfcNameUnique(vspComponentList,
172 component.getComponentCompositionData().getDisplayName())) {
173 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
174 LoggerTragetServiceName.CREATE_COMPONENT, ErrorLevel.ERROR.name(),
175 LoggerErrorCode.PERMISSION_ERROR.getErrorCode(), "Can't create component: "
176 + "vsp component duplicate name");
177 throw new CoreException(
178 new ErrorCode.ErrorCodeBuilder().withCategory(ErrorCategory.APPLICATION)
179 .withId(VendorSoftwareProductErrorCodes.VSP_VFC_DUPLICATE_NAME)
180 .withMessage(VSP_VFC_DUPLICATE_NAME_MSG).build());
184 private boolean isVfcNameUnique(Collection<ComponentEntity> component, String displayName) {
185 for (ComponentEntity comp : component) {
186 if (comp.getComponentCompositionData().getDisplayName().equalsIgnoreCase(displayName)) {
194 public CompositionEntityValidationData updateComponent(ComponentEntity component) {
195 mdcDataDebugMessage.debugEntryMessage("VSP id, component id", component
196 .getVspId(), component.getId());
197 ComponentEntity retrieved =
198 getValidatedComponent(component.getVspId(), component.getVersion(), component.getId());
200 boolean isManual = vspInfoDao.isManual(component.getVspId(), component.getVersion());
202 validateComponentUpdateManual(retrieved);
205 ComponentCompositionSchemaInput schemaInput = new ComponentCompositionSchemaInput();
206 schemaInput.setManual(isManual);
207 schemaInput.setComponent(retrieved.getComponentCompositionData());
209 CompositionEntityValidationData validationData = compositionEntityDataManager
210 .validateEntity(component, SchemaTemplateContext.composition, schemaInput);
211 if (CollectionUtils.isEmpty(validationData.getErrors())) {
213 updateComponentName(component);
215 componentDao.update(component);
217 mdcDataDebugMessage.debugExitMessage("VSP id, component id", component.getVspId(),
220 return validationData;
223 private void validateComponentUpdateManual(ComponentEntity component) {
224 Collection<ComponentEntity> vspComponentList =
225 listComponents(component.getVspId(), component.getVersion());
226 //VFC name should be unique within VSP
227 //Removing VFC with same ID from list to avoid self compare
228 for (ComponentEntity ce : vspComponentList) {
229 if (ce.getId().equals(component.getId())) {
230 vspComponentList.remove(ce);
234 if (!isVfcNameUnique(vspComponentList, component.getComponentCompositionData()
235 .getDisplayName())) {
236 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
237 LoggerTragetServiceName.UPDATE_COMPONENT, ErrorLevel.ERROR.name(),
238 LoggerErrorCode.PERMISSION_ERROR.getErrorCode(), "Component with same name already " +
239 "exists for specified VSP");
240 throw new CoreException(
241 new ErrorCode.ErrorCodeBuilder().withCategory(ErrorCategory.APPLICATION)
242 .withId(VendorSoftwareProductErrorCodes.VSP_VFC_DUPLICATE_NAME)
243 .withMessage("VFC with specified name already present in given VSP.").build());
248 public CompositionEntityResponse<ComponentData> getComponent(String vspId, Version version,
249 String componentId) {
250 mdcDataDebugMessage.debugEntryMessage("VSP id, component id", vspId, componentId);
251 ComponentEntity componentEntity = getValidatedComponent(vspId, version, componentId);
252 ComponentData component = componentEntity.getComponentCompositionData();
254 ComponentCompositionSchemaInput schemaInput = new ComponentCompositionSchemaInput();
255 schemaInput.setManual(vspInfoDao.isManual(vspId, version));
256 schemaInput.setComponent(component);
258 CompositionEntityResponse<ComponentData> response = new CompositionEntityResponse<>();
259 response.setId(componentId);
260 response.setData(component);
261 response.setSchema(getComponentCompositionSchema(schemaInput));
262 mdcDataDebugMessage.debugExitMessage("VSP id, component id", vspId, componentId);
268 public void deleteComponent(String vspId, Version version, String componentId) {
269 mdcDataDebugMessage.debugEntryMessage("VSP id, component id", vspId, componentId);
271 if (!vspInfoDao.isManual(vspId, version)) {
272 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
273 LoggerTragetServiceName.DELETE_COMPONENT, ErrorLevel.ERROR.name(),
274 LoggerErrorCode.PERMISSION_ERROR.getErrorCode(), "Can't delete component");
275 throw new CoreException(
276 new CompositionEditNotAllowedErrorBuilder(vspId, version).build());
279 mdcDataDebugMessage.debugExitMessage("VSP id, component id", vspId, componentId);
283 public QuestionnaireResponse getQuestionnaire(String vspId, Version version,
284 String componentId) {
285 mdcDataDebugMessage.debugEntryMessage("VSP id, component id", vspId, componentId);
287 QuestionnaireResponse questionnaireResponse = new QuestionnaireResponse();
288 ComponentEntity component = componentDao.getQuestionnaireData(vspId, version, componentId);
290 .validateEntityExistence(component, new ComponentEntity(vspId, version, componentId),
291 VspDetails.ENTITY_TYPE);
293 questionnaireResponse.setData(component.getQuestionnaireData());
294 List<String> nicNames = nicManager.listNics(vspId, version, componentId).stream()
295 .map(nic -> nic.getNicCompositionData().getName()).collect(Collectors.toList());
296 questionnaireResponse.setSchema(getComponentQuestionnaireSchema(
297 new ComponentQuestionnaireSchemaInput(nicNames, questionnaireResponse.getData() == null
299 : JsonUtil.json2Object(questionnaireResponse.getData(), Map.class))));
301 mdcDataDebugMessage.debugExitMessage("VSP id, component id", vspId, componentId);
302 return questionnaireResponse;
306 public void updateQuestionnaire(String vspId, Version version, String componentId,
307 String questionnaireData) {
308 mdcDataDebugMessage.debugEntryMessage("VSP id, component id", vspId, componentId);
309 validateComponentExistence(vspId, version, componentId);
311 componentDao.updateQuestionnaireData(vspId, version, componentId, questionnaireData);
313 mdcDataDebugMessage.debugExitMessage("VSP id, component id", vspId, componentId);
317 public void validateComponentExistence(String vspId, Version version, String componentId) {
318 getValidatedComponent(vspId, version, componentId);
321 private ComponentEntity getValidatedComponent(String vspId, Version version, String componentId) {
322 ComponentEntity retrieved = componentDao.get(new ComponentEntity(vspId, version, componentId));
324 .validateEntityExistence(retrieved, new ComponentEntity(vspId, version, componentId),
325 VspDetails.ENTITY_TYPE);
329 protected String getComponentCompositionSchema(ComponentCompositionSchemaInput schemaInput) {
330 return SchemaGenerator
331 .generate(SchemaTemplateContext.composition, CompositionEntityType.component, schemaInput);
334 protected String getComponentQuestionnaireSchema(SchemaTemplateInput schemaInput) {
335 return SchemaGenerator
336 .generate(SchemaTemplateContext.questionnaire, CompositionEntityType.component,