2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021 Nordix Foundation
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.cps.ncmp.api.impl.operation;
23 import com.fasterxml.jackson.annotation.JsonValue;
25 import org.jetbrains.annotations.NotNull;
26 import org.onap.cps.ncmp.api.impl.client.DmiRestClient;
27 import org.springframework.http.HttpHeaders;
28 import org.springframework.http.ResponseEntity;
29 import org.springframework.stereotype.Component;
32 public class DmiOperations {
35 public enum PassThroughEnum {
36 OPERATIONAL("/ncmp-datastore:passthrough-operational/"),
37 RUNNING("/ncmp-datastore:passthrough-running/");
40 PassThroughEnum(final String value) {
46 public String toString() {
47 return String.valueOf(value);
51 private DmiRestClient dmiRestClient;
52 private static final String PARENT_CM_HANDLE_URI =
53 "/v1/ch/{cmHandle}/data/ds";
54 private final int indexCmHandleInUri;
57 * Constructor for {@code DmiOperations}. This method also manipulates url properties.
59 * @param dmiRestClient {@code DmiRestClient}
61 public DmiOperations(final DmiRestClient dmiRestClient) {
62 this.dmiRestClient = dmiRestClient;
63 indexCmHandleInUri = PARENT_CM_HANDLE_URI.indexOf("{cmHandle}");
67 * This method fetches the resource data from operational data store for given cm handle
68 * identifier on given resource using dmi client.
70 * @param dmiBasePath dmi base path
71 * @param cmHandle network resource identifier
72 * @param resourceId resource identifier
73 * @param fieldsQuery fields query
74 * @param depthQuery depth query
75 * @param acceptParam accept parameter
76 * @param jsonBody json body for put operation
77 * @return {@code ResponseEntity} response entity
79 public ResponseEntity<Object> getResouceDataOperationalFromDmi(final String dmiBasePath,
80 final String cmHandle,
81 final String resourceId,
82 final String fieldsQuery,
83 final Integer depthQuery,
84 final String acceptParam,
85 final String jsonBody) {
86 final var builder = getDmiResourceDataUrl(dmiBasePath, cmHandle, resourceId,
87 fieldsQuery, depthQuery, PassThroughEnum.OPERATIONAL);
88 final var httpHeaders = prepareHeader(acceptParam);
89 return dmiRestClient.putOperationWithJsonData(builder.toString(), jsonBody, httpHeaders);
93 * This method fetches the resource data from pass-through running data store for given cm handle
94 * identifier on given resource using dmi client.
96 * @param dmiBasePath dmi base path
97 * @param cmHandle network resource identifier
98 * @param resourceId resource identifier
99 * @param fieldsQuery fields query
100 * @param depthQuery depth query
101 * @param acceptParam accept parameter
102 * @param jsonBody json body for put operation
103 * @return {@code ResponseEntity} response entity
105 public ResponseEntity<Object> getResouceDataPassThroughRunningFromDmi(final String dmiBasePath,
106 final String cmHandle,
107 final String resourceId,
108 final String fieldsQuery,
109 final Integer depthQuery,
110 final String acceptParam,
111 final String jsonBody) {
112 final var builder = getDmiResourceDataUrl(dmiBasePath, cmHandle, resourceId,
113 fieldsQuery, depthQuery, PassThroughEnum.RUNNING);
114 final var httpHeaders = prepareHeader(acceptParam);
115 return dmiRestClient.putOperationWithJsonData(builder.toString(), jsonBody, httpHeaders);
119 private StringBuilder getDmiResourceDataUrl(final String dmiBasePath,
120 final String cmHandle,
121 final String resourceId,
122 final String fieldsQuery,
123 final Integer depthQuery,
124 final PassThroughEnum passThrough) {
125 final var builder = new StringBuilder(PARENT_CM_HANDLE_URI.replace("{cmHandle}", cmHandle));
126 builder.append(passThrough.getValue());
127 builder.insert(builder.length(), resourceId);
128 appendFieldsAndDepth(fieldsQuery, depthQuery, builder);
129 builder.insert(0, dmiBasePath);
133 private void appendFieldsAndDepth(final String fieldsQuery, final Integer depthQuery, final StringBuilder builder) {
134 final var doesFieldExists = (fieldsQuery != null && !fieldsQuery.isEmpty());
135 if (doesFieldExists) {
136 builder.append("?").append("fields=").append(fieldsQuery);
138 if (depthQuery != null) {
139 if (!doesFieldExists) {
144 builder.append("depth=").append(depthQuery);
148 private HttpHeaders prepareHeader(final String acceptParam) {
149 final var httpHeaders = new HttpHeaders();
150 if (acceptParam != null && !acceptParam.isEmpty()) {
151 httpHeaders.set(HttpHeaders.ACCEPT, acceptParam);