d00d2119b15ded3629bb734daec869e1552f34bd
[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.NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME;
27 import static org.onap.cps.ncmp.api.impl.operations.DmiRequestBody.OperationEnum;
28 import static org.onap.cps.ncmp.api.impl.utils.RestQueryParametersValidator.validateCmHandleQueryParameters;
29
30 import com.hazelcast.map.IMap;
31 import java.time.OffsetDateTime;
32 import java.util.ArrayList;
33 import java.util.Collection;
34 import java.util.HashMap;
35 import java.util.HashSet;
36 import java.util.List;
37 import java.util.Map;
38 import java.util.Set;
39 import java.util.stream.Collectors;
40 import lombok.RequiredArgsConstructor;
41 import lombok.extern.slf4j.Slf4j;
42 import org.onap.cps.api.CpsDataService;
43 import org.onap.cps.ncmp.api.NetworkCmProxyCmHandlerQueryService;
44 import org.onap.cps.ncmp.api.NetworkCmProxyDataService;
45 import org.onap.cps.ncmp.api.impl.event.lcm.LcmEventsCmHandleStateHandler;
46 import org.onap.cps.ncmp.api.impl.operations.DmiDataOperations;
47 import org.onap.cps.ncmp.api.impl.operations.DmiOperations;
48 import org.onap.cps.ncmp.api.impl.utils.CmHandleQueryConditions;
49 import org.onap.cps.ncmp.api.impl.utils.InventoryQueryConditions;
50 import org.onap.cps.ncmp.api.impl.utils.YangDataConverter;
51 import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle;
52 import org.onap.cps.ncmp.api.inventory.CmHandleQueries;
53 import org.onap.cps.ncmp.api.inventory.CmHandleState;
54 import org.onap.cps.ncmp.api.inventory.CompositeState;
55 import org.onap.cps.ncmp.api.inventory.CompositeStateUtils;
56 import org.onap.cps.ncmp.api.inventory.DataStoreSyncState;
57 import org.onap.cps.ncmp.api.inventory.InventoryPersistence;
58 import org.onap.cps.ncmp.api.models.CmHandleQueryApiParameters;
59 import org.onap.cps.ncmp.api.models.CmHandleQueryServiceParameters;
60 import org.onap.cps.ncmp.api.models.CmHandleRegistrationResponse;
61 import org.onap.cps.ncmp.api.models.CmHandleRegistrationResponse.RegistrationError;
62 import org.onap.cps.ncmp.api.models.DmiPluginRegistration;
63 import org.onap.cps.ncmp.api.models.DmiPluginRegistrationResponse;
64 import org.onap.cps.ncmp.api.models.NcmpServiceCmHandle;
65 import org.onap.cps.spi.FetchDescendantsOption;
66 import org.onap.cps.spi.exceptions.AlreadyDefinedExceptionBatch;
67 import org.onap.cps.spi.exceptions.CpsException;
68 import org.onap.cps.spi.exceptions.DataNodeNotFoundException;
69 import org.onap.cps.spi.exceptions.DataValidationException;
70 import org.onap.cps.spi.model.ModuleDefinition;
71 import org.onap.cps.spi.model.ModuleReference;
72 import org.onap.cps.utils.JsonObjectMapper;
73 import org.springframework.http.ResponseEntity;
74 import org.springframework.stereotype.Service;
75
76 @Slf4j
77 @Service
78 @RequiredArgsConstructor
79 public class NetworkCmProxyDataServiceImpl implements NetworkCmProxyDataService {
80
81     private final JsonObjectMapper jsonObjectMapper;
82     private final DmiDataOperations dmiDataOperations;
83     private final NetworkCmProxyDataServicePropertyHandler networkCmProxyDataServicePropertyHandler;
84     private final InventoryPersistence inventoryPersistence;
85     private final CmHandleQueries cmHandleQueries;
86     private final NetworkCmProxyCmHandlerQueryService networkCmProxyCmHandlerQueryService;
87     private final LcmEventsCmHandleStateHandler lcmEventsCmHandleStateHandler;
88     private final CpsDataService cpsDataService;
89     private final IMap<String, Object> moduleSyncStartedOnCmHandles;
90
91     @Override
92     public DmiPluginRegistrationResponse updateDmiRegistrationAndSyncModule(
93             final DmiPluginRegistration dmiPluginRegistration) {
94         dmiPluginRegistration.validateDmiPluginRegistration();
95         final DmiPluginRegistrationResponse dmiPluginRegistrationResponse = new DmiPluginRegistrationResponse();
96         dmiPluginRegistrationResponse.setRemovedCmHandles(
97                 parseAndRemoveCmHandlesInDmiRegistration(dmiPluginRegistration.getRemovedCmHandles()));
98         if (!dmiPluginRegistration.getCreatedCmHandles().isEmpty()) {
99             dmiPluginRegistrationResponse.setCreatedCmHandles(
100                     parseAndCreateCmHandlesInDmiRegistrationAndSyncModules(dmiPluginRegistration));
101         }
102         if (!dmiPluginRegistration.getUpdatedCmHandles().isEmpty()) {
103             dmiPluginRegistrationResponse.setUpdatedCmHandles(
104                     networkCmProxyDataServicePropertyHandler
105                             .updateCmHandleProperties(dmiPluginRegistration.getUpdatedCmHandles()));
106         }
107         return dmiPluginRegistrationResponse;
108     }
109
110     @Override
111     public Object getResourceDataOperationalForCmHandle(final String cmHandleId,
112                                                         final String resourceIdentifier,
113                                                         final String optionsParamInQuery,
114                                                         final String topicParamInQuery,
115                                                         final String requestId) {
116         final ResponseEntity<?> responseEntity = dmiDataOperations.getResourceDataFromDmi(cmHandleId,
117                 resourceIdentifier,
118                 optionsParamInQuery,
119                 DmiOperations.DataStoreEnum.PASSTHROUGH_OPERATIONAL,
120                 requestId, topicParamInQuery);
121         return responseEntity.getBody();
122     }
123
124     @Override
125     public Object getResourceDataOperational(final String cmHandleId,
126                                              final String resourceIdentifier,
127                                              final FetchDescendantsOption fetchDescendantsOption) {
128         return cpsDataService.getDataNode(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, cmHandleId, resourceIdentifier,
129                 fetchDescendantsOption);
130     }
131
132     @Override
133     public Object getResourceDataPassThroughRunningForCmHandle(final String cmHandleId,
134                                                                final String resourceIdentifier,
135                                                                final String optionsParamInQuery,
136                                                                final String topicParamInQuery,
137                                                                final String requestId) {
138         final ResponseEntity<?> responseEntity = dmiDataOperations.getResourceDataFromDmi(cmHandleId,
139                 resourceIdentifier,
140                 optionsParamInQuery,
141                 DmiOperations.DataStoreEnum.PASSTHROUGH_RUNNING,
142                 requestId, topicParamInQuery);
143         return responseEntity.getBody();
144     }
145
146     @Override
147     public Object writeResourceDataPassThroughRunningForCmHandle(final String cmHandleId,
148                                                                  final String resourceIdentifier,
149                                                                  final OperationEnum operation,
150                                                                  final String requestData,
151                                                                  final String dataType) {
152         return dmiDataOperations.writeResourceDataPassThroughRunningFromDmi(cmHandleId, resourceIdentifier, operation,
153                 requestData, dataType);
154     }
155
156     @Override
157     public Collection<ModuleReference> getYangResourcesModuleReferences(final String cmHandleId) {
158         return inventoryPersistence.getYangResourcesModuleReferences(cmHandleId);
159     }
160
161     @Override
162     public Collection<ModuleDefinition> getModuleDefinitionsByCmHandleId(final String cmHandleId) {
163         return inventoryPersistence.getModuleDefinitionsByCmHandleId(cmHandleId);
164     }
165
166     /**
167      * Retrieve cm handles with details for the given query parameters.
168      *
169      * @param cmHandleQueryApiParameters cm handle query parameters
170      * @return cm handles with details
171      */
172     @Override
173     public Set<NcmpServiceCmHandle> executeCmHandleSearch(final CmHandleQueryApiParameters cmHandleQueryApiParameters) {
174         final CmHandleQueryServiceParameters cmHandleQueryServiceParameters = jsonObjectMapper.convertToValueType(
175                 cmHandleQueryApiParameters, CmHandleQueryServiceParameters.class);
176         validateCmHandleQueryParameters(cmHandleQueryServiceParameters, CmHandleQueryConditions.ALL_CONDITION_NAMES);
177         return networkCmProxyCmHandlerQueryService.queryCmHandles(cmHandleQueryServiceParameters);
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         final CmHandleQueryServiceParameters cmHandleQueryServiceParameters = jsonObjectMapper.convertToValueType(
189                 cmHandleQueryApiParameters, CmHandleQueryServiceParameters.class);
190         validateCmHandleQueryParameters(cmHandleQueryServiceParameters, CmHandleQueryConditions.ALL_CONDITION_NAMES);
191         return networkCmProxyCmHandlerQueryService.queryCmHandleIds(cmHandleQueryServiceParameters);
192     }
193
194     /**
195      * Set the data sync enabled flag, along with the data sync state
196      * based on the data sync enabled boolean for the cm handle id provided.
197      *
198      * @param cmHandleId      cm handle id
199      * @param dataSyncEnabled data sync enabled flag
200      */
201     @Override
202     public void setDataSyncEnabled(final String cmHandleId, final boolean dataSyncEnabled) {
203         final CompositeState compositeState = inventoryPersistence
204                 .getCmHandleState(cmHandleId);
205         if (compositeState.getDataSyncEnabled().equals(dataSyncEnabled)) {
206             log.info("Data-Sync Enabled flag is already: {} ", dataSyncEnabled);
207         } else if (compositeState.getCmHandleState() != CmHandleState.READY) {
208             throw new CpsException("State mismatch exception.", "Cm-Handle not in READY state. Cm handle state is: "
209                     + compositeState.getCmHandleState());
210         } else {
211             final DataStoreSyncState dataStoreSyncState = compositeState.getDataStores()
212                     .getOperationalDataStore().getDataStoreSyncState();
213             if (!dataSyncEnabled && dataStoreSyncState == DataStoreSyncState.SYNCHRONIZED) {
214                 cpsDataService.deleteDataNode(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, cmHandleId,
215                         "/netconf-state", OffsetDateTime.now());
216             }
217             CompositeStateUtils.setDataSyncEnabledFlagWithDataSyncState(dataSyncEnabled, compositeState);
218             inventoryPersistence.saveCmHandleState(cmHandleId,
219                     compositeState);
220         }
221     }
222
223     /**
224      * Get all cm handle IDs by DMI plugin identifier.
225      *
226      * @param dmiPluginIdentifier DMI plugin identifier
227      * @return set of cm handle IDs
228      */
229     @Override
230     public Set<String> getAllCmHandleIdsByDmiPluginIdentifier(final String dmiPluginIdentifier) {
231         final Set<NcmpServiceCmHandle> ncmpServiceCmHandles =
232                 cmHandleQueries.getCmHandlesByDmiPluginIdentifier(dmiPluginIdentifier);
233         final Set<String> cmHandleIds = new HashSet<>(ncmpServiceCmHandles.size());
234         ncmpServiceCmHandles.forEach(cmHandle -> cmHandleIds.add(cmHandle.getCmHandleId()));
235         return cmHandleIds;
236     }
237
238     /**
239      * Get all cm handle IDs by various properties.
240      *
241      * @param cmHandleQueryServiceParameters cm handle query parameters
242      * @return set of cm handle IDs
243      */
244     @Override
245     public Set<String> executeCmHandleIdSearchForInventory(
246             final CmHandleQueryServiceParameters cmHandleQueryServiceParameters) {
247         validateCmHandleQueryParameters(cmHandleQueryServiceParameters, InventoryQueryConditions.ALL_CONDITION_NAMES);
248         return networkCmProxyCmHandlerQueryService.queryCmHandleIdsForInventory(cmHandleQueryServiceParameters);
249     }
250
251     /**
252      * Retrieve cm handle details for a given cm handle.
253      *
254      * @param cmHandleId cm handle identifier
255      * @return cm handle details
256      */
257     @Override
258     public NcmpServiceCmHandle getNcmpServiceCmHandle(final String cmHandleId) {
259         return YangDataConverter.convertYangModelCmHandleToNcmpServiceCmHandle(
260                 inventoryPersistence.getYangModelCmHandle(cmHandleId));
261     }
262
263     /**
264      * Get cm handle public properties for a given cm handle id.
265      *
266      * @param cmHandleId cm handle identifier
267      * @return cm handle public properties
268      */
269     @Override
270     public Map<String, String> getCmHandlePublicProperties(final String cmHandleId) {
271         final YangModelCmHandle yangModelCmHandle =
272                 inventoryPersistence.getYangModelCmHandle(cmHandleId);
273         final List<YangModelCmHandle.Property> yangModelPublicProperties = yangModelCmHandle.getPublicProperties();
274         final Map<String, String> cmHandlePublicProperties = new HashMap<>();
275         YangDataConverter.asPropertiesMap(yangModelPublicProperties, cmHandlePublicProperties);
276         return cmHandlePublicProperties;
277     }
278
279     /**
280      * Get cm handle composite state for a given cm handle id.
281      *
282      * @param cmHandleId cm handle identifier
283      * @return cm handle state
284      */
285     @Override
286     public CompositeState getCmHandleCompositeState(final String cmHandleId) {
287         return inventoryPersistence.getYangModelCmHandle(cmHandleId).getCompositeState();
288     }
289
290     /**
291      * THis method registers a cm handle and initiates modules sync.
292      *
293      * @param dmiPluginRegistration dmi plugin registration information.
294      * @return cm-handle registration response for create cm-handle requests.
295      */
296     public List<CmHandleRegistrationResponse> parseAndCreateCmHandlesInDmiRegistrationAndSyncModules(
297             final DmiPluginRegistration dmiPluginRegistration) {
298         List<CmHandleRegistrationResponse> cmHandleRegistrationResponses = new ArrayList<>();
299         final Map<YangModelCmHandle, CmHandleState> cmHandleStatePerCmHandle = new HashMap<>();
300         try {
301             dmiPluginRegistration.getCreatedCmHandles()
302                     .forEach(cmHandle -> {
303                         final YangModelCmHandle yangModelCmHandle = YangModelCmHandle.toYangModelCmHandle(
304                                 dmiPluginRegistration.getDmiPlugin(),
305                                 dmiPluginRegistration.getDmiDataPlugin(),
306                                 dmiPluginRegistration.getDmiModelPlugin(),
307                                 cmHandle);
308                         cmHandleStatePerCmHandle.put(yangModelCmHandle, CmHandleState.ADVISED);
309                     });
310             cmHandleRegistrationResponses = registerNewCmHandles(cmHandleStatePerCmHandle);
311         } catch (final DataValidationException dataValidationException) {
312             cmHandleRegistrationResponses.add(CmHandleRegistrationResponse.createFailureResponse(dmiPluginRegistration
313                             .getCreatedCmHandles().stream()
314                             .map(NcmpServiceCmHandle::getCmHandleId).findFirst().orElse(null),
315                     RegistrationError.CM_HANDLE_INVALID_ID));
316         }
317         return cmHandleRegistrationResponses;
318     }
319
320     protected List<CmHandleRegistrationResponse> parseAndRemoveCmHandlesInDmiRegistration(
321             final List<String> tobeRemovedCmHandles) {
322         final List<CmHandleRegistrationResponse> cmHandleRegistrationResponses =
323                 new ArrayList<>(tobeRemovedCmHandles.size());
324         for (final String cmHandleId : tobeRemovedCmHandles) {
325             try {
326                 final YangModelCmHandle yangModelCmHandle = inventoryPersistence.getYangModelCmHandle(cmHandleId);
327                 lcmEventsCmHandleStateHandler.updateCmHandleState(yangModelCmHandle,
328                         CmHandleState.DELETING);
329                 deleteCmHandleFromDbAndModuleSyncMap(cmHandleId);
330                 cmHandleRegistrationResponses.add(CmHandleRegistrationResponse.createSuccessResponse(cmHandleId));
331                 lcmEventsCmHandleStateHandler.updateCmHandleState(yangModelCmHandle,
332                         CmHandleState.DELETED);
333             } catch (final DataNodeNotFoundException dataNodeNotFoundException) {
334                 log.error("Unable to find dataNode for cmHandleId : {} , caused by : {}",
335                         cmHandleId, dataNodeNotFoundException.getMessage());
336                 cmHandleRegistrationResponses.add(CmHandleRegistrationResponse
337                         .createFailureResponse(cmHandleId, RegistrationError.CM_HANDLE_DOES_NOT_EXIST));
338             } catch (final DataValidationException dataValidationException) {
339                 log.error("Unable to de-register cm-handle id: {}, caused by: {}",
340                         cmHandleId, dataValidationException.getMessage());
341                 cmHandleRegistrationResponses.add(CmHandleRegistrationResponse
342                         .createFailureResponse(cmHandleId, RegistrationError.CM_HANDLE_INVALID_ID));
343             } catch (final Exception exception) {
344                 log.error("Unable to de-register cm-handle id : {} , caused by : {}",
345                         cmHandleId, exception.getMessage());
346                 cmHandleRegistrationResponses.add(
347                         CmHandleRegistrationResponse.createFailureResponse(cmHandleId, exception));
348             }
349         }
350         return cmHandleRegistrationResponses;
351     }
352
353     private void deleteCmHandleFromDbAndModuleSyncMap(final String cmHandleId) {
354         inventoryPersistence.deleteSchemaSetWithCascade(cmHandleId);
355         inventoryPersistence.deleteListOrListElement("/dmi-registry/cm-handles[@id='" + cmHandleId + "']");
356         removeDeletedCmHandleFromModuleSyncMap(cmHandleId);
357     }
358
359     // CPS-1239 Robustness cleaning of in progress cache
360     private void removeDeletedCmHandleFromModuleSyncMap(final String deletedCmHandleId) {
361         if (moduleSyncStartedOnCmHandles.remove(deletedCmHandleId) != null) {
362             log.debug("{} removed from in progress map", deletedCmHandleId);
363         }
364     }
365
366     private List<CmHandleRegistrationResponse> registerNewCmHandles(final Map<YangModelCmHandle, CmHandleState>
367                                                                             cmHandleStatePerCmHandle) {
368         final List<String> cmHandleIds = cmHandleStatePerCmHandle.keySet().stream().map(YangModelCmHandle::getId)
369                 .collect(Collectors.toList());
370         try {
371             lcmEventsCmHandleStateHandler.updateCmHandleStateBatch(cmHandleStatePerCmHandle);
372             return CmHandleRegistrationResponse.createSuccessResponses(cmHandleIds);
373         } catch (final AlreadyDefinedExceptionBatch alreadyDefinedExceptionBatch) {
374             return CmHandleRegistrationResponse.createFailureResponses(
375                     alreadyDefinedExceptionBatch.getAlreadyDefinedXpaths(),
376                     RegistrationError.CM_HANDLE_ALREADY_EXIST);
377         } catch (final Exception exception) {
378             return CmHandleRegistrationResponse.createFailureResponses(cmHandleIds, exception);
379         }
380     }
381 }