diff options
author | Niamh Core <niamh.core@est.tech> | 2021-08-20 10:38:54 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@onap.org> | 2021-08-20 10:38:54 +0000 |
commit | dc5ed75c841da857611713bceed8bf9988204d3d (patch) | |
tree | 1200bc43049a530dd7dff9fdc647319282729361 /cps-ncmp-service/src/main/java | |
parent | ffc05ea4b84ee3ca36c2278a4d4ded9f8e0371d1 (diff) | |
parent | c328551bdfd069343cc4c4e0249516d07938c78a (diff) |
Merge "get resource data for operational passthrough"
Diffstat (limited to 'cps-ncmp-service/src/main/java')
7 files changed, 425 insertions, 5 deletions
diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/NetworkCmProxyDataService.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/NetworkCmProxyDataService.java index 6038ea4553..82be7bf55c 100644 --- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/NetworkCmProxyDataService.java +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/NetworkCmProxyDataService.java @@ -106,4 +106,20 @@ public interface NetworkCmProxyDataService { */ void updateDmiPluginRegistration(DmiPluginRegistration dmiPluginRegistration); + /** + * Get resource data for data store pass-through operational + * using dmi. + * + * @param cmHandle cm handle + * @param resourceIdentifier resource identifier + * @param accept accept param + * @param fields fields query + * @param depth depth query + * @return {@code Object} resource data + */ + Object getResourceDataOperationalFoCmHandle(@NonNull String cmHandle, + @NonNull String resourceIdentifier, + String accept, + String fields, + Integer depth); } diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/NetworkCmProxyDataServiceImpl.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/NetworkCmProxyDataServiceImpl.java index d6d1ec75ce..84dcc770d9 100755 --- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/NetworkCmProxyDataServiceImpl.java +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/NetworkCmProxyDataServiceImpl.java @@ -26,21 +26,29 @@ import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import java.util.ArrayList; import java.util.Collection; +import java.util.LinkedHashMap; import java.util.List; +import java.util.Map; import lombok.extern.slf4j.Slf4j; import org.onap.cps.api.CpsDataService; import org.onap.cps.api.CpsQueryService; import org.onap.cps.ncmp.api.NetworkCmProxyDataService; +import org.onap.cps.ncmp.api.impl.exception.NcmpException; +import org.onap.cps.ncmp.api.impl.operation.DmiOperations; import org.onap.cps.ncmp.api.models.CmHandle; import org.onap.cps.ncmp.api.models.DmiPluginRegistration; +import org.onap.cps.ncmp.api.models.GenericRequestBody; import org.onap.cps.ncmp.api.models.PersistenceCmHandle; import org.onap.cps.ncmp.api.models.PersistenceCmHandlesList; import org.onap.cps.spi.FetchDescendantsOption; import org.onap.cps.spi.exceptions.DataValidationException; import org.onap.cps.spi.model.DataNode; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Service; import org.springframework.util.StringUtils; + @Slf4j @Service public class NetworkCmProxyDataServiceImpl implements NetworkCmProxyDataService { @@ -49,7 +57,7 @@ public class NetworkCmProxyDataServiceImpl implements NetworkCmProxyDataService private static final String NCMP_DATASPACE_NAME = "NCMP-Admin"; - private static final String NCMP_ANCHOR_NAME = "ncmp-dmi-registry"; + private static final String NCMP_DMI_REGISTRY_ANCHOR = "ncmp-dmi-registry"; private CpsDataService cpsDataService; @@ -57,14 +65,18 @@ public class NetworkCmProxyDataServiceImpl implements NetworkCmProxyDataService private CpsQueryService cpsQueryService; + private DmiOperations dmiOperations; + /** * Constructor Injection for Dependencies. + * @param dmiOperations dmi operation * @param cpsDataService Data Service Interface * @param cpsQueryService Query Service Interface * @param objectMapper Object Mapper */ - public NetworkCmProxyDataServiceImpl(final CpsDataService cpsDataService, + public NetworkCmProxyDataServiceImpl(final DmiOperations dmiOperations, final CpsDataService cpsDataService, final CpsQueryService cpsQueryService, final ObjectMapper objectMapper) { + this.dmiOperations = dmiOperations; this.cpsDataService = cpsDataService; this.cpsQueryService = cpsQueryService; this.objectMapper = objectMapper; @@ -82,7 +94,7 @@ public class NetworkCmProxyDataServiceImpl implements NetworkCmProxyDataService @Override public Collection<DataNode> queryDataNodes(final String cmHandle, final String cpsPath, - final FetchDescendantsOption fetchDescendantsOption) { + final FetchDescendantsOption fetchDescendantsOption) { return cpsQueryService.queryDataNodes(getDataspaceName(), cmHandle, cpsPath, fetchDescendantsOption); } @@ -124,8 +136,10 @@ public class NetworkCmProxyDataServiceImpl implements NetworkCmProxyDataService } final var persistenceCmHandlesList = new PersistenceCmHandlesList(); persistenceCmHandlesList.setCmHandles(persistenceCmHandles); - final var cmHandleJsonData = objectMapper.writeValueAsString(persistenceCmHandlesList); - cpsDataService.saveListNodeData(NCMP_DATASPACE_NAME, NCMP_ANCHOR_NAME, "/dmi-registry", + final String cmHandleJsonData = objectMapper.writeValueAsString(persistenceCmHandlesList); + cpsDataService.saveListNodeData(NCMP_DATASPACE_NAME, + NCMP_DMI_REGISTRY_ANCHOR, + "/dmi-registry", cmHandleJsonData); } catch (final JsonProcessingException e) { throw new DataValidationException( @@ -133,4 +147,74 @@ public class NetworkCmProxyDataServiceImpl implements NetworkCmProxyDataService .getMessage(), e); } } + + @Override + public Object getResourceDataOperationalFoCmHandle(final String cmHandle, + final String resourceIdentifier, + final String acceptParam, + final String fieldsQueryParam, + final Integer depthQueryParam) { + + final DataNode dataNode = fetchDataNodeFromDmiRegistryForCmHandle(cmHandle); + final String dmiServiceName = String.valueOf(dataNode.getLeaves().get("dmi-service-name")); + final Collection<DataNode> additionalPropsList = dataNode.getChildDataNodes(); + final String jsonBody = prepareOperationBody(GenericRequestBody.OperationEnum.READ, additionalPropsList); + final ResponseEntity<Object> response = dmiOperations.getResouceDataFromDmi(dmiServiceName, + cmHandle, + resourceIdentifier, + fieldsQueryParam, + depthQueryParam, + acceptParam, + jsonBody); + return handleResponse(response); + } + + private DataNode fetchDataNodeFromDmiRegistryForCmHandle(final String cmHandle) { + final String xpathForDmiRegistryToFetchCmHandle = "/dmi-registry/cm-handles[@id='" + cmHandle + "']"; + final var dataNode = cpsDataService.getDataNode(NCMP_DATASPACE_NAME, + NCMP_DMI_REGISTRY_ANCHOR, + xpathForDmiRegistryToFetchCmHandle, + FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS); + return dataNode; + } + + private String prepareOperationBody(final GenericRequestBody.OperationEnum operation, + final Collection<DataNode> additionalPropertyList) { + final GenericRequestBody requestBody = new GenericRequestBody(); + final Map<String, String> additionalPropertyMap = getAdditionalPropertiesMap(additionalPropertyList); + requestBody.setOperation(GenericRequestBody.OperationEnum.READ); + requestBody.setCmHandleProperties(additionalPropertyMap); + try { + final String requestJson = objectMapper.writeValueAsString(requestBody); + return requestJson; + } catch (final JsonProcessingException je) { + log.error("Parsing error occurred while converting Object to JSON."); + throw new NcmpException("Parsing error occurred while converting given object to JSON.", + je.getMessage()); + } + } + + private Map<String, String> getAdditionalPropertiesMap(final Collection<DataNode> additionalPropertyList) { + if (additionalPropertyList == null || additionalPropertyList.size() == 0) { + return null; + } + final Map<String, String> additionalPropertyMap = new LinkedHashMap<>(); + for (final DataNode node: additionalPropertyList) { + additionalPropertyMap.put(String.valueOf(node.getLeaves().get("name")), + String.valueOf(node.getLeaves().get("value"))); + } + return additionalPropertyMap; + } + + private Object handleResponse(final ResponseEntity<Object> responseEntity) { + if (responseEntity.getStatusCode() == HttpStatus.OK) { + return responseEntity.getBody(); + } else { + throw new NcmpException("Not able to get resource data.", + "DMI status code: " + responseEntity.getStatusCodeValue() + + ", DMI response body: " + responseEntity.getBody()); + } + } + + } diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/client/DmiRestClient.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/client/DmiRestClient.java new file mode 100644 index 0000000000..3e4f03dfdb --- /dev/null +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/client/DmiRestClient.java @@ -0,0 +1,52 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2021 Nordix Foundation + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.cps.ncmp.api.impl.client; + +import org.onap.cps.ncmp.api.impl.config.NcmpConfiguration.DmiProperties; +import org.springframework.http.HttpEntity; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpMethod; +import org.springframework.http.ResponseEntity; +import org.springframework.stereotype.Component; +import org.springframework.web.client.RestTemplate; + +@Component +public class DmiRestClient { + + private RestTemplate restTemplate; + private DmiProperties dmiProperties; + + public DmiRestClient(final RestTemplate restTemplate, final DmiProperties dmiProperties) { + this.restTemplate = restTemplate; + this.dmiProperties = dmiProperties; + } + + public ResponseEntity<Object> putOperationWithJsonData(final String dmiResourceUrl, + final String jsonData, final HttpHeaders headers) { + final var httpEntity = new HttpEntity<>(jsonData, configureHttpHeaders(headers)); + return restTemplate.exchange(dmiResourceUrl, HttpMethod.PUT, httpEntity, Object.class); + } + + private HttpHeaders configureHttpHeaders(final HttpHeaders httpHeaders) { + httpHeaders.setBasicAuth(dmiProperties.getAuthUsername(), dmiProperties.getAuthPassword()); + return httpHeaders; + } +}
\ No newline at end of file diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/config/NcmpConfiguration.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/config/NcmpConfiguration.java new file mode 100644 index 0000000000..a834bfcd90 --- /dev/null +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/config/NcmpConfiguration.java @@ -0,0 +1,47 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2021 Nordix Foundation + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.cps.ncmp.api.impl.config; + +import lombok.Getter; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.web.client.RestTemplateBuilder; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.stereotype.Component; +import org.springframework.web.client.RestTemplate; + +@Configuration +public class NcmpConfiguration { + + @Getter + @Component + public static class DmiProperties { + @Value("${dmi.auth.username}") + private String authUsername; + @Value("${dmi.auth.password}") + private String authPassword; + } + + @Bean + public RestTemplate restTemplate(final RestTemplateBuilder restTemplateBuilder) { + return restTemplateBuilder.build(); + } +}
\ No newline at end of file diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/exception/NcmpException.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/exception/NcmpException.java new file mode 100644 index 0000000000..ff53464096 --- /dev/null +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/exception/NcmpException.java @@ -0,0 +1,59 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2021 Nordix Foundation + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.cps.ncmp.api.impl.exception; + +import lombok.Getter; + +/** + * Network CM Proxy exception. + */ +public class NcmpException extends RuntimeException { + + private static final long serialVersionUID = 1482619410918497467L; + + @Getter + final String details; + + /** + * Constructor. + * + * @param message the error message + * @param details the error details + */ + public NcmpException(final String message, final String details) { + super(message); + this.details = details; + } + + /** + * Constructor. + * + * @param message the error message + * @param details the error details + * @param cause the cause of the exception + */ + public NcmpException(final String message, final String details, final Throwable cause) { + super(message, cause); + this.details = details; + } + +} + diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/operation/DmiOperations.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/operation/DmiOperations.java new file mode 100644 index 0000000000..c7554bc482 --- /dev/null +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/operation/DmiOperations.java @@ -0,0 +1,109 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2021 Nordix Foundation + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.cps.ncmp.api.impl.operation; + +import org.jetbrains.annotations.NotNull; +import org.onap.cps.ncmp.api.impl.client.DmiRestClient; +import org.springframework.http.HttpHeaders; +import org.springframework.http.ResponseEntity; +import org.springframework.stereotype.Component; + +@Component +public class DmiOperations { + + private DmiRestClient dmiRestClient; + private static final String GET_RESOURCE_DATA_FOR_PASSTHROUGH_OPERATIONAL = + "/v1/ch/{cmHandle}/data/ds/ncmp-datastore:passthrough-operational/"; + private int indexCmHandleForGetOperational; + + /** + * Constructor for {@code DmiOperations}. This method also manipulates url properties. + * + * @param dmiRestClient {@code DmiRestClient} + */ + public DmiOperations(final DmiRestClient dmiRestClient) { + this.dmiRestClient = dmiRestClient; + indexCmHandleForGetOperational = GET_RESOURCE_DATA_FOR_PASSTHROUGH_OPERATIONAL.indexOf("{cmHandle}"); + } + + /** + * This method fetches the resource data for given cm handle identifier on given resource + * using dmi client. + * + * @param dmiBasePath dmi base path + * @param cmHandle network resource identifier + * @param resourceId resource identifier + * @param fieldsQuery fields query + * @param depthQuery depth query + * @param acceptParam accept parameter + * @param jsonBody json body for put operation + * @return {@code ResponseEntity} response entity + */ + public ResponseEntity<Object> getResouceDataFromDmi(final String dmiBasePath, + final String cmHandle, + final String resourceId, + final String fieldsQuery, + final Integer depthQuery, + final String acceptParam, + final String jsonBody) { + final StringBuilder builder = getDmiResourceDataUrl(dmiBasePath, cmHandle, resourceId, fieldsQuery, depthQuery); + final HttpHeaders httpHeaders = prepareHeader(acceptParam); + return dmiRestClient.putOperationWithJsonData(builder.toString(), jsonBody, httpHeaders); + } + + @NotNull + private StringBuilder getDmiResourceDataUrl(final String dmiBasePath, + final String cmHandle, + final String resourceId, + final String fieldsQuery, + final Integer depthQuery) { + final StringBuilder builder = new StringBuilder(GET_RESOURCE_DATA_FOR_PASSTHROUGH_OPERATIONAL); + builder.replace(indexCmHandleForGetOperational, + indexCmHandleForGetOperational + "{cmHandle}".length(), cmHandle); + builder.insert(builder.length(), resourceId); + appendFieldsAndDepth(fieldsQuery, depthQuery, builder); + builder.insert(0, dmiBasePath); + return builder; + } + + private void appendFieldsAndDepth(final String fieldsQuery, final Integer depthQuery, final StringBuilder builder) { + final boolean doesFieldExists = (fieldsQuery != null && !fieldsQuery.isEmpty()); + if (doesFieldExists) { + builder.append("?").append("fields=").append(fieldsQuery); + } + if (depthQuery != null) { + if (!doesFieldExists) { + builder.append("?"); + } else { + builder.append("&"); + } + builder.append("depth=").append(depthQuery); + } + } + + private HttpHeaders prepareHeader(final String acceptParam) { + final HttpHeaders httpHeaders = new HttpHeaders(); + if (acceptParam != null && !acceptParam.isEmpty()) { + httpHeaders.set(HttpHeaders.ACCEPT, acceptParam); + } + return httpHeaders; + } +} diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/models/GenericRequestBody.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/models/GenericRequestBody.java new file mode 100644 index 0000000000..5b82c51b35 --- /dev/null +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/models/GenericRequestBody.java @@ -0,0 +1,53 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2021 Nordix Foundation + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.cps.ncmp.api.models; + +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonInclude.Include; +import com.fasterxml.jackson.annotation.JsonValue; +import java.util.Map; +import lombok.Getter; +import lombok.Setter; + +@Getter +@Setter +@JsonInclude(Include.NON_NULL) +public class GenericRequestBody { + public enum OperationEnum { + READ("read"); + private String value; + + OperationEnum(final String value) { + this.value = value; + } + + @Override + @JsonValue + public String toString() { + return String.valueOf(value); + } + } + + private OperationEnum operation; + private String dataType; + private String data; + private Map<String, String> cmHandleProperties; +} |