Get resource data from pass through running (Ncmp impl.)
[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 com.fasterxml.jackson.annotation.JsonValue;
24 import lombok.Getter;
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;
30
31 @Component
32 public class DmiOperations {
33
34     @Getter
35     public enum PassThroughEnum {
36         OPERATIONAL("/ncmp-datastore:passthrough-operational/"),
37         RUNNING("/ncmp-datastore:passthrough-running/");
38         private String value;
39
40         PassThroughEnum(final String value) {
41             this.value = value;
42         }
43
44         @Override
45         @JsonValue
46         public String toString() {
47             return String.valueOf(value);
48         }
49     }
50
51     private DmiRestClient dmiRestClient;
52     private static final String PARENT_CM_HANDLE_URI =
53             "/v1/ch/{cmHandle}/data/ds";
54     private final int indexCmHandleInUri;
55
56     /**
57      * Constructor for {@code DmiOperations}. This method also manipulates url properties.
58      *
59      * @param dmiRestClient {@code DmiRestClient}
60      */
61     public DmiOperations(final DmiRestClient dmiRestClient) {
62         this.dmiRestClient = dmiRestClient;
63         indexCmHandleInUri = PARENT_CM_HANDLE_URI.indexOf("{cmHandle}");
64     }
65
66     /**
67      * This method fetches the resource data from operational data store for given cm handle
68      * identifier on given resource using dmi client.
69      *
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
78      */
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);
90     }
91
92     /**
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.
95      *
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
104      */
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);
116     }
117
118     @NotNull
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);
130         return builder;
131     }
132
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);
137         }
138         if (depthQuery != null) {
139             if (!doesFieldExists) {
140                 builder.append("?");
141             } else {
142                 builder.append("&");
143             }
144             builder.append("depth=").append(depthQuery);
145         }
146     }
147
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);
152         }
153         return httpHeaders;
154     }
155 }