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