Merge "Ncmp Producer Setup"
[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-2022 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 import static org.onap.cps.utils.CmHandleQueryRestParametersValidator.validateCmHandleQueryParameters;
34
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 java.util.Set;
41 import java.util.stream.Collectors;
42 import lombok.RequiredArgsConstructor;
43 import lombok.extern.slf4j.Slf4j;
44 import org.onap.cps.api.CpsAdminService;
45 import org.onap.cps.api.CpsDataService;
46 import org.onap.cps.api.CpsModuleService;
47 import org.onap.cps.ncmp.api.NetworkCmProxyCmHandlerQueryService;
48 import org.onap.cps.ncmp.api.NetworkCmProxyDataService;
49 import org.onap.cps.ncmp.api.impl.operations.DmiDataOperations;
50 import org.onap.cps.ncmp.api.impl.operations.DmiOperations;
51 import org.onap.cps.ncmp.api.impl.utils.YangDataConverter;
52 import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle;
53 import org.onap.cps.ncmp.api.inventory.InventoryPersistence;
54 import org.onap.cps.ncmp.api.inventory.sync.ModuleSyncService;
55 import org.onap.cps.ncmp.api.models.CmHandleQueryApiParameters;
56 import org.onap.cps.ncmp.api.models.CmHandleRegistrationResponse;
57 import org.onap.cps.ncmp.api.models.CmHandleRegistrationResponse.RegistrationError;
58 import org.onap.cps.ncmp.api.models.DmiPluginRegistration;
59 import org.onap.cps.ncmp.api.models.DmiPluginRegistrationResponse;
60 import org.onap.cps.ncmp.api.models.NcmpServiceCmHandle;
61 import org.onap.cps.spi.exceptions.AlreadyDefinedException;
62 import org.onap.cps.spi.exceptions.DataNodeNotFoundException;
63 import org.onap.cps.spi.exceptions.DataValidationException;
64 import org.onap.cps.spi.exceptions.SchemaSetNotFoundException;
65 import org.onap.cps.spi.model.CmHandleQueryParameters;
66 import org.onap.cps.spi.model.ModuleReference;
67 import org.onap.cps.utils.CpsValidator;
68 import org.onap.cps.utils.JsonObjectMapper;
69 import org.springframework.http.ResponseEntity;
70 import org.springframework.stereotype.Service;
71
72 @Slf4j
73 @Service
74 @RequiredArgsConstructor
75 public class NetworkCmProxyDataServiceImpl implements NetworkCmProxyDataService {
76
77     private final CpsDataService cpsDataService;
78
79     private final JsonObjectMapper jsonObjectMapper;
80
81     private final DmiDataOperations dmiDataOperations;
82
83     private final CpsModuleService cpsModuleService;
84
85     private final CpsAdminService cpsAdminService;
86
87     private final NetworkCmProxyDataServicePropertyHandler networkCmProxyDataServicePropertyHandler;
88
89     private final InventoryPersistence inventoryPersistence;
90
91     private final ModuleSyncService moduleSyncService;
92
93     private final NetworkCmProxyCmHandlerQueryService networkCmProxyCmHandlerQueryService;
94
95     @Override
96     public DmiPluginRegistrationResponse updateDmiRegistrationAndSyncModule(
97             final DmiPluginRegistration dmiPluginRegistration) {
98         dmiPluginRegistration.validateDmiPluginRegistration();
99         final DmiPluginRegistrationResponse dmiPluginRegistrationResponse = new DmiPluginRegistrationResponse();
100         dmiPluginRegistrationResponse.setRemovedCmHandles(
101                 parseAndRemoveCmHandlesInDmiRegistration(dmiPluginRegistration.getRemovedCmHandles()));
102         if (!dmiPluginRegistration.getCreatedCmHandles().isEmpty()) {
103             dmiPluginRegistrationResponse.setCreatedCmHandles(
104                     parseAndCreateCmHandlesInDmiRegistrationAndSyncModules(dmiPluginRegistration));
105         }
106         if (!dmiPluginRegistration.getUpdatedCmHandles().isEmpty()) {
107             dmiPluginRegistrationResponse.setUpdatedCmHandles(
108                     networkCmProxyDataServicePropertyHandler
109                             .updateCmHandleProperties(dmiPluginRegistration.getUpdatedCmHandles()));
110         }
111         return dmiPluginRegistrationResponse;
112     }
113
114     @Override
115     public Object getResourceDataOperationalForCmHandle(final String cmHandleId,
116                                                         final String resourceIdentifier,
117                                                         final String optionsParamInQuery,
118                                                         final String topicParamInQuery,
119                                                         final String requestId) {
120         final ResponseEntity<?> responseEntity = dmiDataOperations.getResourceDataFromDmi(cmHandleId,
121             resourceIdentifier,
122             optionsParamInQuery,
123             DmiOperations.DataStoreEnum.PASSTHROUGH_OPERATIONAL,
124             requestId, topicParamInQuery);
125         return responseEntity.getBody();
126     }
127
128     @Override
129     public Object getResourceDataPassThroughRunningForCmHandle(final String cmHandleId,
130                                                                final String resourceIdentifier,
131                                                                final String optionsParamInQuery,
132                                                                final String topicParamInQuery,
133                                                                final String requestId) {
134         final ResponseEntity<?> responseEntity = dmiDataOperations.getResourceDataFromDmi(cmHandleId,
135             resourceIdentifier,
136             optionsParamInQuery,
137             DmiOperations.DataStoreEnum.PASSTHROUGH_RUNNING,
138             requestId, topicParamInQuery);
139         return responseEntity.getBody();
140     }
141
142     @Override
143     public Object writeResourceDataPassThroughRunningForCmHandle(final String cmHandleId,
144                                                                  final String resourceIdentifier,
145                                                                  final OperationEnum operation,
146                                                                  final String requestData,
147                                                                  final String dataType) {
148         CpsValidator.validateNameCharacters(cmHandleId);
149         return dmiDataOperations.writeResourceDataPassThroughRunningFromDmi(cmHandleId, resourceIdentifier, operation,
150             requestData, dataType);
151     }
152
153
154     @Override
155     public Collection<ModuleReference> getYangResourcesModuleReferences(final String cmHandleId) {
156         CpsValidator.validateNameCharacters(cmHandleId);
157         return cpsModuleService.getYangResourcesModuleReferences(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, cmHandleId);
158     }
159
160     /**
161      * Retrieve cm handles with details for the given query parameters.
162      *
163      * @param cmHandleQueryApiParameters cm handle query parameters
164      * @return cm handles with details
165      */
166     @Override
167     public Set<NcmpServiceCmHandle> executeCmHandleSearch(final CmHandleQueryApiParameters cmHandleQueryApiParameters) {
168
169         final CmHandleQueryParameters cmHandleQueryParameters = jsonObjectMapper.convertToValueType(
170                 cmHandleQueryApiParameters, CmHandleQueryParameters.class);
171
172         validateCmHandleQueryParameters(cmHandleQueryParameters);
173
174         return networkCmProxyCmHandlerQueryService.queryCmHandles(cmHandleQueryParameters).stream()
175                 .map(dataNode -> YangDataConverter
176                         .convertCmHandleToYangModel(dataNode, dataNode.getLeaves().get("id").toString()))
177                 .map(YangDataConverter::convertYangModelCmHandleToNcmpServiceCmHandle).collect(Collectors.toSet());
178     }
179
180     /**
181      * Retrieve cm handle ids for the given query parameters.
182      *
183      * @param cmHandleQueryApiParameters cm handle query parameters
184      * @return cm handle ids
185      */
186     @Override
187     public Set<String> executeCmHandleIdSearch(final CmHandleQueryApiParameters cmHandleQueryApiParameters) {
188         return executeCmHandleSearch(cmHandleQueryApiParameters).stream().map(NcmpServiceCmHandle::getCmHandleId)
189                 .collect(Collectors.toSet());
190     }
191
192     /**
193      * Retrieve cm handle details for a given cm handle.
194      *
195      * @param cmHandleId cm handle identifier
196      * @return cm handle details
197      */
198     @Override
199     public NcmpServiceCmHandle getNcmpServiceCmHandle(final String cmHandleId) {
200         CpsValidator.validateNameCharacters(cmHandleId);
201         return YangDataConverter.convertYangModelCmHandleToNcmpServiceCmHandle(
202                 inventoryPersistence.getYangModelCmHandle(cmHandleId));
203     }
204
205     /**
206      * Get cm handle public properties for a given cm handle id.
207      *
208      * @param cmHandleId cm handle identifier
209      * @return cm handle public properties
210      */
211     @Override
212     public Map<String, String> getCmHandlePublicProperties(final String cmHandleId) {
213         CpsValidator.validateNameCharacters(cmHandleId);
214         final YangModelCmHandle yangModelCmHandle =
215             inventoryPersistence.getYangModelCmHandle(cmHandleId);
216         final List<YangModelCmHandle.Property> yangModelPublicProperties = yangModelCmHandle.getPublicProperties();
217         final Map<String, String> cmHandlePublicProperties = new HashMap<>();
218         YangDataConverter.asPropertiesMap(yangModelPublicProperties, cmHandlePublicProperties);
219         return cmHandlePublicProperties;
220     }
221
222     /**
223      * THis method registers a cm handle and initiates modules sync.
224      *
225      * @param dmiPluginRegistration dmi plugin registration information.
226      * @return cm-handle registration response for create cm-handle requests.
227      */
228     public List<CmHandleRegistrationResponse> parseAndCreateCmHandlesInDmiRegistrationAndSyncModules(
229             final DmiPluginRegistration dmiPluginRegistration) {
230         List<CmHandleRegistrationResponse> cmHandleRegistrationResponses = new ArrayList<>();
231         try {
232             cmHandleRegistrationResponses = dmiPluginRegistration.getCreatedCmHandles().stream()
233                     .map(cmHandle ->
234                             YangModelCmHandle.toYangModelCmHandle(
235                                     dmiPluginRegistration.getDmiPlugin(),
236                                     dmiPluginRegistration.getDmiDataPlugin(),
237                                     dmiPluginRegistration.getDmiModelPlugin(), cmHandle)
238                     )
239                     .map(this::registerAndSyncNewCmHandle)
240                     .collect(Collectors.toList());
241         } catch (final DataValidationException dataValidationException) {
242             cmHandleRegistrationResponses.add(CmHandleRegistrationResponse.createFailureResponse(dmiPluginRegistration
243                             .getCreatedCmHandles().stream()
244                             .map(NcmpServiceCmHandle::getCmHandleId).findFirst().orElse(null),
245                     RegistrationError.CM_HANDLE_INVALID_ID));
246         }
247         return cmHandleRegistrationResponses;
248     }
249
250     protected void syncModulesAndCreateAnchor(final YangModelCmHandle yangModelCmHandle) {
251         final String schemaSetName = moduleSyncService.syncAndCreateSchemaSet(yangModelCmHandle);
252         final String anchorName = yangModelCmHandle.getId();
253         cpsAdminService.createAnchor(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, schemaSetName,
254             anchorName);
255     }
256
257     protected List<CmHandleRegistrationResponse> parseAndRemoveCmHandlesInDmiRegistration(
258             final List<String> tobeRemovedCmHandles) {
259         final List<CmHandleRegistrationResponse> cmHandleRegistrationResponses =
260                 new ArrayList<>(tobeRemovedCmHandles.size());
261         for (final String cmHandle : tobeRemovedCmHandles) {
262             try {
263                 CpsValidator.validateNameCharacters(cmHandle);
264                 deleteSchemaSetWithCascade(cmHandle);
265                 cpsDataService.deleteListOrListElement(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR,
266                         "/dmi-registry/cm-handles[@id='" + cmHandle + "']", NO_TIMESTAMP);
267                 cmHandleRegistrationResponses.add(CmHandleRegistrationResponse.createSuccessResponse(cmHandle));
268             } catch (final DataNodeNotFoundException dataNodeNotFoundException) {
269                 log.error("Unable to find dataNode for cmHandleId : {} , caused by : {}",
270                         cmHandle, dataNodeNotFoundException.getMessage());
271                 cmHandleRegistrationResponses.add(CmHandleRegistrationResponse
272                         .createFailureResponse(cmHandle, RegistrationError.CM_HANDLE_DOES_NOT_EXIST));
273             } catch (final DataValidationException dataValidationException) {
274                 log.error("Unable to de-register cm-handle id: {}, caused by: {}",
275                         cmHandle, dataValidationException.getMessage());
276                 cmHandleRegistrationResponses.add(CmHandleRegistrationResponse
277                         .createFailureResponse(cmHandle, RegistrationError.CM_HANDLE_INVALID_ID));
278             } catch (final Exception exception) {
279                 log.error("Unable to de-register cm-handle id : {} , caused by : {}",
280                         cmHandle, exception.getMessage());
281                 cmHandleRegistrationResponses.add(
282                         CmHandleRegistrationResponse.createFailureResponse(cmHandle, exception));
283             }
284         }
285         return cmHandleRegistrationResponses;
286     }
287
288     private void deleteSchemaSetWithCascade(final String schemaSetName) {
289         try {
290             cpsModuleService.deleteSchemaSet(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, schemaSetName,
291                     CASCADE_DELETE_ALLOWED);
292         } catch (final SchemaSetNotFoundException schemaSetNotFoundException) {
293             log.warn("Schema set {} does not exist or already deleted", schemaSetName);
294         }
295     }
296
297     private CmHandleRegistrationResponse registerAndSyncNewCmHandle(final YangModelCmHandle yangModelCmHandle) {
298         try {
299             final String cmHandleJsonData = String.format("{\"cm-handles\":[%s]}",
300                     jsonObjectMapper.asJsonString(yangModelCmHandle));
301             cpsDataService.saveListElements(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR, NCMP_DMI_REGISTRY_PARENT,
302                     cmHandleJsonData, NO_TIMESTAMP);
303             syncModulesAndCreateAnchor(yangModelCmHandle);
304             return CmHandleRegistrationResponse.createSuccessResponse(yangModelCmHandle.getId());
305         } catch (final AlreadyDefinedException alreadyDefinedException) {
306             return CmHandleRegistrationResponse.createFailureResponse(
307                     yangModelCmHandle.getId(), RegistrationError.CM_HANDLE_ALREADY_EXIST);
308         } catch (final Exception exception) {
309             return CmHandleRegistrationResponse.createFailureResponse(yangModelCmHandle.getId(), exception);
310         }
311     }
312 }