Refactoring/ Adding Tests for Validation
[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  *  Modifications Copyright (C) 2022 Bell Canada
5  *  ================================================================================
6  *  Licensed under the Apache License, Version 2.0 (the "License");
7  *  you may not use this file except in compliance with the License.
8  *  You may obtain a copy of the License at
9  *
10  *        http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License.
17  *
18  *  SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.cps.ncmp.api.impl.operations;
23
24 import static org.onap.cps.ncmp.api.impl.operations.DmiOperations.DataStoreEnum.PASSTHROUGH_RUNNING;
25 import static org.onap.cps.ncmp.api.impl.operations.DmiRequestBody.OperationEnum;
26 import static org.onap.cps.ncmp.api.impl.operations.DmiRequestBody.OperationEnum.READ;
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.utils.DmiServiceUrlBuilder;
31 import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle;
32 import org.onap.cps.utils.CpsValidator;
33 import org.onap.cps.utils.JsonObjectMapper;
34 import org.springframework.http.ResponseEntity;
35 import org.springframework.stereotype.Component;
36
37 /**
38  * Operations class for DMI data.
39  */
40 @Component
41 public class DmiDataOperations extends DmiOperations {
42
43     /**
44      * Constructor for {@code DmiOperations}. This method also manipulates url properties.
45      *
46      * @param dmiRestClient {@code DmiRestClient}
47      */
48     public DmiDataOperations(final YangModelCmHandleRetriever cmHandlePropertiesRetriever,
49                              final JsonObjectMapper jsonObjectMapper,
50                              final NcmpConfiguration.DmiProperties dmiProperties,
51                              final DmiRestClient dmiRestClient, final DmiServiceUrlBuilder dmiServiceUrlBuilder) {
52         super(cmHandlePropertiesRetriever, jsonObjectMapper, dmiProperties, dmiRestClient, dmiServiceUrlBuilder);
53     }
54
55     /**
56      * This method fetches the resource data from operational data store for given cm handle
57      * identifier on given resource using dmi client.
58      *
59      * @param cmHandleId    network resource identifier
60      * @param resourceId  resource identifier
61      * @param optionsParamInQuery options query
62      * @param dataStore           data store enum
63      * @param requestId           requestId for async responses
64      * @param topicParamInQuery   topic name for (triggering) async responses
65      * @return {@code ResponseEntity} response entity
66      */
67     public ResponseEntity<Object> getResourceDataFromDmi(final String cmHandleId,
68                                                          final String resourceId,
69                                                          final String optionsParamInQuery,
70                                                          final DataStoreEnum dataStore,
71                                                          final String requestId,
72                                                          final String topicParamInQuery) {
73         CpsValidator.validateNameCharacters(cmHandleId);
74         final YangModelCmHandle yangModelCmHandle =
75                 yangModelCmHandleRetriever.getDmiServiceNamesAndProperties(cmHandleId);
76         final DmiRequestBody dmiRequestBody = DmiRequestBody.builder()
77             .operation(READ)
78             .requestId(requestId)
79             .build();
80         dmiRequestBody.asDmiProperties(yangModelCmHandle.getDmiProperties());
81         final String jsonBody = jsonObjectMapper.asJsonString(dmiRequestBody);
82         final String dmiResourceDataUrl = dmiServiceUrlBuilder.getDmiDatastoreUrl(
83                 dmiServiceUrlBuilder.populateQueryParams(resourceId, optionsParamInQuery,
84                 topicParamInQuery), dmiServiceUrlBuilder.populateUriVariables(
85                         yangModelCmHandle, cmHandleId, dataStore));
86         return dmiRestClient.postOperationWithJsonData(dmiResourceDataUrl, jsonBody);
87     }
88
89     /**
90      * This method creates the resource data from pass-through running data store for given cm handle
91      * identifier on given resource using dmi client.
92      *
93      * @param cmHandleId    network resource identifier
94      * @param resourceId  resource identifier
95      * @param operation   operation enum
96      * @param requestData the request data
97      * @param dataType    data type
98      * @return {@code ResponseEntity} response entity
99      */
100     public ResponseEntity<Object> writeResourceDataPassThroughRunningFromDmi(final String cmHandleId,
101                                                                              final String resourceId,
102                                                                              final OperationEnum operation,
103                                                                              final String requestData,
104                                                                              final String dataType) {
105         CpsValidator.validateNameCharacters(cmHandleId);
106         final YangModelCmHandle yangModelCmHandle =
107             yangModelCmHandleRetriever.getDmiServiceNamesAndProperties(cmHandleId);
108         final DmiRequestBody dmiRequestBody = DmiRequestBody.builder()
109             .operation(operation)
110             .data(requestData)
111             .dataType(dataType)
112             .build();
113         dmiRequestBody.asDmiProperties(yangModelCmHandle.getDmiProperties());
114         final String jsonBody = jsonObjectMapper.asJsonString(dmiRequestBody);
115         final String dmiUrl =
116             dmiServiceUrlBuilder.getDmiDatastoreUrl(dmiServiceUrlBuilder.populateQueryParams(resourceId,
117                     null, null),
118                 dmiServiceUrlBuilder.populateUriVariables(yangModelCmHandle, cmHandleId, PASSTHROUGH_RUNNING));
119         return dmiRestClient.postOperationWithJsonData(dmiUrl, jsonBody);
120     }
121
122 }