2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.vendorsoftwareproduct.impl;
23 import org.apache.commons.collections4.CollectionUtils;
24 import org.openecomp.sdc.common.errors.CoreException;
25 import org.openecomp.sdc.common.errors.ErrorCode;
26 import org.openecomp.sdc.datatypes.error.ErrorLevel;
27 import org.openecomp.sdc.logging.context.impl.MdcDataDebugMessage;
28 import org.openecomp.sdc.logging.context.impl.MdcDataErrorMessage;
29 import org.openecomp.sdc.logging.types.LoggerConstants;
30 import org.openecomp.sdc.logging.types.LoggerErrorCode;
31 import org.openecomp.sdc.logging.types.LoggerTragetServiceName;
32 import org.openecomp.sdc.vendorsoftwareproduct.NetworkManager;
33 import org.openecomp.sdc.vendorsoftwareproduct.NicManager;
34 import org.openecomp.sdc.vendorsoftwareproduct.VendorSoftwareProductConstants;
35 import org.openecomp.sdc.vendorsoftwareproduct.dao.NicDao;
36 import org.openecomp.sdc.vendorsoftwareproduct.dao.VendorSoftwareProductInfoDao;
37 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.NetworkEntity;
38 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.NicEntity;
39 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspDetails;
40 import org.openecomp.sdc.vendorsoftwareproduct.errors.DeleteNicErrorBuilder;
41 import org.openecomp.sdc.vendorsoftwareproduct.errors.DuplicateNicInComponentErrorBuilder;
42 import org.openecomp.sdc.vendorsoftwareproduct.errors.NicErrorBuilder;
43 import org.openecomp.sdc.vendorsoftwareproduct.errors.NicInternalNetworkErrorBuilder;
44 import org.openecomp.sdc.vendorsoftwareproduct.errors.NicNetworkIdNotAllowedExternalNetworkErrorBuilder;
45 import org.openecomp.sdc.vendorsoftwareproduct.errors.NotSupportedHeatOnboardMethodErrorBuilder;
46 import org.openecomp.sdc.vendorsoftwareproduct.services.composition.CompositionEntityDataManager;
47 import org.openecomp.sdc.vendorsoftwareproduct.services.schemagenerator.SchemaGenerator;
48 import org.openecomp.sdc.vendorsoftwareproduct.types.CompositionEntityResponse;
49 import org.openecomp.sdc.vendorsoftwareproduct.types.QuestionnaireResponse;
50 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.CompositionEntityType;
51 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.CompositionEntityValidationData;
52 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.Network;
53 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.NetworkType;
54 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.Nic;
55 import org.openecomp.sdc.vendorsoftwareproduct.types.schemagenerator.NicCompositionSchemaInput;
56 import org.openecomp.sdc.vendorsoftwareproduct.types.schemagenerator.SchemaTemplateContext;
57 import org.openecomp.sdc.vendorsoftwareproduct.types.schemagenerator.SchemaTemplateInput;
58 import org.openecomp.sdc.versioning.VersioningUtil;
59 import org.openecomp.sdc.versioning.dao.types.Version;
61 import java.util.Collection;
63 import java.util.stream.Collectors;
65 public class NicManagerImpl implements NicManager {
66 private static MdcDataDebugMessage mdcDataDebugMessage = new MdcDataDebugMessage();
68 private NicDao nicDao;
69 private CompositionEntityDataManager compositionEntityDataManager;
70 private NetworkManager networkManager;
71 private VendorSoftwareProductInfoDao vspInfoDao;
73 public NicManagerImpl(NicDao nicDao,
74 CompositionEntityDataManager compositionEntityDataManager,
75 NetworkManager networkManager,
76 VendorSoftwareProductInfoDao vspInfoDao) {
78 this.compositionEntityDataManager = compositionEntityDataManager;
79 this.networkManager = networkManager;
80 this.vspInfoDao = vspInfoDao;
84 public Collection<NicEntity> listNics(String vspId, Version version, String componentId) {
85 mdcDataDebugMessage.debugEntryMessage("VSP id, component id", vspId, componentId);
87 Collection<NicEntity> nics = nicDao.list(new NicEntity(vspId, version, componentId, null));
89 if (!nics.isEmpty()) {
90 Map<String, String> networksNameById = listNetworksNameById(vspId, version);
91 nics.forEach(nicEntity -> {
92 Nic nic = nicEntity.getNicCompositionData();
93 nic.setNetworkName(networksNameById.get(nic.getNetworkId()));
94 nicEntity.setNicCompositionData(nic);
98 mdcDataDebugMessage.debugExitMessage("VSP id, component id", vspId, componentId);
103 private Map<String, String> listNetworksNameById(String vspId, Version version) {
104 Collection<NetworkEntity> networks = networkManager.listNetworks(vspId, version);
105 return networks.stream().collect(Collectors.toMap(NetworkEntity::getId,
106 networkEntity -> networkEntity.getNetworkCompositionData().getName()));
110 public NicEntity createNic(NicEntity nic) {
111 mdcDataDebugMessage.debugEntryMessage("VSP id, component id", nic.getVspId(),
112 nic.getComponentId());
114 NicEntity createdNic;
115 if (!vspInfoDao.isManual(nic.getVspId(), nic.getVersion())) {
116 ErrorCode onboardingMethodUpdateErrorCode = NotSupportedHeatOnboardMethodErrorBuilder
117 .getAddNicNotSupportedHeatOnboardMethodErrorBuilder();
118 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
119 LoggerTragetServiceName.CREATE_NIC, ErrorLevel.ERROR.name(),
120 LoggerErrorCode.PERMISSION_ERROR.getErrorCode(),
121 onboardingMethodUpdateErrorCode.message());
122 throw new CoreException(onboardingMethodUpdateErrorCode);
125 createdNic = compositionEntityDataManager.createNic(nic);
129 .debugExitMessage("VSP id, component id", nic.getVspId(), nic.getComponentId());
135 private void validateNic(NicEntity nic) {
136 Collection<NicEntity> listNics = listNics(nic.getVspId(), nic.getVersion(), nic
138 String networkId = nic.getNicCompositionData().getNetworkId();
139 NetworkType networkType = nic.getNicCompositionData().getNetworkType();
140 String networkDescription = nic.getNicCompositionData().getNetworkDescription();
142 if(!nic.getNicCompositionData().getName().matches(VendorSoftwareProductConstants.NAME_PATTERN))
144 ErrorCode errorCode = NicErrorBuilder.getNicNameFormatErrorBuilder(nic
145 .getNicCompositionData().getName(), VendorSoftwareProductConstants.NAME_PATTERN);
147 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
148 LoggerTragetServiceName.CREATE_NIC, ErrorLevel.ERROR.name(),
149 errorCode.id(),errorCode.message());
151 throw new CoreException(errorCode);
154 listNics.forEach(nicEntity -> {
155 Nic nicdata = nicEntity.getNicCompositionData();
156 if (nic.getNicCompositionData().getName().equalsIgnoreCase(nicdata.getName())) {
157 final ErrorCode duplicateNicInComponentErrorBuilder =
158 new DuplicateNicInComponentErrorBuilder(nic.getNicCompositionData().getName(),
159 nic.getComponentId()).build();
160 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
161 LoggerTragetServiceName.CREATE_NIC, ErrorLevel.ERROR.name(),
162 LoggerErrorCode.DATA_ERROR.getErrorCode(),
163 duplicateNicInComponentErrorBuilder.message());
164 throw new CoreException(duplicateNicInComponentErrorBuilder);
169 if (networkType.equals(NetworkType.Internal)) {
170 if (!(networkId == null || networkId.equals(""))) {
171 //NetworkEntity ne = getNetwork(nic.getVspId(), activeVersion, networkId);
172 final CompositionEntityResponse<Network> network =
173 networkManager.getNetwork(nic.getVspId(), nic.getVersion(), networkId);
176 if (!(networkDescription == null || networkDescription.equals(""))) {
177 final ErrorCode nicNetworkDescriptionErrorBuilder =
178 NicInternalNetworkErrorBuilder.getNetworkDescriptionInternalNetworkErrorBuilder();
179 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
180 LoggerTragetServiceName.CREATE_NIC, ErrorLevel.ERROR.name(),
181 LoggerErrorCode.DATA_ERROR.getErrorCode(), nicNetworkDescriptionErrorBuilder.message());
182 throw new CoreException(nicNetworkDescriptionErrorBuilder);
185 } else if (networkType.equals(NetworkType.External)) {
186 if (!(networkId == null || networkId.equals(""))) {
187 final ErrorCode nicNetworkIdNotAllowedExternalNetworkErrorBuilder =
188 new NicNetworkIdNotAllowedExternalNetworkErrorBuilder().build();
189 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
190 LoggerTragetServiceName.CREATE_NIC, ErrorLevel.ERROR.name(),
191 LoggerErrorCode.DATA_ERROR.getErrorCode(),
192 nicNetworkIdNotAllowedExternalNetworkErrorBuilder.message());
193 throw new CoreException(nicNetworkIdNotAllowedExternalNetworkErrorBuilder);
199 public CompositionEntityResponse<Nic> getNic(String vspId, Version version, String componentId,
202 .debugEntryMessage("VSP id, component id, nic id", vspId, componentId, nicId);
204 NicEntity nicEntity = getValidatedNic(vspId, version, componentId, nicId);
205 Nic nic = nicEntity.getNicCompositionData();
207 NicCompositionSchemaInput schemaInput = new NicCompositionSchemaInput();
208 schemaInput.setManual(vspInfoDao.isManual(vspId, version));
209 schemaInput.setNic(nic);
210 Map<String, String> networksNameById = listNetworksNameById(vspId, version);
211 nic.setNetworkName(networksNameById.get(nic.getNetworkId()));
212 schemaInput.setNetworkIds(networksNameById.keySet());
214 CompositionEntityResponse<Nic> response = new CompositionEntityResponse<>();
215 response.setId(nicId);
216 response.setData(nic);
217 response.setSchema(getNicCompositionSchema(schemaInput));
220 .debugExitMessage("VSP id, component id", vspId, componentId, nicId);
226 private NicEntity getValidatedNic(String vspId, Version version, String componentId,
228 NicEntity retrieved = nicDao.get(new NicEntity(vspId, version, componentId, nicId));
230 .validateEntityExistence(retrieved, new NicEntity(vspId, version, componentId, nicId),
231 VspDetails.ENTITY_TYPE);
236 public void deleteNic(String vspId, Version version, String componentId, String nicId) {
238 .debugEntryMessage("VSP id, component id", vspId, componentId, nicId);
240 if (!vspInfoDao.isManual(vspId, version)) {
241 final ErrorCode deleteNicErrorBuilder =
242 DeleteNicErrorBuilder.getDeleteNicForHeatOnboardedVspErrorBuilder();
243 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
244 LoggerTragetServiceName.DELETE_NIC, ErrorLevel.ERROR.name(),
245 LoggerErrorCode.PERMISSION_ERROR.getErrorCode(),
246 deleteNicErrorBuilder.message());
247 throw new CoreException(deleteNicErrorBuilder);
250 NicEntity nicEntity = getValidatedNic(vspId, version, componentId, nicId);
251 nicDao.delete(nicEntity);
254 .debugExitMessage("VSP id, component id", vspId, componentId, nicId);
258 public CompositionEntityValidationData updateNic(NicEntity nic) {
260 .debugEntryMessage("VSP id, component id", nic.getVspId(), nic.getComponentId(),
263 NicEntity retrieved =
264 getValidatedNic(nic.getVspId(), nic.getVersion(), nic.getComponentId(), nic.getId());
266 NicCompositionSchemaInput schemaInput = new NicCompositionSchemaInput();
267 schemaInput.setManual(vspInfoDao.isManual(nic.getVspId(), nic.getVersion()));
268 schemaInput.setNic(retrieved.getNicCompositionData());
270 if(schemaInput.isManual() && !nic.getNicCompositionData().getName().matches(VendorSoftwareProductConstants.NAME_PATTERN))
272 ErrorCode errorCode = NicErrorBuilder.getNicNameFormatErrorBuilder(nic
273 .getNicCompositionData().getName(), VendorSoftwareProductConstants.NAME_PATTERN);
275 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
276 LoggerTragetServiceName.UPDATE_NIC, ErrorLevel.ERROR.name(),
277 errorCode.id(),errorCode.message());
279 throw new CoreException(errorCode);
282 CompositionEntityValidationData validationData = compositionEntityDataManager
283 .validateEntity(nic, SchemaTemplateContext.composition, schemaInput);
284 if (CollectionUtils.isEmpty(validationData.getErrors())) {
288 .debugExitMessage("VSP id, component id", nic.getVspId(), nic.getComponentId(),
291 return validationData;
295 public QuestionnaireResponse getNicQuestionnaire(String vspId, Version version,
296 String componentId, String nicId) {
298 .debugEntryMessage("VSP id, component id", vspId, componentId, nicId);
300 QuestionnaireResponse questionnaireResponse = new QuestionnaireResponse();
301 NicEntity nicQuestionnaire = nicDao.getQuestionnaireData(vspId, version, componentId, nicId);
302 VersioningUtil.validateEntityExistence(nicQuestionnaire,
303 new NicEntity(vspId, version, componentId, nicId), VspDetails.ENTITY_TYPE);
305 questionnaireResponse.setData(nicQuestionnaire.getQuestionnaireData());
306 questionnaireResponse.setSchema(getNicQuestionnaireSchema(null));
309 .debugExitMessage("VSP id, component id", vspId, componentId, nicId);
311 return questionnaireResponse;
315 public void updateNicQuestionnaire(String vspId, Version version, String componentId,
316 String nicId, String questionnaireData) {
317 mdcDataDebugMessage.debugEntryMessage("VSP id, component id", vspId, componentId, nicId);
319 getNic(vspId, version, componentId, nicId);
321 nicDao.updateQuestionnaireData(vspId, version, componentId, nicId, questionnaireData);
323 mdcDataDebugMessage.debugExitMessage("VSP id, component id", vspId, componentId, nicId);
326 protected String getNicQuestionnaireSchema(SchemaTemplateInput schemaInput) {
327 return SchemaGenerator
328 .generate(SchemaTemplateContext.questionnaire, CompositionEntityType.nic, schemaInput);
331 protected String getNicCompositionSchema(NicCompositionSchemaInput schemaInput) {
332 return SchemaGenerator
333 .generate(SchemaTemplateContext.composition, CompositionEntityType.nic, schemaInput);