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();
65 private static final String NO_BODY = null;
67 private final ModelMapper modelMapper = new ModelMapper();
68 private final NetworkCmProxyDataService networkCmProxyDataService;
71 * Constructor Injection for Dependencies.
72 * @param networkCmProxyDataService Data Service Interface
74 public NetworkCmProxyController(final NetworkCmProxyDataService networkCmProxyDataService) {
75 this.networkCmProxyDataService = networkCmProxyDataService;
80 * @deprecated This Method is no longer used as part of NCMP.
82 // All deprecated APIs methods will be address into https://jira.onap.org/browse/CPS-642
84 @Deprecated(forRemoval = false)
85 public ResponseEntity<Void> createNode(final String cmHandle, @Valid final Object jsonData,
86 @Valid final String parentNodeXpath) {
87 networkCmProxyDataService.createDataNode(cmHandle, parentNodeXpath, GSON.toJson(jsonData));
88 return new ResponseEntity<>(HttpStatus.CREATED);
92 * Add List-node Child Element.
93 * @deprecated This Method is no longer used as part of NCMP.
95 // All deprecated APIs methods will be address into https://jira.onap.org/browse/CPS-642
97 @Deprecated(forRemoval = false)
98 public ResponseEntity<Void> addListNodeElements(@NotNull @Valid final String parentNodeXpath,
99 final String cmHandle, @Valid final Object jsonData) {
100 networkCmProxyDataService.addListNodeElements(cmHandle, parentNodeXpath, GSON.toJson(jsonData));
101 return new ResponseEntity<>(HttpStatus.CREATED);
105 * Get Node By CM Handle and X-Path.
106 * @deprecated This Method is no longer used as part of NCMP.
108 // All deprecated APIs methods will be address into https://jira.onap.org/browse/CPS-642
110 @Deprecated(forRemoval = false)
111 public ResponseEntity<Object> getNodeByCmHandleAndXpath(final String cmHandle, @Valid final String xpath,
112 @Valid final Boolean includeDescendants) {
113 final FetchDescendantsOption fetchDescendantsOption = Boolean.TRUE.equals(includeDescendants)
114 ? FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS : FetchDescendantsOption.OMIT_DESCENDANTS;
115 final var dataNode = networkCmProxyDataService.getDataNode(cmHandle, xpath, fetchDescendantsOption);
116 return new ResponseEntity<>(DataMapUtils.toDataMap(dataNode), HttpStatus.OK);
121 * @deprecated This Method is no longer used as part of NCMP.
123 // All deprecated APIs methods will be address into https://jira.onap.org/browse/CPS-642
125 @Deprecated(forRemoval = false)
126 public ResponseEntity<Object> queryNodesByCmHandleAndCpsPath(final String cmHandle, @Valid final String cpsPath,
127 @Valid final Boolean includeDescendants) {
128 final FetchDescendantsOption fetchDescendantsOption = Boolean.TRUE.equals(includeDescendants)
129 ? FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS : FetchDescendantsOption.OMIT_DESCENDANTS;
130 final Collection<DataNode> dataNodes =
131 networkCmProxyDataService.queryDataNodes(cmHandle, cpsPath, fetchDescendantsOption);
132 return new ResponseEntity<>(GSON.toJson(dataNodes), HttpStatus.OK);
136 * Replace Node With Descendants.
137 * @deprecated This Method is no longer used as part of NCMP.
139 // All deprecated APIs methods will be address into https://jira.onap.org/browse/CPS-642
141 @Deprecated(forRemoval = false)
142 public ResponseEntity<Object> replaceNode(final String cmHandle, @Valid final Object jsonData,
143 @Valid final String parentNodeXpath) {
144 networkCmProxyDataService.replaceNodeTree(cmHandle, parentNodeXpath, GSON.toJson(jsonData));
145 return new ResponseEntity<>(HttpStatus.OK);
149 * Update Node Leaves.
150 * @deprecated This Method is no longer used as part of NCMP.
152 // All deprecated APIs methods will be address into https://jira.onap.org/browse/CPS-642
154 @Deprecated(forRemoval = false)
155 public ResponseEntity<Object> updateNodeLeaves(final String cmHandle, @Valid final Object jsonData,
156 @Valid final String parentNodeXpath) {
157 networkCmProxyDataService.updateNodeLeaves(cmHandle, parentNodeXpath, GSON.toJson(jsonData));
158 return new ResponseEntity<>(HttpStatus.OK);
162 * Get resource data from operational datastore.
164 * @param cmHandle cm handle identifier
165 * @param resourceIdentifier resource identifier
166 * @param acceptParamInHeader accept header parameter
167 * @param optionsParamInQuery options query parameter
168 * @return {@code ResponseEntity} response from dmi plugin
171 public ResponseEntity<Object> getResourceDataOperationalForCmHandle(final String cmHandle,
172 final @NotNull @Valid String resourceIdentifier,
173 final String acceptParamInHeader,
174 final @Valid String optionsParamInQuery) {
175 final Object responseObject = networkCmProxyDataService.getResourceDataOperationalForCmHandle(cmHandle,
178 optionsParamInQuery);
179 return ResponseEntity.ok(responseObject);
183 * Get resource data from pass-through running datastore.
185 * @param cmHandle cm handle identifier
186 * @param resourceIdentifier resource identifier
187 * @param acceptParamInHeader accept header parameter
188 * @param optionsParamInQuery options query parameter
189 * @return {@code ResponseEntity} response from dmi plugin
192 public ResponseEntity<Object> getResourceDataRunningForCmHandle(final String cmHandle,
193 final @NotNull @Valid String resourceIdentifier,
194 final String acceptParamInHeader,
195 final @Valid String optionsParamInQuery) {
196 final Object responseObject = networkCmProxyDataService.getResourceDataPassThroughRunningForCmHandle(cmHandle,
199 optionsParamInQuery);
200 return ResponseEntity.ok(responseObject);
204 public ResponseEntity<Object> patchResourceDataRunningForCmHandle(final String resourceIdentifier,
205 final String cmHandle,
206 final Object requestBody, final String contentType) {
207 final Object responseObject = networkCmProxyDataService.writeResourceDataPassThroughRunningForCmHandle(cmHandle,
208 resourceIdentifier, PATCH, GSON.toJson(requestBody), contentType);
209 return ResponseEntity.ok(responseObject);
213 * Create resource data in datastore pass-through running for given cm-handle.
215 * @param resourceIdentifier resource identifier
216 * @param cmHandle cm handle identifier
217 * @param requestBody the request body
218 * @param contentType content type of body
219 * @return {@code ResponseEntity} response from dmi plugin
222 public ResponseEntity<Void> createResourceDataRunningForCmHandle(final String resourceIdentifier,
223 final String cmHandle, final Object requestBody, final String contentType) {
224 networkCmProxyDataService.writeResourceDataPassThroughRunningForCmHandle(cmHandle,
225 resourceIdentifier, CREATE, GSON.toJson(requestBody), contentType);
226 return new ResponseEntity<>(HttpStatus.CREATED);
230 * Update resource data in datastore pass-through running for given cm-handle.
232 * @param resourceIdentifier resource identifier
233 * @param cmHandle cm handle identifier
234 * @param requestBody the request body
235 * @param contentType content type of the body
236 * @return response entity
239 public ResponseEntity<Object> updateResourceDataRunningForCmHandle(final String resourceIdentifier,
240 final String cmHandle,
241 final Object requestBody,
242 final String contentType) {
243 networkCmProxyDataService.writeResourceDataPassThroughRunningForCmHandle(cmHandle,
244 resourceIdentifier, UPDATE, GSON.toJson(requestBody), contentType);
245 return new ResponseEntity<>(HttpStatus.OK);
250 * Delete resource data in datastore pass-through running for a given cm-handle.
252 * @param resourceIdentifier resource identifier
253 * @param cmHandle cm handle identifier
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 cmHandle,
259 final String resourceIdentifier,
260 final String contentType) {
261 networkCmProxyDataService.writeResourceDataPassThroughRunningForCmHandle(cmHandle,
262 resourceIdentifier, DELETE, NO_BODY, contentType);
263 return new ResponseEntity<>(HttpStatus.NO_CONTENT);
267 * Execute cm handle search.
269 * @param conditions the conditions
270 * @return cm handles returned from search.
273 public ResponseEntity<CmHandles> executeCmHandleSearch(final Conditions conditions) {
274 final List<ConditionProperties> conditionProperties =
275 conditions.getConditions().stream().collect(Collectors.toList());
276 final CmHandles cmHandles = new CmHandles();
277 cmHandles.setCmHandles(toCmHandleProperties(processConditions(conditionProperties)));
278 return ResponseEntity.ok(cmHandles);
282 * Return module references for a cm handle.
284 * @param cmHandle the cm handle
285 * @return module references for cm handle
287 public ResponseEntity<List<ModuleReference>> getModuleReferencesByCmHandle(final String cmHandle) {
288 final List<ModuleReference> moduleReferences =
289 networkCmProxyDataService.getYangResourcesModuleReferences(cmHandle).stream()
290 .map(moduleReference -> modelMapper.map(moduleReference, ModuleReference.class))
291 .collect(Collectors.toList());
292 return new ResponseEntity<>(moduleReferences, HttpStatus.OK);
295 private Collection<String> processConditions(final List<ConditionProperties> conditionProperties) {
296 for (final ConditionProperties conditionProperty : conditionProperties) {
297 if (conditionProperty.getName().equals("hasAllModules")) {
298 return executeCmHandleSearchesForModuleNames(conditionProperty);
300 log.warn("Unrecognized condition name {}.", conditionProperty.getName());
303 log.warn("No valid conditions found {}.", conditionProperties);
304 return Collections.emptyList();
307 private Collection<String> executeCmHandleSearchesForModuleNames(final ConditionProperties conditionProperties) {
308 return networkCmProxyDataService
309 .executeCmHandleHasAllModulesSearch(getModuleNames(conditionProperties.getConditionParameters()));
312 private Collection<String> getModuleNames(final ModuleNamesAsJsonArray moduleNamesAsJsonArray) {
313 final Collection<String> moduleNames = new ArrayList<>(moduleNamesAsJsonArray.size());
314 for (final ModuleNameAsJsonObject moduleNameAsJsonObject : moduleNamesAsJsonArray) {
315 moduleNames.add(moduleNameAsJsonObject.getModuleName());
320 private CmHandleProperties toCmHandleProperties(final Collection<String> cmHandleIdentifiers) {
321 final CmHandleProperties cmHandleProperties = new CmHandleProperties();
322 for (final String cmHandleIdentifier : cmHandleIdentifiers) {
323 final CmHandleProperty cmHandleProperty = new CmHandleProperty();
324 cmHandleProperty.setCmHandleId(cmHandleIdentifier);
325 cmHandleProperties.add(cmHandleProperty);
327 return cmHandleProperties;