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 com.google.gson.Gson;
26 import com.google.gson.GsonBuilder;
27 import java.util.ArrayList;
28 import java.util.Collection;
29 import java.util.Collections;
30 import java.util.List;
31 import java.util.stream.Collectors;
32 import javax.validation.Valid;
33 import javax.validation.constraints.NotNull;
34 import lombok.extern.slf4j.Slf4j;
35 import org.onap.cps.ncmp.api.NetworkCmProxyDataService;
36 import org.onap.cps.ncmp.rest.api.NetworkCmProxyApi;
37 import org.onap.cps.ncmp.rest.model.CmHandleProperties;
38 import org.onap.cps.ncmp.rest.model.CmHandleProperty;
39 import org.onap.cps.ncmp.rest.model.CmHandles;
40 import org.onap.cps.ncmp.rest.model.ConditionProperties;
41 import org.onap.cps.ncmp.rest.model.Conditions;
42 import org.onap.cps.ncmp.rest.model.ModuleNameAsJsonObject;
43 import org.onap.cps.ncmp.rest.model.ModuleNamesAsJsonArray;
44 import org.onap.cps.spi.FetchDescendantsOption;
45 import org.onap.cps.spi.model.DataNode;
46 import org.onap.cps.spi.model.ModuleReference;
47 import org.onap.cps.utils.DataMapUtils;
48 import org.springframework.http.HttpStatus;
49 import org.springframework.http.ResponseEntity;
50 import org.springframework.web.bind.annotation.RequestMapping;
51 import org.springframework.web.bind.annotation.RestController;
55 @RequestMapping("${rest.api.ncmp-base-path}")
56 public class NetworkCmProxyController implements NetworkCmProxyApi {
58 private static final Gson GSON = new GsonBuilder().create();
60 private final NetworkCmProxyDataService networkCmProxyDataService;
63 * Constructor Injection for Dependencies.
64 * @param networkCmProxyDataService Data Service Interface
66 public NetworkCmProxyController(final NetworkCmProxyDataService networkCmProxyDataService) {
67 this.networkCmProxyDataService = networkCmProxyDataService;
72 * @deprecated This Method is no longer used as part of NCMP.
75 @Deprecated(forRemoval = false)
76 public ResponseEntity<Void> createNode(final String cmHandle, @Valid final String jsonData,
77 @Valid final String parentNodeXpath) {
78 networkCmProxyDataService.createDataNode(cmHandle, parentNodeXpath, jsonData);
79 return new ResponseEntity<>(HttpStatus.CREATED);
83 * Add List-node Child Element.
84 * @deprecated This Method is no longer used as part of NCMP.
87 @Deprecated(forRemoval = false)
88 public ResponseEntity<Void> addListNodeElements(@NotNull @Valid final String parentNodeXpath,
89 final String cmHandle, @Valid final String jsonData) {
90 networkCmProxyDataService.addListNodeElements(cmHandle, parentNodeXpath, jsonData);
91 return new ResponseEntity<>(HttpStatus.CREATED);
95 * Get Node By CM Handle and X-Path.
96 * @deprecated This Method is no longer used as part of NCMP.
99 @Deprecated(forRemoval = false)
100 public ResponseEntity<Object> getNodeByCmHandleAndXpath(final String cmHandle, @Valid final String xpath,
101 @Valid final Boolean includeDescendants) {
102 final FetchDescendantsOption fetchDescendantsOption = Boolean.TRUE.equals(includeDescendants)
103 ? FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS : FetchDescendantsOption.OMIT_DESCENDANTS;
104 final var dataNode = networkCmProxyDataService.getDataNode(cmHandle, xpath, fetchDescendantsOption);
105 return new ResponseEntity<>(DataMapUtils.toDataMap(dataNode), HttpStatus.OK);
110 * @deprecated This Method is no longer used as part of NCMP.
113 @Deprecated(forRemoval = false)
114 public ResponseEntity<Object> queryNodesByCmHandleAndCpsPath(final String cmHandle, @Valid final String cpsPath,
115 @Valid final Boolean includeDescendants) {
116 final FetchDescendantsOption fetchDescendantsOption = Boolean.TRUE.equals(includeDescendants)
117 ? FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS : FetchDescendantsOption.OMIT_DESCENDANTS;
118 final Collection<DataNode> dataNodes =
119 networkCmProxyDataService.queryDataNodes(cmHandle, cpsPath, fetchDescendantsOption);
120 return new ResponseEntity<>(GSON.toJson(dataNodes), HttpStatus.OK);
124 * Replace Node With Descendants.
125 * @deprecated This Method is no longer used as part of NCMP.
128 @Deprecated(forRemoval = false)
129 public ResponseEntity<Object> replaceNode(final String cmHandle, @Valid final String jsonData,
130 @Valid final String parentNodeXpath) {
131 networkCmProxyDataService.replaceNodeTree(cmHandle, parentNodeXpath, jsonData);
132 return new ResponseEntity<>(HttpStatus.OK);
136 public ResponseEntity<Object> updateResourceDataRunningForCmHandle(final String resourceIdentifier,
137 final String cmHandle, final String requestBody, final String contentType) {
138 networkCmProxyDataService.updateResourceDataPassThroughRunningForCmHandle(cmHandle,
139 resourceIdentifier, requestBody, contentType);
140 return new ResponseEntity<>(HttpStatus.OK);
144 * Update Node Leaves.
145 * @deprecated This Method is no longer used as part of NCMP.
148 @Deprecated(forRemoval = false)
149 public ResponseEntity<Object> updateNodeLeaves(final String cmHandle, @Valid final String jsonData,
150 @Valid final String parentNodeXpath) {
151 networkCmProxyDataService.updateNodeLeaves(cmHandle, parentNodeXpath, jsonData);
152 return new ResponseEntity<>(HttpStatus.OK);
156 * Get resource data from operational datastore.
158 * @param cmHandle cm handle identifier
159 * @param resourceIdentifier resource identifier
160 * @param acceptParamInHeader accept header parameter
161 * @param optionsParamInQuery options query parameter
162 * @return {@code ResponseEntity} response from dmi plugin
165 public ResponseEntity<Object> getResourceDataOperationalForCmHandle(final String cmHandle,
166 final @NotNull @Valid String resourceIdentifier,
167 final String acceptParamInHeader,
168 final @Valid String optionsParamInQuery) {
169 final Object responseObject = networkCmProxyDataService.getResourceDataOperationalForCmHandle(cmHandle,
172 optionsParamInQuery);
173 return ResponseEntity.ok(responseObject);
177 * Get resource data from pass-through running datastore.
179 * @param cmHandle cm handle identifier
180 * @param resourceIdentifier resource identifier
181 * @param acceptParamInHeader accept header parameter
182 * @param optionsParamInQuery options query parameter
183 * @return {@code ResponseEntity} response from dmi plugin
186 public ResponseEntity<Object> getResourceDataRunningForCmHandle(final String cmHandle,
187 final @NotNull @Valid String resourceIdentifier,
188 final String acceptParamInHeader,
189 final @Valid String optionsParamInQuery) {
190 final Object responseObject = networkCmProxyDataService.getResourceDataPassThroughRunningForCmHandle(cmHandle,
193 optionsParamInQuery);
194 return ResponseEntity.ok(responseObject);
198 * Create resource data in datastore pass through running
199 * for given cm-handle.
201 * @param resourceIdentifier resource identifier
202 * @param cmHandle cm handle identifier
203 * @param requestBody requestBody
204 * @param contentType content type of body
205 * @return {@code ResponseEntity} response from dmi plugi
208 public ResponseEntity<Void> createResourceDataRunningForCmHandle(final String resourceIdentifier,
209 final String cmHandle,
210 final String requestBody,
211 final String contentType) {
212 networkCmProxyDataService.createResourceDataPassThroughRunningForCmHandle(cmHandle,
213 resourceIdentifier, requestBody, contentType);
214 return new ResponseEntity<>(HttpStatus.CREATED);
218 public ResponseEntity<CmHandles> executeCmHandleSearch(final Conditions conditions) {
219 final List<ConditionProperties> conditionProperties =
220 conditions.getConditions().stream().collect(Collectors.toList());
221 final CmHandles cmHandles = new CmHandles();
222 final Collection<String> cmHandleIdentifiers = processConditions(conditionProperties);
223 cmHandleIdentifiers.forEach(cmHandle -> cmHandles.setCmHandles(toCmHandleProperties(cmHandle)));
224 return ResponseEntity.ok(cmHandles);
228 public ResponseEntity<Object> getModuleReferencesByCmHandle(final String cmHandle) {
229 final Collection<ModuleReference>
230 moduleReferences = networkCmProxyDataService.getYangResourcesModuleReferences(cmHandle);
231 return new ResponseEntity<>(new Gson().toJson(moduleReferences), HttpStatus.OK);
234 private Collection<String> processConditions(final List<ConditionProperties> conditionProperties) {
235 for (final ConditionProperties conditionProperty : conditionProperties) {
236 if (conditionProperty.getName().equals("hasAllModules")) {
237 return executeCmHandleSearchesForModuleNames(conditionProperty);
239 log.warn("Unrecognized condition name {}.", conditionProperty.getName());
242 log.warn("No valid conditions found {}.", conditionProperties);
243 return Collections.emptyList();
246 private Collection<String> executeCmHandleSearchesForModuleNames(final ConditionProperties conditionProperties) {
247 return networkCmProxyDataService
248 .executeCmHandleHasAllModulesSearch(getModuleNames(conditionProperties.getConditionParameters()));
251 private Collection<String> getModuleNames(final ModuleNamesAsJsonArray moduleNamesAsJsonArray) {
252 final Collection<String> moduleNames = new ArrayList<>(moduleNamesAsJsonArray.size());
253 for (final ModuleNameAsJsonObject moduleNameAsJsonObject : moduleNamesAsJsonArray) {
254 moduleNames.add(moduleNameAsJsonObject.getModuleName());
259 private CmHandleProperties toCmHandleProperties(final String cmHandleId) {
260 final CmHandleProperties cmHandleProperties = new CmHandleProperties();
261 final CmHandleProperty cmHandleProperty = new CmHandleProperty();
262 cmHandleProperty.setCmHandleId(cmHandleId);
263 cmHandleProperties.add(cmHandleProperty);
264 return cmHandleProperties;