Condense Liquibase steps
[cps.git] / cps-ncmp-service / src / main / java / org / onap / cps / ncmp / init / InventoryModelLoader.java
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2023-2024 Nordix Foundation
4  *  ================================================================================
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  *  SPDX-License-Identifier: Apache-2.0
18  *  ============LICENSE_END=========================================================
19  */
20
21 package org.onap.cps.ncmp.init;
22
23 import static org.onap.cps.ncmp.api.impl.ncmppersistence.NcmpPersistence.NCMP_DATASPACE_NAME;
24 import static org.onap.cps.ncmp.api.impl.ncmppersistence.NcmpPersistence.NCMP_DMI_REGISTRY_ANCHOR;
25 import static org.onap.cps.ncmp.api.impl.ncmppersistence.NcmpPersistence.NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME;
26
27 import lombok.extern.slf4j.Slf4j;
28 import org.onap.cps.api.CpsAnchorService;
29 import org.onap.cps.api.CpsDataService;
30 import org.onap.cps.api.CpsDataspaceService;
31 import org.onap.cps.api.CpsModuleService;
32 import org.springframework.stereotype.Service;
33
34 @Slf4j
35 @Service
36 public class InventoryModelLoader extends AbstractModelLoader {
37     private static final String NEW_MODEL_FILE_NAME = "dmi-registry@2024-02-23.yang";
38     private static final String NEW_SCHEMA_SET_NAME = "dmi-registry-2024-02-23";
39     private static final String REGISTRY_DATANODE_NAME = "dmi-registry";
40
41     public InventoryModelLoader(final CpsDataspaceService cpsDataspaceService,
42                                 final CpsModuleService cpsModuleService,
43                                 final CpsAnchorService cpsAnchorService,
44                                 final CpsDataService cpsDataService) {
45         super(cpsDataspaceService, cpsModuleService, cpsAnchorService, cpsDataService);
46     }
47
48     @Override
49     public void onboardOrUpgradeModel() {
50         updateInventoryModel();
51         log.info("Inventory Model updated successfully");
52     }
53
54     private void updateInventoryModel() {
55         createDataspace(NCMP_DATASPACE_NAME);
56         createDataspace(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME);
57         createSchemaSet(NCMP_DATASPACE_NAME, NEW_SCHEMA_SET_NAME, NEW_MODEL_FILE_NAME);
58         createAnchor(NCMP_DATASPACE_NAME, NEW_SCHEMA_SET_NAME, NCMP_DMI_REGISTRY_ANCHOR);
59         updateAnchorSchemaSet(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR, NEW_SCHEMA_SET_NAME);
60         createTopLevelDataNode(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR, REGISTRY_DATANODE_NAME);
61         deleteOldButNotThePreviousSchemaSets();
62     }
63
64     private void deleteOldButNotThePreviousSchemaSets() {
65         //No schema sets passed in yet, but wil be required for future updates
66         deleteUnusedSchemaSets(NCMP_DATASPACE_NAME);
67         deleteUnusedSchemaSets(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME);
68     }
69
70 }