3c8d8bc3b7a130e968316e80ec7f4858f3424edc
[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-2022 Nordix Foundation
5  *  Modifications Copyright (C) 2021 Pantheon.tech
6  *  Modifications Copyright (C) 2021 Bell Canada
7  *  ================================================================================
8  *  Licensed under the Apache License, Version 2.0 (the "License");
9  *  you may not use this file except in compliance with the License.
10  *  You may obtain a copy of the License at
11  *
12  *        http://www.apache.org/licenses/LICENSE-2.0
13  *
14  *  Unless required by applicable law or agreed to in writing, software
15  *  distributed under the License is distributed on an "AS IS" BASIS,
16  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  *  See the License for the specific language governing permissions and
18  *  limitations under the License.
19  *
20  *  SPDX-License-Identifier: Apache-2.0
21  *  ============LICENSE_END=========================================================
22  */
23
24 package org.onap.cps.ncmp.api.impl;
25
26 import static org.onap.cps.ncmp.api.impl.constants.DmiRegistryConstants.NCMP_DATASPACE_NAME;
27 import static org.onap.cps.ncmp.api.impl.constants.DmiRegistryConstants.NCMP_DMI_REGISTRY_ANCHOR;
28 import static org.onap.cps.ncmp.api.impl.constants.DmiRegistryConstants.NCMP_DMI_REGISTRY_PARENT;
29 import static org.onap.cps.ncmp.api.impl.constants.DmiRegistryConstants.NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME;
30 import static org.onap.cps.ncmp.api.impl.constants.DmiRegistryConstants.NO_TIMESTAMP;
31 import static org.onap.cps.ncmp.api.impl.operations.DmiRequestBody.OperationEnum;
32 import static org.onap.cps.spi.CascadeDeleteAllowed.CASCADE_DELETE_ALLOWED;
33
34 import com.fasterxml.jackson.core.JsonProcessingException;
35 import java.util.ArrayList;
36 import java.util.Collection;
37 import java.util.HashMap;
38 import java.util.List;
39 import java.util.Map;
40 import lombok.RequiredArgsConstructor;
41 import lombok.extern.slf4j.Slf4j;
42 import org.onap.cps.api.CpsAdminService;
43 import org.onap.cps.api.CpsDataService;
44 import org.onap.cps.api.CpsModuleService;
45 import org.onap.cps.ncmp.api.NetworkCmProxyDataService;
46 import org.onap.cps.ncmp.api.impl.exception.ServerNcmpException;
47 import org.onap.cps.ncmp.api.impl.operations.DmiDataOperations;
48 import org.onap.cps.ncmp.api.impl.operations.DmiModelOperations;
49 import org.onap.cps.ncmp.api.impl.operations.DmiOperations;
50 import org.onap.cps.ncmp.api.models.CmHandle;
51 import org.onap.cps.ncmp.api.models.DmiPluginRegistration;
52 import org.onap.cps.ncmp.api.models.PersistenceCmHandle;
53 import org.onap.cps.ncmp.api.models.PersistenceCmHandlesList;
54 import org.onap.cps.spi.exceptions.DataNodeNotFoundException;
55 import org.onap.cps.spi.exceptions.DataValidationException;
56 import org.onap.cps.spi.model.ModuleReference;
57 import org.onap.cps.utils.JsonObjectMapper;
58 import org.springframework.http.ResponseEntity;
59 import org.springframework.stereotype.Service;
60
61 @Slf4j
62 @Service
63 @RequiredArgsConstructor
64 public class NetworkCmProxyDataServiceImpl implements NetworkCmProxyDataService {
65
66     private final CpsDataService cpsDataService;
67
68     private final JsonObjectMapper jsonObjectMapper;
69
70     private final DmiDataOperations dmiDataOperations;
71
72     private final DmiModelOperations dmiModelOperations;
73
74     private final CpsModuleService cpsModuleService;
75
76     private final CpsAdminService cpsAdminService;
77
78     private final NetworkCmProxyDataServicePropertyHandler networkCmProxyDataServicePropertyHandler;
79
80     @Override
81     public void updateDmiRegistrationAndSyncModule(final DmiPluginRegistration dmiPluginRegistration) {
82         dmiPluginRegistration.validateDmiPluginRegistration();
83         try {
84             if (dmiPluginRegistration.getCreatedCmHandles() != null) {
85                 parseAndCreateCmHandlesInDmiRegistrationAndSyncModules(dmiPluginRegistration);
86             }
87             if (dmiPluginRegistration.getUpdatedCmHandles() != null) {
88                 parseAndUpdateCmHandlesInDmiRegistration(dmiPluginRegistration);
89             }
90             if (dmiPluginRegistration.getRemovedCmHandles() != null) {
91                 parseAndRemoveCmHandlesInDmiRegistration(dmiPluginRegistration);
92             }
93         } catch (final JsonProcessingException | DataNodeNotFoundException e) {
94             final String errorMessage = String.format(
95                     "Error occurred while processing the CM-handle registration request, caused by : [%s]",
96                     e.getMessage());
97             throw new DataValidationException(errorMessage, e.getMessage(), e);
98         }
99     }
100
101     @Override
102     public Object getResourceDataOperationalForCmHandle(final String cmHandle,
103                                                         final String resourceIdentifier,
104                                                         final String acceptParamInHeader,
105                                                         final String optionsParamInQuery) {
106         return handleResponse(dmiDataOperations.getResourceDataFromDmi(
107             cmHandle,
108             resourceIdentifier,
109             optionsParamInQuery,
110             acceptParamInHeader,
111             DmiOperations.DataStoreEnum.PASSTHROUGH_OPERATIONAL), "Not able to get resource data.");
112     }
113
114     @Override
115     public Object getResourceDataPassThroughRunningForCmHandle(final String cmHandle,
116                                                                final String resourceIdentifier,
117                                                                final String acceptParamInHeader,
118                                                                final String optionsParamInQuery) {
119         return handleResponse(dmiDataOperations.getResourceDataFromDmi(
120             cmHandle,
121             resourceIdentifier,
122             optionsParamInQuery,
123             acceptParamInHeader,
124             DmiOperations.DataStoreEnum.PASSTHROUGH_RUNNING), "Not able to get resource data.");
125     }
126
127     @Override
128     public Object writeResourceDataPassThroughRunningForCmHandle(final String cmHandle,
129                                                                final String resourceIdentifier,
130                                                                final OperationEnum operation,
131                                                                final String requestData,
132                                                                final String dataType) {
133         return handleResponse(
134             dmiDataOperations.writeResourceDataPassThroughRunningFromDmi(
135                 cmHandle, resourceIdentifier, operation, requestData, dataType),
136             "Not able to " + operation + " resource data.");
137     }
138
139
140     @Override
141     public Collection<ModuleReference> getYangResourcesModuleReferences(final String cmHandle) {
142         return cpsModuleService.getYangResourcesModuleReferences(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, cmHandle);
143     }
144
145     /**
146      * Retrieve cm handle identifiers for the given list of module names.
147      *
148      * @param moduleNames module names.
149      * @return a collection of anchor identifiers
150      */
151     @Override
152     public Collection<String> executeCmHandleHasAllModulesSearch(final Collection<String> moduleNames) {
153         return cpsAdminService.queryAnchorNames(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, moduleNames);
154     }
155
156     /**
157      * THis method registers a cm handle and intiates modules sync.
158      *
159      * @param dmiPluginRegistration dmi plugin registration information.
160      * @throws JsonProcessingException thrown if json is malformed or missing.
161      */
162     public void parseAndCreateCmHandlesInDmiRegistrationAndSyncModules(
163         final DmiPluginRegistration dmiPluginRegistration) throws JsonProcessingException {
164         final PersistenceCmHandlesList createdPersistenceCmHandlesList =
165             getUpdatedPersistenceCmHandlesList(dmiPluginRegistration, dmiPluginRegistration.getCreatedCmHandles());
166         registerAndSyncNewCmHandles(createdPersistenceCmHandlesList);
167     }
168
169     private static Object handleResponse(final ResponseEntity<?> responseEntity,
170                                          final String exceptionMessage) {
171         if (responseEntity.getStatusCode().is2xxSuccessful()) {
172             return responseEntity.getBody();
173         } else {
174             throw new ServerNcmpException(exceptionMessage,
175                     "DMI status code: " + responseEntity.getStatusCodeValue()
176                             + ", DMI response body: " + responseEntity.getBody());
177         }
178     }
179
180     private void parseAndUpdateCmHandlesInDmiRegistration(final DmiPluginRegistration dmiPluginRegistration) {
181         networkCmProxyDataServicePropertyHandler.updateCmHandleProperties(dmiPluginRegistration.getUpdatedCmHandles());
182     }
183
184     private PersistenceCmHandlesList getUpdatedPersistenceCmHandlesList(
185         final DmiPluginRegistration dmiPluginRegistration,
186         final List<CmHandle> updatedCmHandles) {
187         return PersistenceCmHandlesList.toPersistenceCmHandlesList(
188             dmiPluginRegistration.getDmiPlugin(),
189             dmiPluginRegistration.getDmiDataPlugin(),
190             dmiPluginRegistration.getDmiModelPlugin(),
191             updatedCmHandles);
192     }
193
194     private void registerAndSyncNewCmHandles(final PersistenceCmHandlesList persistenceCmHandlesList) {
195         final String cmHandleJsonData = jsonObjectMapper.asJsonString(persistenceCmHandlesList);
196         cpsDataService.saveListElements(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR, NCMP_DMI_REGISTRY_PARENT,
197                 cmHandleJsonData, NO_TIMESTAMP);
198
199         for (final PersistenceCmHandle persistenceCmHandle : persistenceCmHandlesList.getPersistenceCmHandles()) {
200             syncModulesAndCreateAnchor(persistenceCmHandle);
201         }
202     }
203
204     protected void syncModulesAndCreateAnchor(final PersistenceCmHandle persistenceCmHandle) {
205         syncAndCreateSchemaSet(persistenceCmHandle);
206         createAnchor(persistenceCmHandle);
207     }
208
209     private void parseAndRemoveCmHandlesInDmiRegistration(final DmiPluginRegistration dmiPluginRegistration) {
210         for (final String cmHandle : dmiPluginRegistration.getRemovedCmHandles()) {
211             try {
212                 attemptToDeleteSchemaSetWithCascade(cmHandle);
213                 cpsDataService.deleteListOrListElement(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR,
214                     "/dmi-registry/cm-handles[@id='" + cmHandle + "']", NO_TIMESTAMP);
215             } catch (final DataNodeNotFoundException e) {
216                 log.warn("Datanode {} not deleted message {}", cmHandle, e.getMessage());
217             }
218         }
219     }
220
221     private void attemptToDeleteSchemaSetWithCascade(final String schemaSetName) {
222         try {
223             cpsModuleService.deleteSchemaSet(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, schemaSetName,
224                 CASCADE_DELETE_ALLOWED);
225         } catch (final Exception e) {
226             log.warn("Schema set {} delete failed, reason {}", schemaSetName, e.getMessage());
227         }
228     }
229
230     private void syncAndCreateSchemaSet(final PersistenceCmHandle persistenceCmHandle) {
231         final List<ModuleReference> moduleReferencesFromCmHandle =
232             dmiModelOperations.getModuleReferences(persistenceCmHandle);
233         final List<ModuleReference> existingModuleReferences = new ArrayList<>();
234         final List<ModuleReference> unknownModuleReferences = new ArrayList<>();
235         prepareModuleSubsets(moduleReferencesFromCmHandle, existingModuleReferences, unknownModuleReferences);
236
237         final Map<String, String> newYangResourcesModuleNameToContentMap;
238         if (unknownModuleReferences.isEmpty()) {
239             newYangResourcesModuleNameToContentMap = new HashMap<>();
240         } else {
241             newYangResourcesModuleNameToContentMap = dmiModelOperations.getNewYangResourcesFromDmi(persistenceCmHandle,
242                 unknownModuleReferences);
243         }
244         cpsModuleService
245             .createSchemaSetFromModules(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, persistenceCmHandle.getId(),
246                 newYangResourcesModuleNameToContentMap, existingModuleReferences);
247     }
248
249     private void prepareModuleSubsets(final List<ModuleReference> moduleReferencesFromCmHandle,
250                                       final List<ModuleReference> existingModuleReferences,
251                                       final List<ModuleReference> unknownModuleReferences) {
252
253         final Collection<ModuleReference> knownModuleReferencesInCps =
254             cpsModuleService.getYangResourceModuleReferences(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME);
255
256         for (final ModuleReference moduleReferenceFromDmiForCmHandle : moduleReferencesFromCmHandle) {
257             if (knownModuleReferencesInCps.contains(moduleReferenceFromDmiForCmHandle)) {
258                 existingModuleReferences.add(moduleReferenceFromDmiForCmHandle);
259             } else {
260                 unknownModuleReferences.add(moduleReferenceFromDmiForCmHandle);
261             }
262         }
263     }
264
265     private void createAnchor(final PersistenceCmHandle persistenceCmHandle) {
266         cpsAdminService.createAnchor(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, persistenceCmHandle.getId(),
267             persistenceCmHandle.getId());
268     }
269 }