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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
|
/*
* ============LICENSE_START=======================================================
* Copyright (C) 2021 highstreet technologies GmbH
* Modifications Copyright (C) 2021-2024 Nordix Foundation
* Modifications Copyright (C) 2021 Pantheon.tech
* Modifications Copyright (C) 2021-2022 Bell Canada
* Modifications Copyright (C) 2023 TechMahindra Ltd.
* ================================================================================
* 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.
*
* SPDX-License-Identifier: Apache-2.0
* ============LICENSE_END=========================================================
*/
package org.onap.cps.ncmp.api.impl;
import static org.onap.cps.ncmp.api.NcmpResponseStatus.CM_HANDLES_NOT_FOUND;
import static org.onap.cps.ncmp.api.NcmpResponseStatus.CM_HANDLES_NOT_READY;
import static org.onap.cps.ncmp.api.NcmpResponseStatus.CM_HANDLE_ALREADY_EXIST;
import static org.onap.cps.ncmp.api.NcmpResponseStatus.CM_HANDLE_INVALID_ID;
import static org.onap.cps.ncmp.api.impl.inventory.LockReasonCategory.MODULE_UPGRADE;
import static org.onap.cps.ncmp.api.impl.ncmppersistence.NcmpPersistence.NCMP_DMI_REGISTRY_PARENT;
import static org.onap.cps.ncmp.api.impl.ncmppersistence.NcmpPersistence.NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME;
import static org.onap.cps.ncmp.api.impl.utils.RestQueryParametersValidator.validateCmHandleQueryParameters;
import com.google.common.collect.Lists;
import com.hazelcast.map.IMap;
import java.time.OffsetDateTime;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.onap.cps.api.CpsDataService;
import org.onap.cps.ncmp.api.NetworkCmProxyCmHandleQueryService;
import org.onap.cps.ncmp.api.NetworkCmProxyDataService;
import org.onap.cps.ncmp.api.impl.events.lcm.LcmEventsCmHandleStateHandler;
import org.onap.cps.ncmp.api.impl.inventory.CmHandleQueries;
import org.onap.cps.ncmp.api.impl.inventory.CmHandleState;
import org.onap.cps.ncmp.api.impl.inventory.CompositeState;
import org.onap.cps.ncmp.api.impl.inventory.CompositeStateBuilder;
import org.onap.cps.ncmp.api.impl.inventory.CompositeStateUtils;
import org.onap.cps.ncmp.api.impl.inventory.DataStoreSyncState;
import org.onap.cps.ncmp.api.impl.inventory.InventoryPersistence;
import org.onap.cps.ncmp.api.impl.inventory.sync.ModuleOperationsUtils;
import org.onap.cps.ncmp.api.impl.operations.DmiDataOperations;
import org.onap.cps.ncmp.api.impl.operations.OperationType;
import org.onap.cps.ncmp.api.impl.trustlevel.TrustLevel;
import org.onap.cps.ncmp.api.impl.trustlevel.TrustLevelManager;
import org.onap.cps.ncmp.api.impl.utils.AlternateIdChecker;
import org.onap.cps.ncmp.api.impl.utils.CmHandleQueryConditions;
import org.onap.cps.ncmp.api.impl.utils.InventoryQueryConditions;
import org.onap.cps.ncmp.api.impl.utils.YangDataConverter;
import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle;
import org.onap.cps.ncmp.api.models.CmHandleQueryApiParameters;
import org.onap.cps.ncmp.api.models.CmHandleQueryServiceParameters;
import org.onap.cps.ncmp.api.models.CmHandleRegistrationResponse;
import org.onap.cps.ncmp.api.models.DataOperationRequest;
import org.onap.cps.ncmp.api.models.DmiPluginRegistration;
import org.onap.cps.ncmp.api.models.DmiPluginRegistrationResponse;
import org.onap.cps.ncmp.api.models.NcmpServiceCmHandle;
import org.onap.cps.spi.FetchDescendantsOption;
import org.onap.cps.spi.exceptions.AlreadyDefinedException;
import org.onap.cps.spi.exceptions.CpsException;
import org.onap.cps.spi.exceptions.DataNodeNotFoundException;
import org.onap.cps.spi.exceptions.DataValidationException;
import org.onap.cps.spi.model.ModuleDefinition;
import org.onap.cps.spi.model.ModuleReference;
import org.onap.cps.utils.JsonObjectMapper;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
@Slf4j
@Service
@RequiredArgsConstructor
public class NetworkCmProxyDataServiceImpl implements NetworkCmProxyDataService {
private static final int DELETE_BATCH_SIZE = 100;
private final JsonObjectMapper jsonObjectMapper;
private final DmiDataOperations dmiDataOperations;
private final NetworkCmProxyDataServicePropertyHandler networkCmProxyDataServicePropertyHandler;
private final InventoryPersistence inventoryPersistence;
private final CmHandleQueries cmHandleQueries;
private final NetworkCmProxyCmHandleQueryService networkCmProxyCmHandleQueryService;
private final LcmEventsCmHandleStateHandler lcmEventsCmHandleStateHandler;
private final CpsDataService cpsDataService;
private final IMap<String, Object> moduleSyncStartedOnCmHandles;
private final Map<String, TrustLevel> trustLevelPerDmiPlugin;
private final TrustLevelManager trustLevelManager;
private final AlternateIdChecker alternateIdChecker;
@Override
public DmiPluginRegistrationResponse updateDmiRegistrationAndSyncModule(
final DmiPluginRegistration dmiPluginRegistration) {
dmiPluginRegistration.validateDmiPluginRegistration();
final DmiPluginRegistrationResponse dmiPluginRegistrationResponse = new DmiPluginRegistrationResponse();
setTrustLevelPerDmiPlugin(dmiPluginRegistration);
processRemovedCmHandles(dmiPluginRegistration, dmiPluginRegistrationResponse);
processCreatedCmHandles(dmiPluginRegistration, dmiPluginRegistrationResponse);
processUpdatedCmHandles(dmiPluginRegistration, dmiPluginRegistrationResponse);
processUpgradedCmHandles(dmiPluginRegistration, dmiPluginRegistrationResponse);
return dmiPluginRegistrationResponse;
}
@Override
public Object getResourceDataForCmHandle(final String datastoreName,
final String cmHandleId,
final String resourceIdentifier,
final String optionsParamInQuery,
final String topicParamInQuery,
final String requestId) {
final ResponseEntity<?> responseEntity = dmiDataOperations.getResourceDataFromDmi(datastoreName, cmHandleId,
resourceIdentifier,
optionsParamInQuery,
topicParamInQuery,
requestId);
return responseEntity.getBody();
}
@Override
public Object getResourceDataForCmHandle(final String datastoreName,
final String cmHandleId,
final String resourceIdentifier,
final FetchDescendantsOption fetchDescendantsOption) {
return cpsDataService.getDataNodes(datastoreName, cmHandleId, resourceIdentifier,
fetchDescendantsOption).iterator().next();
}
@Override
public void executeDataOperationForCmHandles(final String topicParamInQuery,
final DataOperationRequest
dataOperationRequest,
final String requestId) {
dmiDataOperations.requestResourceDataFromDmi(topicParamInQuery, dataOperationRequest, requestId);
}
@Override
public Object writeResourceDataPassThroughRunningForCmHandle(final String cmHandleId,
final String resourceIdentifier,
final OperationType operationType,
final String requestData,
final String dataType) {
return dmiDataOperations.writeResourceDataPassThroughRunningFromDmi(cmHandleId, resourceIdentifier,
operationType, requestData, dataType);
}
@Override
public Collection<ModuleReference> getYangResourcesModuleReferences(final String cmHandleId) {
return inventoryPersistence.getYangResourcesModuleReferences(cmHandleId);
}
@Override
public Collection<ModuleDefinition> getModuleDefinitionsByCmHandleId(final String cmHandleId) {
return inventoryPersistence.getModuleDefinitionsByCmHandleId(cmHandleId);
}
@Override
public Collection<ModuleDefinition> getModuleDefinitionsByCmHandleAndModule(final String cmHandleId,
final String moduleName,
final String moduleRevision) {
return inventoryPersistence.getModuleDefinitionsByCmHandleAndModule(cmHandleId, moduleName, moduleRevision);
}
/**
* Retrieve cm handles with details for the given query parameters.
*
* @param cmHandleQueryApiParameters cm handle query parameters
* @return cm handles with details
*/
@Override
public Collection<NcmpServiceCmHandle> executeCmHandleSearch(
final CmHandleQueryApiParameters cmHandleQueryApiParameters) {
final CmHandleQueryServiceParameters cmHandleQueryServiceParameters = jsonObjectMapper.convertToValueType(
cmHandleQueryApiParameters, CmHandleQueryServiceParameters.class);
validateCmHandleQueryParameters(cmHandleQueryServiceParameters, CmHandleQueryConditions.ALL_CONDITION_NAMES);
return networkCmProxyCmHandleQueryService.queryCmHandles(cmHandleQueryServiceParameters);
}
/**
* Retrieve cm handle ids for the given query parameters.
*
* @param cmHandleQueryApiParameters cm handle query parameters
* @return cm handle ids
*/
@Override
public Collection<String> executeCmHandleIdSearch(final CmHandleQueryApiParameters cmHandleQueryApiParameters) {
final CmHandleQueryServiceParameters cmHandleQueryServiceParameters = jsonObjectMapper.convertToValueType(
cmHandleQueryApiParameters, CmHandleQueryServiceParameters.class);
validateCmHandleQueryParameters(cmHandleQueryServiceParameters, CmHandleQueryConditions.ALL_CONDITION_NAMES);
return networkCmProxyCmHandleQueryService.queryCmHandleIds(cmHandleQueryServiceParameters);
}
/**
* Set the data sync enabled flag, along with the data sync state
* based on the data sync enabled boolean for the cm handle id provided.
*
* @param cmHandleId cm handle id
* @param dataSyncEnabledTargetValue data sync enabled flag
*/
@Override
public void setDataSyncEnabled(final String cmHandleId, final Boolean dataSyncEnabledTargetValue) {
final CompositeState compositeState = inventoryPersistence.getCmHandleState(cmHandleId);
if (dataSyncEnabledTargetValue.equals(compositeState.getDataSyncEnabled())) {
log.info("Data-Sync Enabled flag is already: {} ", dataSyncEnabledTargetValue);
return;
}
if (CmHandleState.READY.equals(compositeState.getCmHandleState())) {
final DataStoreSyncState dataStoreSyncState = compositeState.getDataStores()
.getOperationalDataStore().getDataStoreSyncState();
if (Boolean.FALSE.equals(dataSyncEnabledTargetValue)
&& DataStoreSyncState.SYNCHRONIZED.equals(dataStoreSyncState)) {
// TODO : This is hard-coded for onap dmi that need to be addressed
cpsDataService.deleteDataNode(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, cmHandleId,
"/netconf-state", OffsetDateTime.now());
}
CompositeStateUtils.setDataSyncEnabledFlagWithDataSyncState(dataSyncEnabledTargetValue, compositeState);
inventoryPersistence.saveCmHandleState(cmHandleId, compositeState);
} else {
throw new CpsException("State mismatch exception.", "Cm-Handle not in READY state. Cm handle state is: "
+ compositeState.getCmHandleState());
}
}
/**
* Get all cm handle IDs by DMI plugin identifier.
*
* @param dmiPluginIdentifier DMI plugin identifier
* @return set of cm handle IDs
*/
@Override
public Collection<String> getAllCmHandleIdsByDmiPluginIdentifier(final String dmiPluginIdentifier) {
return cmHandleQueries.getCmHandleIdsByDmiPluginIdentifier(dmiPluginIdentifier);
}
/**
* Get all cm handle IDs by various properties.
*
* @param cmHandleQueryServiceParameters cm handle query parameters
* @return set of cm handle IDs
*/
@Override
public Collection<String> executeCmHandleIdSearchForInventory(
final CmHandleQueryServiceParameters cmHandleQueryServiceParameters) {
validateCmHandleQueryParameters(cmHandleQueryServiceParameters, InventoryQueryConditions.ALL_CONDITION_NAMES);
return networkCmProxyCmHandleQueryService.queryCmHandleIdsForInventory(cmHandleQueryServiceParameters);
}
/**
* Retrieve cm handle details for a given cm handle.
*
* @param cmHandleId cm handle identifier
* @return cm handle details
*/
@Override
public NcmpServiceCmHandle getNcmpServiceCmHandle(final String cmHandleId) {
return YangDataConverter.convertYangModelCmHandleToNcmpServiceCmHandle(
inventoryPersistence.getYangModelCmHandle(cmHandleId));
}
/**
* Get cm handle public properties for a given cm handle id.
*
* @param cmHandleId cm handle identifier
* @return cm handle public properties
*/
@Override
public Map<String, String> getCmHandlePublicProperties(final String cmHandleId) {
final YangModelCmHandle yangModelCmHandle = inventoryPersistence.getYangModelCmHandle(cmHandleId);
final List<YangModelCmHandle.Property> yangModelPublicProperties = yangModelCmHandle.getPublicProperties();
final Map<String, String> cmHandlePublicProperties = new HashMap<>();
YangDataConverter.asPropertiesMap(yangModelPublicProperties, cmHandlePublicProperties);
return cmHandlePublicProperties;
}
/**
* Get cm handle composite state for a given cm handle id.
*
* @param cmHandleId cm handle identifier
* @return cm handle state
*/
@Override
public CompositeState getCmHandleCompositeState(final String cmHandleId) {
return inventoryPersistence.getYangModelCmHandle(cmHandleId).getCompositeState();
}
/**
* THis method registers a cm handle and initiates modules sync.
*
* @param dmiPluginRegistration dmi plugin registration information.
* @return cm-handle registration response for create cm-handle requests.
*/
public List<CmHandleRegistrationResponse> parseAndProcessCreatedCmHandlesInRegistration(
final DmiPluginRegistration dmiPluginRegistration, final Collection<String> acceptedCmHandleIds) {
final List<NcmpServiceCmHandle> cmHandlesToBeCreated = dmiPluginRegistration.getCreatedCmHandles();
final Map<String, TrustLevel> initialTrustLevelPerCmHandleId = new HashMap<>(cmHandlesToBeCreated.size());
final List<YangModelCmHandle> yangModelCmHandles = new ArrayList<>(cmHandlesToBeCreated.size());
cmHandlesToBeCreated
.forEach(cmHandle -> {
if (acceptedCmHandleIds.contains(cmHandle.getCmHandleId())) {
final YangModelCmHandle yangModelCmHandle = YangModelCmHandle.toYangModelCmHandle(
dmiPluginRegistration.getDmiPlugin(),
dmiPluginRegistration.getDmiDataPlugin(),
dmiPluginRegistration.getDmiModelPlugin(),
cmHandle,
cmHandle.getModuleSetTag(),
cmHandle.getAlternateId());
yangModelCmHandles.add(yangModelCmHandle);
initialTrustLevelPerCmHandleId.put(cmHandle.getCmHandleId(),
cmHandle.getRegistrationTrustLevel());
}
});
return registerNewCmHandles(yangModelCmHandles, initialTrustLevelPerCmHandleId);
}
protected List<CmHandleRegistrationResponse> parseAndProcessDeletedCmHandlesInRegistration(
final List<String> tobeRemovedCmHandles) {
final List<CmHandleRegistrationResponse> cmHandleRegistrationResponses =
new ArrayList<>(tobeRemovedCmHandles.size());
final Collection<YangModelCmHandle> yangModelCmHandles =
inventoryPersistence.getYangModelCmHandles(tobeRemovedCmHandles);
updateCmHandleStateBatch(yangModelCmHandles, CmHandleState.DELETING);
final Set<String> notDeletedCmHandles = new HashSet<>();
for (final List<String> tobeRemovedCmHandleBatch : Lists.partition(tobeRemovedCmHandles, DELETE_BATCH_SIZE)) {
try {
batchDeleteCmHandlesFromDbAndModuleSyncMap(tobeRemovedCmHandleBatch);
tobeRemovedCmHandleBatch.forEach(cmHandleId ->
cmHandleRegistrationResponses.add(CmHandleRegistrationResponse.createSuccessResponse(cmHandleId)));
} catch (final RuntimeException batchException) {
log.error("Unable to de-register cm-handle batch, retrying on each cm handle");
for (final String cmHandleId : tobeRemovedCmHandleBatch) {
final CmHandleRegistrationResponse cmHandleRegistrationResponse =
deleteCmHandleAndGetCmHandleRegistrationResponse(cmHandleId);
cmHandleRegistrationResponses.add(cmHandleRegistrationResponse);
if (cmHandleRegistrationResponse.getStatus() != CmHandleRegistrationResponse.Status.SUCCESS) {
notDeletedCmHandles.add(cmHandleId);
}
}
}
}
yangModelCmHandles.removeIf(yangModelCmHandle -> notDeletedCmHandles.contains(yangModelCmHandle.getId()));
updateCmHandleStateBatch(yangModelCmHandles, CmHandleState.DELETED);
return cmHandleRegistrationResponses;
}
private void processRemovedCmHandles(final DmiPluginRegistration dmiPluginRegistration,
final DmiPluginRegistrationResponse dmiPluginRegistrationResponse) {
if (!dmiPluginRegistration.getRemovedCmHandles().isEmpty()) {
dmiPluginRegistrationResponse.setRemovedCmHandles(
parseAndProcessDeletedCmHandlesInRegistration(dmiPluginRegistration.getRemovedCmHandles()));
}
}
private void processCreatedCmHandles(final DmiPluginRegistration dmiPluginRegistration,
final DmiPluginRegistrationResponse dmiPluginRegistrationResponse) {
final Collection<String> acceptedCmHandleIds = alternateIdChecker
.getIdsOfCmHandlesWithAcceptableAlternateId(dmiPluginRegistration.getCreatedCmHandles());
if (!acceptedCmHandleIds.isEmpty()) {
dmiPluginRegistrationResponse.setCreatedCmHandles(
parseAndProcessCreatedCmHandlesInRegistration(dmiPluginRegistration, acceptedCmHandleIds));
}
}
private void processUpdatedCmHandles(final DmiPluginRegistration dmiPluginRegistration,
final DmiPluginRegistrationResponse dmiPluginRegistrationResponse) {
if (!dmiPluginRegistration.getUpdatedCmHandles().isEmpty()) {
dmiPluginRegistrationResponse.setUpdatedCmHandles(
networkCmProxyDataServicePropertyHandler
.updateCmHandleProperties(dmiPluginRegistration.getUpdatedCmHandles()));
}
}
private void processUpgradedCmHandles(final DmiPluginRegistration dmiPluginRegistration,
final DmiPluginRegistrationResponse dmiPluginRegistrationResponse) {
if (dmiPluginRegistration.getUpgradedCmHandles() != null
&& !dmiPluginRegistration.getUpgradedCmHandles().getCmHandles().isEmpty()) {
dmiPluginRegistrationResponse.setUpgradedCmHandles(
parseAndProcessUpgradedCmHandlesInRegistration(dmiPluginRegistration));
}
}
protected List<CmHandleRegistrationResponse> parseAndProcessUpgradedCmHandlesInRegistration(
final DmiPluginRegistration dmiPluginRegistration) {
final List<String> upgradedCmHandleIds = dmiPluginRegistration.getUpgradedCmHandles().getCmHandles();
final String upgradedModuleSetTag = dmiPluginRegistration.getUpgradedCmHandles().getModuleSetTag();
final Map<YangModelCmHandle, CmHandleState> acceptedCmHandleStatePerCmHandle
= new HashMap<>(upgradedCmHandleIds.size());
final List<CmHandleRegistrationResponse> cmHandleUpgradeResponses = new ArrayList<>(upgradedCmHandleIds.size());
for (final String cmHandleId : upgradedCmHandleIds) {
try {
final YangModelCmHandle yangModelCmHandle = inventoryPersistence.getYangModelCmHandle(cmHandleId);
if (yangModelCmHandle.getCompositeState().getCmHandleState() == CmHandleState.READY) {
if (moduleUpgradeCanBeSkipped(yangModelCmHandle, upgradedModuleSetTag)) {
cmHandleUpgradeResponses.add(CmHandleRegistrationResponse.createSuccessResponse(cmHandleId));
} else {
updateYangModelCmHandleForUpgrade(yangModelCmHandle, upgradedModuleSetTag);
acceptedCmHandleStatePerCmHandle.put(yangModelCmHandle, CmHandleState.LOCKED);
}
} else {
cmHandleUpgradeResponses.add(
CmHandleRegistrationResponse.createFailureResponse(cmHandleId, CM_HANDLES_NOT_READY));
}
} catch (final DataNodeNotFoundException dataNodeNotFoundException) {
log.error("Unable to find data node for cm handle id : {} , caused by : {}",
cmHandleId, dataNodeNotFoundException.getMessage());
cmHandleUpgradeResponses.add(
CmHandleRegistrationResponse.createFailureResponse(cmHandleId, CM_HANDLES_NOT_FOUND));
} catch (final DataValidationException dataValidationException) {
log.error("Unable to upgrade cm handle id: {}, caused by : {}",
cmHandleId, dataValidationException.getMessage());
cmHandleUpgradeResponses.add(
CmHandleRegistrationResponse.createFailureResponse(cmHandleId, CM_HANDLE_INVALID_ID));
}
}
cmHandleUpgradeResponses.addAll(upgradeCmHandles(acceptedCmHandleStatePerCmHandle));
return cmHandleUpgradeResponses;
}
private static boolean moduleUpgradeCanBeSkipped(final YangModelCmHandle yangModelCmHandle,
final String upgradedModuleSetTag) {
if (StringUtils.isBlank(upgradedModuleSetTag)) {
return false;
}
return yangModelCmHandle.getModuleSetTag().equals(upgradedModuleSetTag);
}
private static void updateYangModelCmHandleForUpgrade(final YangModelCmHandle yangModelCmHandle,
final String upgradedModuleSetTag) {
final String lockReasonWithModuleSetTag = String.format(ModuleOperationsUtils.MODULE_SET_TAG_MESSAGE_FORMAT,
upgradedModuleSetTag);
yangModelCmHandle.setCompositeState(new CompositeStateBuilder().withCmHandleState(CmHandleState.READY)
.withLockReason(MODULE_UPGRADE, lockReasonWithModuleSetTag).build());
}
private CmHandleRegistrationResponse deleteCmHandleAndGetCmHandleRegistrationResponse(final String cmHandleId) {
try {
deleteCmHandleFromDbAndModuleSyncMap(cmHandleId);
return CmHandleRegistrationResponse.createSuccessResponse(cmHandleId);
} catch (final DataNodeNotFoundException dataNodeNotFoundException) {
log.error("Unable to find dataNode for cmHandleId : {} , caused by : {}",
cmHandleId, dataNodeNotFoundException.getMessage());
return CmHandleRegistrationResponse.createFailureResponse(cmHandleId, CM_HANDLES_NOT_FOUND);
} catch (final DataValidationException dataValidationException) {
log.error("Unable to de-register cm-handle id: {}, caused by: {}",
cmHandleId, dataValidationException.getMessage());
return CmHandleRegistrationResponse.createFailureResponse(cmHandleId, CM_HANDLE_INVALID_ID);
} catch (final Exception exception) {
log.error("Unable to de-register cm-handle id : {} , caused by : {}", cmHandleId, exception.getMessage());
return CmHandleRegistrationResponse.createFailureResponse(cmHandleId, exception);
}
}
private void updateCmHandleStateBatch(final Collection<YangModelCmHandle> yangModelCmHandles,
final CmHandleState cmHandleState) {
final Map<YangModelCmHandle, CmHandleState> cmHandleStatePerCmHandle = new HashMap<>(yangModelCmHandles.size());
yangModelCmHandles.forEach(yangModelCmHandle -> cmHandleStatePerCmHandle.put(yangModelCmHandle, cmHandleState));
lcmEventsCmHandleStateHandler.updateCmHandleStateBatch(cmHandleStatePerCmHandle);
}
private void deleteCmHandleFromDbAndModuleSyncMap(final String cmHandleId) {
inventoryPersistence.deleteSchemaSetWithCascade(cmHandleId);
inventoryPersistence.deleteDataNode(NCMP_DMI_REGISTRY_PARENT + "/cm-handles[@id='" + cmHandleId
+ "']");
removeDeletedCmHandleFromModuleSyncMap(cmHandleId);
}
private void batchDeleteCmHandlesFromDbAndModuleSyncMap(final Collection<String> tobeRemovedCmHandles) {
inventoryPersistence.deleteSchemaSetsWithCascade(tobeRemovedCmHandles);
inventoryPersistence.deleteDataNodes(mapCmHandleIdsToXpaths(tobeRemovedCmHandles));
tobeRemovedCmHandles.forEach(this::removeDeletedCmHandleFromModuleSyncMap);
}
private Collection<String> mapCmHandleIdsToXpaths(final Collection<String> cmHandles) {
return cmHandles.stream()
.map(cmHandleId -> NCMP_DMI_REGISTRY_PARENT + "/cm-handles[@id='" + cmHandleId + "']")
.collect(Collectors.toSet());
}
// CPS-1239 Robustness cleaning of in progress cache
private void removeDeletedCmHandleFromModuleSyncMap(final String deletedCmHandleId) {
if (moduleSyncStartedOnCmHandles.remove(deletedCmHandleId) != null) {
log.debug("{} removed from in progress map", deletedCmHandleId);
}
}
private List<CmHandleRegistrationResponse> registerNewCmHandles(final List<YangModelCmHandle> yangModelCmHandles,
final Map<String, TrustLevel>
initialTrustLevelPerCmHandleId) {
final Set<String> cmHandleIds = initialTrustLevelPerCmHandleId.keySet();
try {
lcmEventsCmHandleStateHandler.initiateStateAdvised(yangModelCmHandles);
trustLevelManager.handleInitialRegistrationOfTrustLevels(initialTrustLevelPerCmHandleId);
return CmHandleRegistrationResponse.createSuccessResponses(cmHandleIds);
} catch (final AlreadyDefinedException alreadyDefinedException) {
return CmHandleRegistrationResponse.createFailureResponses(
alreadyDefinedException.getAlreadyDefinedObjectNames(), CM_HANDLE_ALREADY_EXIST);
} catch (final Exception exception) {
return CmHandleRegistrationResponse.createFailureResponses(cmHandleIds, exception);
}
}
private List<CmHandleRegistrationResponse> upgradeCmHandles(final Map<YangModelCmHandle, CmHandleState>
cmHandleStatePerCmHandle) {
final List<String> cmHandleIds = getCmHandleIds(cmHandleStatePerCmHandle);
log.info("Moving cm handles : {} into locked (for upgrade) state.", cmHandleIds);
try {
lcmEventsCmHandleStateHandler.updateCmHandleStateBatch(cmHandleStatePerCmHandle);
return CmHandleRegistrationResponse.createSuccessResponses(cmHandleIds);
} catch (final Exception e) {
log.error("Unable to update cmHandleIds : {} , caused by : {}", cmHandleIds, e.getMessage());
return CmHandleRegistrationResponse.createFailureResponses(cmHandleIds, e);
}
}
private static List<String> getCmHandleIds(final Map<YangModelCmHandle, CmHandleState> cmHandleStatePerCmHandle) {
return cmHandleStatePerCmHandle.keySet().stream().map(YangModelCmHandle::getId).toList();
}
private void setTrustLevelPerDmiPlugin(final DmiPluginRegistration dmiPluginRegistration) {
if (DmiPluginRegistration.isNullEmptyOrBlank(dmiPluginRegistration.getDmiDataPlugin())) {
trustLevelPerDmiPlugin.put(dmiPluginRegistration.getDmiPlugin(), TrustLevel.COMPLETE);
} else {
trustLevelPerDmiPlugin.put(dmiPluginRegistration.getDmiDataPlugin(), TrustLevel.COMPLETE);
}
}
}
|