Add collaboration feature
[sdc.git] / openecomp-be / backend / openecomp-sdc-vendor-software-product-manager / src / main / java / org / openecomp / sdc / vendorsoftwareproduct / impl / ComponentManagerImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.openecomp.sdc.vendorsoftwareproduct.impl;
22
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;
55
56 import java.util.Collection;
57 import java.util.List;
58 import java.util.Map;
59 import java.util.stream.Collectors;
60
61 import static org.openecomp.sdc.tosca.datatypes.ToscaNodeType.COMPUTE_TYPE_PREFIX;
62
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;
69
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;
78   }
79
80   @Override
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));
85   }
86
87   @Override
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());
96     }
97
98     mdcDataDebugMessage.debugExitMessage("VSP id", vspId);
99   }
100
101   /*@Override
102   public ComponentEntity createComponent(ComponentEntity component) {
103     mdcDataDebugMessage.debugEntryMessage("VSP id", component.getId());
104
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())
111               .build());
112
113     }
114     mdcDataDebugMessage.debugExitMessage("VSP id", component.getId());
115     return null;
116   }*/
117
118   @Override
119   public ComponentEntity createComponent(ComponentEntity component) {
120     mdcDataDebugMessage.debugEntryMessage("VSP id", component.getId());
121
122     final String VFC_ADD_NOT_ALLOWED_IN_HEAT_ONBOARDING_MSG =
123         "VFCs cannot be added for VSPs onboarded with HEAT.";
124
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());
134     } else {
135       validateComponentManual(component);
136       updateComponentName(component);
137       createdComponent = compositionEntityDataManager.createComponent(component);
138     }
139
140     mdcDataDebugMessage.debugExitMessage("VSP id", component.getId());
141
142     return createdComponent;
143   }
144
145   private void updateComponentName(ComponentEntity component) {
146     ComponentData data = component.getComponentCompositionData();
147     data.setName(COMPUTE_TYPE_PREFIX + data.getDisplayName());
148     component.setComponentCompositionData(data);
149   }
150
151   private void validateComponentManual(ComponentEntity component) {
152     final String VSP_VFC_COUNT_EXCEED_MSG = "Creation of only one VFC per "
153         + "VSP allowed.";
154
155     final String VSP_VFC_DUPLICATE_NAME_MSG = "VFC with specified name "
156         + "already present in given VSP.";
157
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)
161     {
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());
170     }
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());
181     }
182   }
183
184   private boolean isVfcNameUnique(Collection<ComponentEntity> component, String displayName) {
185     for (ComponentEntity comp : component) {
186       if (comp.getComponentCompositionData().getDisplayName().equalsIgnoreCase(displayName)) {
187         return false;
188       }
189     }
190     return true;
191   }
192
193   @Override
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());
199
200     boolean isManual = vspInfoDao.isManual(component.getVspId(), component.getVersion());
201     if (isManual) {
202       validateComponentUpdateManual(retrieved);
203     }
204
205     ComponentCompositionSchemaInput schemaInput = new ComponentCompositionSchemaInput();
206     schemaInput.setManual(isManual);
207     schemaInput.setComponent(retrieved.getComponentCompositionData());
208
209     CompositionEntityValidationData validationData = compositionEntityDataManager
210         .validateEntity(component, SchemaTemplateContext.composition, schemaInput);
211     if (CollectionUtils.isEmpty(validationData.getErrors())) {
212       if (isManual) {
213         updateComponentName(component);
214       }
215       componentDao.update(component);
216     }
217     mdcDataDebugMessage.debugExitMessage("VSP id, component id", component.getVspId(),
218         component.getId());
219
220     return validationData;
221   }
222
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);
231         break;
232       }
233     }
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());
244
245     }
246   }
247
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();
253
254     ComponentCompositionSchemaInput schemaInput = new ComponentCompositionSchemaInput();
255     schemaInput.setManual(vspInfoDao.isManual(vspId, version));
256     schemaInput.setComponent(component);
257
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);
263
264     return response;
265   }
266
267   @Override
268   public void deleteComponent(String vspId, Version version, String componentId) {
269     mdcDataDebugMessage.debugEntryMessage("VSP id, component id", vspId, componentId);
270
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());
277     }
278
279     mdcDataDebugMessage.debugExitMessage("VSP id, component id", vspId, componentId);
280   }
281
282   @Override
283   public QuestionnaireResponse getQuestionnaire(String vspId, Version version,
284                                                 String componentId) {
285     mdcDataDebugMessage.debugEntryMessage("VSP id, component id", vspId, componentId);
286
287     QuestionnaireResponse questionnaireResponse = new QuestionnaireResponse();
288     ComponentEntity component = componentDao.getQuestionnaireData(vspId, version, componentId);
289     VersioningUtil
290         .validateEntityExistence(component, new ComponentEntity(vspId, version, componentId),
291             VspDetails.ENTITY_TYPE);
292
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
298             ? null
299             : JsonUtil.json2Object(questionnaireResponse.getData(), Map.class))));
300
301     mdcDataDebugMessage.debugExitMessage("VSP id, component id", vspId, componentId);
302     return questionnaireResponse;
303   }
304
305   @Override
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);
310
311     componentDao.updateQuestionnaireData(vspId, version, componentId, questionnaireData);
312
313     mdcDataDebugMessage.debugExitMessage("VSP id, component id", vspId, componentId);
314   }
315
316   @Override
317   public void validateComponentExistence(String vspId, Version version, String componentId) {
318     getValidatedComponent(vspId, version, componentId);
319   }
320
321   private ComponentEntity getValidatedComponent(String vspId, Version version, String componentId) {
322     ComponentEntity retrieved = componentDao.get(new ComponentEntity(vspId, version, componentId));
323     VersioningUtil
324         .validateEntityExistence(retrieved, new ComponentEntity(vspId, version, componentId),
325             VspDetails.ENTITY_TYPE);
326     return retrieved;
327   }
328
329   protected String getComponentCompositionSchema(ComponentCompositionSchemaInput schemaInput) {
330     return SchemaGenerator
331         .generate(SchemaTemplateContext.composition, CompositionEntityType.component, schemaInput);
332   }
333
334   protected String getComponentQuestionnaireSchema(SchemaTemplateInput schemaInput) {
335     return SchemaGenerator
336         .generate(SchemaTemplateContext.questionnaire, CompositionEntityType.component,
337             schemaInput);
338   }
339 }