diff options
Diffstat (limited to 'cps-ncmp-service/src/main/java')
7 files changed, 106 insertions, 34 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 6ba1043b32..d1f72a5ef4 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 @@ -50,6 +50,7 @@ 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.utils.YangDataConverter; import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle; +import org.onap.cps.ncmp.api.inventory.CmHandleState; 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; @@ -230,14 +231,16 @@ public class NetworkCmProxyDataServiceImpl implements NetworkCmProxyDataService List<CmHandleRegistrationResponse> cmHandleRegistrationResponses = new ArrayList<>(); try { cmHandleRegistrationResponses = dmiPluginRegistration.getCreatedCmHandles().stream() - .map(cmHandle -> - YangModelCmHandle.toYangModelCmHandle( - dmiPluginRegistration.getDmiPlugin(), - dmiPluginRegistration.getDmiDataPlugin(), - dmiPluginRegistration.getDmiModelPlugin(), cmHandle) - ) - .map(this::registerAndSyncNewCmHandle) - .collect(Collectors.toList()); + .map(cmHandle -> + YangModelCmHandle.toYangModelCmHandle( + dmiPluginRegistration.getDmiPlugin(), + dmiPluginRegistration.getDmiDataPlugin(), + dmiPluginRegistration.getDmiModelPlugin(), + CmHandleState.ADVISED, + cmHandle) + ) + .map(this::registerNewCmHandle) + .collect(Collectors.toList()); } catch (final DataValidationException dataValidationException) { cmHandleRegistrationResponses.add(CmHandleRegistrationResponse.createFailureResponse(dmiPluginRegistration .getCreatedCmHandles().stream() @@ -247,13 +250,6 @@ public class NetworkCmProxyDataServiceImpl implements NetworkCmProxyDataService return cmHandleRegistrationResponses; } - protected void syncModulesAndCreateAnchor(final YangModelCmHandle yangModelCmHandle) { - final String schemaSetName = moduleSyncService.syncAndCreateSchemaSet(yangModelCmHandle); - final String anchorName = yangModelCmHandle.getId(); - cpsAdminService.createAnchor(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, schemaSetName, - anchorName); - } - protected List<CmHandleRegistrationResponse> parseAndRemoveCmHandlesInDmiRegistration( final List<String> tobeRemovedCmHandles) { final List<CmHandleRegistrationResponse> cmHandleRegistrationResponses = @@ -294,13 +290,12 @@ public class NetworkCmProxyDataServiceImpl implements NetworkCmProxyDataService } } - private CmHandleRegistrationResponse registerAndSyncNewCmHandle(final YangModelCmHandle yangModelCmHandle) { + private CmHandleRegistrationResponse registerNewCmHandle(final YangModelCmHandle yangModelCmHandle) { try { final String cmHandleJsonData = String.format("{\"cm-handles\":[%s]}", jsonObjectMapper.asJsonString(yangModelCmHandle)); cpsDataService.saveListElements(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR, NCMP_DMI_REGISTRY_PARENT, cmHandleJsonData, NO_TIMESTAMP); - syncModulesAndCreateAnchor(yangModelCmHandle); return CmHandleRegistrationResponse.createSuccessResponse(yangModelCmHandle.getId()); } catch (final AlreadyDefinedException alreadyDefinedException) { return CmHandleRegistrationResponse.createFailureResponse( diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/event/NcmpEventsPublisher.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/event/NcmpEventsPublisher.java new file mode 100644 index 0000000000..52ac4685e2 --- /dev/null +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/event/NcmpEventsPublisher.java @@ -0,0 +1,67 @@ +/* + * ============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.impl.event; + +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.onap.ncmp.cmhandle.lcm.event.NcmpEvent; +import org.springframework.kafka.core.KafkaTemplate; +import org.springframework.kafka.support.SendResult; +import org.springframework.stereotype.Service; +import org.springframework.util.concurrent.ListenableFuture; +import org.springframework.util.concurrent.ListenableFutureCallback; + +/** + * NcmpEventsPublisher to publish the NcmpEvents on event of CREATE, UPDATE and DELETE. + */ + +@Slf4j +@Service +@RequiredArgsConstructor +public class NcmpEventsPublisher { + + private final KafkaTemplate<String, NcmpEvent> ncmpEventKafkaTemplate; + + /** + * NCMP Event publisher. + * + * @param topicName valid topic name + * @param eventKey message key + * @param ncmpEvent message payload + */ + public void publishEvent(final String topicName, final String eventKey, final NcmpEvent ncmpEvent) { + final ListenableFuture<SendResult<String, NcmpEvent>> ncmpEventFuture = + ncmpEventKafkaTemplate.send(topicName, eventKey, ncmpEvent); + + ncmpEventFuture.addCallback(new ListenableFutureCallback<>() { + @Override + public void onFailure(final Throwable throwable) { + log.error("Unable to publish event to topic : {} due to {}", topicName, throwable.getMessage()); + } + + @Override + public void onSuccess(final SendResult<String, NcmpEvent> result) { + log.debug("Successfully published event to topic : {} , NcmpEvent : {}", + result.getRecordMetadata().topic(), result.getProducerRecord().value()); + } + }); + } +} diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/utils/YangDataConverter.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/utils/YangDataConverter.java index 1df7bba9a1..82ea00eb32 100644 --- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/utils/YangDataConverter.java +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/utils/YangDataConverter.java @@ -78,6 +78,7 @@ public class YangDataConverter { (String) cmHandleDataNode.getLeaves().get("dmi-service-name"), (String) cmHandleDataNode.getLeaves().get("dmi-data-service-name"), (String) cmHandleDataNode.getLeaves().get("dmi-model-service-name"), + ncmpServiceCmHandle.getCompositeState().getCmHandleState(), ncmpServiceCmHandle ); } diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/yangmodels/YangModelCmHandle.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/yangmodels/YangModelCmHandle.java index 65e03f1f9d..5b719054a6 100644 --- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/yangmodels/YangModelCmHandle.java +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/yangmodels/YangModelCmHandle.java @@ -34,7 +34,9 @@ import lombok.Getter; import lombok.NoArgsConstructor; import lombok.Setter; import org.onap.cps.ncmp.api.impl.operations.RequiredDmiService; +import org.onap.cps.ncmp.api.inventory.CmHandleState; 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.utils.CpsValidator; @@ -68,6 +70,8 @@ public class YangModelCmHandle { @JsonProperty("public-properties") private List<Property> publicProperties; + private static final CompositeStateBuilder compositeStateBuilder = new CompositeStateBuilder(); + /** * Create a yangModelCmHandle. * @@ -80,6 +84,7 @@ public class YangModelCmHandle { public static YangModelCmHandle toYangModelCmHandle(final String dmiServiceName, final String dmiDataServiceName, final String dmiModelServiceName, + final CmHandleState cmHandleState, final NcmpServiceCmHandle ncmpServiceCmHandle) { CpsValidator.validateNameCharacters(ncmpServiceCmHandle.getCmHandleId()); final YangModelCmHandle yangModelCmHandle = new YangModelCmHandle(); @@ -90,7 +95,8 @@ public class YangModelCmHandle { yangModelCmHandle.setDmiProperties(asYangModelCmHandleProperties(ncmpServiceCmHandle.getDmiProperties())); yangModelCmHandle.setPublicProperties(asYangModelCmHandleProperties( ncmpServiceCmHandle.getPublicProperties())); - yangModelCmHandle.setCompositeState(ncmpServiceCmHandle.getCompositeState()); + compositeStateBuilder.withCmHandleState(cmHandleState); + yangModelCmHandle.setCompositeState(compositeStateBuilder.build()); return yangModelCmHandle; } diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/inventory/InventoryPersistence.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/inventory/InventoryPersistence.java index c880ec7537..2fc2dc5c1a 100644 --- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/inventory/InventoryPersistence.java +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/inventory/InventoryPersistence.java @@ -41,6 +41,8 @@ public class InventoryPersistence { private static final String NCMP_DMI_REGISTRY_ANCHOR = "ncmp-dmi-registry"; + private static final String XPATH_TO_CM_HANDLE = "/dmi-registry/cm-handles[@id='" + "%s" + "']"; + private final JsonObjectMapper jsonObjectMapper; private final CpsDataService cpsDataService; @@ -57,7 +59,7 @@ public class InventoryPersistence { */ 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", + String.format(XPATH_TO_CM_HANDLE, cmHandleId) + "/state", FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS); return compositeStateBuilder.fromDataNode(stateAsDataNode).build(); } @@ -72,7 +74,7 @@ public class InventoryPersistence { 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 + "']", + String.format(XPATH_TO_CM_HANDLE, cmHandleId), cmHandleJsonData, OffsetDateTime.now()); } @@ -90,7 +92,7 @@ public class InventoryPersistence { } /** - * This method retrieves DMI service name and DMI properties for a given cm handle. + * This method retrieves DMI service name, DMI properties and the state for a given cm handle. * @param cmHandleId the id of the cm handle * @return yang model cm handle */ @@ -100,10 +102,9 @@ public class InventoryPersistence { } private DataNode getCmHandleDataNode(final String cmHandle) { - final String xpathForDmiRegistryToFetchCmHandle = "/dmi-registry/cm-handles[@id='" + cmHandle + "']"; return cpsDataService.getDataNode(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR, - xpathForDmiRegistryToFetchCmHandle, + String.format(XPATH_TO_CM_HANDLE, cmHandle), FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS); } diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/inventory/sync/ModuleSyncService.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/inventory/sync/ModuleSyncService.java index 1d00f0dc6b..58e2bf3450 100644 --- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/inventory/sync/ModuleSyncService.java +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/inventory/sync/ModuleSyncService.java @@ -28,6 +28,7 @@ import java.util.Map; import java.util.stream.Collectors; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; +import org.onap.cps.api.CpsAdminService; import org.onap.cps.api.CpsModuleService; import org.onap.cps.ncmp.api.impl.operations.DmiModelOperations; import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle; @@ -42,13 +43,14 @@ public class ModuleSyncService { private final DmiModelOperations dmiModelOperations; private final CpsModuleService cpsModuleService; + private final CpsAdminService cpsAdminService; + /** * This method registers a cm handle and initiates modules sync. * * @param yangModelCmHandle the yang model of cm handle. - * @return schemaSetName the name of the schema set (same as cm handle name). */ - public String syncAndCreateSchemaSet(final YangModelCmHandle yangModelCmHandle) { + public void syncAndCreateSchemaSetAndAnchor(final YangModelCmHandle yangModelCmHandle) { final Collection<ModuleReference> moduleReferencesFromCmHandle = dmiModelOperations.getModuleReferences(yangModelCmHandle); @@ -68,17 +70,17 @@ public class ModuleSyncService { newModuleNameToContentMap = dmiModelOperations.getNewYangResourcesFromDmi(yangModelCmHandle, identifiedNewModuleReferencesFromCmHandle); } - return createSchemaSet(yangModelCmHandle, existingModuleReferencesFromCmHandle, newModuleNameToContentMap); + createSchemaSetAndAnchor(yangModelCmHandle, newModuleNameToContentMap, existingModuleReferencesFromCmHandle); } - private String createSchemaSet(final YangModelCmHandle yangModelCmHandle, - final Collection<ModuleReference> existingModuleReferencesFromCmHandle, - final Map<String, String> newModuleNameToContentMap) { - final String schemaSetName = yangModelCmHandle.getId(); - cpsModuleService - .createSchemaSetFromModules(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, schemaSetName, + private void createSchemaSetAndAnchor(final YangModelCmHandle yangModelCmHandle, + final Map<String, String> newModuleNameToContentMap, + final Collection<ModuleReference> existingModuleReferencesFromCmHandle) { + final String schemaSetAndAnchorName = yangModelCmHandle.getId(); + cpsModuleService.createSchemaSetFromModules(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, schemaSetAndAnchorName, newModuleNameToContentMap, existingModuleReferencesFromCmHandle); - return schemaSetName; + cpsAdminService.createAnchor(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, schemaSetAndAnchorName, + schemaSetAndAnchorName); } } 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 2187ec61ce..bcc7daa39d 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 @@ -51,7 +51,7 @@ public class ModuleSyncWatchdog { final String cmHandleId = advisedCmHandle.getId(); final CompositeState compositeState = inventoryPersistence.getCmHandleState(cmHandleId); try { - moduleSyncService.syncAndCreateSchemaSet(advisedCmHandle); + moduleSyncService.syncAndCreateSchemaSetAndAnchor(advisedCmHandle); compositeState.setCmHandleState(CmHandleState.READY); } catch (final Exception e) { compositeState.setCmHandleState(CmHandleState.LOCKED); |