a751ed68a594370e03d0fbc2ef4203d782ff4d55
[sdc.git] /
1 /*
2  * Copyright © 2016-2017 European Support Limited
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.openecomp.sdc.vendorsoftwareproduct.impl;
18
19 import org.apache.commons.collections4.CollectionUtils;
20 import org.openecomp.sdc.common.errors.CoreException;
21 import org.openecomp.sdc.common.errors.ErrorCode;
22 import org.openecomp.sdc.datatypes.error.ErrorLevel;
23 import org.openecomp.sdc.logging.context.impl.MdcDataDebugMessage;
24 import org.openecomp.sdc.logging.context.impl.MdcDataErrorMessage;
25 import org.openecomp.sdc.logging.types.LoggerConstants;
26 import org.openecomp.sdc.logging.types.LoggerErrorCode;
27 import org.openecomp.sdc.logging.types.LoggerTragetServiceName;
28 import org.openecomp.sdc.vendorsoftwareproduct.NetworkManager;
29 import org.openecomp.sdc.vendorsoftwareproduct.NicManager;
30 import org.openecomp.sdc.vendorsoftwareproduct.VendorSoftwareProductConstants;
31 import org.openecomp.sdc.vendorsoftwareproduct.dao.NicDao;
32 import org.openecomp.sdc.vendorsoftwareproduct.dao.VendorSoftwareProductInfoDao;
33 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.NetworkEntity;
34 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.NicEntity;
35 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspDetails;
36 import org.openecomp.sdc.vendorsoftwareproduct.errors.DeleteNicErrorBuilder;
37 import org.openecomp.sdc.vendorsoftwareproduct.errors.DuplicateNicInComponentErrorBuilder;
38 import org.openecomp.sdc.vendorsoftwareproduct.errors.NicErrorBuilder;
39 import org.openecomp.sdc.vendorsoftwareproduct.errors.NicInternalNetworkErrorBuilder;
40 import org.openecomp.sdc.vendorsoftwareproduct.errors.NicNetworkIdNotAllowedExternalNetworkErrorBuilder;
41 import org.openecomp.sdc.vendorsoftwareproduct.errors.NotSupportedHeatOnboardMethodErrorBuilder;
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.CompositionEntityType;
47 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.CompositionEntityValidationData;
48 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.NetworkType;
49 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.Nic;
50 import org.openecomp.sdc.vendorsoftwareproduct.types.schemagenerator.NicCompositionSchemaInput;
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.Map;
58 import java.util.stream.Collectors;
59
60 public class NicManagerImpl implements NicManager {
61   private static final MdcDataDebugMessage MDC_DATA_DEBUG_MESSAGE = new MdcDataDebugMessage();
62
63   private final NicDao nicDao;
64   private final CompositionEntityDataManager compositionEntityDataManager;
65   private final NetworkManager networkManager;
66   private final VendorSoftwareProductInfoDao vspInfoDao;
67   private static final String VSP_ID_COMPONENT_ID = "VSP id, component id";
68
69   public NicManagerImpl(NicDao nicDao,
70                         CompositionEntityDataManager compositionEntityDataManager,
71                         NetworkManager networkManager,
72                         VendorSoftwareProductInfoDao vspInfoDao) {
73     this.nicDao = nicDao;
74     this.compositionEntityDataManager = compositionEntityDataManager;
75     this.networkManager = networkManager;
76     this.vspInfoDao = vspInfoDao;
77   }
78
79   @Override
80   public Collection<NicEntity> listNics(String vspId, Version version, String componentId) {
81     MDC_DATA_DEBUG_MESSAGE.debugEntryMessage(VSP_ID_COMPONENT_ID, vspId, componentId);
82
83     Collection<NicEntity> nics = nicDao.list(new NicEntity(vspId, version, componentId, null));
84
85     if (!nics.isEmpty()) {
86       Map<String, String> networksNameById = listNetworksNameById(vspId, version);
87       nics.forEach(nicEntity -> {
88         Nic nic = nicEntity.getNicCompositionData();
89         nic.setNetworkName(networksNameById.get(nic.getNetworkId()));
90         nicEntity.setNicCompositionData(nic);
91       });
92     }
93
94     MDC_DATA_DEBUG_MESSAGE.debugExitMessage(VSP_ID_COMPONENT_ID, vspId, componentId);
95
96     return nics;
97   }
98
99   private Map<String, String> listNetworksNameById(String vspId, Version version) {
100     Collection<NetworkEntity> networks = networkManager.listNetworks(vspId, version);
101     return networks.stream().collect(Collectors.toMap(NetworkEntity::getId,
102         networkEntity -> networkEntity.getNetworkCompositionData().getName()));
103   }
104
105   @Override
106   public NicEntity createNic(NicEntity nic) {
107     MDC_DATA_DEBUG_MESSAGE.debugEntryMessage(VSP_ID_COMPONENT_ID, nic.getVspId(),
108         nic.getComponentId());
109
110     NicEntity createdNic;
111     if (!vspInfoDao.isManual(nic.getVspId(), nic.getVersion())) {
112       ErrorCode onboardingMethodUpdateErrorCode = NotSupportedHeatOnboardMethodErrorBuilder
113           .getAddNicNotSupportedHeatOnboardMethodErrorBuilder();
114       MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
115           LoggerTragetServiceName.CREATE_NIC, ErrorLevel.ERROR.name(),
116           LoggerErrorCode.PERMISSION_ERROR.getErrorCode(),
117           onboardingMethodUpdateErrorCode.message());
118       throw new CoreException(onboardingMethodUpdateErrorCode);
119     } else {
120       validateNic(nic);
121       createdNic = compositionEntityDataManager.createNic(nic);
122     }
123
124     MDC_DATA_DEBUG_MESSAGE
125         .debugExitMessage(VSP_ID_COMPONENT_ID, nic.getVspId(), nic.getComponentId());
126
127     return createdNic;
128   }
129
130
131   private void validateNic(NicEntity nic) {
132     Collection<NicEntity> listNics = listNics(nic.getVspId(), nic.getVersion(), nic
133         .getComponentId());
134     String networkId = nic.getNicCompositionData().getNetworkId();
135     NetworkType networkType = nic.getNicCompositionData().getNetworkType();
136     String networkDescription = nic.getNicCompositionData().getNetworkDescription();
137
138     if (!nic.getNicCompositionData().getName()
139             .matches(VendorSoftwareProductConstants.NAME_PATTERN)) {
140       ErrorCode errorCode = NicErrorBuilder
141               .getNicNameFormatErrorBuilder(VendorSoftwareProductConstants.NAME_PATTERN);
142
143       MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
144               LoggerTragetServiceName.CREATE_NIC, ErrorLevel.ERROR.name(),
145               errorCode.id(),errorCode.message());
146
147       throw new CoreException(errorCode);
148     }
149
150     validateNics(nic, listNics);
151
152     if (networkType.equals(NetworkType.Internal)) {
153       validateInternalNetworkType(nic, networkId, networkDescription);
154
155     } else if (networkType.equals(NetworkType.External)
156             && !(networkId == null || networkId.isEmpty())) {
157         final ErrorCode nicNetworkIdNotAllowedExternalNetworkErrorBuilder =
158             new NicNetworkIdNotAllowedExternalNetworkErrorBuilder().build();
159         MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
160             LoggerTragetServiceName.CREATE_NIC, ErrorLevel.ERROR.name(),
161             LoggerErrorCode.DATA_ERROR.getErrorCode(),
162             nicNetworkIdNotAllowedExternalNetworkErrorBuilder.message());
163         throw new CoreException(nicNetworkIdNotAllowedExternalNetworkErrorBuilder);
164     }
165   }
166
167   private void validateInternalNetworkType(NicEntity nic, String networkId,
168                                            String networkDescription) {
169     if (!(networkId == null || networkId.isEmpty())) {
170           networkManager.getNetwork(nic.getVspId(), nic.getVersion(), networkId);
171     }
172
173     if (!(networkDescription == null || networkDescription.isEmpty())) {
174       final ErrorCode nicNetworkDescriptionErrorBuilder =
175           NicInternalNetworkErrorBuilder.getNetworkDescriptionInternalNetworkErrorBuilder();
176       MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
177           LoggerTragetServiceName.CREATE_NIC, ErrorLevel.ERROR.name(),
178           LoggerErrorCode.DATA_ERROR.getErrorCode(), nicNetworkDescriptionErrorBuilder.message());
179       throw new CoreException(nicNetworkDescriptionErrorBuilder);
180     }
181   }
182
183   private void validateNics(NicEntity nic, Collection<NicEntity> listNics) {
184     listNics.forEach(nicEntity -> {
185       Nic nicdata = nicEntity.getNicCompositionData();
186       if (nic.getNicCompositionData().getName().equalsIgnoreCase(nicdata.getName())) {
187         final ErrorCode duplicateNicInComponentErrorBuilder =
188             new DuplicateNicInComponentErrorBuilder(nic.getNicCompositionData().getName(),
189                 nic.getComponentId()).build();
190         MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
191             LoggerTragetServiceName.CREATE_NIC, ErrorLevel.ERROR.name(),
192             LoggerErrorCode.DATA_ERROR.getErrorCode(),
193             duplicateNicInComponentErrorBuilder.message());
194         throw new CoreException(duplicateNicInComponentErrorBuilder);
195       }
196
197     });
198   }
199
200   @Override
201   public CompositionEntityResponse<Nic> getNic(String vspId, Version version, String componentId,
202                                                String nicId) {
203     MDC_DATA_DEBUG_MESSAGE
204         .debugEntryMessage("VSP id, component id, nic id", vspId, componentId, nicId);
205
206     NicEntity nicEntity = getValidatedNic(vspId, version, componentId, nicId);
207     Nic nic = nicEntity.getNicCompositionData();
208
209     NicCompositionSchemaInput schemaInput = new NicCompositionSchemaInput();
210     schemaInput.setManual(vspInfoDao.isManual(vspId, version));
211     schemaInput.setNic(nic);
212     Map<String, String> networksNameById = listNetworksNameById(vspId, version);
213     nic.setNetworkName(networksNameById.get(nic.getNetworkId()));
214     schemaInput.setNetworkIds(networksNameById.keySet());
215
216     CompositionEntityResponse<Nic> response = new CompositionEntityResponse<>();
217     response.setId(nicId);
218     response.setData(nic);
219     response.setSchema(getNicCompositionSchema(schemaInput));
220
221     MDC_DATA_DEBUG_MESSAGE
222         .debugExitMessage(VSP_ID_COMPONENT_ID, vspId, componentId, nicId);
223
224     return response;
225   }
226
227
228   private NicEntity getValidatedNic(String vspId, Version version, String componentId,
229                                     String nicId) {
230     NicEntity retrieved = nicDao.get(new NicEntity(vspId, version, componentId, nicId));
231     VersioningUtil
232         .validateEntityExistence(retrieved, new NicEntity(vspId, version, componentId, nicId),
233             VspDetails.ENTITY_TYPE);
234     return retrieved;
235   }
236
237   @Override
238   public void deleteNic(String vspId, Version version, String componentId, String nicId) {
239     MDC_DATA_DEBUG_MESSAGE
240         .debugEntryMessage(VSP_ID_COMPONENT_ID, vspId, componentId, nicId);
241
242     if (!vspInfoDao.isManual(vspId, version)) {
243       final ErrorCode deleteNicErrorBuilder =
244           DeleteNicErrorBuilder.getDeleteNicForHeatOnboardedVspErrorBuilder();
245       MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
246           LoggerTragetServiceName.DELETE_NIC, ErrorLevel.ERROR.name(),
247           LoggerErrorCode.PERMISSION_ERROR.getErrorCode(),
248           deleteNicErrorBuilder.message());
249       throw new CoreException(deleteNicErrorBuilder);
250     }
251
252     NicEntity nicEntity = getValidatedNic(vspId, version, componentId, nicId);
253     nicDao.delete(nicEntity);
254
255     MDC_DATA_DEBUG_MESSAGE
256         .debugExitMessage(VSP_ID_COMPONENT_ID, vspId, componentId, nicId);
257   }
258
259   @Override
260   public CompositionEntityValidationData updateNic(NicEntity nic) {
261     MDC_DATA_DEBUG_MESSAGE
262         .debugEntryMessage(VSP_ID_COMPONENT_ID, nic.getVspId(), nic.getComponentId(),
263             nic.getId());
264
265     NicEntity retrieved =
266         getValidatedNic(nic.getVspId(), nic.getVersion(), nic.getComponentId(), nic.getId());
267
268     NicCompositionSchemaInput schemaInput = new NicCompositionSchemaInput();
269     schemaInput.setManual(vspInfoDao.isManual(nic.getVspId(), nic.getVersion()));
270     schemaInput.setNic(retrieved.getNicCompositionData());
271
272     if (schemaInput.isManual() && !nic.getNicCompositionData().getName()
273             .matches(VendorSoftwareProductConstants.NAME_PATTERN)) {
274       ErrorCode errorCode = NicErrorBuilder
275               .getNicNameFormatErrorBuilder(VendorSoftwareProductConstants.NAME_PATTERN);
276
277       MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
278               LoggerTragetServiceName.UPDATE_NIC, ErrorLevel.ERROR.name(),
279               errorCode.id(),errorCode.message());
280
281       throw new CoreException(errorCode);
282     }
283
284     CompositionEntityValidationData validationData = compositionEntityDataManager
285         .validateEntity(nic, SchemaTemplateContext.composition, schemaInput);
286     if (CollectionUtils.isEmpty(validationData.getErrors())) {
287       nicDao.update(nic);
288     }
289     MDC_DATA_DEBUG_MESSAGE
290         .debugExitMessage(VSP_ID_COMPONENT_ID, nic.getVspId(), nic.getComponentId(),
291             nic.getId());
292
293     return validationData;
294   }
295
296   @Override
297   public QuestionnaireResponse getNicQuestionnaire(String vspId, Version version,
298                                                    String componentId, String nicId) {
299     MDC_DATA_DEBUG_MESSAGE
300         .debugEntryMessage(VSP_ID_COMPONENT_ID, vspId, componentId, nicId);
301
302     QuestionnaireResponse questionnaireResponse = new QuestionnaireResponse();
303     NicEntity nicQuestionnaire = nicDao.getQuestionnaireData(vspId, version, componentId, nicId);
304     VersioningUtil.validateEntityExistence(nicQuestionnaire,
305         new NicEntity(vspId, version, componentId, nicId), VspDetails.ENTITY_TYPE);
306
307     questionnaireResponse.setData(nicQuestionnaire.getQuestionnaireData());
308     questionnaireResponse.setSchema(getNicQuestionnaireSchema(null));
309
310     MDC_DATA_DEBUG_MESSAGE
311         .debugExitMessage(VSP_ID_COMPONENT_ID, vspId, componentId, nicId);
312
313     return questionnaireResponse;
314   }
315
316   @Override
317   public void updateNicQuestionnaire(String vspId, Version version, String componentId,
318                                      String nicId, String questionnaireData) {
319     MDC_DATA_DEBUG_MESSAGE.debugEntryMessage(VSP_ID_COMPONENT_ID, vspId, componentId, nicId);
320
321     getNic(vspId, version, componentId, nicId);
322
323     nicDao.updateQuestionnaireData(vspId, version, componentId, nicId, questionnaireData);
324
325     MDC_DATA_DEBUG_MESSAGE.debugExitMessage(VSP_ID_COMPONENT_ID, vspId, componentId, nicId);
326   }
327
328   protected String getNicQuestionnaireSchema(SchemaTemplateInput schemaInput) {
329     return SchemaGenerator
330         .generate(SchemaTemplateContext.questionnaire, CompositionEntityType.nic, schemaInput);
331   }
332
333   protected String getNicCompositionSchema(NicCompositionSchemaInput schemaInput) {
334     return SchemaGenerator
335         .generate(SchemaTemplateContext.composition, CompositionEntityType.nic, schemaInput);
336   }
337 }