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