From 5201f5172ae24eee7d1b743efa18bf56e5319f71 Mon Sep 17 00:00:00 2001 From: lukegleeson Date: Thu, 23 Mar 2023 17:36:15 +0000 Subject: Persist SubscriptionEvent - Updated subscription.yang to subscription name instead of client name - Implemented Mapper for SubscriptionEvent to yang model version - Implemented Subscription Persistence for storing subscriptions - Minor changes to existing variable names Issue-ID: CPS-1394 Signed-off-by: lukegleeson Change-Id: Ibe066006a913cb7f6e52b6fa8a851e976a338ac3 --- .../impl/event/avc/SubscriptionEventConsumer.java | 12 +++ .../impl/event/avc/SubscriptionEventMapper.java | 52 +++++++++++++ .../api/impl/notifications/avc/AvcEventMapper.java | 7 -- .../subscriptions/SubscriptionPersistence.java | 34 ++++++++ .../subscriptions/SubscriptionPersistenceImpl.java | 55 +++++++++++++ .../api/impl/subscriptions/SubscriptionStatus.java | 27 +++++++ .../yangmodels/YangModelSubscriptionEvent.java | 91 ++++++++++++++++++++++ .../api/inventory/InventoryPersistenceImpl.java | 4 +- .../src/main/resources/model/subscription.yang | 4 +- 9 files changed, 275 insertions(+), 11 deletions(-) create mode 100644 cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/event/avc/SubscriptionEventMapper.java create mode 100644 cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/subscriptions/SubscriptionPersistence.java create mode 100644 cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/subscriptions/SubscriptionPersistenceImpl.java create mode 100644 cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/subscriptions/SubscriptionStatus.java create mode 100644 cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/yangmodels/YangModelSubscriptionEvent.java (limited to 'cps-ncmp-service/src/main') diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/event/avc/SubscriptionEventConsumer.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/event/avc/SubscriptionEventConsumer.java index d08baac5d..1361d98ff 100644 --- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/event/avc/SubscriptionEventConsumer.java +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/event/avc/SubscriptionEventConsumer.java @@ -22,6 +22,8 @@ package org.onap.cps.ncmp.api.impl.event.avc; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; +import org.onap.cps.ncmp.api.impl.subscriptions.SubscriptionPersistence; +import org.onap.cps.ncmp.api.impl.yangmodels.YangModelSubscriptionEvent; import org.onap.cps.ncmp.event.model.InnerSubscriptionEvent; import org.onap.cps.ncmp.event.model.SubscriptionEvent; import org.onap.cps.spi.exceptions.OperationNotYetSupportedException; @@ -36,6 +38,8 @@ import org.springframework.stereotype.Component; public class SubscriptionEventConsumer { private final SubscriptionEventForwarder subscriptionEventForwarder; + private final SubscriptionEventMapper subscriptionEventMapper; + private final SubscriptionPersistence subscriptionPersistence; @Value("${notification.enabled:true}") private boolean notificationFeatureEnabled; @@ -56,6 +60,7 @@ public class SubscriptionEventConsumer { } if ("CM".equals(event.getDataType().getDataCategory())) { log.debug("Consuming event {} ...", subscriptionEvent); + persistSubscriptionEvent(subscriptionEvent); if ("CREATE".equals(subscriptionEvent.getEventType().value())) { log.info("Subscription for ClientID {} with name {} ...", event.getSubscription().getClientID(), @@ -68,4 +73,11 @@ public class SubscriptionEventConsumer { log.trace("Non-CM subscription event ignored"); } } + + private void persistSubscriptionEvent(final SubscriptionEvent subscriptionEvent) { + final YangModelSubscriptionEvent yangModelSubscriptionEvent = + subscriptionEventMapper.toYangModelSubscriptionEvent(subscriptionEvent); + subscriptionPersistence.saveSubscriptionEvent(yangModelSubscriptionEvent); + } + } diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/event/avc/SubscriptionEventMapper.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/event/avc/SubscriptionEventMapper.java new file mode 100644 index 000000000..44f9abb08 --- /dev/null +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/event/avc/SubscriptionEventMapper.java @@ -0,0 +1,52 @@ +/* + * ============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.event.avc; + +import java.util.List; +import java.util.stream.Collectors; +import org.mapstruct.Mapper; +import org.mapstruct.Mapping; +import org.mapstruct.Named; +import org.onap.cps.ncmp.api.impl.yangmodels.YangModelSubscriptionEvent; +import org.onap.cps.ncmp.event.model.SubscriptionEvent; + +@Mapper(componentModel = "spring") +public interface SubscriptionEventMapper { + + @Mapping(source = "event.subscription.clientID", target = "clientId") + @Mapping(source = "event.subscription.name", target = "subscriptionName") + @Mapping(source = "event.subscription.isTagged", target = "tagged", qualifiedByName = "mapIsTagged") + @Mapping(source = "event.predicates.targets", + target = "predicates.targetCmHandles", qualifiedByName = "mapTargetsToCmHandleTargets") + @Mapping(source = "event.predicates.datastore", target = "predicates.datastore") + YangModelSubscriptionEvent toYangModelSubscriptionEvent(SubscriptionEvent subscriptionEvent); + + @Named("mapTargetsToCmHandleTargets") + default List mapTargetsToCmHandleTargets(List targets) { + return targets.stream().map( + target -> new YangModelSubscriptionEvent.TargetCmHandle(target.toString())).collect(Collectors.toList()); + } + + @Named("mapIsTagged") + default boolean mapIsTagged(Boolean isTagged) { + return (isTagged == null) ? false : isTagged; + } +} diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/notifications/avc/AvcEventMapper.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/notifications/avc/AvcEventMapper.java index aaa06c665..adbf08fa2 100644 --- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/notifications/avc/AvcEventMapper.java +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/notifications/avc/AvcEventMapper.java @@ -33,14 +33,7 @@ import org.onap.cps.ncmp.event.model.AvcEvent; @Mapper(componentModel = "spring") public interface AvcEventMapper { - @Mapping(source = "eventTime", target = "eventTime") @Mapping(source = "eventId", target = "eventId", qualifiedByName = "avcEventId") - @Mapping(source = "eventCorrelationId", target = "eventCorrelationId") - @Mapping(source = "eventSchema", target = "eventSchema") - @Mapping(source = "eventSchemaVersion", target = "eventSchemaVersion") - @Mapping(source = "eventSource", target = "eventSource") - @Mapping(source = "eventType", target = "eventType") - @Mapping(source = "event", target = "event") AvcEvent toOutgoingAvcEvent(AvcEvent incomingAvcEvent); @Named("avcEventId") diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/subscriptions/SubscriptionPersistence.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/subscriptions/SubscriptionPersistence.java new file mode 100644 index 000000000..16d9b80f8 --- /dev/null +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/subscriptions/SubscriptionPersistence.java @@ -0,0 +1,34 @@ +/* + * ============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.subscriptions; + +import org.onap.cps.ncmp.api.impl.yangmodels.YangModelSubscriptionEvent; + +public interface SubscriptionPersistence { + + /** + * Save subscription Event. + * + * @param yangModelSubscriptionEvent subscription Event as Yang Model. + */ + void saveSubscriptionEvent(YangModelSubscriptionEvent yangModelSubscriptionEvent); + +} diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/subscriptions/SubscriptionPersistenceImpl.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/subscriptions/SubscriptionPersistenceImpl.java new file mode 100644 index 000000000..4895735af --- /dev/null +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/subscriptions/SubscriptionPersistenceImpl.java @@ -0,0 +1,55 @@ +/* + * ============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.subscriptions; + +import static org.onap.cps.ncmp.api.impl.constants.DmiRegistryConstants.NO_TIMESTAMP; + +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.onap.cps.api.CpsDataService; +import org.onap.cps.ncmp.api.impl.yangmodels.YangModelSubscriptionEvent; +import org.onap.cps.utils.JsonObjectMapper; +import org.springframework.stereotype.Component; + +@Slf4j +@RequiredArgsConstructor +@Component +public class SubscriptionPersistenceImpl implements SubscriptionPersistence { + + private static final String SUBSCRIPTION_DATASPACE_NAME = "NCMP-Admin"; + private static final String SUBSCRIPTION_ANCHOR_NAME = "AVC-Subscriptions"; + private static final String SUBSCRIPTION_REGISTRY_PARENT = "/subscription-registry"; + + private final JsonObjectMapper jsonObjectMapper; + private final CpsDataService cpsDataService; + + @Override + public void saveSubscriptionEvent(final YangModelSubscriptionEvent yangModelSubscriptionEvent) { + final String subscriptionEventJsonData = + createSubscriptionEventJsonData(jsonObjectMapper.asJsonString(yangModelSubscriptionEvent)); + cpsDataService.saveListElements(SUBSCRIPTION_DATASPACE_NAME, SUBSCRIPTION_ANCHOR_NAME, + SUBSCRIPTION_REGISTRY_PARENT, subscriptionEventJsonData, NO_TIMESTAMP); + } + + private static String createSubscriptionEventJsonData(final String yangModelSubscriptionAsJson) { + return "{\"subscription\":[" + yangModelSubscriptionAsJson + "]}"; + } +} diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/subscriptions/SubscriptionStatus.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/subscriptions/SubscriptionStatus.java new file mode 100644 index 000000000..0b4f91fac --- /dev/null +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/subscriptions/SubscriptionStatus.java @@ -0,0 +1,27 @@ +/* + * ============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.subscriptions; + +public enum SubscriptionStatus { + ACCEPTED, + REJECTED, + PENDING +} diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/yangmodels/YangModelSubscriptionEvent.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/yangmodels/YangModelSubscriptionEvent.java new file mode 100644 index 000000000..4dcc5797c --- /dev/null +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/yangmodels/YangModelSubscriptionEvent.java @@ -0,0 +1,91 @@ +/* + * ============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.yangmodels; + +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonInclude.Include; +import com.fasterxml.jackson.annotation.JsonProperty; +import java.util.List; +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; +import org.onap.cps.ncmp.api.impl.subscriptions.SubscriptionStatus; + +/** + * Subscription event model to persist data into DB. + * Yang model subscription event + */ +@Getter +@Setter +@NoArgsConstructor +@JsonInclude(Include.NON_NULL) +@EqualsAndHashCode(onlyExplicitlyIncluded = true) +public class YangModelSubscriptionEvent { + + @EqualsAndHashCode.Include + @JsonProperty("clientID") + private String clientId; + + @EqualsAndHashCode.Include + @JsonProperty("subscriptionName") + private String subscriptionName; + + private String topic; + + @JsonProperty("isTagged") + private boolean isTagged; + + private Predicates predicates; + + + @Data + @JsonInclude(JsonInclude.Include.NON_NULL) + public static class Predicates { + + private String datastore; + + private List targetCmHandles; + + } + + @AllArgsConstructor + @Data + @JsonInclude(JsonInclude.Include.NON_NULL) + public static class TargetCmHandle { + + @JsonProperty() + private final String cmHandleId; + + @JsonProperty() + private final SubscriptionStatus status; + + public TargetCmHandle(final String cmHandleId) { + this.cmHandleId = cmHandleId; + this.status = SubscriptionStatus.PENDING; + } + } +} + + diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/inventory/InventoryPersistenceImpl.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/inventory/InventoryPersistenceImpl.java index c8d6c05f9..7908e629c 100644 --- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/inventory/InventoryPersistenceImpl.java +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/inventory/InventoryPersistenceImpl.java @@ -244,7 +244,7 @@ public class InventoryPersistenceImpl implements InventoryPersistence { return "{\"state\":" + state + "}"; } - private static String createCmHandleJsonData(final String cmHandleId) { - return "{\"cm-handles\":[" + cmHandleId + "]}"; + private static String createCmHandleJsonData(final String yangModelCmHandleAsJson) { + return "{\"cm-handles\":[" + yangModelCmHandleAsJson + "]}"; } } diff --git a/cps-ncmp-service/src/main/resources/model/subscription.yang b/cps-ncmp-service/src/main/resources/model/subscription.yang index 2d4b207e1..8ae1be664 100644 --- a/cps-ncmp-service/src/main/resources/model/subscription.yang +++ b/cps-ncmp-service/src/main/resources/model/subscription.yang @@ -11,13 +11,13 @@ module subscription { container subscription-registry { list subscription { - key "clientID clientName"; + key "clientID subscriptionName"; leaf clientID { type string; } - leaf clientName { + leaf subscriptionName { type string; } -- cgit 1.2.3-korg