2 * Copyright © 2016-2017 European Support Limited
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.openecomp.sdc.vendorsoftwareproduct.impl;
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;
56 import java.util.Collection;
58 import java.util.stream.Collectors;
60 public class NicManagerImpl implements NicManager {
61 private static final MdcDataDebugMessage MDC_DATA_DEBUG_MESSAGE = new MdcDataDebugMessage();
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";
69 public NicManagerImpl(NicDao nicDao,
70 CompositionEntityDataManager compositionEntityDataManager,
71 NetworkManager networkManager,
72 VendorSoftwareProductInfoDao vspInfoDao) {
74 this.compositionEntityDataManager = compositionEntityDataManager;
75 this.networkManager = networkManager;
76 this.vspInfoDao = vspInfoDao;
80 public Collection<NicEntity> listNics(String vspId, Version version, String componentId) {
81 MDC_DATA_DEBUG_MESSAGE.debugEntryMessage(VSP_ID_COMPONENT_ID, vspId, componentId);
83 Collection<NicEntity> nics = nicDao.list(new NicEntity(vspId, version, componentId, null));
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);
94 MDC_DATA_DEBUG_MESSAGE.debugExitMessage(VSP_ID_COMPONENT_ID, vspId, componentId);
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()));
106 public NicEntity createNic(NicEntity nic) {
107 MDC_DATA_DEBUG_MESSAGE.debugEntryMessage(VSP_ID_COMPONENT_ID, nic.getVspId(),
108 nic.getComponentId());
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);
121 createdNic = compositionEntityDataManager.createNic(nic);
124 MDC_DATA_DEBUG_MESSAGE
125 .debugExitMessage(VSP_ID_COMPONENT_ID, nic.getVspId(), nic.getComponentId());
131 private void validateNic(NicEntity nic) {
132 Collection<NicEntity> listNics = listNics(nic.getVspId(), nic.getVersion(), nic
134 String networkId = nic.getNicCompositionData().getNetworkId();
135 NetworkType networkType = nic.getNicCompositionData().getNetworkType();
136 String networkDescription = nic.getNicCompositionData().getNetworkDescription();
138 if (!nic.getNicCompositionData().getName()
139 .matches(VendorSoftwareProductConstants.NAME_PATTERN)) {
140 ErrorCode errorCode = NicErrorBuilder
141 .getNicNameFormatErrorBuilder(VendorSoftwareProductConstants.NAME_PATTERN);
143 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
144 LoggerTragetServiceName.CREATE_NIC, ErrorLevel.ERROR.name(),
145 errorCode.id(),errorCode.message());
147 throw new CoreException(errorCode);
150 validateNics(nic, listNics);
152 if (networkType.equals(NetworkType.Internal)) {
153 validateInternalNetworkType(nic, networkId, networkDescription);
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);
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);
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);
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);
201 public CompositionEntityResponse<Nic> getNic(String vspId, Version version, String componentId,
203 MDC_DATA_DEBUG_MESSAGE
204 .debugEntryMessage("VSP id, component id, nic id", vspId, componentId, nicId);
206 NicEntity nicEntity = getValidatedNic(vspId, version, componentId, nicId);
207 Nic nic = nicEntity.getNicCompositionData();
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());
216 CompositionEntityResponse<Nic> response = new CompositionEntityResponse<>();
217 response.setId(nicId);
218 response.setData(nic);
219 response.setSchema(getNicCompositionSchema(schemaInput));
221 MDC_DATA_DEBUG_MESSAGE
222 .debugExitMessage(VSP_ID_COMPONENT_ID, vspId, componentId, nicId);
228 private NicEntity getValidatedNic(String vspId, Version version, String componentId,
230 NicEntity retrieved = nicDao.get(new NicEntity(vspId, version, componentId, nicId));
232 .validateEntityExistence(retrieved, new NicEntity(vspId, version, componentId, nicId),
233 VspDetails.ENTITY_TYPE);
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);
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);
252 NicEntity nicEntity = getValidatedNic(vspId, version, componentId, nicId);
253 nicDao.delete(nicEntity);
255 MDC_DATA_DEBUG_MESSAGE
256 .debugExitMessage(VSP_ID_COMPONENT_ID, vspId, componentId, nicId);
260 public CompositionEntityValidationData updateNic(NicEntity nic) {
261 MDC_DATA_DEBUG_MESSAGE
262 .debugEntryMessage(VSP_ID_COMPONENT_ID, nic.getVspId(), nic.getComponentId(),
265 NicEntity retrieved =
266 getValidatedNic(nic.getVspId(), nic.getVersion(), nic.getComponentId(), nic.getId());
268 NicCompositionSchemaInput schemaInput = new NicCompositionSchemaInput();
269 schemaInput.setManual(vspInfoDao.isManual(nic.getVspId(), nic.getVersion()));
270 schemaInput.setNic(retrieved.getNicCompositionData());
272 if (schemaInput.isManual() && !nic.getNicCompositionData().getName()
273 .matches(VendorSoftwareProductConstants.NAME_PATTERN)) {
274 ErrorCode errorCode = NicErrorBuilder
275 .getNicNameFormatErrorBuilder(VendorSoftwareProductConstants.NAME_PATTERN);
277 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
278 LoggerTragetServiceName.UPDATE_NIC, ErrorLevel.ERROR.name(),
279 errorCode.id(),errorCode.message());
281 throw new CoreException(errorCode);
284 CompositionEntityValidationData validationData = compositionEntityDataManager
285 .validateEntity(nic, SchemaTemplateContext.composition, schemaInput);
286 if (CollectionUtils.isEmpty(validationData.getErrors())) {
289 MDC_DATA_DEBUG_MESSAGE
290 .debugExitMessage(VSP_ID_COMPONENT_ID, nic.getVspId(), nic.getComponentId(),
293 return validationData;
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);
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);
307 questionnaireResponse.setData(nicQuestionnaire.getQuestionnaireData());
308 questionnaireResponse.setSchema(getNicQuestionnaireSchema(null));
310 MDC_DATA_DEBUG_MESSAGE
311 .debugExitMessage(VSP_ID_COMPONENT_ID, vspId, componentId, nicId);
313 return questionnaireResponse;
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);
321 getNic(vspId, version, componentId, nicId);
323 nicDao.updateQuestionnaireData(vspId, version, componentId, nicId, questionnaireData);
325 MDC_DATA_DEBUG_MESSAGE.debugExitMessage(VSP_ID_COMPONENT_ID, vspId, componentId, nicId);
328 protected String getNicQuestionnaireSchema(SchemaTemplateInput schemaInput) {
329 return SchemaGenerator
330 .generate(SchemaTemplateContext.questionnaire, CompositionEntityType.nic, schemaInput);
333 protected String getNicCompositionSchema(NicCompositionSchemaInput schemaInput) {
334 return SchemaGenerator
335 .generate(SchemaTemplateContext.composition, CompositionEntityType.nic, schemaInput);