72a38bbb5d0c236abac7dc0fb9a1a91eb7983cfd
[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 ComponentDao componentDao;
66   private CompositionEntityDataManager compositionEntityDataManager;
67   private NicManager nicManager;
68   private VendorSoftwareProductInfoDao vspInfoDao;
69
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;
78   }
79
80   @Override
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));
85   }
86
87   @Override
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());
96     }
97
98     //componentDao.updateVspLatestModificationTime(vspId, version);
99     mdcDataDebugMessage.debugExitMessage("VSP id", vspId);
100   }
101
102   /*@Override
103   public ComponentEntity createComponent(ComponentEntity component, String user) {
104     mdcDataDebugMessage.debugEntryMessage("VSP id", component.getId());
105
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())
112               .build());
113
114     }
115     //componentDao.updateVspLatestModificationTime(component.getVspId(), component.getVersion());
116     mdcDataDebugMessage.debugExitMessage("VSP id", component.getId());
117     return null;
118   }*/
119
120   @Override
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)
125             .getActiveVersion();
126     component.setVersion(activeVersion);*/
127
128     final String VFC_ADD_NOT_ALLOWED_IN_HEAT_ONBOARDING_MSG =
129         "VFCs cannot be added for VSPs onboarded with HEAT.";
130
131     ComponentEntity createdComponent = null;
132
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());
141     } else {
142       validateComponentManual(component);
143       updateComponentName(component);
144       createdComponent = createComponent(component);
145     }
146
147     mdcDataDebugMessage.debugExitMessage("VSP id", component.getId());
148
149     return createdComponent;
150   }
151
152   private ComponentEntity createComponent(ComponentEntity component) {
153     return compositionEntityDataManager.createComponent(component);
154   }
155
156   private void updateComponentName(ComponentEntity component) {
157     ComponentData data = component.getComponentCompositionData();
158     data.setName(COMPUTE_TYPE_PREFIX + data.getDisplayName());
159     component.setComponentCompositionData(data);
160   }
161
162   private void validateComponentManual(ComponentEntity component) {
163     final String VSP_VFC_COUNT_EXCEED_MSG = "Creation of only one VFC per "
164         + "VSP allowed.";
165
166     final String VSP_VFC_DUPLICATE_NAME_MSG = "VFC with specified name "
167         + "already present in given VSP.";
168
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)
172     {
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());
181     }
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());
192     }
193   }
194
195   private boolean isVfcNameUnique(Collection<ComponentEntity> component, String displayName) {
196     for (ComponentEntity comp : component) {
197       if (comp.getComponentCompositionData().getDisplayName().equalsIgnoreCase(displayName)) {
198         return false;
199       }
200     }
201     return true;
202   }
203
204   @Override
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());
210
211     boolean isManual = vspInfoDao.isManual(component.getVspId(), component.getVersion());
212     if (isManual) {
213       validateComponentUpdateManual(component, retrieved, user);
214     }
215
216
217     ComponentCompositionSchemaInput schemaInput = new ComponentCompositionSchemaInput();
218     schemaInput.setManual(isManual);
219     schemaInput.setComponent(retrieved.getComponentCompositionData());
220
221     CompositionEntityValidationData validationData = compositionEntityDataManager
222         .validateEntity(component, SchemaTemplateContext.composition, schemaInput);
223     if (CollectionUtils.isEmpty(validationData.getErrors())) {
224       if (isManual) {
225         updateComponentName(component);
226       }
227       componentDao.update(component);
228     }
229     mdcDataDebugMessage.debugExitMessage("VSP id, component id", component.getVspId(),
230         component.getId());
231
232     return validationData;
233   }
234
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);
244         break;
245       }
246     }
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());
257
258     }
259   }
260
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();
266
267     ComponentCompositionSchemaInput schemaInput = new ComponentCompositionSchemaInput();
268     schemaInput.setManual(vspInfoDao.isManual(vspId, version));
269     schemaInput.setComponent(component);
270
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);
276
277     return response;
278   }
279
280   @Override
281   public void deleteComponent(String vspId, Version version, String componentId, String user) {
282     mdcDataDebugMessage.debugEntryMessage("VSP id, component id", vspId, componentId);
283
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());
290     }
291
292     //componentDao.updateVspLatestModificationTime(vspId, version);
293
294     mdcDataDebugMessage.debugExitMessage("VSP id, component id", vspId, componentId);
295   }
296
297   @Override
298   public QuestionnaireResponse getQuestionnaire(String vspId, Version version,
299                                                 String componentId, String user) {
300     mdcDataDebugMessage.debugEntryMessage("VSP id, component id", vspId, componentId);
301
302     QuestionnaireResponse questionnaireResponse = new QuestionnaireResponse();
303     ComponentEntity component = componentDao.getQuestionnaireData(vspId, version, componentId);
304     VersioningUtil
305         .validateEntityExistence(component, new ComponentEntity(vspId, version, componentId),
306             VspDetails.ENTITY_TYPE);
307
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
313             ? null
314             : JsonUtil.json2Object(questionnaireResponse.getData(), Map.class))));
315
316     mdcDataDebugMessage.debugExitMessage("VSP id, component id", vspId, componentId);
317     return questionnaireResponse;
318   }
319
320   @Override
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);
325
326     componentDao.updateQuestionnaireData(vspId, version, componentId, questionnaireData);
327
328     //componentDao.updateVspLatestModificationTime(vspId, version);
329     mdcDataDebugMessage.debugExitMessage("VSP id, component id", vspId, componentId);
330   }
331
332   @Override
333   public void validateComponentExistence(String vspId, Version version, String componentId,
334                                          String user) {
335     getComponent(vspId, version, componentId);
336   }
337
338   private ComponentEntity getComponent(String vspId, Version version, String componentId) {
339     ComponentEntity retrieved = componentDao.get(new ComponentEntity(vspId, version, componentId));
340     VersioningUtil
341         .validateEntityExistence(retrieved, new ComponentEntity(vspId, version, componentId),
342             VspDetails.ENTITY_TYPE);
343     return retrieved;
344   }
345
346   protected String getComponentCompositionSchema(ComponentCompositionSchemaInput schemaInput) {
347     return SchemaGenerator
348         .generate(SchemaTemplateContext.composition, CompositionEntityType.component, schemaInput);
349   }
350
351   protected String getComponentQuestionnaireSchema(SchemaTemplateInput schemaInput) {
352     return SchemaGenerator
353         .generate(SchemaTemplateContext.questionnaire, CompositionEntityType.component,
354             schemaInput);
355   }
356
357   /*private boolean isManual(String vspId, Version version) {
358     return false;
359   }*/
360 }