diff options
author | rajesh.kumar <rk00747546@techmahindra.com> | 2024-09-30 18:11:47 +0530 |
---|---|---|
committer | rajesh.kumar <rk00747546@techmahindra.com> | 2024-12-20 13:47:14 +0530 |
commit | 67973be1deea7de52b750e9bdd6dc53da265da65 (patch) | |
tree | 1d599a01b718eca3a1f706f4bb7eec260e6cec5b /cps-service/src/main/java/org | |
parent | 42dfa67015d7478eca07eb5778ec55c2c24c19a5 (diff) |
Add schema to persist notification subscription information
Add required schema to persist notification subscription information. It should contain
- Schema yang file
- New Dataspace, Anchors or any other database entity
- Refactore duplicate code in NCMP
Issue-ID:CPS-2427
Change-Id: I56c34400dc73c71b936a51260efd300924ababdc
Signed-off-by: rajesh.kumar <rk00747546@techmahindra.com>
Diffstat (limited to 'cps-service/src/main/java/org')
4 files changed, 303 insertions, 0 deletions
diff --git a/cps-service/src/main/java/org/onap/cps/api/exceptions/ModelOnboardingException.java b/cps-service/src/main/java/org/onap/cps/api/exceptions/ModelOnboardingException.java new file mode 100644 index 0000000000..d4455ebc20 --- /dev/null +++ b/cps-service/src/main/java/org/onap/cps/api/exceptions/ModelOnboardingException.java @@ -0,0 +1,28 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2024 TechMahindra Ltd. + * ================================================================================ + * 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.api.exceptions; + +public class ModelOnboardingException extends CpsException { + + public ModelOnboardingException(final String message, final String details) { + super(message, details); + } +} diff --git a/cps-service/src/main/java/org/onap/cps/init/AbstractModelLoader.java b/cps-service/src/main/java/org/onap/cps/init/AbstractModelLoader.java new file mode 100644 index 0000000000..e864633f25 --- /dev/null +++ b/cps-service/src/main/java/org/onap/cps/init/AbstractModelLoader.java @@ -0,0 +1,183 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2023-2024 Nordix Foundation + * Modifications Copyright (C) 2024 TechMahindra Ltd. + * ================================================================================ + * 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.init; + +import com.fasterxml.jackson.databind.ObjectMapper; +import java.io.InputStream; +import java.nio.charset.StandardCharsets; +import java.time.OffsetDateTime; +import java.util.HashMap; +import java.util.Map; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.onap.cps.api.CpsAnchorService; +import org.onap.cps.api.CpsDataService; +import org.onap.cps.api.CpsDataspaceService; +import org.onap.cps.api.CpsModuleService; +import org.onap.cps.api.exceptions.AlreadyDefinedException; +import org.onap.cps.api.exceptions.ModelOnboardingException; +import org.onap.cps.api.parameters.CascadeDeleteAllowed; +import org.onap.cps.utils.JsonObjectMapper; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.context.event.ApplicationStartedEvent; + +@Slf4j +@RequiredArgsConstructor +public abstract class AbstractModelLoader implements ModelLoader { + + protected final CpsDataspaceService cpsDataspaceService; + private final CpsModuleService cpsModuleService; + private final CpsAnchorService cpsAnchorService; + protected final CpsDataService cpsDataService; + + private final JsonObjectMapper jsonObjectMapper = new JsonObjectMapper(new ObjectMapper()); + + private static final int EXIT_CODE_ON_ERROR = 1; + + @Override + public void onApplicationEvent(final ApplicationStartedEvent applicationStartedEvent) { + try { + onboardOrUpgradeModel(); + } catch (final Exception modelOnboardUpException) { + log.error("Exiting application due to failure in onboarding model: {} ", + modelOnboardUpException.getMessage()); + SpringApplication.exit(applicationStartedEvent.getApplicationContext(), () -> EXIT_CODE_ON_ERROR); + } + } + + /** + * Create initial schema set. + * @param dataspaceName dataspace name + * @param schemaSetName schemaset name + * @param resourceNames resource names + */ + public void createSchemaSet(final String dataspaceName, final String schemaSetName, final String... resourceNames) { + try { + final Map<String, String> yangResourcesContentByResourceName = mapYangResourcesToContent(resourceNames); + cpsModuleService.createSchemaSet(dataspaceName, schemaSetName, yangResourcesContentByResourceName); + } catch (final AlreadyDefinedException alreadyDefinedException) { + log.warn("Creating new schema set failed as schema set already exists"); + } catch (final Exception exception) { + log.error("Creating schema set failed: {} ", exception.getMessage()); + throw new ModelOnboardingException("Creating schema set failed", exception.getMessage()); + } + } + + /** + * Create initial dataspace. + * @param dataspaceName dataspace name + */ + public void createDataspace(final String dataspaceName) { + try { + cpsDataspaceService.createDataspace(dataspaceName); + } catch (final AlreadyDefinedException alreadyDefinedException) { + log.debug("Dataspace already exists"); + } catch (final Exception exception) { + log.error("Creating dataspace failed: {} ", exception.getMessage()); + throw new ModelOnboardingException("Creating dataspace failed", exception.getMessage()); + } + } + + /** + * Create initial anchor. + * @param dataspaceName dataspace name + * @param schemaSetName schemaset name + * @param anchorName anchor name + */ + public void createAnchor(final String dataspaceName, final String schemaSetName, final String anchorName) { + try { + cpsAnchorService.createAnchor(dataspaceName, schemaSetName, anchorName); + } catch (final AlreadyDefinedException alreadyDefinedException) { + log.warn("Creating new anchor failed as anchor already exists"); + } catch (final Exception exception) { + log.error("Creating anchor failed: {} ", exception.getMessage()); + throw new ModelOnboardingException("Creating anchor failed", exception.getMessage()); + } + } + + /** + * Create initial top level data node. + * @param dataspaceName dataspace name + * @param anchorName anchor name + * @param dataNodeName data node name + */ + public void createTopLevelDataNode(final String dataspaceName, final String anchorName, final String dataNodeName) { + final String nodeData = jsonObjectMapper.asJsonString(Map.of(dataNodeName, Map.of())); + try { + cpsDataService.saveData(dataspaceName, anchorName, nodeData, OffsetDateTime.now()); + } catch (final AlreadyDefinedException exception) { + log.warn("Creating new data node '{}' failed as data node already exists", dataNodeName); + } catch (final Exception exception) { + log.error("Creating data node failed: {}", exception.getMessage()); + throw new ModelOnboardingException("Creating data node failed", exception.getMessage()); + } + } + + /** + * Delete unused schema set. + * @param dataspaceName dataspace name + * @param schemaSetNames schema set names + */ + public void deleteUnusedSchemaSets(final String dataspaceName, final String... schemaSetNames) { + for (final String schemaSetName : schemaSetNames) { + try { + cpsModuleService.deleteSchemaSet( + dataspaceName, schemaSetName, CascadeDeleteAllowed.CASCADE_DELETE_PROHIBITED); + } catch (final Exception exception) { + log.warn("Deleting schema set failed: {} ", exception.getMessage()); + } + } + } + + /** + * Update anchor schema set. + * @param dataspaceName dataspace name + * @param anchorName anchor name + * @param schemaSetName schemaset name + */ + public void updateAnchorSchemaSet(final String dataspaceName, final String anchorName, final String schemaSetName) { + try { + cpsAnchorService.updateAnchorSchemaSet(dataspaceName, anchorName, schemaSetName); + } catch (final Exception exception) { + log.error("Updating schema set failed: {}", exception.getMessage()); + throw new ModelOnboardingException("Updating schema set failed", exception.getMessage()); + } + } + + Map<String, String> mapYangResourcesToContent(final String... resourceNames) { + final Map<String, String> yangResourceContentByName = new HashMap<>(); + for (final String resourceName: resourceNames) { + yangResourceContentByName.put(resourceName, getFileContentAsString("models/" + resourceName)); + } + return yangResourceContentByName; + } + + private String getFileContentAsString(final String fileName) { + try (final InputStream inputStream = getClass().getClassLoader().getResourceAsStream(fileName)) { + return new String(inputStream.readAllBytes(), StandardCharsets.UTF_8); + } catch (final Exception exception) { + final String message = String.format("Onboarding failed as unable to read file: %s", fileName); + log.debug(message); + throw new ModelOnboardingException(message, exception.getMessage()); + } + } +} diff --git a/cps-service/src/main/java/org/onap/cps/init/CpsNotificationSubscriptionModelLoader.java b/cps-service/src/main/java/org/onap/cps/init/CpsNotificationSubscriptionModelLoader.java new file mode 100644 index 0000000000..0b7d1609ff --- /dev/null +++ b/cps-service/src/main/java/org/onap/cps/init/CpsNotificationSubscriptionModelLoader.java @@ -0,0 +1,60 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2024 TechMahindra Ltd. + * ================================================================================ + * 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.init; + +import lombok.extern.slf4j.Slf4j; +import org.onap.cps.api.CpsAnchorService; +import org.onap.cps.api.CpsDataService; +import org.onap.cps.api.CpsDataspaceService; +import org.onap.cps.api.CpsModuleService; +import org.springframework.stereotype.Service; + +@Slf4j +@Service +public class CpsNotificationSubscriptionModelLoader extends AbstractModelLoader { + + private static final String MODEL_FILENAME = "cps-notification-subscriptions@2024-07-03.yang"; + private static final String SCHEMASET_NAME = "cps-notification-subscriptions"; + private static final String ANCHOR_NAME = "cps-notification-subscriptions"; + private static final String CPS_DATASPACE_NAME = "CPS-Admin"; + private static final String REGISTRY_DATANODE_NAME = "dataspaces"; + + public CpsNotificationSubscriptionModelLoader(final CpsDataspaceService cpsDataspaceService, + final CpsModuleService cpsModuleService, + final CpsAnchorService cpsAnchorService, + final CpsDataService cpsDataService) { + super(cpsDataspaceService, cpsModuleService, cpsAnchorService, cpsDataService); + } + + @Override + public void onboardOrUpgradeModel() { + onboardSubscriptionModels(); + log.info("Subscription models onboarded successfully"); + } + + private void onboardSubscriptionModels() { + createDataspace(CPS_DATASPACE_NAME); + createSchemaSet(CPS_DATASPACE_NAME, SCHEMASET_NAME, MODEL_FILENAME); + createAnchor(CPS_DATASPACE_NAME, SCHEMASET_NAME, ANCHOR_NAME); + createTopLevelDataNode(CPS_DATASPACE_NAME, ANCHOR_NAME, REGISTRY_DATANODE_NAME); + } + +} diff --git a/cps-service/src/main/java/org/onap/cps/init/ModelLoader.java b/cps-service/src/main/java/org/onap/cps/init/ModelLoader.java new file mode 100644 index 0000000000..89c18b3159 --- /dev/null +++ b/cps-service/src/main/java/org/onap/cps/init/ModelLoader.java @@ -0,0 +1,32 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2024 TechMahindra Ltd. + * ================================================================================ + * 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.init; + +import org.springframework.boot.context.event.ApplicationStartedEvent; +import org.springframework.context.ApplicationListener; + +public interface ModelLoader extends ApplicationListener<ApplicationStartedEvent> { + + @Override + void onApplicationEvent(ApplicationStartedEvent applicationStartedEvent); + + void onboardOrUpgradeModel(); +} |