Updating CmHandleStates using batch operation
[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
97         if (!dmiPluginRegistration.getRemovedCmHandles().isEmpty()) {
98             dmiPluginRegistrationResponse.setRemovedCmHandles(
99                     parseAndRemoveCmHandlesInDmiRegistration(dmiPluginRegistration.getRemovedCmHandles()));
100         }
101
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 getResourceDataOperational(final String cmHandleId,
130                                              final String resourceIdentifier,
131                                              final FetchDescendantsOption fetchDescendantsOption) {
132         return cpsDataService.getDataNode(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, cmHandleId, resourceIdentifier,
133                 fetchDescendantsOption);
134     }
135
136     @Override
137     public Object getResourceDataPassThroughRunningForCmHandle(final String cmHandleId,
138                                                                final String resourceIdentifier,
139                                                                final String optionsParamInQuery,
140                                                                final String topicParamInQuery,
141                                                                final String requestId) {
142         final ResponseEntity<?> responseEntity = dmiDataOperations.getResourceDataFromDmi(cmHandleId,
143                 resourceIdentifier,
144                 optionsParamInQuery,
145                 DmiOperations.DataStoreEnum.PASSTHROUGH_RUNNING,
146                 requestId, topicParamInQuery);
147         return responseEntity.getBody();
148     }
149
150     @Override
151     public Object writeResourceDataPassThroughRunningForCmHandle(final String cmHandleId,
152                                                                  final String resourceIdentifier,
153                                                                  final OperationEnum operation,
154                                                                  final String requestData,
155                                                                  final String dataType) {
156         return dmiDataOperations.writeResourceDataPassThroughRunningFromDmi(cmHandleId, resourceIdentifier, operation,
157                 requestData, dataType);
158     }
159
160     @Override
161     public Collection<ModuleReference> getYangResourcesModuleReferences(final String cmHandleId) {
162         return inventoryPersistence.getYangResourcesModuleReferences(cmHandleId);
163     }
164
165     @Override
166     public Collection<ModuleDefinition> getModuleDefinitionsByCmHandleId(final String cmHandleId) {
167         return inventoryPersistence.getModuleDefinitionsByCmHandleId(cmHandleId);
168     }
169
170     /**
171      * Retrieve cm handles with details for the given query parameters.
172      *
173      * @param cmHandleQueryApiParameters cm handle query parameters
174      * @return cm handles with details
175      */
176     @Override
177     public Set<NcmpServiceCmHandle> executeCmHandleSearch(final CmHandleQueryApiParameters cmHandleQueryApiParameters) {
178         final CmHandleQueryServiceParameters cmHandleQueryServiceParameters = jsonObjectMapper.convertToValueType(
179                 cmHandleQueryApiParameters, CmHandleQueryServiceParameters.class);
180         validateCmHandleQueryParameters(cmHandleQueryServiceParameters, CmHandleQueryConditions.ALL_CONDITION_NAMES);
181         return networkCmProxyCmHandlerQueryService.queryCmHandles(cmHandleQueryServiceParameters);
182     }
183
184     /**
185      * Retrieve cm handle ids for the given query parameters.
186      *
187      * @param cmHandleQueryApiParameters cm handle query parameters
188      * @return cm handle ids
189      */
190     @Override
191     public Set<String> executeCmHandleIdSearch(final CmHandleQueryApiParameters cmHandleQueryApiParameters) {
192         final CmHandleQueryServiceParameters cmHandleQueryServiceParameters = jsonObjectMapper.convertToValueType(
193                 cmHandleQueryApiParameters, CmHandleQueryServiceParameters.class);
194         validateCmHandleQueryParameters(cmHandleQueryServiceParameters, CmHandleQueryConditions.ALL_CONDITION_NAMES);
195         return networkCmProxyCmHandlerQueryService.queryCmHandleIds(cmHandleQueryServiceParameters);
196     }
197
198     /**
199      * Set the data sync enabled flag, along with the data sync state
200      * based on the data sync enabled boolean for the cm handle id provided.
201      *
202      * @param cmHandleId      cm handle id
203      * @param dataSyncEnabled data sync enabled flag
204      */
205     @Override
206     public void setDataSyncEnabled(final String cmHandleId, final boolean dataSyncEnabled) {
207         final CompositeState compositeState = inventoryPersistence
208                 .getCmHandleState(cmHandleId);
209         if (compositeState.getDataSyncEnabled().equals(dataSyncEnabled)) {
210             log.info("Data-Sync Enabled flag is already: {} ", dataSyncEnabled);
211         } else if (compositeState.getCmHandleState() != CmHandleState.READY) {
212             throw new CpsException("State mismatch exception.", "Cm-Handle not in READY state. Cm handle state is: "
213                     + compositeState.getCmHandleState());
214         } else {
215             final DataStoreSyncState dataStoreSyncState = compositeState.getDataStores()
216                     .getOperationalDataStore().getDataStoreSyncState();
217             if (!dataSyncEnabled && dataStoreSyncState == DataStoreSyncState.SYNCHRONIZED) {
218                 cpsDataService.deleteDataNode(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, cmHandleId,
219                         "/netconf-state", OffsetDateTime.now());
220             }
221             CompositeStateUtils.setDataSyncEnabledFlagWithDataSyncState(dataSyncEnabled, compositeState);
222             inventoryPersistence.saveCmHandleState(cmHandleId,
223                     compositeState);
224         }
225     }
226
227     /**
228      * Get all cm handle IDs by DMI plugin identifier.
229      *
230      * @param dmiPluginIdentifier DMI plugin identifier
231      * @return set of cm handle IDs
232      */
233     @Override
234     public Set<String> getAllCmHandleIdsByDmiPluginIdentifier(final String dmiPluginIdentifier) {
235         final Set<NcmpServiceCmHandle> ncmpServiceCmHandles =
236                 cmHandleQueries.getCmHandlesByDmiPluginIdentifier(dmiPluginIdentifier);
237         final Set<String> cmHandleIds = new HashSet<>(ncmpServiceCmHandles.size());
238         ncmpServiceCmHandles.forEach(cmHandle -> cmHandleIds.add(cmHandle.getCmHandleId()));
239         return cmHandleIds;
240     }
241
242     /**
243      * Get all cm handle IDs by various properties.
244      *
245      * @param cmHandleQueryServiceParameters cm handle query parameters
246      * @return set of cm handle IDs
247      */
248     @Override
249     public Set<String> executeCmHandleIdSearchForInventory(
250             final CmHandleQueryServiceParameters cmHandleQueryServiceParameters) {
251         validateCmHandleQueryParameters(cmHandleQueryServiceParameters, InventoryQueryConditions.ALL_CONDITION_NAMES);
252         return networkCmProxyCmHandlerQueryService.queryCmHandleIdsForInventory(cmHandleQueryServiceParameters);
253     }
254
255     /**
256      * Retrieve cm handle details for a given cm handle.
257      *
258      * @param cmHandleId cm handle identifier
259      * @return cm handle details
260      */
261     @Override
262     public NcmpServiceCmHandle getNcmpServiceCmHandle(final String cmHandleId) {
263         return YangDataConverter.convertYangModelCmHandleToNcmpServiceCmHandle(
264                 inventoryPersistence.getYangModelCmHandle(cmHandleId));
265     }
266
267     /**
268      * Get cm handle public properties for a given cm handle id.
269      *
270      * @param cmHandleId cm handle identifier
271      * @return cm handle public properties
272      */
273     @Override
274     public Map<String, String> getCmHandlePublicProperties(final String cmHandleId) {
275         final YangModelCmHandle yangModelCmHandle =
276                 inventoryPersistence.getYangModelCmHandle(cmHandleId);
277         final List<YangModelCmHandle.Property> yangModelPublicProperties = yangModelCmHandle.getPublicProperties();
278         final Map<String, String> cmHandlePublicProperties = new HashMap<>();
279         YangDataConverter.asPropertiesMap(yangModelPublicProperties, cmHandlePublicProperties);
280         return cmHandlePublicProperties;
281     }
282
283     /**
284      * Get cm handle composite state for a given cm handle id.
285      *
286      * @param cmHandleId cm handle identifier
287      * @return cm handle state
288      */
289     @Override
290     public CompositeState getCmHandleCompositeState(final String cmHandleId) {
291         return inventoryPersistence.getYangModelCmHandle(cmHandleId).getCompositeState();
292     }
293
294     /**
295      * THis method registers a cm handle and initiates modules sync.
296      *
297      * @param dmiPluginRegistration dmi plugin registration information.
298      * @return cm-handle registration response for create cm-handle requests.
299      */
300     public List<CmHandleRegistrationResponse> parseAndCreateCmHandlesInDmiRegistrationAndSyncModules(
301             final DmiPluginRegistration dmiPluginRegistration) {
302         List<CmHandleRegistrationResponse> cmHandleRegistrationResponses = new ArrayList<>();
303         final Map<YangModelCmHandle, CmHandleState> cmHandleStatePerCmHandle = new HashMap<>();
304         try {
305             dmiPluginRegistration.getCreatedCmHandles()
306                     .forEach(cmHandle -> {
307                         final YangModelCmHandle yangModelCmHandle = YangModelCmHandle.toYangModelCmHandle(
308                                 dmiPluginRegistration.getDmiPlugin(),
309                                 dmiPluginRegistration.getDmiDataPlugin(),
310                                 dmiPluginRegistration.getDmiModelPlugin(),
311                                 cmHandle);
312                         cmHandleStatePerCmHandle.put(yangModelCmHandle, CmHandleState.ADVISED);
313                     });
314             cmHandleRegistrationResponses = registerNewCmHandles(cmHandleStatePerCmHandle);
315         } catch (final DataValidationException dataValidationException) {
316             cmHandleRegistrationResponses.add(CmHandleRegistrationResponse.createFailureResponse(dmiPluginRegistration
317                             .getCreatedCmHandles().stream()
318                             .map(NcmpServiceCmHandle::getCmHandleId).findFirst().orElse(null),
319                     RegistrationError.CM_HANDLE_INVALID_ID));
320         }
321         return cmHandleRegistrationResponses;
322     }
323
324     protected List<CmHandleRegistrationResponse> parseAndRemoveCmHandlesInDmiRegistration(
325             final List<String> tobeRemovedCmHandles) {
326         final List<CmHandleRegistrationResponse> cmHandleRegistrationResponses =
327                 new ArrayList<>(tobeRemovedCmHandles.size());
328
329         setState(tobeRemovedCmHandles, CmHandleState.DELETING);
330
331         for (final String cmHandleId : tobeRemovedCmHandles) {
332             try {
333                 deleteCmHandleFromDbAndModuleSyncMap(cmHandleId);
334                 cmHandleRegistrationResponses.add(CmHandleRegistrationResponse.createSuccessResponse(cmHandleId));
335             } catch (final DataNodeNotFoundException dataNodeNotFoundException) {
336                 log.error("Unable to find dataNode for cmHandleId : {} , caused by : {}",
337                         cmHandleId, dataNodeNotFoundException.getMessage());
338                 cmHandleRegistrationResponses.add(CmHandleRegistrationResponse
339                         .createFailureResponse(cmHandleId, RegistrationError.CM_HANDLE_DOES_NOT_EXIST));
340             } catch (final DataValidationException dataValidationException) {
341                 log.error("Unable to de-register cm-handle id: {}, caused by: {}",
342                         cmHandleId, dataValidationException.getMessage());
343                 cmHandleRegistrationResponses.add(CmHandleRegistrationResponse
344                         .createFailureResponse(cmHandleId, RegistrationError.CM_HANDLE_INVALID_ID));
345             } catch (final Exception exception) {
346                 log.error("Unable to de-register cm-handle id : {} , caused by : {}",
347                         cmHandleId, exception.getMessage());
348                 cmHandleRegistrationResponses.add(
349                         CmHandleRegistrationResponse.createFailureResponse(cmHandleId, exception));
350             }
351         }
352
353         setState(tobeRemovedCmHandles, CmHandleState.DELETED);
354
355         return cmHandleRegistrationResponses;
356     }
357
358     private void setState(final List<String> tobeRemovedCmHandles, final CmHandleState cmHandleState) {
359         final Map<YangModelCmHandle, CmHandleState> cmHandleIdsToBeRemoved = new HashMap<>();
360         for (final String cmHandleId : tobeRemovedCmHandles) {
361             cmHandleIdsToBeRemoved.put(
362                     inventoryPersistence.getYangModelCmHandle(cmHandleId),
363                     cmHandleState);
364         }
365         lcmEventsCmHandleStateHandler.updateCmHandleStateBatch(cmHandleIdsToBeRemoved);
366     }
367
368     private void deleteCmHandleFromDbAndModuleSyncMap(final String cmHandleId) {
369         inventoryPersistence.deleteSchemaSetWithCascade(cmHandleId);
370         inventoryPersistence.deleteListOrListElement("/dmi-registry/cm-handles[@id='" + cmHandleId + "']");
371         removeDeletedCmHandleFromModuleSyncMap(cmHandleId);
372     }
373
374     // CPS-1239 Robustness cleaning of in progress cache
375     private void removeDeletedCmHandleFromModuleSyncMap(final String deletedCmHandleId) {
376         if (moduleSyncStartedOnCmHandles.remove(deletedCmHandleId) != null) {
377             log.debug("{} removed from in progress map", deletedCmHandleId);
378         }
379     }
380
381     private List<CmHandleRegistrationResponse> registerNewCmHandles(final Map<YangModelCmHandle, CmHandleState>
382                                                                             cmHandleStatePerCmHandle) {
383         final List<String> cmHandleIds = cmHandleStatePerCmHandle.keySet().stream().map(YangModelCmHandle::getId)
384                 .collect(Collectors.toList());
385         try {
386             lcmEventsCmHandleStateHandler.updateCmHandleStateBatch(cmHandleStatePerCmHandle);
387             return CmHandleRegistrationResponse.createSuccessResponses(cmHandleIds);
388         } catch (final AlreadyDefinedExceptionBatch alreadyDefinedExceptionBatch) {
389             return CmHandleRegistrationResponse.createFailureResponses(
390                     alreadyDefinedExceptionBatch.getAlreadyDefinedXpaths(),
391                     RegistrationError.CM_HANDLE_ALREADY_EXIST);
392         } catch (final Exception exception) {
393             return CmHandleRegistrationResponse.createFailureResponses(cmHandleIds, exception);
394         }
395     }
396 }