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.
82 @Deprecated(forRemoval = false)
83 public ResponseEntity<Void> createNode(final String cmHandle, @Valid final Object jsonData,
84 @Valid final String parentNodeXpath) {
85 networkCmProxyDataService.createDataNode(cmHandle, parentNodeXpath, GSON.toJson(jsonData));
86 return new ResponseEntity<>(HttpStatus.CREATED);
90 * Add List-node Child Element.
91 * @deprecated This Method is no longer used as part of NCMP.
94 @Deprecated(forRemoval = false)
95 public ResponseEntity<Void> addListNodeElements(@NotNull @Valid final String parentNodeXpath,
96 final String cmHandle, @Valid final Object jsonData) {
97 networkCmProxyDataService.addListNodeElements(cmHandle, parentNodeXpath, GSON.toJson(jsonData));
98 return new ResponseEntity<>(HttpStatus.CREATED);
102 * Get Node By CM Handle and X-Path.
103 * @deprecated This Method is no longer used as part of NCMP.
106 @Deprecated(forRemoval = false)
107 public ResponseEntity<Object> getNodeByCmHandleAndXpath(final String cmHandle, @Valid final String xpath,
108 @Valid final Boolean includeDescendants) {
109 final FetchDescendantsOption fetchDescendantsOption = Boolean.TRUE.equals(includeDescendants)
110 ? FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS : FetchDescendantsOption.OMIT_DESCENDANTS;
111 final var dataNode = networkCmProxyDataService.getDataNode(cmHandle, xpath, fetchDescendantsOption);
112 return new ResponseEntity<>(DataMapUtils.toDataMap(dataNode), HttpStatus.OK);
117 * @deprecated This Method is no longer used as part of NCMP.
120 @Deprecated(forRemoval = false)
121 public ResponseEntity<Object> queryNodesByCmHandleAndCpsPath(final String cmHandle, @Valid final String cpsPath,
122 @Valid final Boolean includeDescendants) {
123 final FetchDescendantsOption fetchDescendantsOption = Boolean.TRUE.equals(includeDescendants)
124 ? FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS : FetchDescendantsOption.OMIT_DESCENDANTS;
125 final Collection<DataNode> dataNodes =
126 networkCmProxyDataService.queryDataNodes(cmHandle, cpsPath, fetchDescendantsOption);
127 return new ResponseEntity<>(GSON.toJson(dataNodes), HttpStatus.OK);
131 * Replace Node With Descendants.
132 * @deprecated This Method is no longer used as part of NCMP.
135 @Deprecated(forRemoval = false)
136 public ResponseEntity<Object> replaceNode(final String cmHandle, @Valid final Object jsonData,
137 @Valid final String parentNodeXpath) {
138 networkCmProxyDataService.replaceNodeTree(cmHandle, parentNodeXpath, GSON.toJson(jsonData));
139 return new ResponseEntity<>(HttpStatus.OK);
143 * Update Node Leaves.
144 * @deprecated This Method is no longer used as part of NCMP.
147 @Deprecated(forRemoval = false)
148 public ResponseEntity<Object> updateNodeLeaves(final String cmHandle, @Valid final Object jsonData,
149 @Valid final String parentNodeXpath) {
150 networkCmProxyDataService.updateNodeLeaves(cmHandle, parentNodeXpath, GSON.toJson(jsonData));
151 return new ResponseEntity<>(HttpStatus.OK);
155 * Get resource data from operational datastore.
157 * @param cmHandle cm handle identifier
158 * @param resourceIdentifier resource identifier
159 * @param acceptParamInHeader accept header parameter
160 * @param optionsParamInQuery options query parameter
161 * @return {@code ResponseEntity} response from dmi plugin
164 public ResponseEntity<Object> getResourceDataOperationalForCmHandle(final String cmHandle,
165 final @NotNull @Valid String resourceIdentifier,
166 final String acceptParamInHeader,
167 final @Valid String optionsParamInQuery) {
168 final Object responseObject = networkCmProxyDataService.getResourceDataOperationalForCmHandle(cmHandle,
171 optionsParamInQuery);
172 return ResponseEntity.ok(responseObject);
176 * Get resource data from pass-through running datastore.
178 * @param cmHandle cm handle identifier
179 * @param resourceIdentifier resource identifier
180 * @param acceptParamInHeader accept header parameter
181 * @param optionsParamInQuery options query parameter
182 * @return {@code ResponseEntity} response from dmi plugin
185 public ResponseEntity<Object> getResourceDataRunningForCmHandle(final String cmHandle,
186 final @NotNull @Valid String resourceIdentifier,
187 final String acceptParamInHeader,
188 final @Valid String optionsParamInQuery) {
189 final Object responseObject = networkCmProxyDataService.getResourceDataPassThroughRunningForCmHandle(cmHandle,
192 optionsParamInQuery);
193 return ResponseEntity.ok(responseObject);
197 public ResponseEntity<Object> patchResourceDataRunningForCmHandle(final String resourceIdentifier,
198 final String cmHandle,
199 final Object requestBody, final String contentType) {
200 networkCmProxyDataService.writeResourceDataPassThroughRunningForCmHandle(cmHandle,
201 resourceIdentifier, PATCH, GSON.toJson(requestBody), contentType);
202 return new ResponseEntity<>(HttpStatus.OK);
206 * Create resource data in datastore pass-through running for given cm-handle.
208 * @param resourceIdentifier resource identifier
209 * @param cmHandle cm handle identifier
210 * @param requestBody the request body
211 * @param contentType content type of body
212 * @return {@code ResponseEntity} response from dmi plugin
215 public ResponseEntity<Void> createResourceDataRunningForCmHandle(final String resourceIdentifier,
216 final String cmHandle, final Object requestBody, final String contentType) {
217 networkCmProxyDataService.writeResourceDataPassThroughRunningForCmHandle(cmHandle,
218 resourceIdentifier, CREATE, GSON.toJson(requestBody), contentType);
219 return new ResponseEntity<>(HttpStatus.CREATED);
223 * Update resource data in datastore pass-through running for given cm-handle.
225 * @param resourceIdentifier resource identifier
226 * @param cmHandle cm handle identifier
227 * @param requestBody the request body
228 * @param contentType content type of the body
229 * @return response entity
232 public ResponseEntity<Object> updateResourceDataRunningForCmHandle(final String resourceIdentifier,
233 final String cmHandle,
234 final Object requestBody,
235 final String contentType) {
236 networkCmProxyDataService.writeResourceDataPassThroughRunningForCmHandle(cmHandle,
237 resourceIdentifier, UPDATE, GSON.toJson(requestBody), contentType);
238 return new ResponseEntity<>(HttpStatus.OK);
243 * Delete resource data in datastore pass-through running for a given cm-handle.
245 * @param resourceIdentifier resource identifier
246 * @param cmHandle cm handle identifier
247 * @param requestBody the request body
248 * @param contentType content type of the body
249 * @return response entity no content if request is successful
252 public ResponseEntity<Void> deleteResourceDataRunningForCmHandle(final String resourceIdentifier,
253 final String cmHandle,
254 final Object requestBody,
255 final String contentType) {
257 networkCmProxyDataService.writeResourceDataPassThroughRunningForCmHandle(cmHandle,
258 resourceIdentifier, DELETE, GSON.toJson(requestBody), contentType);
259 return new ResponseEntity<>(HttpStatus.NO_CONTENT);
263 * Execute cm handle search.
265 * @param conditions the conditions
266 * @return cm handles returned from search.
269 public ResponseEntity<CmHandles> executeCmHandleSearch(final Conditions conditions) {
270 final List<ConditionProperties> conditionProperties =
271 conditions.getConditions().stream().collect(Collectors.toList());
272 final CmHandles cmHandles = new CmHandles();
273 cmHandles.setCmHandles(toCmHandleProperties(processConditions(conditionProperties)));
274 return ResponseEntity.ok(cmHandles);
278 * Return module references for a cm handle.
280 * @param cmHandle the cm handle
281 * @return module references for cm handle
283 public ResponseEntity<List<ModuleReference>> getModuleReferencesByCmHandle(final String cmHandle) {
284 final List<ModuleReference> moduleReferences =
285 networkCmProxyDataService.getYangResourcesModuleReferences(cmHandle).stream()
286 .map(moduleReference -> modelMapper.map(moduleReference, ModuleReference.class))
287 .collect(Collectors.toList());
288 return new ResponseEntity<>(moduleReferences, HttpStatus.OK);
291 private Collection<String> processConditions(final List<ConditionProperties> conditionProperties) {
292 for (final ConditionProperties conditionProperty : conditionProperties) {
293 if (conditionProperty.getName().equals("hasAllModules")) {
294 return executeCmHandleSearchesForModuleNames(conditionProperty);
296 log.warn("Unrecognized condition name {}.", conditionProperty.getName());
299 log.warn("No valid conditions found {}.", conditionProperties);
300 return Collections.emptyList();
303 private Collection<String> executeCmHandleSearchesForModuleNames(final ConditionProperties conditionProperties) {
304 return networkCmProxyDataService
305 .executeCmHandleHasAllModulesSearch(getModuleNames(conditionProperties.getConditionParameters()));
308 private Collection<String> getModuleNames(final ModuleNamesAsJsonArray moduleNamesAsJsonArray) {
309 final Collection<String> moduleNames = new ArrayList<>(moduleNamesAsJsonArray.size());
310 for (final ModuleNameAsJsonObject moduleNameAsJsonObject : moduleNamesAsJsonArray) {
311 moduleNames.add(moduleNameAsJsonObject.getModuleName());
316 private CmHandleProperties toCmHandleProperties(final Collection<String> cmHandleIdentifiers) {
317 final CmHandleProperties cmHandleProperties = new CmHandleProperties();
318 for (final String cmHandleIdentifier : cmHandleIdentifiers) {
319 final CmHandleProperty cmHandleProperty = new CmHandleProperty();
320 cmHandleProperty.setCmHandleId(cmHandleIdentifier);
321 cmHandleProperties.add(cmHandleProperty);
323 return cmHandleProperties;