Handle invalid operations on merged datastore endpoint
[cps.git] / cps-ncmp-rest / src / main / java / org / onap / cps / ncmp / rest / controller / NetworkCmProxyController.java
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2021 Pantheon.tech
4  *  Modifications Copyright (C) 2021-2022 Nordix Foundation
5  *  Modifications Copyright (C) 2021 highstreet technologies GmbH
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.rest.controller;
25
26 import static org.onap.cps.ncmp.api.impl.operations.DmiRequestBody.OperationEnum.CREATE;
27 import static org.onap.cps.ncmp.api.impl.operations.DmiRequestBody.OperationEnum.DELETE;
28 import static org.onap.cps.ncmp.api.impl.operations.DmiRequestBody.OperationEnum.PATCH;
29 import static org.onap.cps.ncmp.api.impl.operations.DmiRequestBody.OperationEnum.UPDATE;
30
31 import java.util.List;
32 import java.util.Set;
33 import java.util.stream.Collectors;
34 import lombok.RequiredArgsConstructor;
35 import lombok.extern.slf4j.Slf4j;
36 import org.onap.cps.ncmp.api.NetworkCmProxyDataService;
37 import org.onap.cps.ncmp.api.inventory.CompositeState;
38 import org.onap.cps.ncmp.api.models.CmHandleQueryApiParameters;
39 import org.onap.cps.ncmp.api.models.NcmpServiceCmHandle;
40 import org.onap.cps.ncmp.rest.api.NetworkCmProxyApi;
41 import org.onap.cps.ncmp.rest.controller.handlers.DatastoreType;
42 import org.onap.cps.ncmp.rest.controller.handlers.NcmpDatastoreResourceRequestHandler;
43 import org.onap.cps.ncmp.rest.controller.handlers.NcmpDatastoreResourceRequestHandlerFactory;
44 import org.onap.cps.ncmp.rest.exceptions.InvalidDatastoreException;
45 import org.onap.cps.ncmp.rest.mapper.CmHandleStateMapper;
46 import org.onap.cps.ncmp.rest.model.CmHandlePublicProperties;
47 import org.onap.cps.ncmp.rest.model.CmHandleQueryParameters;
48 import org.onap.cps.ncmp.rest.model.RestModuleDefinition;
49 import org.onap.cps.ncmp.rest.model.RestModuleReference;
50 import org.onap.cps.ncmp.rest.model.RestOutputCmHandle;
51 import org.onap.cps.ncmp.rest.model.RestOutputCmHandleCompositeState;
52 import org.onap.cps.ncmp.rest.model.RestOutputCmHandlePublicProperties;
53 import org.onap.cps.ncmp.rest.util.DeprecationHelper;
54 import org.onap.cps.utils.JsonObjectMapper;
55 import org.springframework.http.HttpStatus;
56 import org.springframework.http.ResponseEntity;
57 import org.springframework.web.bind.annotation.RequestMapping;
58 import org.springframework.web.bind.annotation.RestController;
59
60 @Slf4j
61 @RestController
62 @RequestMapping("${rest.api.ncmp-base-path}")
63 @RequiredArgsConstructor
64 public class NetworkCmProxyController implements NetworkCmProxyApi {
65
66     private static final String NO_BODY = null;
67     private final NetworkCmProxyDataService networkCmProxyDataService;
68     private final JsonObjectMapper jsonObjectMapper;
69     private final DeprecationHelper deprecationHelper;
70     private final NcmpRestInputMapper ncmpRestInputMapper;
71     private final CmHandleStateMapper cmHandleStateMapper;
72     private final NcmpDatastoreResourceRequestHandlerFactory ncmpDatastoreResourceRequestHandlerFactory;
73
74     /**
75      * Get resource data from datastore.
76      *
77      * @param datastoreName       name of the datastore
78      * @param cmHandle            cm handle identifier
79      * @param resourceIdentifier  resource identifier
80      * @param optionsParamInQuery options query parameter
81      * @param topicParamInQuery   topic query parameter
82      * @param includeDescendants  whether include descendants
83      * @return {@code ResponseEntity} response from dmi plugin
84      */
85
86     @Override
87     public ResponseEntity<Object> getResourceDataForCmHandle(final String datastoreName,
88                                                              final String cmHandle,
89                                                              final String resourceIdentifier,
90                                                              final String optionsParamInQuery,
91                                                              final String topicParamInQuery,
92                                                              final Boolean includeDescendants) {
93
94         final NcmpDatastoreResourceRequestHandler ncmpDatastoreResourceRequestHandler =
95                 ncmpDatastoreResourceRequestHandlerFactory.getNcmpDatastoreResourceRequestHandler(
96                         DatastoreType.fromDatastoreName(datastoreName));
97
98         return ncmpDatastoreResourceRequestHandler.getResourceData(cmHandle, resourceIdentifier,
99                 optionsParamInQuery, topicParamInQuery, includeDescendants);
100     }
101
102     /**
103      * Patch resource data from passthrough-running.
104      *
105      * @param resourceIdentifier resource identifier
106      * @param datastoreName      name of the datastore
107      * @param cmHandle           cm handle identifier
108      * @param requestBody        the request body
109      * @param contentType        content type of body
110      * @return {@code ResponseEntity} response from dmi plugin
111      */
112
113     @Override
114     public ResponseEntity<Object> patchResourceDataRunningForCmHandle(final String resourceIdentifier,
115                                                                       final String datastoreName,
116                                                                       final String cmHandle,
117                                                                       final Object requestBody,
118                                                                       final String contentType) {
119
120         acceptPassthroughRunningOnly(datastoreName);
121
122         final Object responseObject = networkCmProxyDataService
123                 .writeResourceDataPassThroughRunningForCmHandle(
124                         cmHandle, resourceIdentifier, PATCH,
125                         jsonObjectMapper.asJsonString(requestBody), contentType);
126         return ResponseEntity.ok(responseObject);
127     }
128
129     /**
130      * Create resource data in datastore pass-through running for given cm-handle.
131      *
132      * @param resourceIdentifier resource identifier
133      * @param datastoreName      name of the datastore
134      * @param cmHandle           cm handle identifier
135      * @param requestBody        the request body
136      * @param contentType        content type of body
137      * @return {@code ResponseEntity} response from dmi plugin
138      */
139     @Override
140     public ResponseEntity<Void> createResourceDataRunningForCmHandle(final String resourceIdentifier,
141                                                                      final String datastoreName,
142                                                                      final String cmHandle,
143                                                                      final Object requestBody,
144                                                                      final String contentType) {
145
146         acceptPassthroughRunningOnly(datastoreName);
147
148         networkCmProxyDataService.writeResourceDataPassThroughRunningForCmHandle(cmHandle,
149                 resourceIdentifier, CREATE, jsonObjectMapper.asJsonString(requestBody), contentType);
150         return new ResponseEntity<>(HttpStatus.CREATED);
151     }
152
153     /**
154      * Update resource data in datastore pass-through running for given cm-handle.
155      *
156      * @param resourceIdentifier resource identifier
157      * @param datastoreName      name of the datastore
158      * @param cmHandle           cm handle identifier
159      * @param requestBody        the request body
160      * @param contentType        content type of the body
161      * @return response entity
162      */
163
164     @Override
165     public ResponseEntity<Object> updateResourceDataRunningForCmHandle(final String resourceIdentifier,
166                                                                        final String datastoreName,
167                                                                        final String cmHandle,
168                                                                        final Object requestBody,
169                                                                        final String contentType) {
170         acceptPassthroughRunningOnly(datastoreName);
171
172         networkCmProxyDataService.writeResourceDataPassThroughRunningForCmHandle(cmHandle,
173                 resourceIdentifier, UPDATE, jsonObjectMapper.asJsonString(requestBody), contentType);
174         return new ResponseEntity<>(HttpStatus.OK);
175     }
176
177     /**
178      * Delete resource data in datastore pass-through running for a given cm-handle.
179      *
180      * @param datastoreName      name of the datastore
181      * @param cmHandle           cm handle identifier
182      * @param resourceIdentifier resource identifier
183      * @param contentType        content type of the body
184      * @return response entity no content if request is successful
185      */
186     @Override
187     public ResponseEntity<Void> deleteResourceDataRunningForCmHandle(final String datastoreName,
188                                                                      final String cmHandle,
189                                                                      final String resourceIdentifier,
190                                                                      final String contentType) {
191
192         acceptPassthroughRunningOnly(datastoreName);
193
194         networkCmProxyDataService.writeResourceDataPassThroughRunningForCmHandle(cmHandle,
195                 resourceIdentifier, DELETE, NO_BODY, contentType);
196         return new ResponseEntity<>(HttpStatus.NO_CONTENT);
197     }
198
199     /**
200      * Query and return cm handles that match the given query parameters.
201      *
202      * @param cmHandleQueryParameters the cm handle query parameters
203      * @return collection of cm handles
204      */
205     @Override
206     @SuppressWarnings("deprecation") // mapOldConditionProperties method will be removed in Release 12
207     public ResponseEntity<List<RestOutputCmHandle>> searchCmHandles(
208             final CmHandleQueryParameters cmHandleQueryParameters) {
209         final CmHandleQueryApiParameters cmHandleQueryApiParameters =
210                 deprecationHelper.mapOldConditionProperties(cmHandleQueryParameters);
211         final Set<NcmpServiceCmHandle> cmHandles = networkCmProxyDataService
212                 .executeCmHandleSearch(cmHandleQueryApiParameters);
213         final List<RestOutputCmHandle> outputCmHandles =
214                 cmHandles.stream().map(this::toRestOutputCmHandle).collect(Collectors.toList());
215         return ResponseEntity.ok(outputCmHandles);
216     }
217
218     /**
219      * Query and return cm handle ids that match the given query parameters.
220      *
221      * @param cmHandleQueryParameters the cm handle query parameters
222      * @return collection of cm handle ids
223      */
224     @Override
225     public ResponseEntity<List<String>> searchCmHandleIds(
226             final CmHandleQueryParameters cmHandleQueryParameters) {
227         final CmHandleQueryApiParameters cmHandleQueryApiParameters =
228                 jsonObjectMapper.convertToValueType(cmHandleQueryParameters, CmHandleQueryApiParameters.class);
229         final Set<String> cmHandleIds = networkCmProxyDataService.executeCmHandleIdSearch(cmHandleQueryApiParameters);
230         return ResponseEntity.ok(List.copyOf(cmHandleIds));
231     }
232
233     /**
234      * Search for Cm Handle and Properties by Name.
235      *
236      * @param cmHandleId cm-handle identifier
237      * @return cm handle and its properties
238      */
239     @Override
240     public ResponseEntity<RestOutputCmHandle> retrieveCmHandleDetailsById(final String cmHandleId) {
241         final NcmpServiceCmHandle ncmpServiceCmHandle = networkCmProxyDataService.getNcmpServiceCmHandle(cmHandleId);
242         final RestOutputCmHandle restOutputCmHandle = toRestOutputCmHandle(ncmpServiceCmHandle);
243         return ResponseEntity.ok(restOutputCmHandle);
244     }
245
246     /**
247      * Get Cm Handle Properties by Cm Handle Id.
248      *
249      * @param cmHandleId cm-handle identifier
250      * @return cm handle properties
251      */
252     @Override
253     public ResponseEntity<RestOutputCmHandlePublicProperties> getCmHandlePublicPropertiesByCmHandleId(
254             final String cmHandleId) {
255         final CmHandlePublicProperties cmHandlePublicProperties = new CmHandlePublicProperties();
256         cmHandlePublicProperties.add(networkCmProxyDataService.getCmHandlePublicProperties(cmHandleId));
257         final RestOutputCmHandlePublicProperties restOutputCmHandlePublicProperties =
258                 new RestOutputCmHandlePublicProperties();
259         restOutputCmHandlePublicProperties.setPublicCmHandleProperties(cmHandlePublicProperties);
260         return ResponseEntity.ok(restOutputCmHandlePublicProperties);
261     }
262
263     /**
264      * Get Cm Handle State by Cm Handle Id.
265      *
266      * @param cmHandleId cm-handle identifier
267      * @return cm handle state
268      */
269     @Override
270     public ResponseEntity<RestOutputCmHandleCompositeState> getCmHandleStateByCmHandleId(
271             final String cmHandleId) {
272         final CompositeState cmHandleState = networkCmProxyDataService.getCmHandleCompositeState(cmHandleId);
273         final RestOutputCmHandleCompositeState restOutputCmHandleCompositeState =
274                 new RestOutputCmHandleCompositeState();
275         restOutputCmHandleCompositeState.setState(
276                 cmHandleStateMapper.toCmHandleCompositeStateExternalLockReason(cmHandleState));
277         return ResponseEntity.ok(restOutputCmHandleCompositeState);
278     }
279
280     /**
281      * Return module definitions for a cm handle.
282      *
283      * @param cmHandleId cm-handle identifier
284      * @return list of module definitions (module name, revision, yang resource content)
285      */
286     @Override
287     public ResponseEntity<List<RestModuleDefinition>> getModuleDefinitionsByCmHandleId(final String cmHandleId) {
288         final List<RestModuleDefinition> restModuleDefinitions =
289                 networkCmProxyDataService.getModuleDefinitionsByCmHandleId(cmHandleId).stream()
290                         .map(ncmpRestInputMapper::toRestModuleDefinition)
291                         .collect(Collectors.toList());
292         return new ResponseEntity<>(restModuleDefinitions, HttpStatus.OK);
293     }
294
295     /**
296      * Return module references for a cm handle.
297      *
298      * @param cmHandle the cm handle
299      * @return module references for cm handle. Namespace will be always blank because restConf does not include this.
300      */
301     public ResponseEntity<List<RestModuleReference>> getModuleReferencesByCmHandle(final String cmHandle) {
302         final List<RestModuleReference> restModuleReferences =
303                 networkCmProxyDataService.getYangResourcesModuleReferences(cmHandle).stream()
304                         .map(ncmpRestInputMapper::toRestModuleReference)
305                         .collect(Collectors.toList());
306         return new ResponseEntity<>(restModuleReferences, HttpStatus.OK);
307     }
308
309     /**
310      * Set the data sync enabled flag, along with the data sync state for the specified cm handle.
311      *
312      * @param cmHandleId          cm handle id
313      * @param dataSyncEnabledFlag data sync enabled flag
314      * @return response entity ok if request is successful
315      */
316     @Override
317     public ResponseEntity<Object> setDataSyncEnabledFlagForCmHandle(final String cmHandleId,
318                                                                     final Boolean dataSyncEnabledFlag) {
319         networkCmProxyDataService.setDataSyncEnabled(cmHandleId, dataSyncEnabledFlag);
320         return new ResponseEntity<>(HttpStatus.OK);
321     }
322
323
324     private RestOutputCmHandle toRestOutputCmHandle(final NcmpServiceCmHandle ncmpServiceCmHandle) {
325         final RestOutputCmHandle restOutputCmHandle = new RestOutputCmHandle();
326         final CmHandlePublicProperties cmHandlePublicProperties = new CmHandlePublicProperties();
327         restOutputCmHandle.setCmHandle(ncmpServiceCmHandle.getCmHandleId());
328         cmHandlePublicProperties.add(ncmpServiceCmHandle.getPublicProperties());
329         restOutputCmHandle.setPublicCmHandleProperties(cmHandlePublicProperties);
330         restOutputCmHandle.setState(cmHandleStateMapper.toCmHandleCompositeStateExternalLockReason(
331                 ncmpServiceCmHandle.getCompositeState()));
332         return restOutputCmHandle;
333     }
334
335     private void acceptPassthroughRunningOnly(final String datastoreName) {
336         final DatastoreType datastoreType = DatastoreType.fromDatastoreName(datastoreName);
337
338         if (DatastoreType.PASSTHROUGH_RUNNING != datastoreType) {
339             throw new InvalidDatastoreException(datastoreName + " is not supported");
340         }
341     }
342 }
343