Create Endpoint For Get Cm Handles By Name
[cps.git] / cps-ncmp-service / src / main / java / org / onap / cps / ncmp / api / impl / operations / DmiDataOperations.java
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2021-2022 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.operations;
22
23 import static org.onap.cps.ncmp.api.impl.operations.DmiOperations.DataStoreEnum.PASSTHROUGH_RUNNING;
24 import static org.onap.cps.ncmp.api.impl.operations.DmiRequestBody.OperationEnum;
25 import static org.onap.cps.ncmp.api.impl.operations.DmiRequestBody.OperationEnum.READ;
26 import static org.onap.cps.ncmp.api.impl.operations.RequiredDmiService.DATA;
27
28 import org.onap.cps.ncmp.api.impl.client.DmiRestClient;
29 import org.onap.cps.ncmp.api.impl.config.NcmpConfiguration;
30 import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle;
31 import org.onap.cps.utils.JsonObjectMapper;
32 import org.springframework.http.HttpHeaders;
33 import org.springframework.http.ResponseEntity;
34 import org.springframework.stereotype.Component;
35
36 /**
37  * Operations class for DMI data.
38  */
39 @Component
40 public class DmiDataOperations extends DmiOperations {
41
42     /**
43      * Constructor for {@code DmiOperations}. This method also manipulates url properties.
44      *
45      * @param dmiRestClient {@code DmiRestClient}
46      */
47     public DmiDataOperations(final YangModelCmHandleRetriever cmHandlePropertiesRetriever,
48                              final JsonObjectMapper jsonObjectMapper,
49                              final NcmpConfiguration.DmiProperties dmiProperties,
50                              final DmiRestClient dmiRestClient) {
51         super(cmHandlePropertiesRetriever, jsonObjectMapper, dmiProperties, dmiRestClient);
52     }
53
54     /**
55      * This method fetches the resource data from operational data store for given cm handle
56      * identifier on given resource using dmi client.
57      *
58      * @param cmHandleId    network resource identifier
59      * @param resourceId  resource identifier
60      * @param optionsParamInQuery options query
61      * @param acceptParamInHeader accept parameter
62      * @param dataStore  data store enum
63      * @return {@code ResponseEntity} response entity
64      */
65     public ResponseEntity<Object> getResourceDataFromDmi(final String cmHandleId,
66                                                           final String resourceId,
67                                                           final String optionsParamInQuery,
68                                                           final String acceptParamInHeader,
69                                                           final DataStoreEnum dataStore) {
70         final YangModelCmHandle yangModelCmHandle =
71             yangModelCmHandleRetriever.getDmiServiceNamesAndProperties(cmHandleId);
72         final DmiRequestBody dmiRequestBody = DmiRequestBody.builder()
73             .operation(READ)
74             .build();
75         dmiRequestBody.asDmiProperties(yangModelCmHandle.getDmiProperties());
76         final String jsonBody = jsonObjectMapper.asJsonString(dmiRequestBody);
77
78         final var dmiResourceDataUrl = getDmiDatastoreUrlWithOptions(
79             yangModelCmHandle.resolveDmiServiceName(DATA), cmHandleId, resourceId,
80             optionsParamInQuery, dataStore);
81         final var httpHeaders = prepareHeader(acceptParamInHeader);
82         return dmiRestClient.postOperationWithJsonData(dmiResourceDataUrl, jsonBody, httpHeaders);
83     }
84
85     /**
86      * This method creates the resource data from pass-through running data store for given cm handle
87      * identifier on given resource using dmi client.
88      *
89      * @param cmHandleId    network resource identifier
90      * @param resourceId  resource identifier
91      * @param operation   operation enum
92      * @param requestData the request data
93      * @param dataType    data type
94      * @return {@code ResponseEntity} response entity
95      */
96     public ResponseEntity<Object> writeResourceDataPassThroughRunningFromDmi(final String cmHandleId,
97                                                                              final String resourceId,
98                                                                              final OperationEnum operation,
99                                                                              final String requestData,
100                                                                              final String dataType) {
101         final YangModelCmHandle yangModelCmHandle =
102             yangModelCmHandleRetriever.getDmiServiceNamesAndProperties(cmHandleId);
103         final DmiRequestBody dmiRequestBody = DmiRequestBody.builder()
104             .operation(operation)
105             .data(requestData)
106             .dataType(dataType)
107             .build();
108         dmiRequestBody.asDmiProperties(yangModelCmHandle.getDmiProperties());
109         final String jsonBody = jsonObjectMapper.asJsonString(dmiRequestBody);
110         final String dmiUrl =
111             getResourceInDataStoreUrl(yangModelCmHandle.resolveDmiServiceName(DATA),
112                 cmHandleId, resourceId, PASSTHROUGH_RUNNING);
113         return dmiRestClient.postOperationWithJsonData(dmiUrl, jsonBody, new HttpHeaders());
114     }
115
116     private String getResourceInDataStoreUrl(final String dmiServiceName,
117                                              final String cmHandleId,
118                                              final String resourceId,
119                                              final DataStoreEnum dataStoreEnum) {
120         return getCmHandleUrl(dmiServiceName, cmHandleId)
121             + "data"
122             + URL_SEPARATOR
123             + "ds"
124             + URL_SEPARATOR
125             + dataStoreEnum.getValue()
126             + "?resourceIdentifier="
127             + resourceId;
128     }
129
130     private String getDmiDatastoreUrlWithOptions(final String dmiServiceName,
131                                                  final String cmHandleId,
132                                                  final String resourceId,
133                                                  final String optionsParamInQuery,
134                                                  final DataStoreEnum dataStoreEnum) {
135         final String resourceInDataStoreUrl = getResourceInDataStoreUrl(dmiServiceName,
136             cmHandleId, resourceId, dataStoreEnum);
137         return appendOptionsQuery(resourceInDataStoreUrl, optionsParamInQuery);
138     }
139
140 }