summaryrefslogtreecommitdiffstats
path: root/cps-ncmp-service/src/main/java
diff options
context:
space:
mode:
authormpriyank <priyank.maheshwari@est.tech>2023-08-10 14:27:51 +0100
committermpriyank <priyank.maheshwari@est.tech>2023-08-11 12:53:49 +0100
commit2d73a48da8ee7a9e78854cb08ef7ffb72933d613 (patch)
tree288c353e3a157542b02ab460a7ac03e5a80207b4 /cps-ncmp-service/src/main/java
parent2fe802a8bdda1e628c4558f9079439893e7bd52e (diff)
Generic Cloud Event Mapper
- Introduced a generic event cloud mapper to be used all over the codebase - Added as a utility so we dont have to inject it as spring bean - Refactored code and added tests - Incorporated review comments Issue-ID: CPS-1827 Change-Id: I8dab914791e01bbf6492ce96e7ba6eaa8f75a6cb Signed-off-by: mpriyank <priyank.maheshwari@est.tech>
Diffstat (limited to 'cps-ncmp-service/src/main/java')
-rw-r--r--cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/events/cmsubscription/CmSubscriptionDmiOutEventConsumer.java6
-rw-r--r--cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/events/cmsubscription/CmSubscriptionNcmpInEventConsumer.java5
-rw-r--r--cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/events/mapper/CloudEventMapper.java62
-rw-r--r--cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/utils/CmSubscriptionEventCloudMapper.java25
-rw-r--r--cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/utils/SubscriptionEventResponseCloudMapper.java58
5 files changed, 68 insertions, 88 deletions
diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/events/cmsubscription/CmSubscriptionDmiOutEventConsumer.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/events/cmsubscription/CmSubscriptionDmiOutEventConsumer.java
index 3a7e0c6cf5..1ac4044114 100644
--- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/events/cmsubscription/CmSubscriptionDmiOutEventConsumer.java
+++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/events/cmsubscription/CmSubscriptionDmiOutEventConsumer.java
@@ -20,6 +20,8 @@
package org.onap.cps.ncmp.api.impl.events.cmsubscription;
+import static org.onap.cps.ncmp.api.impl.events.mapper.CloudEventMapper.toTargetEvent;
+
import com.hazelcast.map.IMap;
import io.cloudevents.CloudEvent;
import java.util.Collection;
@@ -33,7 +35,6 @@ import org.onap.cps.ncmp.api.impl.config.embeddedcache.ForwardedSubscriptionEven
import org.onap.cps.ncmp.api.impl.subscriptions.SubscriptionPersistence;
import org.onap.cps.ncmp.api.impl.subscriptions.SubscriptionStatus;
import org.onap.cps.ncmp.api.impl.utils.DataNodeHelper;
-import org.onap.cps.ncmp.api.impl.utils.SubscriptionEventResponseCloudMapper;
import org.onap.cps.ncmp.api.impl.yangmodels.YangModelSubscriptionEvent;
import org.onap.cps.ncmp.events.cmsubscription1_0_0.dmi_to_ncmp.CmSubscriptionDmiOutEvent;
import org.onap.cps.spi.model.DataNode;
@@ -51,7 +52,6 @@ public class CmSubscriptionDmiOutEventConsumer {
private final CmSubscriptionDmiOutEventToYangModelSubscriptionEventMapper
cmSubscriptionDmiOutEventToYangModelSubscriptionEventMapper;
private final CmSubscriptionNcmpOutEventPublisher cmSubscriptionNcmpOutEventPublisher;
- private final SubscriptionEventResponseCloudMapper subscriptionEventResponseCloudMapper;
@Value("${notification.enabled:true}")
private boolean notificationFeatureEnabled;
@@ -71,7 +71,7 @@ public class CmSubscriptionDmiOutEventConsumer {
final CloudEvent cloudEvent = cmSubscriptionDmiOutConsumerRecord.value();
final String eventType = cmSubscriptionDmiOutConsumerRecord.value().getType();
final CmSubscriptionDmiOutEvent cmSubscriptionDmiOutEvent =
- subscriptionEventResponseCloudMapper.toCmSubscriptionDmiOutEvent(cloudEvent);
+ toTargetEvent(cloudEvent, CmSubscriptionDmiOutEvent.class);
final String clientId = cmSubscriptionDmiOutEvent.getData().getClientId();
log.info("subscription event response of clientId: {} is received.", clientId);
final String subscriptionName = cmSubscriptionDmiOutEvent.getData().getSubscriptionName();
diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/events/cmsubscription/CmSubscriptionNcmpInEventConsumer.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/events/cmsubscription/CmSubscriptionNcmpInEventConsumer.java
index c64ebacb01..f1c64c788a 100644
--- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/events/cmsubscription/CmSubscriptionNcmpInEventConsumer.java
+++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/events/cmsubscription/CmSubscriptionNcmpInEventConsumer.java
@@ -20,6 +20,7 @@
package org.onap.cps.ncmp.api.impl.events.cmsubscription;
+import static org.onap.cps.ncmp.api.impl.events.mapper.CloudEventMapper.toTargetEvent;
import static org.onap.cps.ncmp.api.impl.operations.DatastoreType.PASSTHROUGH_OPERATIONAL;
import static org.onap.cps.ncmp.api.impl.operations.DatastoreType.PASSTHROUGH_RUNNING;
@@ -28,7 +29,6 @@ import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.onap.cps.ncmp.api.impl.subscriptions.SubscriptionPersistence;
-import org.onap.cps.ncmp.api.impl.utils.CmSubscriptionEventCloudMapper;
import org.onap.cps.ncmp.api.impl.yangmodels.YangModelSubscriptionEvent;
import org.onap.cps.ncmp.events.cmsubscription1_0_0.client_to_ncmp.CmSubscriptionNcmpInEvent;
import org.springframework.beans.factory.annotation.Value;
@@ -44,7 +44,6 @@ public class CmSubscriptionNcmpInEventConsumer {
private final CmSubscriptionNcmpInEventForwarder cmSubscriptionNcmpInEventForwarder;
private final CmSubscriptionNcmpInEventMapper cmSubscriptionNcmpInEventMapper;
private final SubscriptionPersistence subscriptionPersistence;
- private final CmSubscriptionEventCloudMapper cmSubscriptionEventCloudMapper;
@Value("${notification.enabled:true}")
private boolean notificationFeatureEnabled;
@@ -63,7 +62,7 @@ public class CmSubscriptionNcmpInEventConsumer {
final CloudEvent cloudEvent = subscriptionEventConsumerRecord.value();
final String eventType = subscriptionEventConsumerRecord.value().getType();
final CmSubscriptionNcmpInEvent cmSubscriptionNcmpInEvent =
- cmSubscriptionEventCloudMapper.toCmSubscriptionNcmpInEvent(cloudEvent);
+ toTargetEvent(cloudEvent, CmSubscriptionNcmpInEvent.class);
final String eventDatastore = cmSubscriptionNcmpInEvent.getData().getPredicates().getDatastore();
if (!eventDatastore.equals(PASSTHROUGH_RUNNING.getDatastoreName()) || eventDatastore.equals(
PASSTHROUGH_OPERATIONAL.getDatastoreName())) {
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
new file mode 100644
index 0000000000..98ba953864
--- /dev/null
+++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/events/mapper/CloudEventMapper.java
@@ -0,0 +1,62 @@
+/*
+ * ============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 io.cloudevents.rw.CloudEventRWException;
+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 <T> target event class type
+ * @return mapped target event
+ */
+ public static <T> T toTargetEvent(final CloudEvent cloudEvent, final Class<T> targetEventClass) {
+ PojoCloudEventData<T> mappedCloudEvent = null;
+
+ try {
+ mappedCloudEvent =
+ CloudEventUtils.mapData(cloudEvent, PojoCloudEventDataMapper.from(objectMapper, targetEventClass));
+
+ } catch (final CloudEventRWException cloudEventRwException) {
+ log.error("Unable to map cloud event to target event class type : {} with cause : {}", targetEventClass,
+ cloudEventRwException.getMessage());
+ }
+
+ return mappedCloudEvent == null ? null : mappedCloudEvent.getValue();
+ }
+
+}
diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/utils/CmSubscriptionEventCloudMapper.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/utils/CmSubscriptionEventCloudMapper.java
index 5bc38e1dcd..8e565d2200 100644
--- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/utils/CmSubscriptionEventCloudMapper.java
+++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/utils/CmSubscriptionEventCloudMapper.java
@@ -23,15 +23,11 @@ package org.onap.cps.ncmp.api.impl.utils;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.cloudevents.CloudEvent;
-import io.cloudevents.core.CloudEventUtils;
import io.cloudevents.core.builder.CloudEventBuilder;
-import io.cloudevents.core.data.PojoCloudEventData;
-import io.cloudevents.jackson.PojoCloudEventDataMapper;
import java.net.URI;
import java.util.UUID;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
-import org.onap.cps.ncmp.events.cmsubscription1_0_0.client_to_ncmp.CmSubscriptionNcmpInEvent;
import org.onap.cps.ncmp.events.cmsubscription1_0_0.ncmp_to_dmi.CmSubscriptionDmiInEvent;
import org.springframework.stereotype.Component;
@@ -45,29 +41,10 @@ public class CmSubscriptionEventCloudMapper {
private static String randomId = UUID.randomUUID().toString();
/**
- * Maps CloudEvent object to CmSubscriptionNcmpInEvent.
- *
- * @param cloudEvent object.
- * @return CmSubscriptionNcmpInEvent deserialized.
- */
- public CmSubscriptionNcmpInEvent toCmSubscriptionNcmpInEvent(final CloudEvent cloudEvent) {
- final PojoCloudEventData<CmSubscriptionNcmpInEvent> deserializedCloudEvent = CloudEventUtils
- .mapData(cloudEvent, PojoCloudEventDataMapper.from(objectMapper, CmSubscriptionNcmpInEvent.class));
- if (deserializedCloudEvent == null) {
- log.debug("No data found in the consumed event");
- return null;
- } else {
- final CmSubscriptionNcmpInEvent cmSubscriptionNcmpInEvent = deserializedCloudEvent.getValue();
- log.debug("Consuming event {}", cmSubscriptionNcmpInEvent);
- return cmSubscriptionNcmpInEvent;
- }
- }
-
- /**
* Maps CmSubscriptionDmiInEvent to a CloudEvent.
*
* @param cmSubscriptionDmiInEvent object.
- * @param eventKey as String.
+ * @param eventKey as String.
* @return CloudEvent built.
*/
public CloudEvent toCloudEvent(final CmSubscriptionDmiInEvent cmSubscriptionDmiInEvent, final String eventKey,
diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/utils/SubscriptionEventResponseCloudMapper.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/utils/SubscriptionEventResponseCloudMapper.java
deleted file mode 100644
index 0721d1d569..0000000000
--- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/utils/SubscriptionEventResponseCloudMapper.java
+++ /dev/null
@@ -1,58 +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.utils;
-
-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.RequiredArgsConstructor;
-import lombok.extern.slf4j.Slf4j;
-import org.onap.cps.ncmp.events.cmsubscription1_0_0.dmi_to_ncmp.CmSubscriptionDmiOutEvent;
-import org.springframework.stereotype.Component;
-
-@Slf4j
-@Component
-@RequiredArgsConstructor
-public class SubscriptionEventResponseCloudMapper {
-
- private final ObjectMapper objectMapper;
-
- /**
- * Maps CloudEvent object to CmSubscriptionDmiOutEvent.
- *
- * @param cloudEvent object
- * @return CmSubscriptionDmiOutEvent deserialized
- */
- public CmSubscriptionDmiOutEvent toCmSubscriptionDmiOutEvent(final CloudEvent cloudEvent) {
- final PojoCloudEventData<CmSubscriptionDmiOutEvent> deserializedCloudEvent = CloudEventUtils
- .mapData(cloudEvent, PojoCloudEventDataMapper.from(objectMapper, CmSubscriptionDmiOutEvent.class));
- if (deserializedCloudEvent == null) {
- log.debug("No data found in the consumed subscription response event");
- return null;
- } else {
- final CmSubscriptionDmiOutEvent cmSubscriptionDmiOutEvent = deserializedCloudEvent.getValue();
- log.debug("Consuming subscription response event {}", cmSubscriptionDmiOutEvent);
- return cmSubscriptionDmiOutEvent;
- }
- }
-}