From df31bcf92002b2b7cba9b1187e68995c2af53cf0 Mon Sep 17 00:00:00 2001 From: ToineSiebelink Date: Thu, 27 Jun 2024 15:35:37 +0100 Subject: move lcm/common events classes -moved api.impl.events.lcm to impl.inventory.sync.lcm -moved most api.impl.events class to utils.events (remaining classes in api.impl.events belong to cmsubscrption and Priyank wil take care of those) Issue-ID: CPS-2258 Change-Id: I1818194e708aa0d9c713b470e878997c2f26fb13 Signed-off-by: ToineSiebelink --- .../ncmp/api/impl/NcmpDatastoreRequestHandler.java | 2 +- .../NcmpPassthroughResourceRequestHandler.java | 2 +- .../onap/cps/ncmp/api/impl/events/NcmpEvent.java | 66 ------ .../events/avc/ncmptoclient/AvcEventPublisher.java | 82 -------- ...otificationSubscriptionDmiOutEventConsumer.java | 4 +- ...otificationSubscriptionNcmpInEventConsumer.java | 4 +- .../api/impl/events/lcm/LcmEventHeaderMapper.java | 36 ---- .../cps/ncmp/api/impl/events/lcm/LcmEventType.java | 38 ---- .../events/lcm/LcmEventsCmHandleStateHandler.java | 55 ----- .../LcmEventsCmHandleStateHandlerAsyncHelper.java | 79 ------- .../lcm/LcmEventsCmHandleStateHandlerImpl.java | 208 ------------------- .../ncmp/api/impl/events/lcm/LcmEventsCreator.java | 131 ------------ .../impl/events/lcm/LcmEventsCreatorHelper.java | 227 --------------------- .../ncmp/api/impl/events/lcm/LcmEventsService.java | 74 ------- .../api/impl/events/mapper/CloudEventMapper.java | 61 ------ .../data/operation/DataOperationEventCreator.java | 2 +- .../inventory/CmHandleRegistrationService.java | 2 +- .../ncmp/impl/inventory/sync/ModuleSyncTasks.java | 2 +- .../inventory/sync/lcm/LcmEventHeaderMapper.java | 36 ++++ .../ncmp/impl/inventory/sync/lcm/LcmEventType.java | 38 ++++ .../sync/lcm/LcmEventsCmHandleStateHandler.java | 55 +++++ .../LcmEventsCmHandleStateHandlerAsyncHelper.java | 79 +++++++ .../lcm/LcmEventsCmHandleStateHandlerImpl.java | 208 +++++++++++++++++++ .../impl/inventory/sync/lcm/LcmEventsCreator.java | 131 ++++++++++++ .../inventory/sync/lcm/LcmEventsCreatorHelper.java | 227 +++++++++++++++++++++ .../impl/inventory/sync/lcm/LcmEventsService.java | 74 +++++++ .../DeviceTrustLevelMessageConsumer.java | 2 +- .../inventory/trustlevel/TrustLevelManager.java | 8 +- .../org/onap/cps/ncmp/utils/TopicValidator.java | 47 ----- .../cps/ncmp/utils/events/CloudEventMapper.java | 61 ++++++ .../cps/ncmp/utils/events/CmAvcEventPublisher.java | 81 ++++++++ .../org/onap/cps/ncmp/utils/events/NcmpEvent.java | 66 ++++++ .../onap/cps/ncmp/utils/events/TopicValidator.java | 47 +++++ 33 files changed, 1117 insertions(+), 1118 deletions(-) delete mode 100644 cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/events/NcmpEvent.java delete mode 100644 cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/events/avc/ncmptoclient/AvcEventPublisher.java delete mode 100644 cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/events/lcm/LcmEventHeaderMapper.java delete mode 100644 cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/events/lcm/LcmEventType.java delete mode 100644 cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/events/lcm/LcmEventsCmHandleStateHandler.java delete mode 100644 cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/events/lcm/LcmEventsCmHandleStateHandlerAsyncHelper.java delete mode 100644 cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/events/lcm/LcmEventsCmHandleStateHandlerImpl.java delete mode 100644 cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/events/lcm/LcmEventsCreator.java delete mode 100644 cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/events/lcm/LcmEventsCreatorHelper.java delete mode 100644 cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/events/lcm/LcmEventsService.java delete mode 100644 cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/events/mapper/CloudEventMapper.java create mode 100644 cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/inventory/sync/lcm/LcmEventHeaderMapper.java create mode 100644 cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/inventory/sync/lcm/LcmEventType.java create mode 100644 cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/inventory/sync/lcm/LcmEventsCmHandleStateHandler.java create mode 100644 cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/inventory/sync/lcm/LcmEventsCmHandleStateHandlerAsyncHelper.java create mode 100644 cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/inventory/sync/lcm/LcmEventsCmHandleStateHandlerImpl.java create mode 100644 cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/inventory/sync/lcm/LcmEventsCreator.java create mode 100644 cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/inventory/sync/lcm/LcmEventsCreatorHelper.java create mode 100644 cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/inventory/sync/lcm/LcmEventsService.java delete mode 100644 cps-ncmp-service/src/main/java/org/onap/cps/ncmp/utils/TopicValidator.java create mode 100644 cps-ncmp-service/src/main/java/org/onap/cps/ncmp/utils/events/CloudEventMapper.java create mode 100644 cps-ncmp-service/src/main/java/org/onap/cps/ncmp/utils/events/CmAvcEventPublisher.java create mode 100644 cps-ncmp-service/src/main/java/org/onap/cps/ncmp/utils/events/NcmpEvent.java create mode 100644 cps-ncmp-service/src/main/java/org/onap/cps/ncmp/utils/events/TopicValidator.java (limited to 'cps-ncmp-service/src/main/java/org') diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/NcmpDatastoreRequestHandler.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/NcmpDatastoreRequestHandler.java index 302ba449c7..95453a71da 100644 --- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/NcmpDatastoreRequestHandler.java +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/NcmpDatastoreRequestHandler.java @@ -24,7 +24,7 @@ import java.util.Map; import java.util.UUID; import lombok.extern.slf4j.Slf4j; import org.onap.cps.ncmp.api.models.CmResourceAddress; -import org.onap.cps.ncmp.utils.TopicValidator; +import org.onap.cps.ncmp.utils.events.TopicValidator; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import reactor.core.publisher.Mono; diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/NcmpPassthroughResourceRequestHandler.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/NcmpPassthroughResourceRequestHandler.java index 0fd32501c3..0867ab44a5 100644 --- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/NcmpPassthroughResourceRequestHandler.java +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/NcmpPassthroughResourceRequestHandler.java @@ -34,7 +34,7 @@ import org.onap.cps.ncmp.api.models.CmResourceAddress; import org.onap.cps.ncmp.api.models.DataOperationRequest; import org.onap.cps.ncmp.exceptions.OperationNotSupportedException; import org.onap.cps.ncmp.exceptions.PayloadTooLargeException; -import org.onap.cps.ncmp.utils.TopicValidator; +import org.onap.cps.ncmp.utils.events.TopicValidator; import org.springframework.stereotype.Service; import reactor.core.publisher.Mono; diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/events/NcmpEvent.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/events/NcmpEvent.java deleted file mode 100644 index 248db9805c..0000000000 --- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/events/NcmpEvent.java +++ /dev/null @@ -1,66 +0,0 @@ -/* - * ============LICENSE_START======================================================= - * Copyright (C) 2023-2024 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.events; - -import io.cloudevents.CloudEvent; -import io.cloudevents.core.builder.CloudEventBuilder; -import java.net.URI; -import java.util.Map; -import java.util.UUID; -import lombok.Builder; -import org.apache.commons.lang3.StringUtils; -import org.onap.cps.ncmp.api.impl.utils.EventDateTimeFormatter; -import org.onap.cps.ncmp.api.impl.utils.context.CpsApplicationContext; -import org.onap.cps.utils.JsonObjectMapper; - -@Builder -public class NcmpEvent { - - private Object data; - private Map extensions; - private String type; - @Builder.Default - private static final String CLOUD_EVENT_SPEC_VERSION_V1 = "1.0.0"; - @Builder.Default - private static final String CLOUD_EVENT_SOURCE = "NCMP"; - - /** - * Creates ncmp cloud event with provided attributes. - * - * @return Cloud Event - */ - public CloudEvent asCloudEvent() { - final JsonObjectMapper jsonObjectMapper = CpsApplicationContext.getCpsBean(JsonObjectMapper.class); - final CloudEventBuilder cloudEventBuilder = CloudEventBuilder.v1() - .withId(UUID.randomUUID().toString()) - .withSource(URI.create(CLOUD_EVENT_SOURCE)) - .withType(type) - .withDataSchema(URI.create("urn:cps:" + type + ":" + CLOUD_EVENT_SPEC_VERSION_V1)) - .withTime(EventDateTimeFormatter.toIsoOffsetDateTime( - EventDateTimeFormatter.getCurrentIsoFormattedDateTime())) - .withData(jsonObjectMapper.asJsonBytes(data)); - extensions.entrySet().stream() - .filter(extensionEntry -> StringUtils.isNotBlank(extensionEntry.getValue())) - .forEach(extensionEntry -> - cloudEventBuilder.withExtension(extensionEntry.getKey(), extensionEntry.getValue())); - return cloudEventBuilder.build(); - } -} diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/events/avc/ncmptoclient/AvcEventPublisher.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/events/avc/ncmptoclient/AvcEventPublisher.java deleted file mode 100644 index 7afe606f4f..0000000000 --- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/events/avc/ncmptoclient/AvcEventPublisher.java +++ /dev/null @@ -1,82 +0,0 @@ -/* - * ============LICENSE_START======================================================= - * Copyright (C) 2023-2024 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.events.avc.ncmptoclient; - -import io.cloudevents.CloudEvent; -import java.util.Collections; -import java.util.HashMap; -import java.util.Map; -import lombok.RequiredArgsConstructor; -import org.onap.cps.events.EventsPublisher; -import org.onap.cps.ncmp.api.impl.events.NcmpEvent; -import org.onap.cps.ncmp.events.avc.ncmp_to_client.Avc; -import org.onap.cps.ncmp.events.avc.ncmp_to_client.AvcEvent; -import org.onap.cps.ncmp.events.avc.ncmp_to_client.Data; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.stereotype.Service; - -@Service -@RequiredArgsConstructor -public class AvcEventPublisher { - - private final EventsPublisher eventsPublisher; - - @Value("${app.ncmp.avc.cm-events-topic}") - private String avcTopic; - - /** - * Publish attribute value change event. - * - * @param eventKey id of the cmHandle being registered - */ - public void publishAvcEvent(final String eventKey, final String attributeName, - final String oldAttributeValue, final String newAttributeValue) { - final AvcEvent avcEvent = buildAvcEvent(attributeName, oldAttributeValue, newAttributeValue); - - final Map extensions = createAvcEventExtensions(eventKey); - final CloudEvent avcCloudEvent = - NcmpEvent.builder().type(AvcEvent.class.getTypeName()) - .data(avcEvent).extensions(extensions).build().asCloudEvent(); - - eventsPublisher.publishCloudEvent(avcTopic, eventKey, avcCloudEvent); - } - - private AvcEvent buildAvcEvent(final String attributeName, - final String oldAttributeValue, - final String newAttributeValue) { - final Avc avc = new Avc(); - avc.setAttributeName(attributeName); - avc.setOldAttributeValue(oldAttributeValue); - avc.setNewAttributeValue(newAttributeValue); - - final Data payload = new Data(); - payload.setAttributeValueChange(Collections.singletonList(avc)); - final AvcEvent avcEvent = new AvcEvent(); - avcEvent.setData(payload); - return avcEvent; - } - - private Map createAvcEventExtensions(final String eventKey) { - final Map extensions = new HashMap<>(); - extensions.put("correlationid", eventKey); - return extensions; - } -} diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/events/cmsubscription/consumer/CmNotificationSubscriptionDmiOutEventConsumer.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/events/cmsubscription/consumer/CmNotificationSubscriptionDmiOutEventConsumer.java index 051949c5e8..978a4cdfe2 100644 --- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/events/cmsubscription/consumer/CmNotificationSubscriptionDmiOutEventConsumer.java +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/events/cmsubscription/consumer/CmNotificationSubscriptionDmiOutEventConsumer.java @@ -22,7 +22,7 @@ package org.onap.cps.ncmp.api.impl.events.cmsubscription.consumer; import static org.onap.cps.ncmp.api.NcmpResponseStatus.CM_DATA_SUBSCRIPTION_ACCEPTED; import static org.onap.cps.ncmp.api.NcmpResponseStatus.CM_DATA_SUBSCRIPTION_REJECTED; -import static org.onap.cps.ncmp.api.impl.events.mapper.CloudEventMapper.toTargetEvent; +import static org.onap.cps.ncmp.utils.events.CloudEventMapper.toTargetEvent; import io.cloudevents.CloudEvent; import java.util.Map; @@ -120,4 +120,4 @@ public class CmNotificationSubscriptionDmiOutEventConsumer { && ncmpResponseStatus.getMessage() .equals(cmNotificationSubscriptionDmiOutData.getStatusMessage()); } -} \ No newline at end of file +} diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/events/cmsubscription/consumer/CmNotificationSubscriptionNcmpInEventConsumer.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/events/cmsubscription/consumer/CmNotificationSubscriptionNcmpInEventConsumer.java index fb3388c117..65f4ee8c89 100644 --- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/events/cmsubscription/consumer/CmNotificationSubscriptionNcmpInEventConsumer.java +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/events/cmsubscription/consumer/CmNotificationSubscriptionNcmpInEventConsumer.java @@ -20,7 +20,7 @@ package org.onap.cps.ncmp.api.impl.events.cmsubscription.consumer; -import static org.onap.cps.ncmp.api.impl.events.mapper.CloudEventMapper.toTargetEvent; +import static org.onap.cps.ncmp.utils.events.CloudEventMapper.toTargetEvent; import io.cloudevents.CloudEvent; import java.util.List; @@ -67,4 +67,4 @@ public class CmNotificationSubscriptionNcmpInEventConsumer { cmNotificationSubscriptionHandlerService.processSubscriptionDeleteRequest(subscriptionId, predicates); } } -} \ No newline at end of file +} diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/events/lcm/LcmEventHeaderMapper.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/events/lcm/LcmEventHeaderMapper.java deleted file mode 100644 index f7707d9f76..0000000000 --- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/events/lcm/LcmEventHeaderMapper.java +++ /dev/null @@ -1,36 +0,0 @@ -/* - * ============LICENSE_START======================================================= - * Copyright (C) 2023 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.events.lcm; - -import org.mapstruct.Mapper; -import org.onap.cps.ncmp.events.lcm.v1.LcmEvent; -import org.onap.cps.ncmp.events.lcm.v1.LcmEventHeader; - -@Mapper(componentModel = "spring") -public interface LcmEventHeaderMapper { - - /** - * Mapper for converting incoming {@link LcmEvent} to outgoing {@link LcmEventHeader}. - */ - - LcmEventHeader toLcmEventHeader(LcmEvent lcmEvent); - -} diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/events/lcm/LcmEventType.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/events/lcm/LcmEventType.java deleted file mode 100644 index a8d00f7e31..0000000000 --- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/events/lcm/LcmEventType.java +++ /dev/null @@ -1,38 +0,0 @@ -/* - * ============LICENSE_START======================================================= - * Copyright (C) 2022-2023 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.events.lcm; - -public enum LcmEventType { - - CREATE("create"), UPDATE("update"), DELETE("delete"); - - private final String eventName; - - private final String eventTypeTemplate = "org.onap.ncmp.cmhandle-lcm-event.%s"; - - LcmEventType(final String eventName) { - this.eventName = String.format(eventTypeTemplate, eventName); - } - - public String getEventType() { - return this.eventName; - } -} diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/events/lcm/LcmEventsCmHandleStateHandler.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/events/lcm/LcmEventsCmHandleStateHandler.java deleted file mode 100644 index 3a49957864..0000000000 --- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/events/lcm/LcmEventsCmHandleStateHandler.java +++ /dev/null @@ -1,55 +0,0 @@ -/* - * ============LICENSE_START======================================================= - * Copyright (C) 2022-2023 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.events.lcm; - -import java.util.Collection; -import java.util.Map; -import org.onap.cps.ncmp.impl.inventory.models.CmHandleState; -import org.onap.cps.ncmp.impl.inventory.models.YangModelCmHandle; - -/** - * The implementation of it should handle the persisting of composite state and delegate the request to publish the - * corresponding lcm event. - */ -public interface LcmEventsCmHandleStateHandler { - - /** - * Updates the composite state of cmHandle based on cmHandleState. - * - * @param yangModelCmHandle cm handle represented as yang model - * @param targetCmHandleState target cm handle state - */ - void updateCmHandleState(final YangModelCmHandle yangModelCmHandle, final CmHandleState targetCmHandleState); - - /** - * Updates the composite state of cmHandle based on cmHandleState in batch. - * - * @param cmHandleStatePerCmHandle Map of Yang Model Cm Handle and corresponding cm handle state. - */ - void updateCmHandleStateBatch(final Map cmHandleStatePerCmHandle); - - /** - * Sets the initial state of cmHandles to ADVISED. - * - * @param yangModelCmHandles List of Yang Model Cm Handle. - */ - void initiateStateAdvised(Collection yangModelCmHandles); -} diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/events/lcm/LcmEventsCmHandleStateHandlerAsyncHelper.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/events/lcm/LcmEventsCmHandleStateHandlerAsyncHelper.java deleted file mode 100644 index af745caa44..0000000000 --- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/events/lcm/LcmEventsCmHandleStateHandlerAsyncHelper.java +++ /dev/null @@ -1,79 +0,0 @@ -/* - * ============LICENSE_START======================================================= - * Copyright (C) 2023 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.events.lcm; - -import java.util.Collection; -import lombok.RequiredArgsConstructor; -import org.onap.cps.ncmp.api.inventory.models.NcmpServiceCmHandle; -import org.onap.cps.ncmp.events.lcm.v1.LcmEvent; -import org.onap.cps.ncmp.events.lcm.v1.LcmEventHeader; -import org.onap.cps.ncmp.impl.inventory.models.YangModelCmHandle; -import org.onap.cps.ncmp.impl.utils.YangDataConverter; -import org.springframework.scheduling.annotation.Async; -import org.springframework.stereotype.Service; - -@Service -@RequiredArgsConstructor -public class LcmEventsCmHandleStateHandlerAsyncHelper { - - private final LcmEventsCreator lcmEventsCreator; - private final LcmEventsService lcmEventsService; - - /** - * Publish LCM Event in asynchronous manner. - * - * @param targetNcmpServiceCmHandle target NcmpServiceCmHandle - * @param currentNcmpServiceCmHandle current NcmpServiceCmHandle - */ - @Async("notificationExecutor") - public void publishLcmEventAsynchronously(final NcmpServiceCmHandle targetNcmpServiceCmHandle, - final NcmpServiceCmHandle currentNcmpServiceCmHandle) { - publishLcmEvent(targetNcmpServiceCmHandle, currentNcmpServiceCmHandle); - } - - /** - * Publish LcmEvent in batches and in asynchronous manner. - * - * @param cmHandleTransitionPairs Pair of existing and modified cm handle represented as YangModelCmHandle - */ - @Async("notificationExecutor") - public void publishLcmEventBatchAsynchronously( - final Collection cmHandleTransitionPairs) { - cmHandleTransitionPairs.forEach(cmHandleTransitionPair -> publishLcmEvent( - toNcmpServiceCmHandle(cmHandleTransitionPair.getTargetYangModelCmHandle()), - toNcmpServiceCmHandle(cmHandleTransitionPair.getCurrentYangModelCmHandle()))); - } - - private void publishLcmEvent(final NcmpServiceCmHandle targetNcmpServiceCmHandle, - final NcmpServiceCmHandle existingNcmpServiceCmHandle) { - final String cmHandleId = targetNcmpServiceCmHandle.getCmHandleId(); - final LcmEventHeader lcmEventHeader = - lcmEventsCreator.populateLcmEventHeader(cmHandleId, targetNcmpServiceCmHandle, - existingNcmpServiceCmHandle); - final LcmEvent lcmEvent = - lcmEventsCreator.populateLcmEvent(cmHandleId, targetNcmpServiceCmHandle, existingNcmpServiceCmHandle); - lcmEventsService.publishLcmEvent(cmHandleId, lcmEvent, lcmEventHeader); - } - - private static NcmpServiceCmHandle toNcmpServiceCmHandle(final YangModelCmHandle yangModelCmHandle) { - return YangDataConverter.toNcmpServiceCmHandle(yangModelCmHandle); - } -} diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/events/lcm/LcmEventsCmHandleStateHandlerImpl.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/events/lcm/LcmEventsCmHandleStateHandlerImpl.java deleted file mode 100644 index 4376014f36..0000000000 --- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/events/lcm/LcmEventsCmHandleStateHandlerImpl.java +++ /dev/null @@ -1,208 +0,0 @@ -/* - * ============LICENSE_START======================================================= - * Copyright (C) 2022-2024 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.events.lcm; - -import static org.onap.cps.ncmp.impl.inventory.models.CmHandleState.ADVISED; -import static org.onap.cps.ncmp.impl.inventory.models.CmHandleState.DELETED; -import static org.onap.cps.ncmp.impl.inventory.models.CmHandleState.LOCKED; -import static org.onap.cps.ncmp.impl.inventory.models.CmHandleState.READY; - -import io.micrometer.core.annotation.Timed; -import java.util.ArrayList; -import java.util.Collection; -import java.util.HashMap; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; -import lombok.Getter; -import lombok.NoArgsConstructor; -import lombok.RequiredArgsConstructor; -import lombok.Setter; -import lombok.extern.slf4j.Slf4j; -import org.onap.cps.ncmp.api.inventory.models.CompositeState; -import org.onap.cps.ncmp.api.inventory.models.NcmpServiceCmHandle; -import org.onap.cps.ncmp.impl.inventory.CompositeStateUtils; -import org.onap.cps.ncmp.impl.inventory.InventoryPersistence; -import org.onap.cps.ncmp.impl.inventory.models.CmHandleState; -import org.onap.cps.ncmp.impl.inventory.models.YangModelCmHandle; -import org.onap.cps.ncmp.impl.utils.YangDataConverter; -import org.springframework.stereotype.Service; - -@Slf4j -@Service -@RequiredArgsConstructor -public class LcmEventsCmHandleStateHandlerImpl implements LcmEventsCmHandleStateHandler { - - private final InventoryPersistence inventoryPersistence; - private final LcmEventsCmHandleStateHandlerAsyncHelper lcmEventsCmHandleStateHandlerAsyncHelper; - - @Override - public void updateCmHandleState(final YangModelCmHandle updatedYangModelCmHandle, - final CmHandleState targetCmHandleState) { - - final CompositeState compositeState = updatedYangModelCmHandle.getCompositeState(); - - if (isCompositeStateSame(compositeState, targetCmHandleState)) { - log.debug("CmHandle with id : {} already in state : {}", updatedYangModelCmHandle.getId(), - targetCmHandleState); - } else { - final YangModelCmHandle currentYangModelCmHandle = YangModelCmHandle.deepCopyOf(updatedYangModelCmHandle); - updateToSpecifiedCmHandleState(updatedYangModelCmHandle, targetCmHandleState); - persistCmHandle(updatedYangModelCmHandle, currentYangModelCmHandle); - lcmEventsCmHandleStateHandlerAsyncHelper.publishLcmEventAsynchronously( - toNcmpServiceCmHandle(updatedYangModelCmHandle), - toNcmpServiceCmHandle(currentYangModelCmHandle)); - } - } - - @Override - @Timed(value = "cps.ncmp.cmhandle.state.update.batch", - description = "Time taken to update a batch of cm handle states") - public void updateCmHandleStateBatch(final Map cmHandleStatePerCmHandle) { - final Collection cmHandleTransitionPairs = - prepareCmHandleTransitionBatch(cmHandleStatePerCmHandle); - persistCmHandleBatch(cmHandleTransitionPairs); - lcmEventsCmHandleStateHandlerAsyncHelper.publishLcmEventBatchAsynchronously(cmHandleTransitionPairs); - } - - @Override - public void initiateStateAdvised(final Collection yangModelCmHandles) { - final Map cmHandleStatePerCmHandle = new HashMap<>(yangModelCmHandles.size()); - for (final YangModelCmHandle yangModelCmHandle : yangModelCmHandles) { - cmHandleStatePerCmHandle.put(yangModelCmHandle, ADVISED); - } - updateCmHandleStateBatch(cmHandleStatePerCmHandle); - } - - private Collection prepareCmHandleTransitionBatch( - final Map cmHandleStatePerCmHandle) { - final List cmHandleTransitionPairs = new ArrayList<>(cmHandleStatePerCmHandle.size()); - cmHandleStatePerCmHandle.forEach((yangModelCmHandle, targetCmHandleState) -> { - - final CompositeState compositeState = yangModelCmHandle.getCompositeState(); - - if (isCompositeStateSame(compositeState, targetCmHandleState)) { - log.debug("CmHandle with id : {} already in state : {}", yangModelCmHandle.getId(), - targetCmHandleState); - } else { - final CmHandleTransitionPair cmHandleTransitionPair = new CmHandleTransitionPair(); - cmHandleTransitionPair.setCurrentYangModelCmHandle(YangModelCmHandle.deepCopyOf(yangModelCmHandle)); - updateToSpecifiedCmHandleState(yangModelCmHandle, targetCmHandleState); - cmHandleTransitionPair.setTargetYangModelCmHandle(yangModelCmHandle); - cmHandleTransitionPairs.add(cmHandleTransitionPair); - } - }); - return cmHandleTransitionPairs; - } - - - private void persistCmHandle(final YangModelCmHandle targetYangModelCmHandle, - final YangModelCmHandle currentYangModelCmHandle) { - if (isNew(currentYangModelCmHandle.getCompositeState())) { - log.debug("Registering a new cm handle {}", targetYangModelCmHandle.getId()); - inventoryPersistence.saveCmHandle(targetYangModelCmHandle); - } else if (isDeleted(targetYangModelCmHandle.getCompositeState())) { - log.info("CmHandle with Id : {} is DELETED", targetYangModelCmHandle.getId()); - } else { - inventoryPersistence.saveCmHandleState(targetYangModelCmHandle.getId(), - targetYangModelCmHandle.getCompositeState()); - } - } - - private void persistCmHandleBatch(final Collection cmHandleTransitionPairs) { - - final List newCmHandles = new ArrayList<>(); - final Map compositeStatePerCmHandleId = new LinkedHashMap<>(); - - cmHandleTransitionPairs.forEach(cmHandleTransitionPair -> { - if (isNew(cmHandleTransitionPair.getCurrentYangModelCmHandle().getCompositeState() - )) { - newCmHandles.add(cmHandleTransitionPair.getTargetYangModelCmHandle()); - } else if (!isDeleted(cmHandleTransitionPair.getTargetYangModelCmHandle().getCompositeState())) { - compositeStatePerCmHandleId.put(cmHandleTransitionPair.getTargetYangModelCmHandle().getId(), - cmHandleTransitionPair.getTargetYangModelCmHandle().getCompositeState()); - } - }); - - inventoryPersistence.saveCmHandleBatch(newCmHandles); - inventoryPersistence.saveCmHandleStateBatch(compositeStatePerCmHandleId); - - } - - private void updateToSpecifiedCmHandleState(final YangModelCmHandle yangModelCmHandle, - final CmHandleState targetCmHandleState) { - - if (READY == targetCmHandleState) { - setInitialStates(yangModelCmHandle); - } else if (ADVISED == targetCmHandleState) { - if (yangModelCmHandle.getCompositeState() == null) { - registerNewCmHandle(yangModelCmHandle); - } else if (yangModelCmHandle.getCompositeState().getCmHandleState() == LOCKED) { - retryCmHandle(yangModelCmHandle); - } - } else { - setCmHandleState(yangModelCmHandle, targetCmHandleState); - } - } - - private void setInitialStates(final YangModelCmHandle yangModelCmHandle) { - CompositeStateUtils.setInitialDataStoreSyncState(yangModelCmHandle.getCompositeState()); - CompositeStateUtils.setCompositeState(READY, yangModelCmHandle.getCompositeState()); - } - - private void retryCmHandle(final YangModelCmHandle yangModelCmHandle) { - CompositeStateUtils.setCompositeStateForRetry(yangModelCmHandle.getCompositeState()); - } - - private void registerNewCmHandle(final YangModelCmHandle yangModelCmHandle) { - yangModelCmHandle.setCompositeState(new CompositeState()); - setCmHandleState(yangModelCmHandle, ADVISED); - } - - private void setCmHandleState(final YangModelCmHandle yangModelCmHandle, final CmHandleState targetCmHandleState) { - CompositeStateUtils.setCompositeState(targetCmHandleState, yangModelCmHandle.getCompositeState()); - } - - private boolean isNew(final CompositeState existingCompositeState) { - return (existingCompositeState == null); - } - - private boolean isDeleted(final CompositeState targetCompositeState) { - return targetCompositeState.getCmHandleState() == DELETED; - } - - private boolean isCompositeStateSame(final CompositeState compositeState, final CmHandleState targetCmHandleState) { - return (compositeState != null && compositeState.getCmHandleState() == targetCmHandleState); - } - - private NcmpServiceCmHandle toNcmpServiceCmHandle(final YangModelCmHandle yangModelCmHandle) { - return YangDataConverter.toNcmpServiceCmHandle(yangModelCmHandle); - } - - @Getter - @Setter - @NoArgsConstructor - static class CmHandleTransitionPair { - - private YangModelCmHandle currentYangModelCmHandle; - private YangModelCmHandle targetYangModelCmHandle; - } -} diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/events/lcm/LcmEventsCreator.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/events/lcm/LcmEventsCreator.java deleted file mode 100644 index 4231c99678..0000000000 --- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/events/lcm/LcmEventsCreator.java +++ /dev/null @@ -1,131 +0,0 @@ -/* - * ============LICENSE_START======================================================= - * Copyright (C) 2022-2024 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.events.lcm; - -import java.util.UUID; -import lombok.Getter; -import lombok.NoArgsConstructor; -import lombok.RequiredArgsConstructor; -import lombok.Setter; -import lombok.extern.slf4j.Slf4j; -import org.onap.cps.ncmp.api.impl.utils.EventDateTimeFormatter; -import org.onap.cps.ncmp.api.inventory.models.NcmpServiceCmHandle; -import org.onap.cps.ncmp.events.lcm.v1.Event; -import org.onap.cps.ncmp.events.lcm.v1.LcmEvent; -import org.onap.cps.ncmp.events.lcm.v1.LcmEventHeader; -import org.onap.cps.ncmp.events.lcm.v1.Values; -import org.springframework.stereotype.Component; - - -/** - * LcmEventsCreator to create LcmEvent based on relevant operation. - */ -@Slf4j -@Component -@RequiredArgsConstructor -public class LcmEventsCreator { - - private final LcmEventHeaderMapper lcmEventHeaderMapper; - - /** - * Populate Lifecycle Management Event. - * - * @param cmHandleId cm handle identifier - * @param targetNcmpServiceCmHandle target ncmp service cmhandle - * @param existingNcmpServiceCmHandle existing ncmp service cmhandle - * @return Populated LcmEvent - */ - public LcmEvent populateLcmEvent(final String cmHandleId, final NcmpServiceCmHandle targetNcmpServiceCmHandle, - final NcmpServiceCmHandle existingNcmpServiceCmHandle) { - return createLcmEvent(cmHandleId, targetNcmpServiceCmHandle, existingNcmpServiceCmHandle); - } - - /** - * Populate Lifecycle Management Event Header. - * - * @param cmHandleId cm handle identifier - * @param targetNcmpServiceCmHandle target ncmp service cmhandle - * @param existingNcmpServiceCmHandle existing ncmp service cmhandle - * @return Populated LcmEventHeader - */ - public LcmEventHeader populateLcmEventHeader(final String cmHandleId, - final NcmpServiceCmHandle targetNcmpServiceCmHandle, - final NcmpServiceCmHandle existingNcmpServiceCmHandle) { - return createLcmEventHeader(cmHandleId, targetNcmpServiceCmHandle, existingNcmpServiceCmHandle); - } - - private LcmEvent createLcmEvent(final String cmHandleId, final NcmpServiceCmHandle targetNcmpServiceCmHandle, - final NcmpServiceCmHandle existingNcmpServiceCmHandle) { - final LcmEventType lcmEventType = - LcmEventsCreatorHelper.determineEventType(targetNcmpServiceCmHandle, existingNcmpServiceCmHandle); - final LcmEvent lcmEvent = lcmEventHeader(cmHandleId, lcmEventType); - lcmEvent.setEvent( - lcmEventPayload(cmHandleId, targetNcmpServiceCmHandle, existingNcmpServiceCmHandle, lcmEventType)); - return lcmEvent; - } - - private LcmEventHeader createLcmEventHeader(final String cmHandleId, - final NcmpServiceCmHandle targetNcmpServiceCmHandle, - final NcmpServiceCmHandle existingNcmpServiceCmHandle) { - final LcmEventType lcmEventType = - LcmEventsCreatorHelper.determineEventType(targetNcmpServiceCmHandle, existingNcmpServiceCmHandle); - final LcmEvent lcmEventWithHeaderInformation = lcmEventHeader(cmHandleId, lcmEventType); - return lcmEventHeaderMapper.toLcmEventHeader(lcmEventWithHeaderInformation); - } - - private Event lcmEventPayload(final String eventCorrelationId, final NcmpServiceCmHandle targetNcmpServiceCmHandle, - final NcmpServiceCmHandle existingNcmpServiceCmHandle, final LcmEventType lcmEventType) { - final Event event = new Event(); - event.setCmHandleId(eventCorrelationId); - event.setAlternateId(targetNcmpServiceCmHandle.getAlternateId()); - event.setModuleSetTag(targetNcmpServiceCmHandle.getModuleSetTag()); - event.setDataProducerIdentifier(targetNcmpServiceCmHandle.getDataProducerIdentifier()); - final CmHandleValuesHolder cmHandleValuesHolder = - LcmEventsCreatorHelper.determineEventValues(targetNcmpServiceCmHandle, existingNcmpServiceCmHandle, - lcmEventType); - event.setOldValues(cmHandleValuesHolder.getOldValues()); - event.setNewValues(cmHandleValuesHolder.getNewValues()); - - return event; - } - - private LcmEvent lcmEventHeader(final String eventCorrelationId, final LcmEventType lcmEventType) { - final LcmEvent lcmEvent = new LcmEvent(); - lcmEvent.setEventId(UUID.randomUUID().toString()); - lcmEvent.setEventCorrelationId(eventCorrelationId); - lcmEvent.setEventTime(EventDateTimeFormatter.getCurrentIsoFormattedDateTime()); - lcmEvent.setEventSource("org.onap.ncmp"); - lcmEvent.setEventType(lcmEventType.getEventType()); - lcmEvent.setEventSchema("org.onap.ncmp:cmhandle-lcm-event"); - lcmEvent.setEventSchemaVersion("1.0"); - return lcmEvent; - } - - @NoArgsConstructor - @Getter - @Setter - static class CmHandleValuesHolder { - - private Values oldValues; - private Values newValues; - } - -} diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/events/lcm/LcmEventsCreatorHelper.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/events/lcm/LcmEventsCreatorHelper.java deleted file mode 100644 index 7c7a9722ce..0000000000 --- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/events/lcm/LcmEventsCreatorHelper.java +++ /dev/null @@ -1,227 +0,0 @@ -/* - * ============LICENSE_START======================================================= - * Copyright (C) 2022-2023 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.events.lcm; - -import static org.onap.cps.ncmp.api.impl.events.lcm.LcmEventType.CREATE; -import static org.onap.cps.ncmp.api.impl.events.lcm.LcmEventType.DELETE; -import static org.onap.cps.ncmp.api.impl.events.lcm.LcmEventType.UPDATE; -import static org.onap.cps.ncmp.impl.inventory.models.CmHandleState.DELETED; - -import com.google.common.collect.MapDifference; -import com.google.common.collect.Maps; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import lombok.AccessLevel; -import lombok.NoArgsConstructor; -import lombok.extern.slf4j.Slf4j; -import org.onap.cps.ncmp.api.inventory.models.NcmpServiceCmHandle; -import org.onap.cps.ncmp.events.lcm.v1.Values; - -/** - * LcmEventsCreatorHelper has helper methods to create LcmEvent. - * Determine the lcm event type i.e create,update and delete. - * Based on lcm event type create the LcmEvent payload. - */ -@Slf4j -@NoArgsConstructor(access = AccessLevel.PRIVATE) -public class LcmEventsCreatorHelper { - - /** - * Determining the event type based on the composite state. - * - * @param targetNcmpServiceCmHandle target ncmpServiceCmHandle - * @param existingNcmpServiceCmHandle existing ncmpServiceCmHandle - * @return Event Type - */ - public static LcmEventType determineEventType(final NcmpServiceCmHandle targetNcmpServiceCmHandle, - final NcmpServiceCmHandle existingNcmpServiceCmHandle) { - - if (existingNcmpServiceCmHandle.getCompositeState() == null) { - return CREATE; - } else if (targetNcmpServiceCmHandle.getCompositeState().getCmHandleState() == DELETED) { - return DELETE; - } - return UPDATE; - } - - /** - * Determine the cmhandle value difference pair.Contains the difference in the form of oldValues and newValues. - * - * @param targetNcmpServiceCmHandle target ncmpServiceCmHandle - * @param existingNcmpServiceCmHandle existing ncmpServiceCmHandle - * @param lcmEventType lcm event type - * @return Lcm Event Value difference pair - */ - public static LcmEventsCreator.CmHandleValuesHolder determineEventValues( - final NcmpServiceCmHandle targetNcmpServiceCmHandle, final NcmpServiceCmHandle existingNcmpServiceCmHandle, - final LcmEventType lcmEventType) { - - if (CREATE == lcmEventType) { - return determineCreateEventValues(targetNcmpServiceCmHandle); - } else if (UPDATE == lcmEventType) { - return determineUpdateEventValues(targetNcmpServiceCmHandle, existingNcmpServiceCmHandle); - } - return new LcmEventsCreator.CmHandleValuesHolder(); - - } - - private static LcmEventsCreator.CmHandleValuesHolder determineCreateEventValues( - final NcmpServiceCmHandle ncmpServiceCmHandle) { - final LcmEventsCreator.CmHandleValuesHolder cmHandleValuesHolder = new LcmEventsCreator.CmHandleValuesHolder(); - cmHandleValuesHolder.setNewValues(new Values()); - cmHandleValuesHolder.getNewValues().setDataSyncEnabled(getDataSyncEnabledFlag(ncmpServiceCmHandle)); - cmHandleValuesHolder.getNewValues() - .setCmHandleState(mapCmHandleStateToLcmEventCmHandleState(ncmpServiceCmHandle)); - cmHandleValuesHolder.getNewValues().setCmHandleProperties(List.of(ncmpServiceCmHandle.getPublicProperties())); - return cmHandleValuesHolder; - } - - private static LcmEventsCreator.CmHandleValuesHolder determineUpdateEventValues( - final NcmpServiceCmHandle targetNcmpServiceCmHandle, - final NcmpServiceCmHandle existingNcmpServiceCmHandle) { - - final boolean hasDataSyncFlagEnabledChanged = - hasDataSyncEnabledFlagChanged(targetNcmpServiceCmHandle, existingNcmpServiceCmHandle); - final boolean hasCmHandleStateChanged = - hasCmHandleStateChanged(targetNcmpServiceCmHandle, existingNcmpServiceCmHandle); - final boolean arePublicCmHandlePropertiesEqual = - arePublicCmHandlePropertiesEqual(targetNcmpServiceCmHandle.getPublicProperties(), - existingNcmpServiceCmHandle.getPublicProperties()); - - final LcmEventsCreator.CmHandleValuesHolder cmHandleValuesHolder = new LcmEventsCreator.CmHandleValuesHolder(); - - if (hasDataSyncFlagEnabledChanged || hasCmHandleStateChanged || (!arePublicCmHandlePropertiesEqual)) { - cmHandleValuesHolder.setOldValues(new Values()); - cmHandleValuesHolder.setNewValues(new Values()); - } else { - return cmHandleValuesHolder; - } - - if (hasDataSyncFlagEnabledChanged) { - setDataSyncEnabledFlag(targetNcmpServiceCmHandle, existingNcmpServiceCmHandle, cmHandleValuesHolder); - } - - if (hasCmHandleStateChanged) { - setCmHandleStateChange(targetNcmpServiceCmHandle, existingNcmpServiceCmHandle, cmHandleValuesHolder); - } - - if (!arePublicCmHandlePropertiesEqual) { - setPublicCmHandlePropertiesChange(targetNcmpServiceCmHandle, existingNcmpServiceCmHandle, - cmHandleValuesHolder); - } - - return cmHandleValuesHolder; - - } - - private static void setDataSyncEnabledFlag(final NcmpServiceCmHandle targetNcmpServiceCmHandle, - final NcmpServiceCmHandle existingNcmpServiceCmHandle, - final LcmEventsCreator.CmHandleValuesHolder cmHandleValuesHolder) { - - cmHandleValuesHolder.getOldValues().setDataSyncEnabled(getDataSyncEnabledFlag(existingNcmpServiceCmHandle)); - cmHandleValuesHolder.getNewValues().setDataSyncEnabled(getDataSyncEnabledFlag(targetNcmpServiceCmHandle)); - - } - - private static void setCmHandleStateChange(final NcmpServiceCmHandle targetNcmpServiceCmHandle, - final NcmpServiceCmHandle existingNcmpServiceCmHandle, - final LcmEventsCreator.CmHandleValuesHolder cmHandleValuesHolder) { - cmHandleValuesHolder.getOldValues() - .setCmHandleState(mapCmHandleStateToLcmEventCmHandleState(existingNcmpServiceCmHandle)); - cmHandleValuesHolder.getNewValues() - .setCmHandleState(mapCmHandleStateToLcmEventCmHandleState(targetNcmpServiceCmHandle)); - } - - private static void setPublicCmHandlePropertiesChange(final NcmpServiceCmHandle targetNcmpServiceCmHandle, - final NcmpServiceCmHandle existingNcmpServiceCmHandle, - final LcmEventsCreator.CmHandleValuesHolder cmHandleValuesHolder) { - - final Map> publicCmHandlePropertiesDifference = - getPublicCmHandlePropertiesDifference(targetNcmpServiceCmHandle.getPublicProperties(), - existingNcmpServiceCmHandle.getPublicProperties()); - cmHandleValuesHolder.getOldValues() - .setCmHandleProperties(List.of(publicCmHandlePropertiesDifference.get("oldValues"))); - cmHandleValuesHolder.getNewValues() - .setCmHandleProperties(List.of(publicCmHandlePropertiesDifference.get("newValues"))); - - } - - private static Values.CmHandleState mapCmHandleStateToLcmEventCmHandleState( - final NcmpServiceCmHandle ncmpServiceCmHandle) { - return Values.CmHandleState.fromValue(ncmpServiceCmHandle.getCompositeState().getCmHandleState().name()); - } - - private static Boolean getDataSyncEnabledFlag(final NcmpServiceCmHandle ncmpServiceCmHandle) { - return ncmpServiceCmHandle.getCompositeState().getDataSyncEnabled(); - } - - private static boolean hasDataSyncEnabledFlagChanged(final NcmpServiceCmHandle targetNcmpServiceCmHandle, - final NcmpServiceCmHandle existingNcmpServiceCmHandle) { - - final Boolean targetDataSyncFlag = targetNcmpServiceCmHandle.getCompositeState().getDataSyncEnabled(); - final Boolean existingDataSyncFlag = existingNcmpServiceCmHandle.getCompositeState().getDataSyncEnabled(); - - if (targetDataSyncFlag == null) { - return existingDataSyncFlag != null; - } - - return !targetDataSyncFlag.equals(existingDataSyncFlag); - } - - private static boolean hasCmHandleStateChanged(final NcmpServiceCmHandle targetNcmpServiceCmHandle, - final NcmpServiceCmHandle existingNcmpServiceCmHandle) { - - return targetNcmpServiceCmHandle.getCompositeState().getCmHandleState() - != existingNcmpServiceCmHandle.getCompositeState().getCmHandleState(); - } - - private static boolean arePublicCmHandlePropertiesEqual(final Map targetCmHandleProperties, - final Map existingCmHandleProperties) { - if (targetCmHandleProperties.size() != existingCmHandleProperties.size()) { - return false; - } - - return targetCmHandleProperties.equals(existingCmHandleProperties); - } - - private static Map> getPublicCmHandlePropertiesDifference( - final Map targetCmHandleProperties, final Map existingCmHandleProperties) { - final Map> oldAndNewPropertiesDifferenceMap = new HashMap<>(2); - - final MapDifference cmHandlePropertiesDifference = - Maps.difference(targetCmHandleProperties, existingCmHandleProperties); - - final Map newValues = new HashMap<>(cmHandlePropertiesDifference.entriesOnlyOnLeft()); - final Map oldValues = new HashMap<>(cmHandlePropertiesDifference.entriesOnlyOnRight()); - - cmHandlePropertiesDifference.entriesDiffering().keySet().forEach(cmHandlePropertyName -> { - oldValues.put(cmHandlePropertyName, existingCmHandleProperties.get(cmHandlePropertyName)); - newValues.put(cmHandlePropertyName, targetCmHandleProperties.get(cmHandlePropertyName)); - }); - - oldAndNewPropertiesDifferenceMap.put("oldValues", oldValues); - oldAndNewPropertiesDifferenceMap.put("newValues", newValues); - - return oldAndNewPropertiesDifferenceMap; - } - -} diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/events/lcm/LcmEventsService.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/events/lcm/LcmEventsService.java deleted file mode 100644 index f51b58c3ef..0000000000 --- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/events/lcm/LcmEventsService.java +++ /dev/null @@ -1,74 +0,0 @@ -/* - * ============LICENSE_START======================================================= - * Copyright (C) 2022-2024 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.events.lcm; - -import io.micrometer.core.annotation.Timed; -import java.util.Map; -import lombok.RequiredArgsConstructor; -import lombok.extern.slf4j.Slf4j; -import org.onap.cps.events.EventsPublisher; -import org.onap.cps.ncmp.events.lcm.v1.LcmEvent; -import org.onap.cps.ncmp.events.lcm.v1.LcmEventHeader; -import org.onap.cps.utils.JsonObjectMapper; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.kafka.KafkaException; -import org.springframework.stereotype.Service; - -/** - * LcmEventsService to call the publisher and publish on the dedicated topic. - */ - -@Slf4j -@Service -@RequiredArgsConstructor -public class LcmEventsService { - - private final EventsPublisher eventsPublisher; - private final JsonObjectMapper jsonObjectMapper; - - @Value("${app.lcm.events.topic:ncmp-events}") - private String topicName; - - @Value("${notification.enabled:true}") - private boolean notificationsEnabled; - - /** - * Publish the LcmEvent with header to the public topic. - * - * @param cmHandleId Cm Handle Id - * @param lcmEvent Lcm Event - * @param lcmEventHeader Lcm Event Header - */ - @Timed(value = "cps.ncmp.lcm.events.publish", description = "Time taken to publish a LCM event") - public void publishLcmEvent(final String cmHandleId, final LcmEvent lcmEvent, final LcmEventHeader lcmEventHeader) { - if (notificationsEnabled) { - try { - final Map lcmEventHeadersMap = - jsonObjectMapper.convertToValueType(lcmEventHeader, Map.class); - eventsPublisher.publishEvent(topicName, cmHandleId, lcmEventHeadersMap, lcmEvent); - } catch (final KafkaException e) { - log.error("Unable to publish message to topic : {} and cause : {}", topicName, e.getMessage()); - } - } else { - log.debug("Notifications disabled."); - } - } -} diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/events/mapper/CloudEventMapper.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/events/mapper/CloudEventMapper.java deleted file mode 100644 index 4120970e52..0000000000 --- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/events/mapper/CloudEventMapper.java +++ /dev/null @@ -1,61 +0,0 @@ -/* - * ============LICENSE_START======================================================= - * Copyright (c) 2023 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.events.mapper; - -import com.fasterxml.jackson.databind.ObjectMapper; -import io.cloudevents.CloudEvent; -import io.cloudevents.core.CloudEventUtils; -import io.cloudevents.core.data.PojoCloudEventData; -import io.cloudevents.jackson.PojoCloudEventDataMapper; -import lombok.AccessLevel; -import lombok.NoArgsConstructor; -import lombok.extern.slf4j.Slf4j; - -@Slf4j -@NoArgsConstructor(access = AccessLevel.PRIVATE) -public class CloudEventMapper { - - private static final ObjectMapper objectMapper = new ObjectMapper(); - - /** - * Generic method to map cloud event data to target event class object. - * - * @param cloudEvent input cloud event - * @param targetEventClass target event class - * @param target event class type - * @return mapped target event - */ - public static T toTargetEvent(final CloudEvent cloudEvent, final Class targetEventClass) { - PojoCloudEventData mappedCloudEvent = null; - - try { - mappedCloudEvent = - CloudEventUtils.mapData(cloudEvent, PojoCloudEventDataMapper.from(objectMapper, targetEventClass)); - - } catch (final RuntimeException runtimeException) { - log.error("Unable to map cloud event to target event class type : {} with cause : {}", targetEventClass, - runtimeException.getMessage()); - } - - return mappedCloudEvent == null ? null : mappedCloudEvent.getValue(); - } - -} diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/utils/data/operation/DataOperationEventCreator.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/utils/data/operation/DataOperationEventCreator.java index 42bad89f52..68b14349df 100644 --- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/utils/data/operation/DataOperationEventCreator.java +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/utils/data/operation/DataOperationEventCreator.java @@ -29,11 +29,11 @@ import lombok.AccessLevel; import lombok.NoArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.onap.cps.ncmp.api.NcmpResponseStatus; -import org.onap.cps.ncmp.api.impl.events.NcmpEvent; import org.onap.cps.ncmp.api.impl.operations.DmiDataOperation; import org.onap.cps.ncmp.events.async1_0_0.Data; import org.onap.cps.ncmp.events.async1_0_0.DataOperationEvent; import org.onap.cps.ncmp.events.async1_0_0.Response; +import org.onap.cps.ncmp.utils.events.NcmpEvent; import org.springframework.util.MultiValueMap; @Slf4j diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/inventory/CmHandleRegistrationService.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/inventory/CmHandleRegistrationService.java index 1cbba7d6c7..78feca56f4 100644 --- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/inventory/CmHandleRegistrationService.java +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/inventory/CmHandleRegistrationService.java @@ -47,7 +47,6 @@ 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.impl.events.lcm.LcmEventsCmHandleStateHandler; import org.onap.cps.ncmp.api.impl.utils.AlternateIdChecker; import org.onap.cps.ncmp.api.inventory.models.CmHandleRegistrationResponse; import org.onap.cps.ncmp.api.inventory.models.CompositeState; @@ -59,6 +58,7 @@ import org.onap.cps.ncmp.api.inventory.models.TrustLevel; import org.onap.cps.ncmp.impl.inventory.models.CmHandleState; import org.onap.cps.ncmp.impl.inventory.models.YangModelCmHandle; import org.onap.cps.ncmp.impl.inventory.sync.ModuleOperationsUtils; +import org.onap.cps.ncmp.impl.inventory.sync.lcm.LcmEventsCmHandleStateHandler; import org.onap.cps.ncmp.impl.inventory.trustlevel.TrustLevelCacheConfig; import org.onap.cps.ncmp.impl.inventory.trustlevel.TrustLevelManager; import org.onap.cps.spi.exceptions.AlreadyDefinedException; diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/inventory/sync/ModuleSyncTasks.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/inventory/sync/ModuleSyncTasks.java index 08db195ad6..39ea38aafc 100644 --- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/inventory/sync/ModuleSyncTasks.java +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/inventory/sync/ModuleSyncTasks.java @@ -29,12 +29,12 @@ import java.util.concurrent.CompletableFuture; import java.util.concurrent.atomic.AtomicInteger; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; -import org.onap.cps.ncmp.api.impl.events.lcm.LcmEventsCmHandleStateHandler; import org.onap.cps.ncmp.api.inventory.models.CompositeState; import org.onap.cps.ncmp.impl.inventory.InventoryPersistence; import org.onap.cps.ncmp.impl.inventory.models.CmHandleState; import org.onap.cps.ncmp.impl.inventory.models.LockReasonCategory; import org.onap.cps.ncmp.impl.inventory.models.YangModelCmHandle; +import org.onap.cps.ncmp.impl.inventory.sync.lcm.LcmEventsCmHandleStateHandler; import org.onap.cps.ncmp.impl.utils.YangDataConverter; import org.onap.cps.spi.model.DataNode; import org.springframework.stereotype.Component; diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/inventory/sync/lcm/LcmEventHeaderMapper.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/inventory/sync/lcm/LcmEventHeaderMapper.java new file mode 100644 index 0000000000..7395838306 --- /dev/null +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/inventory/sync/lcm/LcmEventHeaderMapper.java @@ -0,0 +1,36 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2023 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.impl.inventory.sync.lcm; + +import org.mapstruct.Mapper; +import org.onap.cps.ncmp.events.lcm.v1.LcmEvent; +import org.onap.cps.ncmp.events.lcm.v1.LcmEventHeader; + +@Mapper(componentModel = "spring") +public interface LcmEventHeaderMapper { + + /** + * Mapper for converting incoming {@link LcmEvent} to outgoing {@link LcmEventHeader}. + */ + + LcmEventHeader toLcmEventHeader(LcmEvent lcmEvent); + +} diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/inventory/sync/lcm/LcmEventType.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/inventory/sync/lcm/LcmEventType.java new file mode 100644 index 0000000000..4bc2f10218 --- /dev/null +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/inventory/sync/lcm/LcmEventType.java @@ -0,0 +1,38 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2022-2023 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.impl.inventory.sync.lcm; + +public enum LcmEventType { + + CREATE("create"), UPDATE("update"), DELETE("delete"); + + private final String eventName; + + private final String eventTypeTemplate = "org.onap.ncmp.cmhandle-lcm-event.%s"; + + LcmEventType(final String eventName) { + this.eventName = String.format(eventTypeTemplate, eventName); + } + + public String getEventType() { + return this.eventName; + } +} diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/inventory/sync/lcm/LcmEventsCmHandleStateHandler.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/inventory/sync/lcm/LcmEventsCmHandleStateHandler.java new file mode 100644 index 0000000000..6cce153269 --- /dev/null +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/inventory/sync/lcm/LcmEventsCmHandleStateHandler.java @@ -0,0 +1,55 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2022-2023 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.impl.inventory.sync.lcm; + +import java.util.Collection; +import java.util.Map; +import org.onap.cps.ncmp.impl.inventory.models.CmHandleState; +import org.onap.cps.ncmp.impl.inventory.models.YangModelCmHandle; + +/** + * The implementation of it should handle the persisting of composite state and delegate the request to publish the + * corresponding lcm event. + */ +public interface LcmEventsCmHandleStateHandler { + + /** + * Updates the composite state of cmHandle based on cmHandleState. + * + * @param yangModelCmHandle cm handle represented as yang model + * @param targetCmHandleState target cm handle state + */ + void updateCmHandleState(final YangModelCmHandle yangModelCmHandle, final CmHandleState targetCmHandleState); + + /** + * Updates the composite state of cmHandle based on cmHandleState in batch. + * + * @param cmHandleStatePerCmHandle Map of Yang Model Cm Handle and corresponding cm handle state. + */ + void updateCmHandleStateBatch(final Map cmHandleStatePerCmHandle); + + /** + * Sets the initial state of cmHandles to ADVISED. + * + * @param yangModelCmHandles List of Yang Model Cm Handle. + */ + void initiateStateAdvised(Collection yangModelCmHandles); +} diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/inventory/sync/lcm/LcmEventsCmHandleStateHandlerAsyncHelper.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/inventory/sync/lcm/LcmEventsCmHandleStateHandlerAsyncHelper.java new file mode 100644 index 0000000000..cf7921c350 --- /dev/null +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/inventory/sync/lcm/LcmEventsCmHandleStateHandlerAsyncHelper.java @@ -0,0 +1,79 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2023 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.impl.inventory.sync.lcm; + +import java.util.Collection; +import lombok.RequiredArgsConstructor; +import org.onap.cps.ncmp.api.inventory.models.NcmpServiceCmHandle; +import org.onap.cps.ncmp.events.lcm.v1.LcmEvent; +import org.onap.cps.ncmp.events.lcm.v1.LcmEventHeader; +import org.onap.cps.ncmp.impl.inventory.models.YangModelCmHandle; +import org.onap.cps.ncmp.impl.utils.YangDataConverter; +import org.springframework.scheduling.annotation.Async; +import org.springframework.stereotype.Service; + +@Service +@RequiredArgsConstructor +public class LcmEventsCmHandleStateHandlerAsyncHelper { + + private final LcmEventsCreator lcmEventsCreator; + private final LcmEventsService lcmEventsService; + + /** + * Publish LCM Event in asynchronous manner. + * + * @param targetNcmpServiceCmHandle target NcmpServiceCmHandle + * @param currentNcmpServiceCmHandle current NcmpServiceCmHandle + */ + @Async("notificationExecutor") + public void publishLcmEventAsynchronously(final NcmpServiceCmHandle targetNcmpServiceCmHandle, + final NcmpServiceCmHandle currentNcmpServiceCmHandle) { + publishLcmEvent(targetNcmpServiceCmHandle, currentNcmpServiceCmHandle); + } + + /** + * Publish LcmEvent in batches and in asynchronous manner. + * + * @param cmHandleTransitionPairs Pair of existing and modified cm handle represented as YangModelCmHandle + */ + @Async("notificationExecutor") + public void publishLcmEventBatchAsynchronously( + final Collection cmHandleTransitionPairs) { + cmHandleTransitionPairs.forEach(cmHandleTransitionPair -> publishLcmEvent( + toNcmpServiceCmHandle(cmHandleTransitionPair.getTargetYangModelCmHandle()), + toNcmpServiceCmHandle(cmHandleTransitionPair.getCurrentYangModelCmHandle()))); + } + + private void publishLcmEvent(final NcmpServiceCmHandle targetNcmpServiceCmHandle, + final NcmpServiceCmHandle existingNcmpServiceCmHandle) { + final String cmHandleId = targetNcmpServiceCmHandle.getCmHandleId(); + final LcmEventHeader lcmEventHeader = + lcmEventsCreator.populateLcmEventHeader(cmHandleId, targetNcmpServiceCmHandle, + existingNcmpServiceCmHandle); + final LcmEvent lcmEvent = + lcmEventsCreator.populateLcmEvent(cmHandleId, targetNcmpServiceCmHandle, existingNcmpServiceCmHandle); + lcmEventsService.publishLcmEvent(cmHandleId, lcmEvent, lcmEventHeader); + } + + private static NcmpServiceCmHandle toNcmpServiceCmHandle(final YangModelCmHandle yangModelCmHandle) { + return YangDataConverter.toNcmpServiceCmHandle(yangModelCmHandle); + } +} diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/inventory/sync/lcm/LcmEventsCmHandleStateHandlerImpl.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/inventory/sync/lcm/LcmEventsCmHandleStateHandlerImpl.java new file mode 100644 index 0000000000..b1b7b955f7 --- /dev/null +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/inventory/sync/lcm/LcmEventsCmHandleStateHandlerImpl.java @@ -0,0 +1,208 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2022-2024 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.impl.inventory.sync.lcm; + +import static org.onap.cps.ncmp.impl.inventory.models.CmHandleState.ADVISED; +import static org.onap.cps.ncmp.impl.inventory.models.CmHandleState.DELETED; +import static org.onap.cps.ncmp.impl.inventory.models.CmHandleState.LOCKED; +import static org.onap.cps.ncmp.impl.inventory.models.CmHandleState.READY; + +import io.micrometer.core.annotation.Timed; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.RequiredArgsConstructor; +import lombok.Setter; +import lombok.extern.slf4j.Slf4j; +import org.onap.cps.ncmp.api.inventory.models.CompositeState; +import org.onap.cps.ncmp.api.inventory.models.NcmpServiceCmHandle; +import org.onap.cps.ncmp.impl.inventory.CompositeStateUtils; +import org.onap.cps.ncmp.impl.inventory.InventoryPersistence; +import org.onap.cps.ncmp.impl.inventory.models.CmHandleState; +import org.onap.cps.ncmp.impl.inventory.models.YangModelCmHandle; +import org.onap.cps.ncmp.impl.utils.YangDataConverter; +import org.springframework.stereotype.Service; + +@Slf4j +@Service +@RequiredArgsConstructor +public class LcmEventsCmHandleStateHandlerImpl implements LcmEventsCmHandleStateHandler { + + private final InventoryPersistence inventoryPersistence; + private final LcmEventsCmHandleStateHandlerAsyncHelper lcmEventsCmHandleStateHandlerAsyncHelper; + + @Override + public void updateCmHandleState(final YangModelCmHandle updatedYangModelCmHandle, + final CmHandleState targetCmHandleState) { + + final CompositeState compositeState = updatedYangModelCmHandle.getCompositeState(); + + if (isCompositeStateSame(compositeState, targetCmHandleState)) { + log.debug("CmHandle with id : {} already in state : {}", updatedYangModelCmHandle.getId(), + targetCmHandleState); + } else { + final YangModelCmHandle currentYangModelCmHandle = YangModelCmHandle.deepCopyOf(updatedYangModelCmHandle); + updateToSpecifiedCmHandleState(updatedYangModelCmHandle, targetCmHandleState); + persistCmHandle(updatedYangModelCmHandle, currentYangModelCmHandle); + lcmEventsCmHandleStateHandlerAsyncHelper.publishLcmEventAsynchronously( + toNcmpServiceCmHandle(updatedYangModelCmHandle), + toNcmpServiceCmHandle(currentYangModelCmHandle)); + } + } + + @Override + @Timed(value = "cps.ncmp.cmhandle.state.update.batch", + description = "Time taken to update a batch of cm handle states") + public void updateCmHandleStateBatch(final Map cmHandleStatePerCmHandle) { + final Collection cmHandleTransitionPairs = + prepareCmHandleTransitionBatch(cmHandleStatePerCmHandle); + persistCmHandleBatch(cmHandleTransitionPairs); + lcmEventsCmHandleStateHandlerAsyncHelper.publishLcmEventBatchAsynchronously(cmHandleTransitionPairs); + } + + @Override + public void initiateStateAdvised(final Collection yangModelCmHandles) { + final Map cmHandleStatePerCmHandle = new HashMap<>(yangModelCmHandles.size()); + for (final YangModelCmHandle yangModelCmHandle : yangModelCmHandles) { + cmHandleStatePerCmHandle.put(yangModelCmHandle, ADVISED); + } + updateCmHandleStateBatch(cmHandleStatePerCmHandle); + } + + private Collection prepareCmHandleTransitionBatch( + final Map cmHandleStatePerCmHandle) { + final List cmHandleTransitionPairs = new ArrayList<>(cmHandleStatePerCmHandle.size()); + cmHandleStatePerCmHandle.forEach((yangModelCmHandle, targetCmHandleState) -> { + + final CompositeState compositeState = yangModelCmHandle.getCompositeState(); + + if (isCompositeStateSame(compositeState, targetCmHandleState)) { + log.debug("CmHandle with id : {} already in state : {}", yangModelCmHandle.getId(), + targetCmHandleState); + } else { + final CmHandleTransitionPair cmHandleTransitionPair = new CmHandleTransitionPair(); + cmHandleTransitionPair.setCurrentYangModelCmHandle(YangModelCmHandle.deepCopyOf(yangModelCmHandle)); + updateToSpecifiedCmHandleState(yangModelCmHandle, targetCmHandleState); + cmHandleTransitionPair.setTargetYangModelCmHandle(yangModelCmHandle); + cmHandleTransitionPairs.add(cmHandleTransitionPair); + } + }); + return cmHandleTransitionPairs; + } + + + private void persistCmHandle(final YangModelCmHandle targetYangModelCmHandle, + final YangModelCmHandle currentYangModelCmHandle) { + if (isNew(currentYangModelCmHandle.getCompositeState())) { + log.debug("Registering a new cm handle {}", targetYangModelCmHandle.getId()); + inventoryPersistence.saveCmHandle(targetYangModelCmHandle); + } else if (isDeleted(targetYangModelCmHandle.getCompositeState())) { + log.info("CmHandle with Id : {} is DELETED", targetYangModelCmHandle.getId()); + } else { + inventoryPersistence.saveCmHandleState(targetYangModelCmHandle.getId(), + targetYangModelCmHandle.getCompositeState()); + } + } + + private void persistCmHandleBatch(final Collection cmHandleTransitionPairs) { + + final List newCmHandles = new ArrayList<>(); + final Map compositeStatePerCmHandleId = new LinkedHashMap<>(); + + cmHandleTransitionPairs.forEach(cmHandleTransitionPair -> { + if (isNew(cmHandleTransitionPair.getCurrentYangModelCmHandle().getCompositeState() + )) { + newCmHandles.add(cmHandleTransitionPair.getTargetYangModelCmHandle()); + } else if (!isDeleted(cmHandleTransitionPair.getTargetYangModelCmHandle().getCompositeState())) { + compositeStatePerCmHandleId.put(cmHandleTransitionPair.getTargetYangModelCmHandle().getId(), + cmHandleTransitionPair.getTargetYangModelCmHandle().getCompositeState()); + } + }); + + inventoryPersistence.saveCmHandleBatch(newCmHandles); + inventoryPersistence.saveCmHandleStateBatch(compositeStatePerCmHandleId); + + } + + private void updateToSpecifiedCmHandleState(final YangModelCmHandle yangModelCmHandle, + final CmHandleState targetCmHandleState) { + + if (READY == targetCmHandleState) { + setInitialStates(yangModelCmHandle); + } else if (ADVISED == targetCmHandleState) { + if (yangModelCmHandle.getCompositeState() == null) { + registerNewCmHandle(yangModelCmHandle); + } else if (yangModelCmHandle.getCompositeState().getCmHandleState() == LOCKED) { + retryCmHandle(yangModelCmHandle); + } + } else { + setCmHandleState(yangModelCmHandle, targetCmHandleState); + } + } + + private void setInitialStates(final YangModelCmHandle yangModelCmHandle) { + CompositeStateUtils.setInitialDataStoreSyncState(yangModelCmHandle.getCompositeState()); + CompositeStateUtils.setCompositeState(READY, yangModelCmHandle.getCompositeState()); + } + + private void retryCmHandle(final YangModelCmHandle yangModelCmHandle) { + CompositeStateUtils.setCompositeStateForRetry(yangModelCmHandle.getCompositeState()); + } + + private void registerNewCmHandle(final YangModelCmHandle yangModelCmHandle) { + yangModelCmHandle.setCompositeState(new CompositeState()); + setCmHandleState(yangModelCmHandle, ADVISED); + } + + private void setCmHandleState(final YangModelCmHandle yangModelCmHandle, final CmHandleState targetCmHandleState) { + CompositeStateUtils.setCompositeState(targetCmHandleState, yangModelCmHandle.getCompositeState()); + } + + private boolean isNew(final CompositeState existingCompositeState) { + return (existingCompositeState == null); + } + + private boolean isDeleted(final CompositeState targetCompositeState) { + return targetCompositeState.getCmHandleState() == DELETED; + } + + private boolean isCompositeStateSame(final CompositeState compositeState, final CmHandleState targetCmHandleState) { + return (compositeState != null && compositeState.getCmHandleState() == targetCmHandleState); + } + + private NcmpServiceCmHandle toNcmpServiceCmHandle(final YangModelCmHandle yangModelCmHandle) { + return YangDataConverter.toNcmpServiceCmHandle(yangModelCmHandle); + } + + @Getter + @Setter + @NoArgsConstructor + static class CmHandleTransitionPair { + + private YangModelCmHandle currentYangModelCmHandle; + private YangModelCmHandle targetYangModelCmHandle; + } +} diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/inventory/sync/lcm/LcmEventsCreator.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/inventory/sync/lcm/LcmEventsCreator.java new file mode 100644 index 0000000000..3ce6b91590 --- /dev/null +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/inventory/sync/lcm/LcmEventsCreator.java @@ -0,0 +1,131 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2022-2024 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.impl.inventory.sync.lcm; + +import java.util.UUID; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.RequiredArgsConstructor; +import lombok.Setter; +import lombok.extern.slf4j.Slf4j; +import org.onap.cps.ncmp.api.impl.utils.EventDateTimeFormatter; +import org.onap.cps.ncmp.api.inventory.models.NcmpServiceCmHandle; +import org.onap.cps.ncmp.events.lcm.v1.Event; +import org.onap.cps.ncmp.events.lcm.v1.LcmEvent; +import org.onap.cps.ncmp.events.lcm.v1.LcmEventHeader; +import org.onap.cps.ncmp.events.lcm.v1.Values; +import org.springframework.stereotype.Component; + + +/** + * LcmEventsCreator to create LcmEvent based on relevant operation. + */ +@Slf4j +@Component +@RequiredArgsConstructor +public class LcmEventsCreator { + + private final LcmEventHeaderMapper lcmEventHeaderMapper; + + /** + * Populate Lifecycle Management Event. + * + * @param cmHandleId cm handle identifier + * @param targetNcmpServiceCmHandle target ncmp service cmhandle + * @param existingNcmpServiceCmHandle existing ncmp service cmhandle + * @return Populated LcmEvent + */ + public LcmEvent populateLcmEvent(final String cmHandleId, final NcmpServiceCmHandle targetNcmpServiceCmHandle, + final NcmpServiceCmHandle existingNcmpServiceCmHandle) { + return createLcmEvent(cmHandleId, targetNcmpServiceCmHandle, existingNcmpServiceCmHandle); + } + + /** + * Populate Lifecycle Management Event Header. + * + * @param cmHandleId cm handle identifier + * @param targetNcmpServiceCmHandle target ncmp service cmhandle + * @param existingNcmpServiceCmHandle existing ncmp service cmhandle + * @return Populated LcmEventHeader + */ + public LcmEventHeader populateLcmEventHeader(final String cmHandleId, + final NcmpServiceCmHandle targetNcmpServiceCmHandle, + final NcmpServiceCmHandle existingNcmpServiceCmHandle) { + return createLcmEventHeader(cmHandleId, targetNcmpServiceCmHandle, existingNcmpServiceCmHandle); + } + + private LcmEvent createLcmEvent(final String cmHandleId, final NcmpServiceCmHandle targetNcmpServiceCmHandle, + final NcmpServiceCmHandle existingNcmpServiceCmHandle) { + final LcmEventType lcmEventType = + LcmEventsCreatorHelper.determineEventType(targetNcmpServiceCmHandle, existingNcmpServiceCmHandle); + final LcmEvent lcmEvent = lcmEventHeader(cmHandleId, lcmEventType); + lcmEvent.setEvent( + lcmEventPayload(cmHandleId, targetNcmpServiceCmHandle, existingNcmpServiceCmHandle, lcmEventType)); + return lcmEvent; + } + + private LcmEventHeader createLcmEventHeader(final String cmHandleId, + final NcmpServiceCmHandle targetNcmpServiceCmHandle, + final NcmpServiceCmHandle existingNcmpServiceCmHandle) { + final LcmEventType lcmEventType = + LcmEventsCreatorHelper.determineEventType(targetNcmpServiceCmHandle, existingNcmpServiceCmHandle); + final LcmEvent lcmEventWithHeaderInformation = lcmEventHeader(cmHandleId, lcmEventType); + return lcmEventHeaderMapper.toLcmEventHeader(lcmEventWithHeaderInformation); + } + + private Event lcmEventPayload(final String eventCorrelationId, final NcmpServiceCmHandle targetNcmpServiceCmHandle, + final NcmpServiceCmHandle existingNcmpServiceCmHandle, final LcmEventType lcmEventType) { + final Event event = new Event(); + event.setCmHandleId(eventCorrelationId); + event.setAlternateId(targetNcmpServiceCmHandle.getAlternateId()); + event.setModuleSetTag(targetNcmpServiceCmHandle.getModuleSetTag()); + event.setDataProducerIdentifier(targetNcmpServiceCmHandle.getDataProducerIdentifier()); + final CmHandleValuesHolder cmHandleValuesHolder = + LcmEventsCreatorHelper.determineEventValues(targetNcmpServiceCmHandle, existingNcmpServiceCmHandle, + lcmEventType); + event.setOldValues(cmHandleValuesHolder.getOldValues()); + event.setNewValues(cmHandleValuesHolder.getNewValues()); + + return event; + } + + private LcmEvent lcmEventHeader(final String eventCorrelationId, final LcmEventType lcmEventType) { + final LcmEvent lcmEvent = new LcmEvent(); + lcmEvent.setEventId(UUID.randomUUID().toString()); + lcmEvent.setEventCorrelationId(eventCorrelationId); + lcmEvent.setEventTime(EventDateTimeFormatter.getCurrentIsoFormattedDateTime()); + lcmEvent.setEventSource("org.onap.ncmp"); + lcmEvent.setEventType(lcmEventType.getEventType()); + lcmEvent.setEventSchema("org.onap.ncmp:cmhandle-lcm-event"); + lcmEvent.setEventSchemaVersion("1.0"); + return lcmEvent; + } + + @NoArgsConstructor + @Getter + @Setter + static class CmHandleValuesHolder { + + private Values oldValues; + private Values newValues; + } + +} diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/inventory/sync/lcm/LcmEventsCreatorHelper.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/inventory/sync/lcm/LcmEventsCreatorHelper.java new file mode 100644 index 0000000000..348894d1b4 --- /dev/null +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/inventory/sync/lcm/LcmEventsCreatorHelper.java @@ -0,0 +1,227 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2022-2023 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.impl.inventory.sync.lcm; + +import static org.onap.cps.ncmp.impl.inventory.models.CmHandleState.DELETED; +import static org.onap.cps.ncmp.impl.inventory.sync.lcm.LcmEventType.CREATE; +import static org.onap.cps.ncmp.impl.inventory.sync.lcm.LcmEventType.DELETE; +import static org.onap.cps.ncmp.impl.inventory.sync.lcm.LcmEventType.UPDATE; + +import com.google.common.collect.MapDifference; +import com.google.common.collect.Maps; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import lombok.AccessLevel; +import lombok.NoArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.onap.cps.ncmp.api.inventory.models.NcmpServiceCmHandle; +import org.onap.cps.ncmp.events.lcm.v1.Values; + +/** + * LcmEventsCreatorHelper has helper methods to create LcmEvent. + * Determine the lcm event type i.e create,update and delete. + * Based on lcm event type create the LcmEvent payload. + */ +@Slf4j +@NoArgsConstructor(access = AccessLevel.PRIVATE) +public class LcmEventsCreatorHelper { + + /** + * Determining the event type based on the composite state. + * + * @param targetNcmpServiceCmHandle target ncmpServiceCmHandle + * @param existingNcmpServiceCmHandle existing ncmpServiceCmHandle + * @return Event Type + */ + public static LcmEventType determineEventType(final NcmpServiceCmHandle targetNcmpServiceCmHandle, + final NcmpServiceCmHandle existingNcmpServiceCmHandle) { + + if (existingNcmpServiceCmHandle.getCompositeState() == null) { + return CREATE; + } else if (targetNcmpServiceCmHandle.getCompositeState().getCmHandleState() == DELETED) { + return DELETE; + } + return UPDATE; + } + + /** + * Determine the cmhandle value difference pair.Contains the difference in the form of oldValues and newValues. + * + * @param targetNcmpServiceCmHandle target ncmpServiceCmHandle + * @param existingNcmpServiceCmHandle existing ncmpServiceCmHandle + * @param lcmEventType lcm event type + * @return Lcm Event Value difference pair + */ + public static LcmEventsCreator.CmHandleValuesHolder determineEventValues( + final NcmpServiceCmHandle targetNcmpServiceCmHandle, final NcmpServiceCmHandle existingNcmpServiceCmHandle, + final LcmEventType lcmEventType) { + + if (CREATE == lcmEventType) { + return determineCreateEventValues(targetNcmpServiceCmHandle); + } else if (UPDATE == lcmEventType) { + return determineUpdateEventValues(targetNcmpServiceCmHandle, existingNcmpServiceCmHandle); + } + return new LcmEventsCreator.CmHandleValuesHolder(); + + } + + private static LcmEventsCreator.CmHandleValuesHolder determineCreateEventValues( + final NcmpServiceCmHandle ncmpServiceCmHandle) { + final LcmEventsCreator.CmHandleValuesHolder cmHandleValuesHolder = new LcmEventsCreator.CmHandleValuesHolder(); + cmHandleValuesHolder.setNewValues(new Values()); + cmHandleValuesHolder.getNewValues().setDataSyncEnabled(getDataSyncEnabledFlag(ncmpServiceCmHandle)); + cmHandleValuesHolder.getNewValues() + .setCmHandleState(mapCmHandleStateToLcmEventCmHandleState(ncmpServiceCmHandle)); + cmHandleValuesHolder.getNewValues().setCmHandleProperties(List.of(ncmpServiceCmHandle.getPublicProperties())); + return cmHandleValuesHolder; + } + + private static LcmEventsCreator.CmHandleValuesHolder determineUpdateEventValues( + final NcmpServiceCmHandle targetNcmpServiceCmHandle, + final NcmpServiceCmHandle existingNcmpServiceCmHandle) { + + final boolean hasDataSyncFlagEnabledChanged = + hasDataSyncEnabledFlagChanged(targetNcmpServiceCmHandle, existingNcmpServiceCmHandle); + final boolean hasCmHandleStateChanged = + hasCmHandleStateChanged(targetNcmpServiceCmHandle, existingNcmpServiceCmHandle); + final boolean arePublicCmHandlePropertiesEqual = + arePublicCmHandlePropertiesEqual(targetNcmpServiceCmHandle.getPublicProperties(), + existingNcmpServiceCmHandle.getPublicProperties()); + + final LcmEventsCreator.CmHandleValuesHolder cmHandleValuesHolder = new LcmEventsCreator.CmHandleValuesHolder(); + + if (hasDataSyncFlagEnabledChanged || hasCmHandleStateChanged || (!arePublicCmHandlePropertiesEqual)) { + cmHandleValuesHolder.setOldValues(new Values()); + cmHandleValuesHolder.setNewValues(new Values()); + } else { + return cmHandleValuesHolder; + } + + if (hasDataSyncFlagEnabledChanged) { + setDataSyncEnabledFlag(targetNcmpServiceCmHandle, existingNcmpServiceCmHandle, cmHandleValuesHolder); + } + + if (hasCmHandleStateChanged) { + setCmHandleStateChange(targetNcmpServiceCmHandle, existingNcmpServiceCmHandle, cmHandleValuesHolder); + } + + if (!arePublicCmHandlePropertiesEqual) { + setPublicCmHandlePropertiesChange(targetNcmpServiceCmHandle, existingNcmpServiceCmHandle, + cmHandleValuesHolder); + } + + return cmHandleValuesHolder; + + } + + private static void setDataSyncEnabledFlag(final NcmpServiceCmHandle targetNcmpServiceCmHandle, + final NcmpServiceCmHandle existingNcmpServiceCmHandle, + final LcmEventsCreator.CmHandleValuesHolder cmHandleValuesHolder) { + + cmHandleValuesHolder.getOldValues().setDataSyncEnabled(getDataSyncEnabledFlag(existingNcmpServiceCmHandle)); + cmHandleValuesHolder.getNewValues().setDataSyncEnabled(getDataSyncEnabledFlag(targetNcmpServiceCmHandle)); + + } + + private static void setCmHandleStateChange(final NcmpServiceCmHandle targetNcmpServiceCmHandle, + final NcmpServiceCmHandle existingNcmpServiceCmHandle, + final LcmEventsCreator.CmHandleValuesHolder cmHandleValuesHolder) { + cmHandleValuesHolder.getOldValues() + .setCmHandleState(mapCmHandleStateToLcmEventCmHandleState(existingNcmpServiceCmHandle)); + cmHandleValuesHolder.getNewValues() + .setCmHandleState(mapCmHandleStateToLcmEventCmHandleState(targetNcmpServiceCmHandle)); + } + + private static void setPublicCmHandlePropertiesChange(final NcmpServiceCmHandle targetNcmpServiceCmHandle, + final NcmpServiceCmHandle existingNcmpServiceCmHandle, + final LcmEventsCreator.CmHandleValuesHolder cmHandleValuesHolder) { + + final Map> publicCmHandlePropertiesDifference = + getPublicCmHandlePropertiesDifference(targetNcmpServiceCmHandle.getPublicProperties(), + existingNcmpServiceCmHandle.getPublicProperties()); + cmHandleValuesHolder.getOldValues() + .setCmHandleProperties(List.of(publicCmHandlePropertiesDifference.get("oldValues"))); + cmHandleValuesHolder.getNewValues() + .setCmHandleProperties(List.of(publicCmHandlePropertiesDifference.get("newValues"))); + + } + + private static Values.CmHandleState mapCmHandleStateToLcmEventCmHandleState( + final NcmpServiceCmHandle ncmpServiceCmHandle) { + return Values.CmHandleState.fromValue(ncmpServiceCmHandle.getCompositeState().getCmHandleState().name()); + } + + private static Boolean getDataSyncEnabledFlag(final NcmpServiceCmHandle ncmpServiceCmHandle) { + return ncmpServiceCmHandle.getCompositeState().getDataSyncEnabled(); + } + + private static boolean hasDataSyncEnabledFlagChanged(final NcmpServiceCmHandle targetNcmpServiceCmHandle, + final NcmpServiceCmHandle existingNcmpServiceCmHandle) { + + final Boolean targetDataSyncFlag = targetNcmpServiceCmHandle.getCompositeState().getDataSyncEnabled(); + final Boolean existingDataSyncFlag = existingNcmpServiceCmHandle.getCompositeState().getDataSyncEnabled(); + + if (targetDataSyncFlag == null) { + return existingDataSyncFlag != null; + } + + return !targetDataSyncFlag.equals(existingDataSyncFlag); + } + + private static boolean hasCmHandleStateChanged(final NcmpServiceCmHandle targetNcmpServiceCmHandle, + final NcmpServiceCmHandle existingNcmpServiceCmHandle) { + + return targetNcmpServiceCmHandle.getCompositeState().getCmHandleState() + != existingNcmpServiceCmHandle.getCompositeState().getCmHandleState(); + } + + private static boolean arePublicCmHandlePropertiesEqual(final Map targetCmHandleProperties, + final Map existingCmHandleProperties) { + if (targetCmHandleProperties.size() != existingCmHandleProperties.size()) { + return false; + } + + return targetCmHandleProperties.equals(existingCmHandleProperties); + } + + private static Map> getPublicCmHandlePropertiesDifference( + final Map targetCmHandleProperties, final Map existingCmHandleProperties) { + final Map> oldAndNewPropertiesDifferenceMap = new HashMap<>(2); + + final MapDifference cmHandlePropertiesDifference = + Maps.difference(targetCmHandleProperties, existingCmHandleProperties); + + final Map newValues = new HashMap<>(cmHandlePropertiesDifference.entriesOnlyOnLeft()); + final Map oldValues = new HashMap<>(cmHandlePropertiesDifference.entriesOnlyOnRight()); + + cmHandlePropertiesDifference.entriesDiffering().keySet().forEach(cmHandlePropertyName -> { + oldValues.put(cmHandlePropertyName, existingCmHandleProperties.get(cmHandlePropertyName)); + newValues.put(cmHandlePropertyName, targetCmHandleProperties.get(cmHandlePropertyName)); + }); + + oldAndNewPropertiesDifferenceMap.put("oldValues", oldValues); + oldAndNewPropertiesDifferenceMap.put("newValues", newValues); + + return oldAndNewPropertiesDifferenceMap; + } + +} diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/inventory/sync/lcm/LcmEventsService.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/inventory/sync/lcm/LcmEventsService.java new file mode 100644 index 0000000000..10aebfa45d --- /dev/null +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/inventory/sync/lcm/LcmEventsService.java @@ -0,0 +1,74 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2022-2024 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.impl.inventory.sync.lcm; + +import io.micrometer.core.annotation.Timed; +import java.util.Map; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.onap.cps.events.EventsPublisher; +import org.onap.cps.ncmp.events.lcm.v1.LcmEvent; +import org.onap.cps.ncmp.events.lcm.v1.LcmEventHeader; +import org.onap.cps.utils.JsonObjectMapper; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.kafka.KafkaException; +import org.springframework.stereotype.Service; + +/** + * LcmEventsService to call the publisher and publish on the dedicated topic. + */ + +@Slf4j +@Service +@RequiredArgsConstructor +public class LcmEventsService { + + private final EventsPublisher eventsPublisher; + private final JsonObjectMapper jsonObjectMapper; + + @Value("${app.lcm.events.topic:ncmp-events}") + private String topicName; + + @Value("${notification.enabled:true}") + private boolean notificationsEnabled; + + /** + * Publish the LcmEvent with header to the public topic. + * + * @param cmHandleId Cm Handle Id + * @param lcmEvent Lcm Event + * @param lcmEventHeader Lcm Event Header + */ + @Timed(value = "cps.ncmp.lcm.events.publish", description = "Time taken to publish a LCM event") + public void publishLcmEvent(final String cmHandleId, final LcmEvent lcmEvent, final LcmEventHeader lcmEventHeader) { + if (notificationsEnabled) { + try { + final Map lcmEventHeadersMap = + jsonObjectMapper.convertToValueType(lcmEventHeader, Map.class); + eventsPublisher.publishEvent(topicName, cmHandleId, lcmEventHeadersMap, lcmEvent); + } catch (final KafkaException e) { + log.error("Unable to publish message to topic : {} and cause : {}", topicName, e.getMessage()); + } + } else { + log.debug("Notifications disabled."); + } + } +} diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/inventory/trustlevel/DeviceTrustLevelMessageConsumer.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/inventory/trustlevel/DeviceTrustLevelMessageConsumer.java index 64ae800879..617fe7f01d 100644 --- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/inventory/trustlevel/DeviceTrustLevelMessageConsumer.java +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/inventory/trustlevel/DeviceTrustLevelMessageConsumer.java @@ -24,9 +24,9 @@ import io.cloudevents.CloudEvent; import io.cloudevents.kafka.impl.KafkaHeaders; import lombok.RequiredArgsConstructor; import org.apache.kafka.clients.consumer.ConsumerRecord; -import org.onap.cps.ncmp.api.impl.events.mapper.CloudEventMapper; import org.onap.cps.ncmp.api.inventory.models.TrustLevel; import org.onap.cps.ncmp.events.trustlevel.DeviceTrustLevel; +import org.onap.cps.ncmp.utils.events.CloudEventMapper; import org.springframework.kafka.annotation.KafkaListener; import org.springframework.stereotype.Component; diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/inventory/trustlevel/TrustLevelManager.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/inventory/trustlevel/TrustLevelManager.java index c7bfb1dbb9..22311c6b5a 100644 --- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/inventory/trustlevel/TrustLevelManager.java +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/inventory/trustlevel/TrustLevelManager.java @@ -24,11 +24,11 @@ import java.util.Collection; import java.util.Map; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; -import org.onap.cps.ncmp.api.impl.events.avc.ncmptoclient.AvcEventPublisher; import org.onap.cps.ncmp.api.impl.operations.RequiredDmiService; import org.onap.cps.ncmp.api.inventory.models.TrustLevel; import org.onap.cps.ncmp.impl.inventory.InventoryPersistence; import org.onap.cps.ncmp.impl.inventory.models.YangModelCmHandle; +import org.onap.cps.ncmp.utils.events.CmAvcEventPublisher; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Service; @@ -44,7 +44,7 @@ public class TrustLevelManager { private final Map trustLevelPerDmiPlugin; private final InventoryPersistence inventoryPersistence; - private final AvcEventPublisher avcEventPublisher; + private final CmAvcEventPublisher cmAvcEventPublisher; private static final String AVC_CHANGED_ATTRIBUTE_NAME = "trustLevel"; private static final String AVC_NO_OLD_VALUE = null; @@ -65,7 +65,7 @@ public class TrustLevelManager { } trustLevelPerCmHandle.put(cmHandleId, initialTrustLevel); if (TrustLevel.NONE.equals(initialTrustLevel)) { - avcEventPublisher.publishAvcEvent(cmHandleId, + cmAvcEventPublisher.publishAvcEvent(cmHandleId, AVC_CHANGED_ATTRIBUTE_NAME, AVC_NO_OLD_VALUE, initialTrustLevel.name()); @@ -126,7 +126,7 @@ public class TrustLevelManager { } else { log.info("The trust level for Cm Handle: {} is now: {} ", notificationCandidateCmHandleId, newEffectiveTrustLevel); - avcEventPublisher.publishAvcEvent(notificationCandidateCmHandleId, + cmAvcEventPublisher.publishAvcEvent(notificationCandidateCmHandleId, AVC_CHANGED_ATTRIBUTE_NAME, oldEffectiveTrustLevel.name(), newEffectiveTrustLevel.name()); diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/utils/TopicValidator.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/utils/TopicValidator.java deleted file mode 100644 index f9fed8d437..0000000000 --- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/utils/TopicValidator.java +++ /dev/null @@ -1,47 +0,0 @@ -/* - * ============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.utils; - -import java.util.regex.Pattern; -import lombok.AccessLevel; -import lombok.NoArgsConstructor; -import org.onap.cps.ncmp.exceptions.InvalidTopicException; - -@NoArgsConstructor(access = AccessLevel.PRIVATE) -public class TopicValidator { - - private static final Pattern TOPIC_NAME_PATTERN = Pattern.compile("^[a-zA-Z0-9]([._-](?![._-])|" - + "[a-zA-Z0-9]){0,120}[a-zA-Z0-9]$"); - - /** - * Validate kafka topic name pattern. - * - * @param topicName name of the topic to be validated - * - * @throws InvalidTopicException if the topic is not valid - */ - public static void validateTopicName(final String topicName) { - if (!TOPIC_NAME_PATTERN.matcher(topicName).matches()) { - throw new InvalidTopicException("Topic name " + topicName + " is invalid", "invalid topic"); - } - } - -} diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/utils/events/CloudEventMapper.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/utils/events/CloudEventMapper.java new file mode 100644 index 0000000000..4462b169e2 --- /dev/null +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/utils/events/CloudEventMapper.java @@ -0,0 +1,61 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (c) 2023 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.utils.events; + +import com.fasterxml.jackson.databind.ObjectMapper; +import io.cloudevents.CloudEvent; +import io.cloudevents.core.CloudEventUtils; +import io.cloudevents.core.data.PojoCloudEventData; +import io.cloudevents.jackson.PojoCloudEventDataMapper; +import lombok.AccessLevel; +import lombok.NoArgsConstructor; +import lombok.extern.slf4j.Slf4j; + +@Slf4j +@NoArgsConstructor(access = AccessLevel.PRIVATE) +public class CloudEventMapper { + + private static final ObjectMapper objectMapper = new ObjectMapper(); + + /** + * Generic method to map cloud event data to target event class object. + * + * @param cloudEvent input cloud event + * @param targetEventClass target event class + * @param target event class type + * @return mapped target event + */ + public static T toTargetEvent(final CloudEvent cloudEvent, final Class targetEventClass) { + PojoCloudEventData mappedCloudEvent = null; + + try { + mappedCloudEvent = + CloudEventUtils.mapData(cloudEvent, PojoCloudEventDataMapper.from(objectMapper, targetEventClass)); + + } catch (final RuntimeException runtimeException) { + log.error("Unable to map cloud event to target event class type : {} with cause : {}", targetEventClass, + runtimeException.getMessage()); + } + + return mappedCloudEvent == null ? null : mappedCloudEvent.getValue(); + } + +} diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/utils/events/CmAvcEventPublisher.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/utils/events/CmAvcEventPublisher.java new file mode 100644 index 0000000000..2a9717cc1a --- /dev/null +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/utils/events/CmAvcEventPublisher.java @@ -0,0 +1,81 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2023-2024 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.utils.events; + +import io.cloudevents.CloudEvent; +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; +import lombok.RequiredArgsConstructor; +import org.onap.cps.events.EventsPublisher; +import org.onap.cps.ncmp.events.avc.ncmp_to_client.Avc; +import org.onap.cps.ncmp.events.avc.ncmp_to_client.AvcEvent; +import org.onap.cps.ncmp.events.avc.ncmp_to_client.Data; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Service; + +@Service +@RequiredArgsConstructor +public class CmAvcEventPublisher { + + private final EventsPublisher eventsPublisher; + + @Value("${app.ncmp.avc.cm-events-topic}") + private String avcTopic; + + /** + * Publish attribute value change event. + * + * @param eventKey id of the cmHandle being registered + */ + public void publishAvcEvent(final String eventKey, final String attributeName, + final String oldAttributeValue, final String newAttributeValue) { + final AvcEvent avcEvent = buildAvcEvent(attributeName, oldAttributeValue, newAttributeValue); + + final Map extensions = createAvcEventExtensions(eventKey); + final CloudEvent avcCloudEvent = + NcmpEvent.builder().type(AvcEvent.class.getTypeName()) + .data(avcEvent).extensions(extensions).build().asCloudEvent(); + + eventsPublisher.publishCloudEvent(avcTopic, eventKey, avcCloudEvent); + } + + private AvcEvent buildAvcEvent(final String attributeName, + final String oldAttributeValue, + final String newAttributeValue) { + final Avc avc = new Avc(); + avc.setAttributeName(attributeName); + avc.setOldAttributeValue(oldAttributeValue); + avc.setNewAttributeValue(newAttributeValue); + + final Data payload = new Data(); + payload.setAttributeValueChange(Collections.singletonList(avc)); + final AvcEvent avcEvent = new AvcEvent(); + avcEvent.setData(payload); + return avcEvent; + } + + private Map createAvcEventExtensions(final String eventKey) { + final Map extensions = new HashMap<>(); + extensions.put("correlationid", eventKey); + return extensions; + } +} diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/utils/events/NcmpEvent.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/utils/events/NcmpEvent.java new file mode 100644 index 0000000000..67128bd159 --- /dev/null +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/utils/events/NcmpEvent.java @@ -0,0 +1,66 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2023-2024 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.utils.events; + +import io.cloudevents.CloudEvent; +import io.cloudevents.core.builder.CloudEventBuilder; +import java.net.URI; +import java.util.Map; +import java.util.UUID; +import lombok.Builder; +import org.apache.commons.lang3.StringUtils; +import org.onap.cps.ncmp.api.impl.utils.EventDateTimeFormatter; +import org.onap.cps.ncmp.api.impl.utils.context.CpsApplicationContext; +import org.onap.cps.utils.JsonObjectMapper; + +@Builder +public class NcmpEvent { + + private Object data; + private Map extensions; + private String type; + @Builder.Default + private static final String CLOUD_EVENT_SPEC_VERSION_V1 = "1.0.0"; + @Builder.Default + private static final String CLOUD_EVENT_SOURCE = "NCMP"; + + /** + * Creates ncmp cloud event with provided attributes. + * + * @return Cloud Event + */ + public CloudEvent asCloudEvent() { + final JsonObjectMapper jsonObjectMapper = CpsApplicationContext.getCpsBean(JsonObjectMapper.class); + final CloudEventBuilder cloudEventBuilder = CloudEventBuilder.v1() + .withId(UUID.randomUUID().toString()) + .withSource(URI.create(CLOUD_EVENT_SOURCE)) + .withType(type) + .withDataSchema(URI.create("urn:cps:" + type + ":" + CLOUD_EVENT_SPEC_VERSION_V1)) + .withTime(EventDateTimeFormatter.toIsoOffsetDateTime( + EventDateTimeFormatter.getCurrentIsoFormattedDateTime())) + .withData(jsonObjectMapper.asJsonBytes(data)); + extensions.entrySet().stream() + .filter(extensionEntry -> StringUtils.isNotBlank(extensionEntry.getValue())) + .forEach(extensionEntry -> + cloudEventBuilder.withExtension(extensionEntry.getKey(), extensionEntry.getValue())); + return cloudEventBuilder.build(); + } +} diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/utils/events/TopicValidator.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/utils/events/TopicValidator.java new file mode 100644 index 0000000000..a7ad86cda5 --- /dev/null +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/utils/events/TopicValidator.java @@ -0,0 +1,47 @@ +/* + * ============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.utils.events; + +import java.util.regex.Pattern; +import lombok.AccessLevel; +import lombok.NoArgsConstructor; +import org.onap.cps.ncmp.exceptions.InvalidTopicException; + +@NoArgsConstructor(access = AccessLevel.PRIVATE) +public class TopicValidator { + + private static final Pattern TOPIC_NAME_PATTERN = Pattern.compile("^[a-zA-Z0-9]([._-](?![._-])|" + + "[a-zA-Z0-9]){0,120}[a-zA-Z0-9]$"); + + /** + * Validate kafka topic name pattern. + * + * @param topicName name of the topic to be validated + * + * @throws InvalidTopicException if the topic is not valid + */ + public static void validateTopicName(final String topicName) { + if (!TOPIC_NAME_PATTERN.matcher(topicName).matches()) { + throw new InvalidTopicException("Topic name " + topicName + " is invalid", "invalid topic"); + } + } + +} -- cgit 1.2.3-korg