Exclude CM-Handles that are not in state 'READY'
[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         CpsValidator.validateNameCharacters(cmHandleId);
79         final YangModelCmHandle yangModelCmHandle =
80                 inventoryPersistence.getYangModelCmHandle(cmHandleId);
81         final CmHandleState cmHandleState = yangModelCmHandle.getCompositeState().getCmHandleState();
82         isCmHandleStateReady(yangModelCmHandle, cmHandleState);
83         final DmiRequestBody dmiRequestBody = DmiRequestBody.builder()
84             .operation(READ)
85             .requestId(requestId)
86             .build();
87         dmiRequestBody.asDmiProperties(yangModelCmHandle.getDmiProperties());
88         final String jsonBody = jsonObjectMapper.asJsonString(dmiRequestBody);
89         final String dmiResourceDataUrl = dmiServiceUrlBuilder.getDmiDatastoreUrl(
90                 dmiServiceUrlBuilder.populateQueryParams(resourceId, optionsParamInQuery,
91                 topicParamInQuery), dmiServiceUrlBuilder.populateUriVariables(
92                         yangModelCmHandle, cmHandleId, dataStore));
93         return dmiRestClient.postOperationWithJsonData(dmiResourceDataUrl, jsonBody, READ);
94     }
95
96     /**
97      * This method creates the resource data from pass-through running data store for given cm handle
98      * identifier on given resource using dmi client.
99      *
100      * @param cmHandleId    network resource identifier
101      * @param resourceId  resource identifier
102      * @param operation   operation enum
103      * @param requestData the request data
104      * @param dataType    data type
105      * @return {@code ResponseEntity} response entity
106      */
107     public ResponseEntity<Object> writeResourceDataPassThroughRunningFromDmi(final String cmHandleId,
108                                                                              final String resourceId,
109                                                                              final OperationEnum operation,
110                                                                              final String requestData,
111                                                                              final String dataType) {
112         CpsValidator.validateNameCharacters(cmHandleId);
113         final YangModelCmHandle yangModelCmHandle =
114             inventoryPersistence.getYangModelCmHandle(cmHandleId);
115         final CmHandleState cmHandleState = yangModelCmHandle.getCompositeState().getCmHandleState();
116         isCmHandleStateReady(yangModelCmHandle, cmHandleState);
117         final DmiRequestBody dmiRequestBody = DmiRequestBody.builder()
118             .operation(operation)
119             .data(requestData)
120             .dataType(dataType)
121             .build();
122         dmiRequestBody.asDmiProperties(yangModelCmHandle.getDmiProperties());
123         final String jsonBody = jsonObjectMapper.asJsonString(dmiRequestBody);
124         final String dmiUrl =
125             dmiServiceUrlBuilder.getDmiDatastoreUrl(dmiServiceUrlBuilder.populateQueryParams(resourceId,
126                     null, null),
127                 dmiServiceUrlBuilder.populateUriVariables(yangModelCmHandle, cmHandleId, PASSTHROUGH_RUNNING));
128         return dmiRestClient.postOperationWithJsonData(dmiUrl, jsonBody, operation);
129     }
130
131     private void isCmHandleStateReady(final YangModelCmHandle yangModelCmHandle, final CmHandleState cmHandleState) {
132         if (cmHandleState != CmHandleState.READY) {
133             throw new CpsException("State mismatch exception.", "Cm-Handle not in READY state. "
134                 + "cm handle state is "
135                 + yangModelCmHandle.getCompositeState().getCmHandleState());
136         }
137     }
138
139 }