63c4d49a9152cff308b96ad825443345ff5807ef
[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 DataStoreEnum {
36         PASSTHROUGH_OPERATIONAL("ncmp-datastore:passthrough-operational"),
37         PASSTHROUGH_RUNNING("ncmp-datastore:passthrough-running");
38         private String value;
39
40         DataStoreEnum(final String value) {
41             this.value = value;
42         }
43
44         @Override
45         @JsonValue
46         public String toString() {
47             return value;
48         }
49     }
50
51     private DmiRestClient dmiRestClient;
52     private static final String DMI_BASE_PATH = "/dmi/api";
53     private static final String PARENT_CM_HANDLE_URI =
54             "/v1/ch/{cmHandle}/data/ds";
55     private static final String URL_SEPARATOR = "/";
56
57     /**
58      * Constructor for {@code DmiOperations}. This method also manipulates url properties.
59      *
60      * @param dmiRestClient {@code DmiRestClient}
61      */
62     public DmiOperations(final DmiRestClient dmiRestClient) {
63         this.dmiRestClient = dmiRestClient;
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> getResourceDataOperationalFromDmi(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 dmiResourceDataUrl = getDmiResourceDataUrl(dmiBasePath, cmHandle, resourceId,
87                 fieldsQuery, depthQuery, DataStoreEnum.PASSTHROUGH_OPERATIONAL);
88         final var httpHeaders = prepareHeader(acceptParam);
89         return dmiRestClient.putOperationWithJsonData(dmiResourceDataUrl, 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> getResourceDataPassThroughRunningFromDmi(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 dmiResourceDataUrl = getDmiResourceDataUrl(dmiBasePath, cmHandle, resourceId,
113                 fieldsQuery, depthQuery, DataStoreEnum.PASSTHROUGH_RUNNING);
114         final var httpHeaders = prepareHeader(acceptParam);
115         return dmiRestClient.putOperationWithJsonData(dmiResourceDataUrl, jsonBody, httpHeaders);
116     }
117
118     /**
119      * This method creates the resource data from pass-through running data store for given cm handle
120      * identifier on given resource using dmi client.
121      *
122      * @param dmiBasePath dmi base path
123      * @param cmHandle network resource identifier
124      * @param resourceId resource identifier
125      * @param jsonBody json body for put operation
126      * @return {@code ResponseEntity} response entity
127      */
128     public ResponseEntity<Void> createResourceDataPassThroughRunningFromDmi(final String dmiBasePath,
129                                                                             final String cmHandle,
130                                                                             final String resourceId,
131                                                                             final String jsonBody) {
132         final var stringBuilder = getStringBuilderForPassThroughRunningUrl(dmiBasePath,
133                 cmHandle, resourceId, DataStoreEnum.PASSTHROUGH_RUNNING);
134         return dmiRestClient.postOperationWithJsonData(stringBuilder.toString(), jsonBody, new HttpHeaders());
135     }
136
137     @NotNull
138     private String getDmiResourceDataUrl(final String dmiBasePath,
139                                                 final String cmHandle,
140                                                 final String resourceId,
141                                                 final String fieldsQuery,
142                                                 final Integer depthQuery,
143                                                 final DataStoreEnum dataStoreEnum) {
144         final var stringBuilder = getStringBuilderForPassThroughRunningUrl(dmiBasePath,
145                 cmHandle, resourceId, dataStoreEnum);
146         appendFieldsAndDepth(stringBuilder, fieldsQuery, depthQuery);
147         return stringBuilder.toString();
148     }
149
150     @NotNull
151     private StringBuilder getStringBuilderForPassThroughRunningUrl(final String dmiServiceName,
152                                                                    final String cmHandle,
153                                                                    final String resourceId,
154                                                                    final DataStoreEnum dataStoreEnum) {
155         final var stringBuilder =  new StringBuilder(dmiServiceName);
156         stringBuilder.append(DMI_BASE_PATH);
157         stringBuilder.append(PARENT_CM_HANDLE_URI.replace("{cmHandle}", cmHandle));
158         stringBuilder.append(URL_SEPARATOR + dataStoreEnum.getValue());
159         stringBuilder.insert(stringBuilder.length(), URL_SEPARATOR + resourceId);
160         return stringBuilder;
161     }
162
163     private void appendFieldsAndDepth(final StringBuilder stringBuilder,
164                                       final String fieldsQuery,
165                                       final Integer depthQuery) {
166         final var doesFieldExists = (fieldsQuery != null && !fieldsQuery.isEmpty());
167         if (doesFieldExists) {
168             stringBuilder.append("?").append("fields=").append(fieldsQuery);
169         }
170         if (depthQuery != null) {
171             if (doesFieldExists) {
172                 stringBuilder.append("&");
173             } else {
174                 stringBuilder.append("?");
175             }
176             stringBuilder.append("depth=").append(depthQuery);
177         }
178     }
179
180     private HttpHeaders prepareHeader(final String acceptParam) {
181         final var httpHeaders = new HttpHeaders();
182         if (acceptParam != null && !acceptParam.isEmpty()) {
183             httpHeaders.set(HttpHeaders.ACCEPT, acceptParam);
184         }
185         return httpHeaders;
186     }
187 }