f509658b37b7cfcb22403ce88896b506dbb322c3
[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.MdcDataErrorMessage;
30 import org.openecomp.sdc.logging.types.LoggerConstants;
31 import org.openecomp.sdc.logging.types.LoggerErrorCode;
32 import org.openecomp.sdc.logging.types.LoggerTragetServiceName;
33 import org.openecomp.sdc.vendorsoftwareproduct.ComponentManager;
34 import org.openecomp.sdc.vendorsoftwareproduct.NicManager;
35 import org.openecomp.sdc.vendorsoftwareproduct.dao.ComponentDao;
36 import org.openecomp.sdc.vendorsoftwareproduct.dao.VendorSoftwareProductInfoDao;
37 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentEntity;
38 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspDetails;
39 import org.openecomp.sdc.vendorsoftwareproduct.errors.CompositionEditNotAllowedErrorBuilder;
40 import org.openecomp.sdc.vendorsoftwareproduct.errors.VendorSoftwareProductErrorCodes;
41 import org.openecomp.sdc.vendorsoftwareproduct.services.composition.CompositionEntityDataManager;
42 import org.openecomp.sdc.vendorsoftwareproduct.services.schemagenerator.SchemaGenerator;
43 import org.openecomp.sdc.vendorsoftwareproduct.types.CompositionEntityResponse;
44 import org.openecomp.sdc.vendorsoftwareproduct.types.QuestionnaireResponse;
45 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.ComponentData;
46 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.CompositionEntityType;
47 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.CompositionEntityValidationData;
48 import org.openecomp.sdc.vendorsoftwareproduct.types.schemagenerator.ComponentCompositionSchemaInput;
49 import org.openecomp.sdc.vendorsoftwareproduct.types.schemagenerator.ComponentQuestionnaireSchemaInput;
50 import org.openecomp.sdc.vendorsoftwareproduct.types.schemagenerator.SchemaTemplateContext;
51 import org.openecomp.sdc.vendorsoftwareproduct.types.schemagenerator.SchemaTemplateInput;
52 import org.openecomp.sdc.versioning.VersioningUtil;
53 import org.openecomp.sdc.versioning.dao.types.Version;
54
55 import java.util.Collection;
56 import java.util.List;
57 import java.util.Map;
58 import java.util.stream.Collectors;
59
60 import static org.openecomp.sdc.tosca.datatypes.ToscaNodeType.COMPUTE_TYPE_PREFIX;
61
62 public class ComponentManagerImpl implements ComponentManager {
63   private final ComponentDao componentDao;
64   private final CompositionEntityDataManager compositionEntityDataManager;
65   private final NicManager nicManager;
66   private final VendorSoftwareProductInfoDao vspInfoDao;
67   private static final String VSP_ID = "VSP id";
68   private static final String VSP_ID_COMPONENT_ID = "VSP id, component id";
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     return componentDao.list(new ComponentEntity(vspId, version, null));
83   }
84
85   @Override
86   public void deleteComponents(String vspId, Version version) {
87     if (!vspInfoDao.isManual(vspId, version)) {
88       MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
89           LoggerTragetServiceName.DELETE_COMPONENT, ErrorLevel.ERROR.name(),
90           LoggerErrorCode.PERMISSION_ERROR.getErrorCode(), "Can't delete component");
91       throw new CoreException(
92           new CompositionEditNotAllowedErrorBuilder(vspId, version).build());
93     }
94   }
95
96   @Override
97   public ComponentEntity createComponent(ComponentEntity component) {
98     final String vfcAddNotAllowedInHeatOnboardingMsg =
99         "VFCs cannot be added for VSPs onboarded with HEAT.";
100
101     ComponentEntity createdComponent;
102     if (!vspInfoDao.isManual(component.getVspId(), component.getVersion())) {
103       MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
104           LoggerTragetServiceName.CREATE_COMPONENT, ErrorLevel.ERROR.name(),
105           LoggerErrorCode.PERMISSION_ERROR.getErrorCode(), "Can't create component");
106       throw new CoreException(
107           new ErrorCode.ErrorCodeBuilder().withCategory(ErrorCategory.APPLICATION)
108               .withId(VendorSoftwareProductErrorCodes.VFC_ADD_NOT_ALLOWED_IN_HEAT_ONBOARDING)
109               .withMessage(vfcAddNotAllowedInHeatOnboardingMsg).build());
110     } else {
111       validateComponentManual(component);
112       updateComponentName(component);
113       createdComponent = compositionEntityDataManager.createComponent(component);
114     }
115     return createdComponent;
116   }
117
118   private void updateComponentName(ComponentEntity component) {
119     ComponentData data = component.getComponentCompositionData();
120     data.setName(COMPUTE_TYPE_PREFIX + data.getDisplayName());
121     component.setComponentCompositionData(data);
122   }
123
124   private void validateComponentManual(ComponentEntity component) {
125     final String vspVfcCountExceedMsg = "Creation of only one VFC per "
126         + "VSP allowed.";
127
128     final String vspVfcDuplicateNameMsg = "VFC with specified name "
129         + "already present in given VSP.";
130
131     Collection<ComponentEntity> vspComponentList =
132         listComponents(component.getVspId(), component.getVersion());
133     if (!vspComponentList.isEmpty()) //1707 release only supports 1 VFC in VSP (manual creation)
134     {
135       MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
136           LoggerTragetServiceName.CREATE_COMPONENT, ErrorLevel.ERROR.name(),
137           LoggerErrorCode.PERMISSION_ERROR.getErrorCode(), "Can't create component: "
138               + "vsp component count exceed");
139       throw new CoreException(
140           new ErrorCode.ErrorCodeBuilder().withCategory(ErrorCategory.APPLICATION)
141               .withId(VendorSoftwareProductErrorCodes.VSP_VFC_COUNT_EXCEED)
142               .withMessage(vspVfcCountExceedMsg).build());
143     }
144     if (!isVfcNameUnique(vspComponentList,
145         component.getComponentCompositionData().getDisplayName())) {
146       MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
147           LoggerTragetServiceName.CREATE_COMPONENT, ErrorLevel.ERROR.name(),
148           LoggerErrorCode.PERMISSION_ERROR.getErrorCode(), "Can't create component: "
149               + "vsp component duplicate name");
150       throw new CoreException(
151           new ErrorCode.ErrorCodeBuilder().withCategory(ErrorCategory.APPLICATION)
152               .withId(VendorSoftwareProductErrorCodes.VSP_VFC_DUPLICATE_NAME)
153               .withMessage(vspVfcDuplicateNameMsg).build());
154     }
155   }
156
157   private boolean isVfcNameUnique(Collection<ComponentEntity> component, String displayName) {
158     for (ComponentEntity comp : component) {
159       if (comp.getComponentCompositionData().getDisplayName().equalsIgnoreCase(displayName)) {
160         return false;
161       }
162     }
163     return true;
164   }
165
166   @Override
167   public CompositionEntityValidationData updateComponent(ComponentEntity component) {
168     ComponentEntity retrieved =
169         getValidatedComponent(component.getVspId(), component.getVersion(), component.getId());
170
171     boolean isManual = vspInfoDao.isManual(component.getVspId(), component.getVersion());
172     if (isManual) {
173       validateComponentUpdateManual(retrieved);
174     }
175
176     ComponentCompositionSchemaInput schemaInput = new ComponentCompositionSchemaInput();
177     schemaInput.setManual(isManual);
178     schemaInput.setComponent(retrieved.getComponentCompositionData());
179
180     CompositionEntityValidationData validationData = compositionEntityDataManager
181         .validateEntity(component, SchemaTemplateContext.composition, schemaInput);
182     if (CollectionUtils.isEmpty(validationData.getErrors())) {
183       if (isManual) {
184         updateComponentName(component);
185       }
186       componentDao.update(component);
187     }
188     return validationData;
189   }
190
191   private void validateComponentUpdateManual(ComponentEntity component) {
192     Collection<ComponentEntity> vspComponentList =
193         listComponents(component.getVspId(), component.getVersion());
194     //VFC name should be unique within VSP
195     //Removing VFC with same ID from list to avoid self compare
196     for (ComponentEntity ce : vspComponentList) {
197       if (ce.getId().equals(component.getId())) {
198         vspComponentList.remove(ce);
199         break;
200       }
201     }
202     if (!isVfcNameUnique(vspComponentList, component.getComponentCompositionData()
203         .getDisplayName())) {
204       MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
205           LoggerTragetServiceName.UPDATE_COMPONENT, ErrorLevel.ERROR.name(),
206           LoggerErrorCode.PERMISSION_ERROR.getErrorCode(), "Component with same name already " +
207               "exists for specified VSP");
208       throw new CoreException(
209           new ErrorCode.ErrorCodeBuilder().withCategory(ErrorCategory.APPLICATION)
210               .withId(VendorSoftwareProductErrorCodes.VSP_VFC_DUPLICATE_NAME)
211               .withMessage("VFC with specified name already present in given VSP.").build());
212
213     }
214   }
215
216   @Override
217   public CompositionEntityResponse<ComponentData> getComponent(String vspId, Version version,
218                                                                String componentId) {
219     ComponentEntity componentEntity = getValidatedComponent(vspId, version, componentId);
220     ComponentData component = componentEntity.getComponentCompositionData();
221
222     ComponentCompositionSchemaInput schemaInput = new ComponentCompositionSchemaInput();
223     schemaInput.setManual(vspInfoDao.isManual(vspId, version));
224     schemaInput.setComponent(component);
225
226     CompositionEntityResponse<ComponentData> response = new CompositionEntityResponse<>();
227     response.setId(componentId);
228     response.setData(component);
229     response.setSchema(getComponentCompositionSchema(schemaInput));
230     return response;
231   }
232
233   @Override
234   public void deleteComponent(String vspId, Version version, String componentId) {
235     if (!vspInfoDao.isManual(vspId, version)) {
236       MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
237           LoggerTragetServiceName.DELETE_COMPONENT, ErrorLevel.ERROR.name(),
238           LoggerErrorCode.PERMISSION_ERROR.getErrorCode(), "Can't delete component");
239       throw new CoreException(
240           new CompositionEditNotAllowedErrorBuilder(vspId, version).build());
241     }
242   }
243
244   @Override
245   public QuestionnaireResponse getQuestionnaire(String vspId, Version version,
246                                                 String componentId) {
247     QuestionnaireResponse questionnaireResponse = new QuestionnaireResponse();
248     ComponentEntity component = componentDao.getQuestionnaireData(vspId, version, componentId);
249     VersioningUtil
250         .validateEntityExistence(component, new ComponentEntity(vspId, version, componentId),
251             VspDetails.ENTITY_TYPE);
252
253     questionnaireResponse.setData(component.getQuestionnaireData());
254     List<String> nicNames = nicManager.listNics(vspId, version, componentId).stream()
255         .map(nic -> nic.getNicCompositionData().getName()).collect(Collectors.toList());
256     questionnaireResponse.setSchema(getComponentQuestionnaireSchema(
257         new ComponentQuestionnaireSchemaInput(nicNames, questionnaireResponse.getData() == null
258             ? null
259             : JsonUtil.json2Object(questionnaireResponse.getData(), Map.class))));
260     return questionnaireResponse;
261   }
262
263   @Override
264   public void updateQuestionnaire(String vspId, Version version, String componentId,
265                                   String questionnaireData) {
266     validateComponentExistence(vspId, version, componentId);
267
268     componentDao.updateQuestionnaireData(vspId, version, componentId, questionnaireData);
269   }
270
271   @Override
272   public void validateComponentExistence(String vspId, Version version, String componentId) {
273     getValidatedComponent(vspId, version, componentId);
274   }
275
276   private ComponentEntity getValidatedComponent(String vspId, Version version, String componentId) {
277     ComponentEntity retrieved = componentDao.get(new ComponentEntity(vspId, version, componentId));
278     VersioningUtil
279         .validateEntityExistence(retrieved, new ComponentEntity(vspId, version, componentId),
280             VspDetails.ENTITY_TYPE);
281     return retrieved;
282   }
283
284   protected String getComponentCompositionSchema(ComponentCompositionSchemaInput schemaInput) {
285     return SchemaGenerator
286         .generate(SchemaTemplateContext.composition, CompositionEntityType.component, schemaInput);
287   }
288
289   protected String getComponentQuestionnaireSchema(SchemaTemplateInput schemaInput) {
290     return SchemaGenerator
291         .generate(SchemaTemplateContext.questionnaire, CompositionEntityType.component,
292             schemaInput);
293   }
294 }