dab000951f1789a82f56f7711313c6863852bec6
[sdc.git] /
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 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";
71
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;
80   }
81
82   @Override
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));
87   }
88
89   @Override
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());
98     }
99
100     mdcDataDebugMessage.debugExitMessage(VSP_ID, vspId);
101   }
102
103   @Override
104   public ComponentEntity createComponent(ComponentEntity component) {
105     mdcDataDebugMessage.debugEntryMessage(VSP_ID, component.getId());
106
107     final String vfcAddNotAllowedInHeatOnboardingMsg =
108         "VFCs cannot be added for VSPs onboarded with HEAT.";
109
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());
119     } else {
120       validateComponentManual(component);
121       updateComponentName(component);
122       createdComponent = compositionEntityDataManager.createComponent(component);
123     }
124
125     mdcDataDebugMessage.debugExitMessage(VSP_ID, component.getId());
126
127     return createdComponent;
128   }
129
130   private void updateComponentName(ComponentEntity component) {
131     ComponentData data = component.getComponentCompositionData();
132     data.setName(COMPUTE_TYPE_PREFIX + data.getDisplayName());
133     component.setComponentCompositionData(data);
134   }
135
136   private void validateComponentManual(ComponentEntity component) {
137     final String vspVfcCountExceedMsg = "Creation of only one VFC per "
138         + "VSP allowed.";
139
140     final String vspVfcDuplicateNameMsg = "VFC with specified name "
141         + "already present in given VSP.";
142
143     Collection<ComponentEntity> vspComponentList =
144         listComponents(component.getVspId(), component.getVersion());
145     if (!vspComponentList.isEmpty()) //1707 release only supports 1 VFC in VSP (manual creation)
146     {
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());
155     }
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());
166     }
167   }
168
169   private boolean isVfcNameUnique(Collection<ComponentEntity> component, String displayName) {
170     for (ComponentEntity comp : component) {
171       if (comp.getComponentCompositionData().getDisplayName().equalsIgnoreCase(displayName)) {
172         return false;
173       }
174     }
175     return true;
176   }
177
178   @Override
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());
184
185     boolean isManual = vspInfoDao.isManual(component.getVspId(), component.getVersion());
186     if (isManual) {
187       validateComponentUpdateManual(retrieved);
188     }
189
190     ComponentCompositionSchemaInput schemaInput = new ComponentCompositionSchemaInput();
191     schemaInput.setManual(isManual);
192     schemaInput.setComponent(retrieved.getComponentCompositionData());
193
194     CompositionEntityValidationData validationData = compositionEntityDataManager
195         .validateEntity(component, SchemaTemplateContext.composition, schemaInput);
196     if (CollectionUtils.isEmpty(validationData.getErrors())) {
197       if (isManual) {
198         updateComponentName(component);
199       }
200       componentDao.update(component);
201     }
202     mdcDataDebugMessage.debugExitMessage(VSP_ID_COMPONENT_ID, component.getVspId(),
203         component.getId());
204
205     return validationData;
206   }
207
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);
216         break;
217       }
218     }
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());
229
230     }
231   }
232
233   @Override
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();
239
240     ComponentCompositionSchemaInput schemaInput = new ComponentCompositionSchemaInput();
241     schemaInput.setManual(vspInfoDao.isManual(vspId, version));
242     schemaInput.setComponent(component);
243
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);
249
250     return response;
251   }
252
253   @Override
254   public void deleteComponent(String vspId, Version version, String componentId) {
255     mdcDataDebugMessage.debugEntryMessage(VSP_ID_COMPONENT_ID, vspId, componentId);
256
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());
263     }
264
265     mdcDataDebugMessage.debugExitMessage(VSP_ID_COMPONENT_ID, vspId, componentId);
266   }
267
268   @Override
269   public QuestionnaireResponse getQuestionnaire(String vspId, Version version,
270                                                 String componentId) {
271     mdcDataDebugMessage.debugEntryMessage(VSP_ID_COMPONENT_ID, vspId, componentId);
272
273     QuestionnaireResponse questionnaireResponse = new QuestionnaireResponse();
274     ComponentEntity component = componentDao.getQuestionnaireData(vspId, version, componentId);
275     VersioningUtil
276         .validateEntityExistence(component, new ComponentEntity(vspId, version, componentId),
277             VspDetails.ENTITY_TYPE);
278
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
284             ? null
285             : JsonUtil.json2Object(questionnaireResponse.getData(), Map.class))));
286
287     mdcDataDebugMessage.debugExitMessage(VSP_ID_COMPONENT_ID, vspId, componentId);
288     return questionnaireResponse;
289   }
290
291   @Override
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);
296
297     componentDao.updateQuestionnaireData(vspId, version, componentId, questionnaireData);
298
299     mdcDataDebugMessage.debugExitMessage(VSP_ID_COMPONENT_ID, vspId, componentId);
300   }
301
302   @Override
303   public void validateComponentExistence(String vspId, Version version, String componentId) {
304     getValidatedComponent(vspId, version, componentId);
305   }
306
307   private ComponentEntity getValidatedComponent(String vspId, Version version, String componentId) {
308     ComponentEntity retrieved = componentDao.get(new ComponentEntity(vspId, version, componentId));
309     VersioningUtil
310         .validateEntityExistence(retrieved, new ComponentEntity(vspId, version, componentId),
311             VspDetails.ENTITY_TYPE);
312     return retrieved;
313   }
314
315   protected String getComponentCompositionSchema(ComponentCompositionSchemaInput schemaInput) {
316     return SchemaGenerator
317         .generate(SchemaTemplateContext.composition, CompositionEntityType.component, schemaInput);
318   }
319
320   protected String getComponentQuestionnaireSchema(SchemaTemplateInput schemaInput) {
321     return SchemaGenerator
322         .generate(SchemaTemplateContext.questionnaire, CompositionEntityType.component,
323             schemaInput);
324   }
325 }