Data Sync Watchdog Process
[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 lombok.extern.slf4j.Slf4j;
29 import org.onap.cps.ncmp.api.impl.client.DmiRestClient;
30 import org.onap.cps.ncmp.api.impl.config.NcmpConfiguration;
31 import org.onap.cps.ncmp.api.impl.utils.DmiServiceUrlBuilder;
32 import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle;
33 import org.onap.cps.ncmp.api.inventory.CmHandleState;
34 import org.onap.cps.ncmp.api.inventory.InventoryPersistence;
35 import org.onap.cps.spi.exceptions.CpsException;
36 import org.onap.cps.utils.CpsValidator;
37 import org.onap.cps.utils.JsonObjectMapper;
38 import org.springframework.http.ResponseEntity;
39 import org.springframework.stereotype.Component;
40
41 /**
42  * Operations class for DMI data.
43  */
44 @Component
45 @Slf4j
46 public class DmiDataOperations extends DmiOperations {
47
48     /**
49      * Constructor for {@code DmiOperations}. This method also manipulates url properties.
50      *
51      * @param dmiRestClient {@code DmiRestClient}
52      */
53     public DmiDataOperations(final InventoryPersistence inventoryPersistence,
54                              final JsonObjectMapper jsonObjectMapper,
55                              final NcmpConfiguration.DmiProperties dmiProperties,
56                              final DmiRestClient dmiRestClient, final DmiServiceUrlBuilder dmiServiceUrlBuilder) {
57         super(inventoryPersistence, jsonObjectMapper, dmiProperties, dmiRestClient, dmiServiceUrlBuilder);
58     }
59
60     /**
61      * This method fetches the resource data from operational data store for given cm handle
62      * identifier on given resource using dmi client.
63      *
64      * @param cmHandleId    network resource identifier
65      * @param resourceId  resource identifier
66      * @param optionsParamInQuery options query
67      * @param dataStore           data store enum
68      * @param requestId           requestId for async responses
69      * @param topicParamInQuery   topic name for (triggering) async responses
70      * @return {@code ResponseEntity} response entity
71      */
72     public ResponseEntity<Object> getResourceDataFromDmi(final String cmHandleId,
73                                                          final String resourceId,
74                                                          final String optionsParamInQuery,
75                                                          final DataStoreEnum dataStore,
76                                                          final String requestId,
77                                                          final String topicParamInQuery) {
78         final YangModelCmHandle yangModelCmHandle = getYangModelCmHandle(cmHandleId);
79         final String jsonBody = getDmiRequestBody(READ, requestId, null, null, yangModelCmHandle);
80         final String dmiResourceDataUrl = getDmiRequestUrl(cmHandleId, resourceId, optionsParamInQuery, dataStore,
81                 topicParamInQuery, yangModelCmHandle);
82         final CmHandleState cmHandleState = yangModelCmHandle.getCompositeState().getCmHandleState();
83         isCmHandleStateReady(yangModelCmHandle, cmHandleState);
84         return dmiRestClient.postOperationWithJsonData(dmiResourceDataUrl, jsonBody, READ);
85     }
86
87     /**
88      * This method fetches all the resource data from operational data store for given cm handle
89      * identifier using dmi client.
90      *
91      * @param cmHandleId network resource identifier
92      * @param dataStore  data store enum
93      * @param requestId  requestId for async responses
94      * @return {@code ResponseEntity} response entity
95      */
96     public ResponseEntity<Object> getResourceDataFromDmi(final String cmHandleId,
97                                                          final DataStoreEnum dataStore,
98                                                          final String requestId) {
99         final YangModelCmHandle yangModelCmHandle = getYangModelCmHandle(cmHandleId);
100         final String jsonBody = getDmiRequestBody(READ, requestId, null, null, yangModelCmHandle);
101         final String dmiResourceDataUrl = getDmiRequestUrl(cmHandleId, "/", null, dataStore,
102                 null, yangModelCmHandle);
103         final CmHandleState cmHandleState = yangModelCmHandle.getCompositeState().getCmHandleState();
104         isCmHandleStateReady(yangModelCmHandle, cmHandleState);
105         return dmiRestClient.postOperationWithJsonData(dmiResourceDataUrl, jsonBody, READ);
106     }
107
108     /**
109      * This method creates the resource data from pass-through running data store for given cm handle
110      * identifier on given resource using dmi client.
111      *
112      * @param cmHandleId    network resource identifier
113      * @param resourceId  resource identifier
114      * @param operation   operation enum
115      * @param requestData the request data
116      * @param dataType    data type
117      * @return {@code ResponseEntity} response entity
118      */
119     public ResponseEntity<Object> writeResourceDataPassThroughRunningFromDmi(final String cmHandleId,
120                                                                              final String resourceId,
121                                                                              final OperationEnum operation,
122                                                                              final String requestData,
123                                                                              final String dataType) {
124         final YangModelCmHandle yangModelCmHandle = getYangModelCmHandle(cmHandleId);
125         final String jsonBody = getDmiRequestBody(operation, null, requestData, dataType, yangModelCmHandle);
126         final String dmiUrl = getDmiRequestUrl(cmHandleId, resourceId, null, PASSTHROUGH_RUNNING,
127                 null, yangModelCmHandle);
128         final CmHandleState cmHandleState = yangModelCmHandle.getCompositeState().getCmHandleState();
129         isCmHandleStateReady(yangModelCmHandle, cmHandleState);
130         return dmiRestClient.postOperationWithJsonData(dmiUrl, jsonBody, operation);
131     }
132
133     private YangModelCmHandle getYangModelCmHandle(final String cmHandleId) {
134         CpsValidator.validateNameCharacters(cmHandleId);
135         return inventoryPersistence.getYangModelCmHandle(cmHandleId);
136     }
137
138     private String getDmiRequestBody(final OperationEnum operation, final String requestId, final String requestData,
139                                      final String dataType, final YangModelCmHandle yangModelCmHandle) {
140         final DmiRequestBody dmiRequestBody = DmiRequestBody.builder()
141                 .operation(operation)
142                 .requestId(requestId)
143                 .data(requestData)
144                 .dataType(dataType)
145                 .build();
146         dmiRequestBody.asDmiProperties(yangModelCmHandle.getDmiProperties());
147         return jsonObjectMapper.asJsonString(dmiRequestBody);
148     }
149
150     private String getDmiRequestUrl(final String cmHandleId,
151                                       final String resourceId,
152                                       final String optionsParamInQuery,
153                                       final DataStoreEnum dataStore,
154                                       final String topicParamInQuery,
155                                       final YangModelCmHandle yangModelCmHandle) {
156         return dmiServiceUrlBuilder.getDmiDatastoreUrl(
157                 dmiServiceUrlBuilder.populateQueryParams(resourceId, optionsParamInQuery,
158                         topicParamInQuery), dmiServiceUrlBuilder.populateUriVariables(
159                         yangModelCmHandle, cmHandleId, dataStore));
160     }
161
162     private void isCmHandleStateReady(final YangModelCmHandle yangModelCmHandle, final CmHandleState cmHandleState) {
163         if (cmHandleState != CmHandleState.READY) {
164             throw new CpsException("State mismatch exception.", "Cm-Handle not in READY state. "
165                 + "cm handle state is "
166                 + yangModelCmHandle.getCompositeState().getCmHandleState());
167         }
168     }
169
170 }