Implement DMI Registration (NCMP-Side)
[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 static final String NCMP_DATA_TOP_PATH = "/dmi-registry";
55
56     private CpsDataService cpsDataService;
57
58     private ObjectMapper objectMapper;
59
60     private CpsQueryService cpsQueryService;
61
62     /**
63      * Constructor Injection for Dependencies.
64      * @param cpsDataService Data Service Interface
65      * @param cpsQueryService Query Service Interface
66      * @param objectMapper Object Mapper
67      */
68     public NetworkCmProxyDataServiceImpl(final CpsDataService cpsDataService,
69         final CpsQueryService cpsQueryService, final ObjectMapper objectMapper) {
70         this.cpsDataService = cpsDataService;
71         this.cpsQueryService = cpsQueryService;
72         this.objectMapper = objectMapper;
73     }
74
75     private String getDataspaceName() {
76         return NF_PROXY_DATASPACE_NAME;
77     }
78
79     @Override
80     public DataNode getDataNode(final String cmHandle, final String xpath,
81         final FetchDescendantsOption fetchDescendantsOption) {
82         return cpsDataService.getDataNode(getDataspaceName(), cmHandle, xpath, fetchDescendantsOption);
83     }
84
85     @Override
86     public Collection<DataNode> queryDataNodes(final String cmHandle, final String cpsPath,
87         final FetchDescendantsOption fetchDescendantsOption) {
88         return cpsQueryService.queryDataNodes(getDataspaceName(), cmHandle, cpsPath, fetchDescendantsOption);
89     }
90
91     @Override
92     public void createDataNode(final String cmHandle, final String parentNodeXpath, final String jsonData) {
93         if (!StringUtils.hasText(parentNodeXpath) || "/".equals(parentNodeXpath)) {
94             cpsDataService.saveData(getDataspaceName(), cmHandle, jsonData);
95         } else {
96             cpsDataService.saveData(getDataspaceName(), cmHandle, parentNodeXpath, jsonData);
97         }
98     }
99
100     @Override
101     public void addListNodeElements(final String cmHandle, final String parentNodeXpath, final String jsonData) {
102         cpsDataService.saveListNodeData(getDataspaceName(), cmHandle, parentNodeXpath, jsonData);
103     }
104
105     @Override
106     public void updateNodeLeaves(final String cmHandle, final String parentNodeXpath, final String jsonData) {
107         cpsDataService.updateNodeLeaves(getDataspaceName(), cmHandle, parentNodeXpath, jsonData);
108     }
109
110     @Override
111     public void replaceNodeTree(final String cmHandle, final String parentNodeXpath, final String jsonData) {
112         cpsDataService.replaceNodeTree(getDataspaceName(), cmHandle, parentNodeXpath, jsonData);
113     }
114
115     @Override
116     public void updateDmiPluginRegistration(final DmiPluginRegistration dmiPluginRegistration) {
117         try {
118             final List<PersistenceCmHandle> persistenceCmHandles =
119                 new ArrayList<>();
120             for (final CmHandle cmHandle: dmiPluginRegistration.getCreatedCmHandles()) {
121                 final PersistenceCmHandle persistenceCmHandle = new PersistenceCmHandle();
122                 persistenceCmHandle.setDmiServiceName(dmiPluginRegistration.getDmiPlugin());
123                 persistenceCmHandle.setId(cmHandle.getCmHandle());
124                 persistenceCmHandle.setAdditionalProperties(cmHandle.getCmHandleProperties());
125                 persistenceCmHandles.add(persistenceCmHandle);
126             }
127             final PersistenceCmHandlesList persistenceCmHandlesList = new PersistenceCmHandlesList();
128             persistenceCmHandlesList.setCmHandles(persistenceCmHandles);
129             final String cmHandleJsonData = objectMapper.writeValueAsString(persistenceCmHandlesList);
130             cpsDataService.saveListNodeData(NCMP_DATASPACE_NAME, NCMP_ANCHOR_NAME, NCMP_DATA_TOP_PATH,
131                 cmHandleJsonData);
132         } catch (final JsonProcessingException e) {
133             throw new DataValidationException(
134                 "Parsing error occurred while processing DMI Plugin Registration" + dmiPluginRegistration, e
135                 .getMessage(), e);
136         }
137     }
138 }