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