Merge "CPS-508: Create anchor/schemaset from new modules and existing modules"
[cps.git] / cps-ncmp-service / src / main / java / org / onap / cps / ncmp / api / impl / NetworkCmProxyDataServiceImpl.java
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2021 highstreet technologies GmbH
4  *  Modifications Copyright (C) 2021 Nordix Foundation
5  *  Modifications Copyright (C) 2021 Pantheon.tech
6  *  ================================================================================
7  *  Licensed under the Apache License, Version 2.0 (the "License");
8  *  you may not use this file except in compliance with the License.
9  *  You may obtain a copy of the License at
10  *
11  *        http://www.apache.org/licenses/LICENSE-2.0
12  *
13  *  Unless required by applicable law or agreed to in writing, software
14  *  distributed under the License is distributed on an "AS IS" BASIS,
15  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  *  See the License for the specific language governing permissions and
17  *  limitations under the License.
18  *
19  *  SPDX-License-Identifier: Apache-2.0
20  *  ============LICENSE_END=========================================================
21  */
22
23 package org.onap.cps.ncmp.api.impl;
24
25 import com.fasterxml.jackson.core.JsonProcessingException;
26 import com.fasterxml.jackson.databind.ObjectMapper;
27 import java.util.ArrayList;
28 import java.util.Collection;
29 import java.util.List;
30 import lombok.extern.slf4j.Slf4j;
31 import org.onap.cps.api.CpsDataService;
32 import org.onap.cps.api.CpsQueryService;
33 import org.onap.cps.ncmp.api.NetworkCmProxyDataService;
34 import org.onap.cps.ncmp.api.models.CmHandle;
35 import org.onap.cps.ncmp.api.models.DmiPluginRegistration;
36 import org.onap.cps.ncmp.api.models.PersistenceCmHandle;
37 import org.onap.cps.ncmp.api.models.PersistenceCmHandlesList;
38 import org.onap.cps.spi.FetchDescendantsOption;
39 import org.onap.cps.spi.exceptions.DataValidationException;
40 import org.onap.cps.spi.model.DataNode;
41 import org.springframework.stereotype.Service;
42 import org.springframework.util.StringUtils;
43
44 @Slf4j
45 @Service
46 public class NetworkCmProxyDataServiceImpl implements NetworkCmProxyDataService {
47
48     private static final String NF_PROXY_DATASPACE_NAME = "NFP-Operational";
49
50     private static final String NCMP_DATASPACE_NAME = "NCMP-Admin";
51
52     private static final String NCMP_ANCHOR_NAME = "ncmp-dmi-registry";
53
54     private CpsDataService cpsDataService;
55
56     private ObjectMapper objectMapper;
57
58     private CpsQueryService cpsQueryService;
59
60     /**
61      * Constructor Injection for Dependencies.
62      * @param cpsDataService Data Service Interface
63      * @param cpsQueryService Query Service Interface
64      * @param objectMapper Object Mapper
65      */
66     public NetworkCmProxyDataServiceImpl(final CpsDataService cpsDataService,
67         final CpsQueryService cpsQueryService, final ObjectMapper objectMapper) {
68         this.cpsDataService = cpsDataService;
69         this.cpsQueryService = cpsQueryService;
70         this.objectMapper = objectMapper;
71     }
72
73     private String getDataspaceName() {
74         return NF_PROXY_DATASPACE_NAME;
75     }
76
77     @Override
78     public DataNode getDataNode(final String cmHandle, final String xpath,
79         final FetchDescendantsOption fetchDescendantsOption) {
80         return cpsDataService.getDataNode(getDataspaceName(), cmHandle, xpath, fetchDescendantsOption);
81     }
82
83     @Override
84     public Collection<DataNode> queryDataNodes(final String cmHandle, final String cpsPath,
85         final FetchDescendantsOption fetchDescendantsOption) {
86         return cpsQueryService.queryDataNodes(getDataspaceName(), cmHandle, cpsPath, fetchDescendantsOption);
87     }
88
89     @Override
90     public void createDataNode(final String cmHandle, final String parentNodeXpath, final String jsonData) {
91         if (!StringUtils.hasText(parentNodeXpath) || "/".equals(parentNodeXpath)) {
92             cpsDataService.saveData(getDataspaceName(), cmHandle, jsonData);
93         } else {
94             cpsDataService.saveData(getDataspaceName(), cmHandle, parentNodeXpath, jsonData);
95         }
96     }
97
98     @Override
99     public void addListNodeElements(final String cmHandle, final String parentNodeXpath, final String jsonData) {
100         cpsDataService.saveListNodeData(getDataspaceName(), cmHandle, parentNodeXpath, jsonData);
101     }
102
103     @Override
104     public void updateNodeLeaves(final String cmHandle, final String parentNodeXpath, final String jsonData) {
105         cpsDataService.updateNodeLeaves(getDataspaceName(), cmHandle, parentNodeXpath, jsonData);
106     }
107
108     @Override
109     public void replaceNodeTree(final String cmHandle, final String parentNodeXpath, final String jsonData) {
110         cpsDataService.replaceNodeTree(getDataspaceName(), cmHandle, parentNodeXpath, jsonData);
111     }
112
113     @Override
114     public void updateDmiPluginRegistration(final DmiPluginRegistration dmiPluginRegistration) {
115         try {
116             final List<PersistenceCmHandle> persistenceCmHandles =
117                 new ArrayList<>();
118             for (final CmHandle cmHandle: dmiPluginRegistration.getCreatedCmHandles()) {
119                 final var persistenceCmHandle = new PersistenceCmHandle();
120                 persistenceCmHandle.setDmiServiceName(dmiPluginRegistration.getDmiPlugin());
121                 persistenceCmHandle.setId(cmHandle.getCmHandleID());
122                 persistenceCmHandle.setAdditionalProperties(cmHandle.getCmHandleProperties());
123                 persistenceCmHandles.add(persistenceCmHandle);
124             }
125             final var persistenceCmHandlesList = new PersistenceCmHandlesList();
126             persistenceCmHandlesList.setCmHandles(persistenceCmHandles);
127             final var cmHandleJsonData = objectMapper.writeValueAsString(persistenceCmHandlesList);
128             cpsDataService.saveListNodeData(NCMP_DATASPACE_NAME, NCMP_ANCHOR_NAME, "/dmi-registry",
129                 cmHandleJsonData);
130         } catch (final JsonProcessingException e) {
131             throw new DataValidationException(
132                 "Parsing error occurred while processing DMI Plugin Registration" + dmiPluginRegistration, e
133                 .getMessage(), e);
134         }
135     }
136 }