2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021 Pantheon.tech
4 * Modifications (C) 2021 Nordix Foundation
5 * Modification Copyright (C) 2021 highstreet technologies GmbH
6 * Modifications (C) 2021 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
12 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.cps.ncmp.rest.controller;
25 import static org.onap.cps.ncmp.api.impl.operations.DmiRequestBody.OperationEnum.CREATE;
26 import static org.onap.cps.ncmp.api.impl.operations.DmiRequestBody.OperationEnum.DELETE;
27 import static org.onap.cps.ncmp.api.impl.operations.DmiRequestBody.OperationEnum.PATCH;
28 import static org.onap.cps.ncmp.api.impl.operations.DmiRequestBody.OperationEnum.UPDATE;
30 import com.google.gson.Gson;
31 import com.google.gson.GsonBuilder;
32 import java.util.ArrayList;
33 import java.util.Collection;
34 import java.util.Collections;
35 import java.util.List;
36 import java.util.stream.Collectors;
37 import javax.validation.Valid;
38 import javax.validation.constraints.NotNull;
39 import lombok.extern.slf4j.Slf4j;
40 import org.modelmapper.ModelMapper;
41 import org.onap.cps.ncmp.api.NetworkCmProxyDataService;
42 import org.onap.cps.ncmp.rest.api.NetworkCmProxyApi;
43 import org.onap.cps.ncmp.rest.model.CmHandleProperties;
44 import org.onap.cps.ncmp.rest.model.CmHandleProperty;
45 import org.onap.cps.ncmp.rest.model.CmHandles;
46 import org.onap.cps.ncmp.rest.model.ConditionProperties;
47 import org.onap.cps.ncmp.rest.model.Conditions;
48 import org.onap.cps.ncmp.rest.model.ModuleNameAsJsonObject;
49 import org.onap.cps.ncmp.rest.model.ModuleNamesAsJsonArray;
50 import org.onap.cps.ncmp.rest.model.ModuleReference;
51 import org.onap.cps.spi.FetchDescendantsOption;
52 import org.onap.cps.spi.model.DataNode;
53 import org.onap.cps.utils.DataMapUtils;
54 import org.springframework.http.HttpStatus;
55 import org.springframework.http.ResponseEntity;
56 import org.springframework.web.bind.annotation.RequestMapping;
57 import org.springframework.web.bind.annotation.RestController;
61 @RequestMapping("${rest.api.ncmp-base-path}")
62 public class NetworkCmProxyController implements NetworkCmProxyApi {
64 private static final Gson GSON = new GsonBuilder().create();
66 private final ModelMapper modelMapper = new ModelMapper();
67 private final NetworkCmProxyDataService networkCmProxyDataService;
70 * Constructor Injection for Dependencies.
71 * @param networkCmProxyDataService Data Service Interface
73 public NetworkCmProxyController(final NetworkCmProxyDataService networkCmProxyDataService) {
74 this.networkCmProxyDataService = networkCmProxyDataService;
79 * @deprecated This Method is no longer used as part of NCMP.
81 // All deprecated APIs methods will be address into https://jira.onap.org/browse/CPS-642
83 @Deprecated(forRemoval = false)
84 public ResponseEntity<Void> createNode(final String cmHandle, @Valid final Object jsonData,
85 @Valid final String parentNodeXpath) {
86 networkCmProxyDataService.createDataNode(cmHandle, parentNodeXpath, GSON.toJson(jsonData));
87 return new ResponseEntity<>(HttpStatus.CREATED);
91 * Add List-node Child Element.
92 * @deprecated This Method is no longer used as part of NCMP.
94 // All deprecated APIs methods will be address into https://jira.onap.org/browse/CPS-642
96 @Deprecated(forRemoval = false)
97 public ResponseEntity<Void> addListNodeElements(@NotNull @Valid final String parentNodeXpath,
98 final String cmHandle, @Valid final Object jsonData) {
99 networkCmProxyDataService.addListNodeElements(cmHandle, parentNodeXpath, GSON.toJson(jsonData));
100 return new ResponseEntity<>(HttpStatus.CREATED);
104 * Get Node By CM Handle and X-Path.
105 * @deprecated This Method is no longer used as part of NCMP.
107 // All deprecated APIs methods will be address into https://jira.onap.org/browse/CPS-642
109 @Deprecated(forRemoval = false)
110 public ResponseEntity<Object> getNodeByCmHandleAndXpath(final String cmHandle, @Valid final String xpath,
111 @Valid final Boolean includeDescendants) {
112 final FetchDescendantsOption fetchDescendantsOption = Boolean.TRUE.equals(includeDescendants)
113 ? FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS : FetchDescendantsOption.OMIT_DESCENDANTS;
114 final var dataNode = networkCmProxyDataService.getDataNode(cmHandle, xpath, fetchDescendantsOption);
115 return new ResponseEntity<>(DataMapUtils.toDataMap(dataNode), HttpStatus.OK);
120 * @deprecated This Method is no longer used as part of NCMP.
122 // All deprecated APIs methods will be address into https://jira.onap.org/browse/CPS-642
124 @Deprecated(forRemoval = false)
125 public ResponseEntity<Object> queryNodesByCmHandleAndCpsPath(final String cmHandle, @Valid final String cpsPath,
126 @Valid final Boolean includeDescendants) {
127 final FetchDescendantsOption fetchDescendantsOption = Boolean.TRUE.equals(includeDescendants)
128 ? FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS : FetchDescendantsOption.OMIT_DESCENDANTS;
129 final Collection<DataNode> dataNodes =
130 networkCmProxyDataService.queryDataNodes(cmHandle, cpsPath, fetchDescendantsOption);
131 return new ResponseEntity<>(GSON.toJson(dataNodes), HttpStatus.OK);
135 * Replace Node With Descendants.
136 * @deprecated This Method is no longer used as part of NCMP.
138 // All deprecated APIs methods will be address into https://jira.onap.org/browse/CPS-642
140 @Deprecated(forRemoval = false)
141 public ResponseEntity<Object> replaceNode(final String cmHandle, @Valid final Object jsonData,
142 @Valid final String parentNodeXpath) {
143 networkCmProxyDataService.replaceNodeTree(cmHandle, parentNodeXpath, GSON.toJson(jsonData));
144 return new ResponseEntity<>(HttpStatus.OK);
148 * Update Node Leaves.
149 * @deprecated This Method is no longer used as part of NCMP.
151 // All deprecated APIs methods will be address into https://jira.onap.org/browse/CPS-642
153 @Deprecated(forRemoval = false)
154 public ResponseEntity<Object> updateNodeLeaves(final String cmHandle, @Valid final Object jsonData,
155 @Valid final String parentNodeXpath) {
156 networkCmProxyDataService.updateNodeLeaves(cmHandle, parentNodeXpath, GSON.toJson(jsonData));
157 return new ResponseEntity<>(HttpStatus.OK);
161 * Get resource data from operational datastore.
163 * @param cmHandle cm handle identifier
164 * @param resourceIdentifier resource identifier
165 * @param acceptParamInHeader accept header parameter
166 * @param optionsParamInQuery options query parameter
167 * @return {@code ResponseEntity} response from dmi plugin
170 public ResponseEntity<Object> getResourceDataOperationalForCmHandle(final String cmHandle,
171 final @NotNull @Valid String resourceIdentifier,
172 final String acceptParamInHeader,
173 final @Valid String optionsParamInQuery) {
174 final Object responseObject = networkCmProxyDataService.getResourceDataOperationalForCmHandle(cmHandle,
177 optionsParamInQuery);
178 return ResponseEntity.ok(responseObject);
182 * Get resource data from pass-through running datastore.
184 * @param cmHandle cm handle identifier
185 * @param resourceIdentifier resource identifier
186 * @param acceptParamInHeader accept header parameter
187 * @param optionsParamInQuery options query parameter
188 * @return {@code ResponseEntity} response from dmi plugin
191 public ResponseEntity<Object> getResourceDataRunningForCmHandle(final String cmHandle,
192 final @NotNull @Valid String resourceIdentifier,
193 final String acceptParamInHeader,
194 final @Valid String optionsParamInQuery) {
195 final Object responseObject = networkCmProxyDataService.getResourceDataPassThroughRunningForCmHandle(cmHandle,
198 optionsParamInQuery);
199 return ResponseEntity.ok(responseObject);
203 public ResponseEntity<Object> patchResourceDataRunningForCmHandle(final String resourceIdentifier,
204 final String cmHandle,
205 final Object requestBody, final String contentType) {
206 networkCmProxyDataService.writeResourceDataPassThroughRunningForCmHandle(cmHandle,
207 resourceIdentifier, PATCH, GSON.toJson(requestBody), contentType);
208 return new ResponseEntity<>(HttpStatus.OK);
212 * Create resource data in datastore pass-through running for given cm-handle.
214 * @param resourceIdentifier resource identifier
215 * @param cmHandle cm handle identifier
216 * @param requestBody the request body
217 * @param contentType content type of body
218 * @return {@code ResponseEntity} response from dmi plugin
221 public ResponseEntity<Void> createResourceDataRunningForCmHandle(final String resourceIdentifier,
222 final String cmHandle, final Object requestBody, final String contentType) {
223 networkCmProxyDataService.writeResourceDataPassThroughRunningForCmHandle(cmHandle,
224 resourceIdentifier, CREATE, GSON.toJson(requestBody), contentType);
225 return new ResponseEntity<>(HttpStatus.CREATED);
229 * Update resource data in datastore pass-through running for given cm-handle.
231 * @param resourceIdentifier resource identifier
232 * @param cmHandle cm handle identifier
233 * @param requestBody the request body
234 * @param contentType content type of the body
235 * @return response entity
238 public ResponseEntity<Object> updateResourceDataRunningForCmHandle(final String resourceIdentifier,
239 final String cmHandle,
240 final Object requestBody,
241 final String contentType) {
242 networkCmProxyDataService.writeResourceDataPassThroughRunningForCmHandle(cmHandle,
243 resourceIdentifier, UPDATE, GSON.toJson(requestBody), contentType);
244 return new ResponseEntity<>(HttpStatus.OK);
249 * Delete resource data in datastore pass-through running for a given cm-handle.
251 * @param resourceIdentifier resource identifier
252 * @param cmHandle cm handle identifier
253 * @param requestBody the request body
254 * @param contentType content type of the body
255 * @return response entity no content if request is successful
258 public ResponseEntity<Void> deleteResourceDataRunningForCmHandle(final String resourceIdentifier,
259 final String cmHandle,
260 final Object requestBody,
261 final String contentType) {
263 networkCmProxyDataService.writeResourceDataPassThroughRunningForCmHandle(cmHandle,
264 resourceIdentifier, DELETE, GSON.toJson(requestBody), contentType);
265 return new ResponseEntity<>(HttpStatus.NO_CONTENT);
269 * Execute cm handle search.
271 * @param conditions the conditions
272 * @return cm handles returned from search.
275 public ResponseEntity<CmHandles> executeCmHandleSearch(final Conditions conditions) {
276 final List<ConditionProperties> conditionProperties =
277 conditions.getConditions().stream().collect(Collectors.toList());
278 final CmHandles cmHandles = new CmHandles();
279 cmHandles.setCmHandles(toCmHandleProperties(processConditions(conditionProperties)));
280 return ResponseEntity.ok(cmHandles);
284 * Return module references for a cm handle.
286 * @param cmHandle the cm handle
287 * @return module references for cm handle
289 public ResponseEntity<List<ModuleReference>> getModuleReferencesByCmHandle(final String cmHandle) {
290 final List<ModuleReference> moduleReferences =
291 networkCmProxyDataService.getYangResourcesModuleReferences(cmHandle).stream()
292 .map(moduleReference -> modelMapper.map(moduleReference, ModuleReference.class))
293 .collect(Collectors.toList());
294 return new ResponseEntity<>(moduleReferences, HttpStatus.OK);
297 private Collection<String> processConditions(final List<ConditionProperties> conditionProperties) {
298 for (final ConditionProperties conditionProperty : conditionProperties) {
299 if (conditionProperty.getName().equals("hasAllModules")) {
300 return executeCmHandleSearchesForModuleNames(conditionProperty);
302 log.warn("Unrecognized condition name {}.", conditionProperty.getName());
305 log.warn("No valid conditions found {}.", conditionProperties);
306 return Collections.emptyList();
309 private Collection<String> executeCmHandleSearchesForModuleNames(final ConditionProperties conditionProperties) {
310 return networkCmProxyDataService
311 .executeCmHandleHasAllModulesSearch(getModuleNames(conditionProperties.getConditionParameters()));
314 private Collection<String> getModuleNames(final ModuleNamesAsJsonArray moduleNamesAsJsonArray) {
315 final Collection<String> moduleNames = new ArrayList<>(moduleNamesAsJsonArray.size());
316 for (final ModuleNameAsJsonObject moduleNameAsJsonObject : moduleNamesAsJsonArray) {
317 moduleNames.add(moduleNameAsJsonObject.getModuleName());
322 private CmHandleProperties toCmHandleProperties(final Collection<String> cmHandleIdentifiers) {
323 final CmHandleProperties cmHandleProperties = new CmHandleProperties();
324 for (final String cmHandleIdentifier : cmHandleIdentifiers) {
325 final CmHandleProperty cmHandleProperty = new CmHandleProperty();
326 cmHandleProperty.setCmHandleId(cmHandleIdentifier);
327 cmHandleProperties.add(cmHandleProperty);
329 return cmHandleProperties;