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