diff options
author | DylanB95EST <dylan.byrne@est.tech> | 2022-05-19 14:09:58 +0100 |
---|---|---|
committer | DylanB95EST <dylan.byrne@est.tech> | 2022-06-01 14:48:21 +0100 |
commit | 912c86dec96e675c2635298cea8869d014938042 (patch) | |
tree | 504061c0e1f9782da7e676da428b67b0d4de9eee /cps-ncmp-service/src/main | |
parent | 5d3ed6ff2fe76e2510867379fdf58d4e99b2cc9f (diff) |
Module Sync Lock State implementation
Implementation of Lock state for module sync watchdog
Cm Handle state is locked if any exception is found
during sync process
Make changes around READY state method in line with the
new schema set
Add last updated time to composite state
Remove running datastore references as this is being done
at a later time
Issue-ID: CPS-875
Change-Id: I6bd159faefef2fa84dbf536c292ff0a132793381
Signed-off-by: DylanB95EST <dylan.byrne@est.tech>
Diffstat (limited to 'cps-ncmp-service/src/main')
11 files changed, 192 insertions, 87 deletions
diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/NetworkCmProxyDataServiceImpl.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/NetworkCmProxyDataServiceImpl.java index 64942e4bbb..717cae565c 100755 --- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/NetworkCmProxyDataServiceImpl.java +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/NetworkCmProxyDataServiceImpl.java @@ -48,8 +48,8 @@ import org.onap.cps.api.CpsModuleService; import org.onap.cps.ncmp.api.NetworkCmProxyDataService; import org.onap.cps.ncmp.api.impl.operations.DmiDataOperations; import org.onap.cps.ncmp.api.impl.operations.DmiOperations; -import org.onap.cps.ncmp.api.impl.operations.YangModelCmHandleRetriever; import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle; +import org.onap.cps.ncmp.api.inventory.InventoryPersistence; import org.onap.cps.ncmp.api.inventory.sync.ModuleSyncService; import org.onap.cps.ncmp.api.models.CmHandleQueryApiParameters; import org.onap.cps.ncmp.api.models.CmHandleRegistrationResponse; @@ -84,7 +84,7 @@ public class NetworkCmProxyDataServiceImpl implements NetworkCmProxyDataService private final NetworkCmProxyDataServicePropertyHandler networkCmProxyDataServicePropertyHandler; - private final YangModelCmHandleRetriever yangModelCmHandleRetriever; + private final InventoryPersistence inventoryPersistence; private final ModuleSyncService moduleSyncService; @@ -92,7 +92,7 @@ public class NetworkCmProxyDataServiceImpl implements NetworkCmProxyDataService public DmiPluginRegistrationResponse updateDmiRegistrationAndSyncModule( final DmiPluginRegistration dmiPluginRegistration) { dmiPluginRegistration.validateDmiPluginRegistration(); - final var dmiPluginRegistrationResponse = new DmiPluginRegistrationResponse(); + final DmiPluginRegistrationResponse dmiPluginRegistrationResponse = new DmiPluginRegistrationResponse(); dmiPluginRegistrationResponse.setRemovedCmHandles( parseAndRemoveCmHandlesInDmiRegistration(dmiPluginRegistration.getRemovedCmHandles())); if (!dmiPluginRegistration.getCreatedCmHandles().isEmpty()) { @@ -189,7 +189,7 @@ public class NetworkCmProxyDataServiceImpl implements NetworkCmProxyDataService CpsValidator.validateNameCharacters(cmHandleId); final NcmpServiceCmHandle ncmpServiceCmHandle = new NcmpServiceCmHandle(); final YangModelCmHandle yangModelCmHandle = - yangModelCmHandleRetriever.getYangModelCmHandle(cmHandleId); + inventoryPersistence.getYangModelCmHandle(cmHandleId); final List<YangModelCmHandle.Property> dmiProperties = yangModelCmHandle.getDmiProperties(); final List<YangModelCmHandle.Property> publicProperties = yangModelCmHandle.getPublicProperties(); ncmpServiceCmHandle.setCmHandleId(yangModelCmHandle.getId()); @@ -209,7 +209,7 @@ public class NetworkCmProxyDataServiceImpl implements NetworkCmProxyDataService public Map<String, String> getCmHandlePublicProperties(final String cmHandleId) { CpsValidator.validateNameCharacters(cmHandleId); final YangModelCmHandle yangModelCmHandle = - yangModelCmHandleRetriever.getYangModelCmHandle(cmHandleId); + inventoryPersistence.getYangModelCmHandle(cmHandleId); final List<YangModelCmHandle.Property> yangModelPublicProperties = yangModelCmHandle.getPublicProperties(); final Map<String, String> cmHandlePublicProperties = new HashMap<>(); asPropertiesMap(yangModelPublicProperties, cmHandlePublicProperties); diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/operations/DmiDataOperations.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/operations/DmiDataOperations.java index 8e2c0946a2..d409a80e55 100644 --- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/operations/DmiDataOperations.java +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/operations/DmiDataOperations.java @@ -29,6 +29,7 @@ import org.onap.cps.ncmp.api.impl.client.DmiRestClient; import org.onap.cps.ncmp.api.impl.config.NcmpConfiguration; import org.onap.cps.ncmp.api.impl.utils.DmiServiceUrlBuilder; import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle; +import org.onap.cps.ncmp.api.inventory.InventoryPersistence; import org.onap.cps.utils.CpsValidator; import org.onap.cps.utils.JsonObjectMapper; import org.springframework.http.ResponseEntity; @@ -45,11 +46,11 @@ public class DmiDataOperations extends DmiOperations { * * @param dmiRestClient {@code DmiRestClient} */ - public DmiDataOperations(final YangModelCmHandleRetriever cmHandlePropertiesRetriever, + public DmiDataOperations(final InventoryPersistence inventoryPersistence, final JsonObjectMapper jsonObjectMapper, final NcmpConfiguration.DmiProperties dmiProperties, final DmiRestClient dmiRestClient, final DmiServiceUrlBuilder dmiServiceUrlBuilder) { - super(cmHandlePropertiesRetriever, jsonObjectMapper, dmiProperties, dmiRestClient, dmiServiceUrlBuilder); + super(inventoryPersistence, jsonObjectMapper, dmiProperties, dmiRestClient, dmiServiceUrlBuilder); } /** @@ -72,7 +73,7 @@ public class DmiDataOperations extends DmiOperations { final String topicParamInQuery) { CpsValidator.validateNameCharacters(cmHandleId); final YangModelCmHandle yangModelCmHandle = - yangModelCmHandleRetriever.getYangModelCmHandle(cmHandleId); + inventoryPersistence.getYangModelCmHandle(cmHandleId); final DmiRequestBody dmiRequestBody = DmiRequestBody.builder() .operation(READ) .requestId(requestId) @@ -104,7 +105,7 @@ public class DmiDataOperations extends DmiOperations { final String dataType) { CpsValidator.validateNameCharacters(cmHandleId); final YangModelCmHandle yangModelCmHandle = - yangModelCmHandleRetriever.getYangModelCmHandle(cmHandleId); + inventoryPersistence.getYangModelCmHandle(cmHandleId); final DmiRequestBody dmiRequestBody = DmiRequestBody.builder() .operation(operation) .data(requestData) diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/operations/DmiModelOperations.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/operations/DmiModelOperations.java index 7ab5798694..d8d03041fb 100644 --- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/operations/DmiModelOperations.java +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/operations/DmiModelOperations.java @@ -34,6 +34,7 @@ import org.onap.cps.ncmp.api.impl.client.DmiRestClient; import org.onap.cps.ncmp.api.impl.config.NcmpConfiguration; import org.onap.cps.ncmp.api.impl.utils.DmiServiceUrlBuilder; import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle; +import org.onap.cps.ncmp.api.inventory.InventoryPersistence; import org.onap.cps.ncmp.api.models.YangResource; import org.onap.cps.spi.model.ModuleReference; import org.onap.cps.utils.JsonObjectMapper; @@ -51,11 +52,11 @@ public class DmiModelOperations extends DmiOperations { * * @param dmiRestClient {@code DmiRestClient} */ - public DmiModelOperations(final YangModelCmHandleRetriever dmiPropertiesRetriever, + public DmiModelOperations(final InventoryPersistence inventoryPersistence, final JsonObjectMapper jsonObjectMapper, final NcmpConfiguration.DmiProperties dmiProperties, final DmiRestClient dmiRestClient, final DmiServiceUrlBuilder dmiServiceUrlBuilder) { - super(dmiPropertiesRetriever, jsonObjectMapper, dmiProperties, dmiRestClient, dmiServiceUrlBuilder); + super(inventoryPersistence, jsonObjectMapper, dmiProperties, dmiRestClient, dmiServiceUrlBuilder); } /** diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/operations/DmiOperations.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/operations/DmiOperations.java index 745007bd44..e26ffef870 100644 --- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/operations/DmiOperations.java +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/operations/DmiOperations.java @@ -26,6 +26,7 @@ import lombok.RequiredArgsConstructor; import org.onap.cps.ncmp.api.impl.client.DmiRestClient; import org.onap.cps.ncmp.api.impl.config.NcmpConfiguration; import org.onap.cps.ncmp.api.impl.utils.DmiServiceUrlBuilder; +import org.onap.cps.ncmp.api.inventory.InventoryPersistence; import org.onap.cps.utils.JsonObjectMapper; import org.springframework.stereotype.Service; @@ -44,7 +45,7 @@ public class DmiOperations { } } - protected final YangModelCmHandleRetriever yangModelCmHandleRetriever; + protected final InventoryPersistence inventoryPersistence; protected final JsonObjectMapper jsonObjectMapper; protected final NcmpConfiguration.DmiProperties dmiProperties; protected final DmiRestClient dmiRestClient; diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/inventory/CmHandleState.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/inventory/CmHandleState.java index 24fab32a5c..0c16adca91 100644 --- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/inventory/CmHandleState.java +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/inventory/CmHandleState.java @@ -21,5 +21,5 @@ package org.onap.cps.ncmp.api.inventory; public enum CmHandleState { - ADVISED, READY; + ADVISED, READY, LOCKED } diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/inventory/CompositeState.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/inventory/CompositeState.java index 9ac49a6ceb..eeaa4cd0be 100644 --- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/inventory/CompositeState.java +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/inventory/CompositeState.java @@ -22,6 +22,8 @@ package org.onap.cps.ncmp.api.inventory; import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonProperty; +import java.time.OffsetDateTime; +import java.time.format.DateTimeFormatter; import lombok.Builder; import lombok.Data; import lombok.Getter; @@ -38,7 +40,7 @@ import lombok.Setter; public class CompositeState { @JsonProperty("cm-handle-state") - private CmHandleState cmhandleState; + private CmHandleState cmHandleState; @JsonProperty("lock-reason") private LockReason lockReason; @@ -52,13 +54,24 @@ public class CompositeState { @JsonProperty("datastores") private DataStores dataStores; + /** + * Date and Time in the format of yyyy-MM-dd'T'HH:mm:ss.SSSZ + */ + public static final DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ"); + + + /** + * This will specify the latest lock reason for a specific cm handle. If a cm handle is in a state other than LOCKED + * it specifies the last lock reason. + * This can be used to track retry attempts as part of the lock details. + */ @Data @Builder @JsonInclude(JsonInclude.Include.NON_NULL) public static class LockReason { @JsonProperty("reason") - private String reason; + private LockReasonCategory lockReasonCategory; @JsonProperty("details") private String details; @@ -72,9 +85,6 @@ public class CompositeState { @JsonProperty("operational") private Operational operationalDataStore; - - @JsonProperty("running") - private Running runningDataStore; } @Data @@ -101,4 +111,20 @@ public class CompositeState { private String lastSyncTime; } + /** + * The date and time format used for the cm handle sync state. + * + * @return the date and time in the format of yyyy-MM-dd'T'HH:mm:ss.SSSZ + */ + public static String nowInSyncTimeFormat() { + return dateTimeFormatter.format(OffsetDateTime.now()); + } + + /** + * Sets the last updated date and time for the cm handle sync state. + */ + public void setLastUpdateTimeNow() { + lastUpdateTime = CompositeState.nowInSyncTimeFormat(); + } + } diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/inventory/CompositeStateBuilder.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/inventory/CompositeStateBuilder.java index d8f7080311..4ab0cecbf5 100644 --- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/inventory/CompositeStateBuilder.java +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/inventory/CompositeStateBuilder.java @@ -1,6 +1,7 @@ /* * ============LICENSE_START======================================================= * Copyright (C) 2022 Bell Canada + * Copyright (C) 2022 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -23,7 +24,6 @@ package org.onap.cps.ncmp.api.inventory; import org.onap.cps.ncmp.api.inventory.CompositeState.DataStores; import org.onap.cps.ncmp.api.inventory.CompositeState.LockReason; import org.onap.cps.ncmp.api.inventory.CompositeState.Operational; -import org.onap.cps.ncmp.api.inventory.CompositeState.Running; import org.onap.cps.spi.model.DataNode; public class CompositeStateBuilder { @@ -40,7 +40,7 @@ public class CompositeStateBuilder { */ public CompositeState build() { final CompositeState compositeState = new CompositeState(); - compositeState.setCmhandleState(cmHandleState); + compositeState.setCmHandleState(cmHandleState); compositeState.setLockReason(lockReason); compositeState.setDataStores(datastores); compositeState.setLastUpdateTime(lastUpdatedTime); @@ -65,8 +65,8 @@ public class CompositeStateBuilder { * @param details for the locked state * @return CompositeStateBuilder */ - public CompositeStateBuilder withLockReason(final String reason, final String details) { - this.lockReason = LockReason.builder().reason(reason).details(details).build(); + public CompositeStateBuilder withLockReason(final LockReasonCategory reason, final String details) { + this.lockReason = LockReason.builder().lockReasonCategory(reason).details(details).build(); return this; } @@ -84,13 +84,10 @@ public class CompositeStateBuilder { /** * To use attributes for creating {@link CompositeState}. * - * @param syncState for the locked state - * @param lastSyncTime for the locked state - * @return CompositeStateBuilder + * @return composite state builder */ - public CompositeStateBuilder withOperationalDataStores(final String syncState, final String lastSyncTime) { - this.datastores = DataStores.builder().operationalDataStore( - Operational.builder().syncState(syncState).lastSyncTime(lastSyncTime).build()).build(); + public CompositeStateBuilder withLastUpdatedTimeNow() { + this.lastUpdatedTime = CompositeState.nowInSyncTimeFormat(); return this; } @@ -101,9 +98,9 @@ public class CompositeStateBuilder { * @param lastSyncTime for the locked state * @return CompositeStateBuilder */ - public CompositeStateBuilder withRunningDataStores(final String syncState, final String lastSyncTime) { - this.datastores = DataStores.builder().runningDataStore( - Running.builder().syncState(syncState).lastSyncTime(lastSyncTime).build()).build(); + public CompositeStateBuilder withOperationalDataStores(final String syncState, final String lastSyncTime) { + this.datastores = DataStores.builder().operationalDataStore( + Operational.builder().syncState(syncState).lastSyncTime(lastSyncTime).build()).build(); return this; } @@ -118,26 +115,20 @@ public class CompositeStateBuilder { .get("cm-handle-state")); for (final DataNode stateChildNode : dataNode.getChildDataNodes()) { if (stateChildNode.getXpath().endsWith("/lock-reason")) { - this.lockReason = new LockReason((String) stateChildNode.getLeaves().get("reason"), + this.lockReason = new LockReason(LockReasonCategory.valueOf( + (String) stateChildNode.getLeaves().get("reason")), (String) stateChildNode.getLeaves().get("details")); } if (stateChildNode.getXpath().endsWith("/datastores")) { for (final DataNode dataStoreNodes : stateChildNode.getChildDataNodes()) { Operational operationalDataStore = null; - Running runningDataStore = null; if (dataStoreNodes.getXpath().contains("/operational")) { operationalDataStore = Operational.builder() .syncState((String) dataStoreNodes.getLeaves().get("sync-state")) .lastSyncTime((String) dataStoreNodes.getLeaves().get("last-sync-time")) .build(); - } else { - runningDataStore = Running.builder() - .syncState((String) dataStoreNodes.getLeaves().get("sync-state")) - .lastSyncTime((String) dataStoreNodes.getLeaves().get("last-sync-time")) - .build(); } - this.datastores = DataStores.builder().operationalDataStore(operationalDataStore) - .runningDataStore(runningDataStore).build(); + this.datastores = DataStores.builder().operationalDataStore(operationalDataStore).build(); } } } diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/operations/YangModelCmHandleRetriever.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/inventory/InventoryPersistence.java index 0edd68c3d5..873a44913a 100644 --- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/operations/YangModelCmHandleRetriever.java +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/inventory/InventoryPersistence.java @@ -1,7 +1,6 @@ /* * ============LICENSE_START======================================================= - * Copyright (C) 2021-2022 Nordix Foundation - * Modifications Copyright (C) 2021 Bell Canada + * Copyright (C) 2022 Nordix Foundation * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -19,32 +18,78 @@ * ============LICENSE_END========================================================= */ -package org.onap.cps.ncmp.api.impl.operations; +package org.onap.cps.ncmp.api.inventory; +import java.time.OffsetDateTime; import java.util.LinkedHashMap; +import java.util.List; import java.util.Map; -import lombok.AllArgsConstructor; +import lombok.RequiredArgsConstructor; import org.onap.cps.api.CpsDataService; import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle; -import org.onap.cps.ncmp.api.inventory.CompositeState; -import org.onap.cps.ncmp.api.inventory.CompositeStateBuilder; import org.onap.cps.ncmp.api.models.NcmpServiceCmHandle; +import org.onap.cps.spi.CpsDataPersistenceService; import org.onap.cps.spi.FetchDescendantsOption; import org.onap.cps.spi.model.DataNode; import org.onap.cps.utils.CpsValidator; +import org.onap.cps.utils.JsonObjectMapper; import org.springframework.stereotype.Component; -/** - * Retrieves YangModelCmHandles & properties. - */ +@RequiredArgsConstructor @Component -@AllArgsConstructor -public class YangModelCmHandleRetriever { +public class InventoryPersistence { private static final String NCMP_DATASPACE_NAME = "NCMP-Admin"; + private static final String NCMP_DMI_REGISTRY_ANCHOR = "ncmp-dmi-registry"; - private CpsDataService cpsDataService; + private final JsonObjectMapper jsonObjectMapper; + + private final CpsDataService cpsDataService; + + private final CpsDataPersistenceService cpsDataPersistenceService; + + private static final CompositeStateBuilder compositeStateBuilder = new CompositeStateBuilder(); + + /** + * Get the Cm Handle Composite State from the data node. + * + * @param cmHandleId cm handle id + * @return the cm handle composite state + */ + public CompositeState getCmHandleState(final String cmHandleId) { + final DataNode stateAsDataNode = cpsDataService.getDataNode(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR, + "/dmi-registry/cm-handles[@id='" + cmHandleId + "']/state", + FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS); + return compositeStateBuilder.fromDataNode(stateAsDataNode).build(); + } + + /** + * Save the cm handles state. + * + * @param cmHandleId cm handle id + * @param compositeState composite state + */ + public void saveCmHandleState(final String cmHandleId, final CompositeState compositeState) { + final String cmHandleJsonData = String.format("{\"state\":%s}", + jsonObjectMapper.asJsonString(compositeState)); + cpsDataService.replaceNodeTree(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR, + "/dmi-registry/cm-handles[@id='" + cmHandleId + "']", + cmHandleJsonData, OffsetDateTime.now()); + } + + /** + * Method which returns cm handles by the cm handles state. + * + * @param cmHandleState cm handle state + * @return a list of cm handles + */ + public List<DataNode> getCmHandlesByState(final CmHandleState cmHandleState) { + return cpsDataPersistenceService.queryDataNodes(NCMP_DATASPACE_NAME, + NCMP_DMI_REGISTRY_ANCHOR, "//state[@cm-handle-state=\"" + + cmHandleState + "\"]/ancestor::cm-handles", + FetchDescendantsOption.OMIT_DESCENDANTS); + } /** * This method retrieves DMI service name and DMI properties for a given cm handle. @@ -74,7 +119,7 @@ public class YangModelCmHandleRetriever { } private static void populateCmHandleDetails(final DataNode cmHandleDataNode, - final NcmpServiceCmHandle ncmpServiceCmHandle) { + final NcmpServiceCmHandle ncmpServiceCmHandle) { final Map<String, String> dmiProperties = new LinkedHashMap<>(); final Map<String, String> publicProperties = new LinkedHashMap<>(); final CompositeStateBuilder compositeStateBuilder = new CompositeStateBuilder(); diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/inventory/LockReasonCategory.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/inventory/LockReasonCategory.java new file mode 100644 index 0000000000..596fcb7f99 --- /dev/null +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/inventory/LockReasonCategory.java @@ -0,0 +1,25 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2022 Nordix Foundation + * ================================================================================ + * 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.inventory; + +public enum LockReasonCategory { + LOCKED_MISBEHAVING +} diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/inventory/sync/ModuleSyncWatchdog.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/inventory/sync/ModuleSyncWatchdog.java index 353db9d68d..2187ec61ce 100644 --- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/inventory/sync/ModuleSyncWatchdog.java +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/inventory/sync/ModuleSyncWatchdog.java @@ -1,5 +1,5 @@ /* - * ============LICENSE_START======================================================= + * ============LICENSE_START======================================================= * Copyright (C) 2022 Nordix Foundation * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); @@ -24,6 +24,9 @@ import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle; import org.onap.cps.ncmp.api.inventory.CmHandleState; +import org.onap.cps.ncmp.api.inventory.CompositeState; +import org.onap.cps.ncmp.api.inventory.InventoryPersistence; +import org.onap.cps.ncmp.api.inventory.LockReasonCategory; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; @@ -32,6 +35,8 @@ import org.springframework.stereotype.Component; @Component public class ModuleSyncWatchdog { + private final InventoryPersistence inventoryPersistence; + private final SyncUtils syncUtils; private final ModuleSyncService moduleSyncService; @@ -43,11 +48,21 @@ public class ModuleSyncWatchdog { public void executeAdvisedCmHandlePoll() { YangModelCmHandle advisedCmHandle = syncUtils.getAnAdvisedCmHandle(); while (advisedCmHandle != null) { - moduleSyncService.syncAndCreateSchemaSet(advisedCmHandle); - // ToDo Lock Cm Handle if module sync fails - syncUtils.updateCmHandleState(advisedCmHandle, CmHandleState.READY); - log.info("{} is now in {} state", advisedCmHandle.getId(), - advisedCmHandle.getCompositeState().getCmhandleState()); + final String cmHandleId = advisedCmHandle.getId(); + final CompositeState compositeState = inventoryPersistence.getCmHandleState(cmHandleId); + try { + moduleSyncService.syncAndCreateSchemaSet(advisedCmHandle); + compositeState.setCmHandleState(CmHandleState.READY); + } catch (final Exception e) { + compositeState.setCmHandleState(CmHandleState.LOCKED); + syncUtils.updateLockReasonDetailsAndAttempts(compositeState, + LockReasonCategory.LOCKED_MISBEHAVING, + e.getMessage()); + } + compositeState.setLastUpdateTimeNow(); + inventoryPersistence.saveCmHandleState(cmHandleId, compositeState); + log.info("{} is now in {} state", cmHandleId, + advisedCmHandle.getCompositeState().getCmHandleState()); advisedCmHandle = syncUtils.getAnAdvisedCmHandle(); } log.debug("No Cm-Handles currently found in an ADVISED state"); diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/inventory/sync/SyncUtils.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/inventory/sync/SyncUtils.java index 3bc43c5665..a4f29de3e8 100644 --- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/inventory/sync/SyncUtils.java +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/inventory/sync/SyncUtils.java @@ -1,5 +1,5 @@ /* - * ============LICENSE_START======================================================= + * ============LICENSE_START======================================================= * Copyright (C) 2022 Nordix Foundation * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); @@ -20,23 +20,18 @@ package org.onap.cps.ncmp.api.inventory.sync; -import static org.onap.cps.ncmp.api.impl.constants.DmiRegistryConstants.NCMP_DATASPACE_NAME; -import static org.onap.cps.ncmp.api.impl.constants.DmiRegistryConstants.NCMP_DMI_REGISTRY_ANCHOR; -import static org.onap.cps.ncmp.api.impl.constants.DmiRegistryConstants.NCMP_DMI_REGISTRY_PARENT; - import java.security.SecureRandom; -import java.time.OffsetDateTime; import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; -import org.onap.cps.api.CpsDataService; -import org.onap.cps.ncmp.api.impl.operations.YangModelCmHandleRetriever; import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle; import org.onap.cps.ncmp.api.inventory.CmHandleState; -import org.onap.cps.spi.CpsDataPersistenceService; -import org.onap.cps.spi.FetchDescendantsOption; +import org.onap.cps.ncmp.api.inventory.CompositeState; +import org.onap.cps.ncmp.api.inventory.InventoryPersistence; +import org.onap.cps.ncmp.api.inventory.LockReasonCategory; import org.onap.cps.spi.model.DataNode; -import org.onap.cps.utils.JsonObjectMapper; import org.springframework.stereotype.Component; @Slf4j @@ -45,13 +40,11 @@ import org.springframework.stereotype.Component; public class SyncUtils { private static final SecureRandom secureRandom = new SecureRandom(); - private final CpsDataService cpsDataService; - private final CpsDataPersistenceService cpsDataPersistenceService; - private final JsonObjectMapper jsonObjectMapper; + private final InventoryPersistence inventoryPersistence; - private final YangModelCmHandleRetriever yangModelCmHandleRetriever; + private static final Pattern retryAttemptPattern = Pattern.compile("^Attempt #(\\d+) failed:"); /** * Query data nodes for cm handles with an "ADVISED" cm handle state, and select a random entry for processing. @@ -59,30 +52,37 @@ public class SyncUtils { * @return a random yang model cm handle with an ADVISED state, return null if not found */ public YangModelCmHandle getAnAdvisedCmHandle() { - final List<DataNode> advisedCmHandles = cpsDataPersistenceService.queryDataNodes("NCMP-Admin", - "ncmp-dmi-registry", "//cm-handles[@state=\"ADVISED\"]", - FetchDescendantsOption.OMIT_DESCENDANTS); + final List<DataNode> advisedCmHandles = inventoryPersistence.getCmHandlesByState(CmHandleState.ADVISED); if (advisedCmHandles.isEmpty()) { return null; } final int randomElementIndex = secureRandom.nextInt(advisedCmHandles.size()); final String cmHandleId = advisedCmHandles.get(randomElementIndex).getLeaves() .get("id").toString(); - return yangModelCmHandleRetriever.getYangModelCmHandle(cmHandleId); + return inventoryPersistence.getYangModelCmHandle(cmHandleId); } + /** - * Update the Cm Handle state to "READY". + * Update Composite State attempts counter and set new lock reason and details. * - * @param yangModelCmHandle yang model cm handle - * @param cmHandleState cm handle state + * @param lockReasonCategory lock reason category + * @param errorMessage error message */ - public void updateCmHandleState(final YangModelCmHandle yangModelCmHandle, final CmHandleState cmHandleState) { - yangModelCmHandle.getCompositeState().setCmhandleState(cmHandleState); - final String cmHandleJsonData = String.format("{\"cm-handles\":[%s]}", - jsonObjectMapper.asJsonString(yangModelCmHandle)); - cpsDataService.updateNodeLeaves(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR, NCMP_DMI_REGISTRY_PARENT, - cmHandleJsonData, OffsetDateTime.now()); + public void updateLockReasonDetailsAndAttempts(final CompositeState compositeState, + final LockReasonCategory lockReasonCategory, + final String errorMessage) { + int attempt = 1; + if (compositeState.getLockReason() != null) { + final Matcher matcher = retryAttemptPattern.matcher(compositeState.getLockReason().getDetails()); + if (matcher.find()) { + attempt = 1 + Integer.parseInt(matcher.group(1)); + } + } + compositeState.setLockReason(CompositeState.LockReason.builder() + .details(String.format("Attempt #%d failed: %s", attempt, errorMessage)) + .lockReasonCategory(lockReasonCategory).build()); } + } |