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(
71 ComponentDao componentDao,
72 CompositionEntityDataManager compositionEntityDataManager,
73 NicManager nicManager, 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, String user) {
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, String user) {
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 //componentDao.updateVspLatestModificationTime(vspId, version);
99 mdcDataDebugMessage.debugExitMessage("VSP id", vspId);
103 public ComponentEntity createComponent(ComponentEntity component, String user) {
104 mdcDataDebugMessage.debugEntryMessage("VSP id", component.getId());
106 if (!isManual(component.getVspId(), component.getVersion())) {
107 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
108 LoggerTragetServiceName.CREATE_COMPONENT, ErrorLevel.ERROR.name(),
109 LoggerErrorCode.PERMISSION_ERROR.getErrorCode(), "Can't create component");
110 throw new CoreException(
111 new CompositionEditNotAllowedErrorBuilder(component.getVspId(), component.getVersion())
115 //componentDao.updateVspLatestModificationTime(component.getVspId(), component.getVersion());
116 mdcDataDebugMessage.debugExitMessage("VSP id", component.getId());
121 public ComponentEntity createComponent(ComponentEntity component, String user) {
122 mdcDataDebugMessage.debugEntryMessage("VSP id", component.getId());
123 /*Version activeVersion =
124 getVersionInfo(component.getVspId(), VersionableEntityAction.Write, user)
126 component.setVersion(activeVersion);*/
128 final String VFC_ADD_NOT_ALLOWED_IN_HEAT_ONBOARDING_MSG =
129 "VFCs cannot be added for VSPs onboarded with HEAT.";
131 ComponentEntity createdComponent = null;
133 if (!vspInfoDao.isManual(component.getVspId(), component.getVersion())) {
134 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
135 LoggerTragetServiceName.CREATE_COMPONENT, ErrorLevel.ERROR.name(),
136 LoggerErrorCode.PERMISSION_ERROR.getErrorCode(), "Can't create component");
137 throw new CoreException(
138 new ErrorCode.ErrorCodeBuilder().withCategory(ErrorCategory.APPLICATION)
139 .withId(VendorSoftwareProductErrorCodes.VFC_ADD_NOT_ALLOWED_IN_HEAT_ONBOARDING)
140 .withMessage(VFC_ADD_NOT_ALLOWED_IN_HEAT_ONBOARDING_MSG).build());
142 validateComponentManual(component);
143 updateComponentName(component);
144 createdComponent = createComponent(component);
147 mdcDataDebugMessage.debugExitMessage("VSP id", component.getId());
149 return createdComponent;
152 private ComponentEntity createComponent(ComponentEntity component) {
153 return compositionEntityDataManager.createComponent(component);
156 private void updateComponentName(ComponentEntity component) {
157 ComponentData data = component.getComponentCompositionData();
158 data.setName(COMPUTE_TYPE_PREFIX + data.getDisplayName());
159 component.setComponentCompositionData(data);
162 private void validateComponentManual(ComponentEntity component) {
163 final String VSP_VFC_COUNT_EXCEED_MSG = "Creation of only one VFC per "
166 final String VSP_VFC_DUPLICATE_NAME_MSG = "VFC with specified name "
167 + "already present in given VSP.";
169 Collection<ComponentEntity> vspComponentList = listComponents(component.getVspId()
170 , component.getVersion(), null);
171 if (vspComponentList.size() >= 1) //1707 release only supports 1 VFC in VSP (manual creation)
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 count exceed");
177 throw new CoreException(
178 new ErrorCode.ErrorCodeBuilder().withCategory(ErrorCategory.APPLICATION)
179 .withId(VendorSoftwareProductErrorCodes.VSP_VFC_COUNT_EXCEED)
180 .withMessage(VSP_VFC_COUNT_EXCEED_MSG).build());
182 if (!isVfcNameUnique(vspComponentList,
183 component.getComponentCompositionData().getDisplayName())) {
184 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
185 LoggerTragetServiceName.CREATE_COMPONENT, ErrorLevel.ERROR.name(),
186 LoggerErrorCode.PERMISSION_ERROR.getErrorCode(), "Can't create component: "
187 + "vsp component duplicate name");
188 throw new CoreException(
189 new ErrorCode.ErrorCodeBuilder().withCategory(ErrorCategory.APPLICATION)
190 .withId(VendorSoftwareProductErrorCodes.VSP_VFC_DUPLICATE_NAME)
191 .withMessage(VSP_VFC_DUPLICATE_NAME_MSG).build());
195 private boolean isVfcNameUnique(Collection<ComponentEntity> component, String displayName) {
196 for (ComponentEntity comp : component) {
197 if (comp.getComponentCompositionData().getDisplayName().equalsIgnoreCase(displayName)) {
205 public CompositionEntityValidationData updateComponent(ComponentEntity component, String user) {
206 mdcDataDebugMessage.debugEntryMessage("VSP id, component id", component
207 .getVspId(), component.getId());
208 ComponentEntity retrieved =
209 getComponent(component.getVspId(), component.getVersion(), component.getId());
211 boolean isManual = vspInfoDao.isManual(component.getVspId(), component.getVersion());
213 validateComponentUpdateManual(component, retrieved, user);
217 ComponentCompositionSchemaInput schemaInput = new ComponentCompositionSchemaInput();
218 schemaInput.setManual(isManual);
219 schemaInput.setComponent(retrieved.getComponentCompositionData());
221 CompositionEntityValidationData validationData = compositionEntityDataManager
222 .validateEntity(component, SchemaTemplateContext.composition, schemaInput);
223 if (CollectionUtils.isEmpty(validationData.getErrors())) {
225 updateComponentName(component);
227 componentDao.update(component);
229 mdcDataDebugMessage.debugExitMessage("VSP id, component id", component.getVspId(),
232 return validationData;
235 private void validateComponentUpdateManual(ComponentEntity component, ComponentEntity
236 retrieved, String user) {
237 Collection<ComponentEntity> vspComponentList =
238 listComponents(component.getVspId(), component.getVersion(), user);
239 //VFC name should be unique within VSP
240 //Removing VFC with same ID from list to avoid self compare
241 for(ComponentEntity ce : vspComponentList) {
242 if (ce.getId().equals(component.getId())) {
243 vspComponentList.remove(ce);
247 if (!isVfcNameUnique(vspComponentList, component.getComponentCompositionData()
248 .getDisplayName())) {
249 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
250 LoggerTragetServiceName.UPDATE_COMPONENT, ErrorLevel.ERROR.name(),
251 LoggerErrorCode.PERMISSION_ERROR.getErrorCode(), "Component with same name already " +
252 "exists for specified VSP");
253 throw new CoreException(
254 new ErrorCode.ErrorCodeBuilder().withCategory(ErrorCategory.APPLICATION)
255 .withId(VendorSoftwareProductErrorCodes.VSP_VFC_DUPLICATE_NAME)
256 .withMessage("VFC with specified name already present in given VSP.").build());
261 public CompositionEntityResponse<ComponentData> getComponent(String vspId, Version version,
262 String componentId, String user) {
263 mdcDataDebugMessage.debugEntryMessage("VSP id, component id", vspId, componentId);
264 ComponentEntity componentEntity = getComponent(vspId, version, componentId);
265 ComponentData component = componentEntity.getComponentCompositionData();
267 ComponentCompositionSchemaInput schemaInput = new ComponentCompositionSchemaInput();
268 schemaInput.setManual(vspInfoDao.isManual(vspId, version));
269 schemaInput.setComponent(component);
271 CompositionEntityResponse<ComponentData> response = new CompositionEntityResponse<>();
272 response.setId(componentId);
273 response.setData(component);
274 response.setSchema(getComponentCompositionSchema(schemaInput));
275 mdcDataDebugMessage.debugExitMessage("VSP id, component id", vspId, componentId);
281 public void deleteComponent(String vspId, Version version, String componentId, String user) {
282 mdcDataDebugMessage.debugEntryMessage("VSP id, component id", vspId, componentId);
284 if (!vspInfoDao.isManual(vspId, version)) {
285 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
286 LoggerTragetServiceName.DELETE_COMPONENT, ErrorLevel.ERROR.name(),
287 LoggerErrorCode.PERMISSION_ERROR.getErrorCode(), "Can't delete component");
288 throw new CoreException(
289 new CompositionEditNotAllowedErrorBuilder(vspId, version).build());
292 //componentDao.updateVspLatestModificationTime(vspId, version);
294 mdcDataDebugMessage.debugExitMessage("VSP id, component id", vspId, componentId);
298 public QuestionnaireResponse getQuestionnaire(String vspId, Version version,
299 String componentId, String user) {
300 mdcDataDebugMessage.debugEntryMessage("VSP id, component id", vspId, componentId);
302 QuestionnaireResponse questionnaireResponse = new QuestionnaireResponse();
303 ComponentEntity component = componentDao.getQuestionnaireData(vspId, version, componentId);
305 .validateEntityExistence(component, new ComponentEntity(vspId, version, componentId),
306 VspDetails.ENTITY_TYPE);
308 questionnaireResponse.setData(component.getQuestionnaireData());
309 List<String> nicNames = nicManager.listNics(vspId, version, componentId, user).stream()
310 .map(nic -> nic.getNicCompositionData().getName()).collect(Collectors.toList());
311 questionnaireResponse.setSchema(getComponentQuestionnaireSchema(
312 new ComponentQuestionnaireSchemaInput(nicNames, questionnaireResponse.getData() == null
314 : JsonUtil.json2Object(questionnaireResponse.getData(), Map.class))));
316 mdcDataDebugMessage.debugExitMessage("VSP id, component id", vspId, componentId);
317 return questionnaireResponse;
321 public void updateQuestionnaire(String vspId, Version version, String componentId,
322 String questionnaireData, String user) {
323 mdcDataDebugMessage.debugEntryMessage("VSP id, component id", vspId, componentId);
324 validateComponentExistence(vspId, version, componentId, user);
326 componentDao.updateQuestionnaireData(vspId, version, componentId, questionnaireData);
328 //componentDao.updateVspLatestModificationTime(vspId, version);
329 mdcDataDebugMessage.debugExitMessage("VSP id, component id", vspId, componentId);
333 public void validateComponentExistence(String vspId, Version version, String componentId,
335 getComponent(vspId, version, componentId);
338 private ComponentEntity getComponent(String vspId, Version version, String componentId) {
339 ComponentEntity retrieved = componentDao.get(new ComponentEntity(vspId, version, componentId));
341 .validateEntityExistence(retrieved, new ComponentEntity(vspId, version, componentId),
342 VspDetails.ENTITY_TYPE);
346 protected String getComponentCompositionSchema(ComponentCompositionSchemaInput schemaInput) {
347 return SchemaGenerator
348 .generate(SchemaTemplateContext.composition, CompositionEntityType.component, schemaInput);
351 protected String getComponentQuestionnaireSchema(SchemaTemplateInput schemaInput) {
352 return SchemaGenerator
353 .generate(SchemaTemplateContext.questionnaire, CompositionEntityType.component,
357 /*private boolean isManual(String vspId, Version version) {