get resource data for operational passthrough
[cps.git] / cps-ncmp-service / src / main / java / org / onap / cps / ncmp / api / impl / operation / DmiOperations.java
1 /*
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
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  *  SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.cps.ncmp.api.impl.operation;
22
23 import org.jetbrains.annotations.NotNull;
24 import org.onap.cps.ncmp.api.impl.client.DmiRestClient;
25 import org.springframework.http.HttpHeaders;
26 import org.springframework.http.ResponseEntity;
27 import org.springframework.stereotype.Component;
28
29 @Component
30 public class DmiOperations {
31
32     private DmiRestClient dmiRestClient;
33     private static final String GET_RESOURCE_DATA_FOR_PASSTHROUGH_OPERATIONAL =
34             "/v1/ch/{cmHandle}/data/ds/ncmp-datastore:passthrough-operational/";
35     private int indexCmHandleForGetOperational;
36
37     /**
38      * Constructor for {@code DmiOperations}. This method also manipulates url properties.
39      *
40      * @param dmiRestClient {@code DmiRestClient}
41      */
42     public DmiOperations(final DmiRestClient dmiRestClient) {
43         this.dmiRestClient = dmiRestClient;
44         indexCmHandleForGetOperational = GET_RESOURCE_DATA_FOR_PASSTHROUGH_OPERATIONAL.indexOf("{cmHandle}");
45     }
46
47     /**
48      * This method fetches the resource data for given cm handle identifier on given resource
49      * using dmi client.
50      *
51      * @param dmiBasePath dmi base path
52      * @param cmHandle network resource identifier
53      * @param resourceId resource identifier
54      * @param fieldsQuery fields query
55      * @param depthQuery depth query
56      * @param acceptParam accept parameter
57      * @param jsonBody json body for put operation
58      * @return {@code ResponseEntity} response entity
59      */
60     public ResponseEntity<Object> getResouceDataFromDmi(final String dmiBasePath,
61                                                         final String cmHandle,
62                                                         final String resourceId,
63                                                         final String fieldsQuery,
64                                                         final Integer depthQuery,
65                                                         final String acceptParam,
66                                                         final String jsonBody) {
67         final StringBuilder builder = getDmiResourceDataUrl(dmiBasePath, cmHandle, resourceId, fieldsQuery, depthQuery);
68         final HttpHeaders httpHeaders = prepareHeader(acceptParam);
69         return dmiRestClient.putOperationWithJsonData(builder.toString(), jsonBody, httpHeaders);
70     }
71
72     @NotNull
73     private StringBuilder getDmiResourceDataUrl(final String dmiBasePath,
74                                                 final String cmHandle,
75                                                 final String resourceId,
76                                                 final String fieldsQuery,
77                                                 final Integer depthQuery) {
78         final StringBuilder builder = new StringBuilder(GET_RESOURCE_DATA_FOR_PASSTHROUGH_OPERATIONAL);
79         builder.replace(indexCmHandleForGetOperational,
80                 indexCmHandleForGetOperational + "{cmHandle}".length(), cmHandle);
81         builder.insert(builder.length(), resourceId);
82         appendFieldsAndDepth(fieldsQuery, depthQuery, builder);
83         builder.insert(0, dmiBasePath);
84         return builder;
85     }
86
87     private void appendFieldsAndDepth(final String fieldsQuery, final Integer depthQuery, final StringBuilder builder) {
88         final boolean doesFieldExists = (fieldsQuery != null && !fieldsQuery.isEmpty());
89         if (doesFieldExists) {
90             builder.append("?").append("fields=").append(fieldsQuery);
91         }
92         if (depthQuery != null) {
93             if (!doesFieldExists) {
94                 builder.append("?");
95             } else {
96                 builder.append("&");
97             }
98             builder.append("depth=").append(depthQuery);
99         }
100     }
101
102     private HttpHeaders prepareHeader(final String acceptParam) {
103         final HttpHeaders httpHeaders = new HttpHeaders();
104         if (acceptParam != null && !acceptParam.isEmpty()) {
105             httpHeaders.set(HttpHeaders.ACCEPT, acceptParam);
106         }
107         return httpHeaders;
108     }
109 }