From: RehanRaza Date: Wed, 6 Nov 2019 14:30:22 +0000 (+0100) Subject: Add Http Response Code in A1 Controller Api X-Git-Tag: 1.7.3~23^2 X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=commitdiff_plain;h=238fd97d5c8320cc8a53e04e04cfd39dcf900c4e;p=nonrtric.git Add Http Response Code in A1 Controller Api Change-Id: I4da040a6ffa0fa6f9ea2597612a19e86ec701e11 Issue-ID: NONRTRIC-64 Signed-off-by: RehanRaza --- diff --git a/dashboard/a1-controller-client/src/main/resources/a1_controller_0.1.0.yaml b/dashboard/a1-controller-client/src/main/resources/a1_controller_0.1.0.yaml index 8d74a419..86bb8234 100644 --- a/dashboard/a1-controller-client/src/main/resources/a1_controller_0.1.0.yaml +++ b/dashboard/a1-controller-client/src/main/resources/a1_controller_0.1.0.yaml @@ -1,6 +1,5 @@ # ================================================================================== -# Copyright (c) 2019 Nokia -# Copyright (c) 2018-2019 AT&T Intellectual Property. +# Copyright (c) 2019 Nordix Foundation. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -29,12 +28,12 @@ paths: responses: '200': description: > - Successfully got the list of all nearRT-RICs. + Successfully got the response. content: application/json: schema: - "$ref": "#/components/schemas/output_NRRids_list_schema" - + "$ref": "#/components/schemas/output_NRRids_list_code_schema" + '/A1-ADAPTER-API:getHealthCheck': post: description: > @@ -51,12 +50,12 @@ paths: responses: '200': description: > - Successfully got the health status. + Successfully got the response. content: application/json: schema: - "$ref": "#/components/schemas/output_healthstatus_schema" - + "$ref": "#/components/schemas/output_healthstatus_code_schema" + '/A1-ADAPTER-API:getPolicyTypes': post: description: > @@ -73,12 +72,12 @@ paths: responses: '200': description: > - Successfully got the list of all policy-type-ids. + Successfully got the response. content: application/json: schema: - "$ref": "#/components/schemas/output_PTids_list_schema" - + "$ref": "#/components/schemas/output_PTids_list_code_schema" + '/A1-ADAPTER-API:createPolicyType': post: description: > @@ -93,17 +92,14 @@ paths: schema: "$ref": "#/components/schemas/input_NRRid_PTid_desc_name_PT_schema" responses: - '201': + '200': description: > - Successfully created the policy type. + Successfully got the response. content: application/json: schema: - "$ref": "#/components/schemas/output_status_code_schema" - '400': - description: > - illegal policy_type_id, or this policy type already exists - + "$ref": "#/components/schemas/output_code_schema" + '/A1-ADAPTER-API:getPolicyType': post: description: > @@ -120,15 +116,12 @@ paths: responses: '200': description: > - Successfully got the policy type. + Successfully got the response. content: application/json: schema: - "$ref": "#/components/schemas/output_desc_name_PT_schema" - '404': - description: > - there is no policy type with this policy_type_id - + "$ref": "#/components/schemas/output_desc_name_PT_code_schema" + '/A1-ADAPTER-API:deletePolicyType': post: description: > @@ -143,16 +136,14 @@ paths: schema: "$ref": "#/components/schemas/input_NRRid_PTid_schema" responses: - '204': - description: > - Successfully deleted the policy type. - '400': - description: > - Policy type cannot be deleted because there are instances. All instances must be removed before a policy type can be deleted. - '404': + '200': description: > - there is no policy type with this policy_type_id - + Successfully got the response. + content: + application/json: + schema: + "$ref": "#/components/schemas/output_code_schema" + '/A1-ADAPTER-API:getPolicyInstances': post: description: > @@ -169,15 +160,12 @@ paths: responses: '200': description: > - Successfully got the list of all policy-instance-ids for this policy-type-id. + Successfully got the response. content: application/json: schema: - "$ref": "#/components/schemas/output_PIids_list_schema" - '404': - description: > - there is no policy type with this policy_type_id - + "$ref": "#/components/schemas/output_PIids_list_code_schema" + '/A1-ADAPTER-API:createPolicyInstance': post: description: > @@ -192,16 +180,14 @@ paths: schema: "$ref": "#/components/schemas/input_NRRid_PTid_PIid_PI_schema" responses: - '201': - description: > - Successfully created the policy instance. - '400': - description: > - Bad input data for this policy instance. - '404': + '200': description: > - there is no policy type with this policy_type_id - + Successfully got the response. + content: + application/json: + schema: + "$ref": "#/components/schemas/output_code_schema" + '/A1-ADAPTER-API:getPolicyInstance': post: description: > @@ -218,14 +204,11 @@ paths: responses: '200': description: > - Successfully got the policy instance. + Successfully got the response. content: application/json: schema: - "$ref": "#/components/schemas/output_PI_schema" - '404': - description: > - there is no policy instance with this policy_instance_id or there is no policy type with this policy_type_id + "$ref": "#/components/schemas/output_PI_code_schema" '/A1-ADAPTER-API:deletePolicyInstance': post: @@ -241,13 +224,14 @@ paths: schema: "$ref": "#/components/schemas/input_NRRid_PTid_PIid_schema" responses: - '204': - description: > - Successfully deleted the policy instance. - '404': + '200': description: > - there is no policy instance with this policy_instance_id or there is no policy type with this policy_type_id - + Successfully got the response. + content: + application/json: + schema: + "$ref": "#/components/schemas/output_code_schema" + '/A1-ADAPTER-API:getStatus': post: description: > @@ -264,16 +248,13 @@ paths: responses: '200': description: > - Successfully got the policy instance status. + Successfully got the response. content: application/json: schema: - "$ref": "#/components/schemas/output_status_schema" - '404': - description: > - there is no policy instance with this policy_instance_id or there is no policy type with this policy_type_id + "$ref": "#/components/schemas/output_status_code_schema" -components: +components: schemas: input_NRRid_schema: type: object @@ -289,7 +270,7 @@ components: properties: near-rt-ric-id: "$ref": "#/components/schemas/near_rt_ric_id" - + input_NRRid_PTid_schema: type: object required: @@ -307,7 +288,7 @@ components: "$ref": "#/components/schemas/near_rt_ric_id" policy-type-id: "$ref": "#/components/schemas/policy_type_id" - + input_NRRid_PTid_PIid_schema: type: object required: @@ -328,7 +309,7 @@ components: "$ref": "#/components/schemas/policy_type_id" policy-instance-id: "$ref": "#/components/schemas/policy_instance_id" - + input_NRRid_PTid_PIid_PI_schema: type: object required: @@ -352,7 +333,7 @@ components: "$ref": "#/components/schemas/policy_instance_id" policy-instance: "$ref": "#/components/schemas/policy_instance" - + input_NRRid_PTid_desc_name_PT_schema: type: object required: @@ -379,8 +360,8 @@ components: type: string policy-type: "$ref": "#/components/schemas/policy_type" - - output_NRRids_list_schema: + + output_NRRids_list_code_schema: type: object required: - output @@ -390,14 +371,17 @@ components: type: object required: - near-rt-ric-id-list + - code additionalProperties: false properties: near-rt-ric-id-list: type: array items: "$ref": "#/components/schemas/near_rt_ric_id" - - output_healthstatus_schema: + code: + type: string + + output_healthstatus_code_schema: type: object required: - output @@ -407,12 +391,15 @@ components: type: object required: - health-status + - code additionalProperties: false properties: health-status: type: boolean - - output_desc_name_PT_schema: + code: + type: string + + output_desc_name_PT_code_schema: type: object required: - output @@ -424,6 +411,7 @@ components: - description - name - policy_type + - code additionalProperties: false properties: description: @@ -432,8 +420,10 @@ components: type: string policy-type: "$ref": "#/components/schemas/policy_type" - - output_PTids_list_schema: + code: + type: string + + output_PTids_list_code_schema: type: object required: - output @@ -443,14 +433,17 @@ components: type: object required: - policy-type-id-list + - code additionalProperties: false properties: policy-type-id-list: type: array items: "$ref": "#/components/schemas/policy_type_id" - - output_PIids_list_schema: + code: + type: string + + output_PIids_list_code_schema: type: object required: - output @@ -460,14 +453,17 @@ components: type: object required: - policy-instance-id-list + - code additionalProperties: false properties: policy-instance-id-list: type: array items: "$ref": "#/components/schemas/policy_instance_id" - - output_PI_schema: + code: + type: string + + output_PI_code_schema: type: object required: - output @@ -477,12 +473,15 @@ components: type: object required: - policy-instance + - code additionalProperties: false properties: policy-instance: "$ref": "#/components/schemas/policy_instance" - - output_status_schema: + code: + type: string + + output_code_schema: type: object required: - output @@ -491,12 +490,12 @@ components: output: type: object required: - - status + - code additionalProperties: false properties: - status: + code: type: string - + output_status_code_schema: type: object required: @@ -514,19 +513,17 @@ components: type: string code: type: string - + near_rt_ric_id: description: > represents a near RT RIC identifier. Currently this can be any string. type: string example: near-rt-ric-1 - + policy_type_id: description: > - represents a policy type identifier. Currently this is restricted to an integer range. + represents a policy type identifier. Currently this is an integer. type: integer - minimum: 20000 - maximum: 21023 example: 20000 policy_instance_id: @@ -534,25 +531,25 @@ components: represents a policy instance identifier. UUIDs are advisable but can be any string type: string example: 3d2157af-6a8f-4a7c-810f-38c2f824bf12 - + policy_type: description: > represents a policy type. String is used for now to represent this type: string - example: + example: "{type: A}" - + policy_instance: description: > represents a policy instance. String is used for now to represent this type: string - example: + example: "{slice_id: slice-1, priority_level: high}" - + securitySchemes: basicAuth: type: http scheme: basic - + security: - basicAuth: [] \ No newline at end of file diff --git a/dashboard/webapp-backend/src/main/java/org/oransc/ric/portal/dashboard/controller/A1Controller.java b/dashboard/webapp-backend/src/main/java/org/oransc/ric/portal/dashboard/controller/A1Controller.java index 386d43c1..8cd61bdd 100644 --- a/dashboard/webapp-backend/src/main/java/org/oransc/ric/portal/dashboard/controller/A1Controller.java +++ b/dashboard/webapp-backend/src/main/java/org/oransc/ric/portal/dashboard/controller/A1Controller.java @@ -31,13 +31,18 @@ import org.oransc.ric.a1controller.client.model.InputNRRidPTidSchema; import org.oransc.ric.a1controller.client.model.InputNRRidPTidSchemaInput; import org.oransc.ric.a1controller.client.model.InputNRRidSchema; import org.oransc.ric.a1controller.client.model.InputNRRidSchemaInput; -import org.oransc.ric.a1controller.client.model.OutputDescNamePTSchema; -import org.oransc.ric.a1controller.client.model.OutputDescNamePTSchemaOutput; -import org.oransc.ric.a1controller.client.model.OutputPISchema; -import org.oransc.ric.a1controller.client.model.OutputPIidsListSchema; -import org.oransc.ric.a1controller.client.model.OutputPTidsListSchema; +import org.oransc.ric.a1controller.client.model.OutputCodeSchema; +import org.oransc.ric.a1controller.client.model.OutputDescNamePTCodeSchema; +import org.oransc.ric.a1controller.client.model.OutputDescNamePTCodeSchemaOutput; +import org.oransc.ric.a1controller.client.model.OutputPICodeSchema; +import org.oransc.ric.a1controller.client.model.OutputPIidsListCodeSchema; +import org.oransc.ric.a1controller.client.model.OutputPTidsListCodeSchema; import org.oransc.ric.portal.dashboard.DashboardApplication; import org.oransc.ric.portal.dashboard.DashboardConstants; +import org.oransc.ric.portal.dashboard.exceptions.HttpBadRequestException; +import org.oransc.ric.portal.dashboard.exceptions.HttpInternalServerErrorException; +import org.oransc.ric.portal.dashboard.exceptions.HttpNotFoundException; +import org.oransc.ric.portal.dashboard.exceptions.HttpNotImplementedException; import org.oransc.ric.portal.dashboard.model.PolicyInstance; import org.oransc.ric.portal.dashboard.model.PolicyInstances; import org.oransc.ric.portal.dashboard.model.PolicyType; @@ -46,6 +51,7 @@ import org.oransc.ric.portal.dashboard.model.SuccessTransport; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.security.access.annotation.Secured; import org.springframework.util.Assert; @@ -116,9 +122,10 @@ public class A1Controller { nrrid.setNearRtRicId(NEAR_RT_RIC_ID); InputNRRidSchema inputSchema = new InputNRRidSchema(); inputSchema.setInput(nrrid); - OutputPTidsListSchema outputPTidsListSchema = + OutputPTidsListCodeSchema outputPTidsListCodeSchema = a1ControllerApi.a1ControllerGetAllPolicyTypes(inputSchema); - List policyTypeIds = outputPTidsListSchema.getOutput().getPolicyTypeIdList(); + checkHttpError(outputPTidsListCodeSchema.getOutput().getCode()); + List policyTypeIds = outputPTidsListCodeSchema.getOutput().getPolicyTypeIdList(); PolicyTypes policyTypes = new PolicyTypes(); InputNRRidPTidSchema typeSchema = new InputNRRidPTidSchema(); InputNRRidPTidSchemaInput typeId = new InputNRRidPTidSchemaInput(); @@ -126,9 +133,10 @@ public class A1Controller { for (Integer policyTypeId : policyTypeIds) { typeId.setPolicyTypeId(policyTypeId); typeSchema.setInput(typeId); - OutputDescNamePTSchema controllerGetPolicyType = + OutputDescNamePTCodeSchema controllerGetPolicyType = a1ControllerApi.a1ControllerGetPolicyType(typeSchema); - OutputDescNamePTSchemaOutput policyTypeSchema = controllerGetPolicyType.getOutput(); + checkHttpError(controllerGetPolicyType.getOutput().getCode()); + OutputDescNamePTCodeSchemaOutput policyTypeSchema = controllerGetPolicyType.getOutput(); PolicyType type = new PolicyType(policyTypeId, policyTypeSchema.getName(), policyTypeSchema.getDescription(), policyTypeSchema.getPolicyType().toString()); policyTypes.add(type); @@ -147,8 +155,9 @@ public class A1Controller { typeIdInput.setPolicyTypeId(policyTypeId); InputNRRidPTidSchema inputSchema = new InputNRRidPTidSchema(); inputSchema.setInput(typeIdInput); - OutputPIidsListSchema controllerGetAllInstancesForType = + OutputPIidsListCodeSchema controllerGetAllInstancesForType = a1ControllerApi.a1ControllerGetAllInstancesForType(inputSchema); + checkHttpError(controllerGetAllInstancesForType.getOutput().getCode()); List instancesForType = controllerGetAllInstancesForType.getOutput().getPolicyInstanceIdList(); PolicyInstances instances = new PolicyInstances(); InputNRRidPTidPIidSchemaInput instanceIdInput = new InputNRRidPTidPIidSchemaInput(); @@ -158,8 +167,9 @@ public class A1Controller { for (String instanceId : instancesForType) { instanceIdInput.setPolicyInstanceId(instanceId); instanceInputSchema.setInput(instanceIdInput); - OutputPISchema policyInstance = + OutputPICodeSchema policyInstance = a1ControllerApi.a1ControllerGetPolicyInstance(instanceInputSchema); + checkHttpError(policyInstance.getOutput().getCode()); PolicyInstance instance = new PolicyInstance(instanceId, policyInstance.getOutput().getPolicyInstance()); instances.add(instance); @@ -180,7 +190,8 @@ public class A1Controller { instanceIdInput.setPolicyInstanceId(policyInstanceId); InputNRRidPTidPIidSchema inputSchema = new InputNRRidPTidPIidSchema(); inputSchema.setInput(instanceIdInput); - OutputPISchema policyInstance = a1ControllerApi.a1ControllerGetPolicyInstance(inputSchema); + OutputPICodeSchema policyInstance = a1ControllerApi.a1ControllerGetPolicyInstance(inputSchema); + checkHttpError(policyInstance.getOutput().getCode()); return policyInstance.getOutput().getPolicyInstance(); } @@ -199,7 +210,8 @@ public class A1Controller { createInstanceInput.setPolicyInstance(instance); InputNRRidPTidPIidPISchema inputSchema = new InputNRRidPTidPIidPISchema(); inputSchema.setInput(createInstanceInput); - a1ControllerApi.a1ControllerCreatePolicyInstance(inputSchema); + OutputCodeSchema outputCodeSchema = a1ControllerApi.a1ControllerCreatePolicyInstance(inputSchema); + checkHttpError(outputCodeSchema.getOutput().getCode()); } @ApiOperation(value = "Deletes the policy instances for the given policy type.") @@ -215,6 +227,24 @@ public class A1Controller { instanceIdInput.setPolicyInstanceId(policyInstanceId); InputNRRidPTidPIidSchema inputSchema = new InputNRRidPTidPIidSchema(); inputSchema.setInput(instanceIdInput); - a1ControllerApi.a1ControllerDeletePolicyInstance(inputSchema); + OutputCodeSchema outputCodeSchema = a1ControllerApi.a1ControllerDeletePolicyInstance(inputSchema); + checkHttpError(outputCodeSchema.getOutput().getCode()); + } + + private void checkHttpError(String httpCode) { + logger.debug("Http Response Code: {}", httpCode); + if (httpCode.equals(String.valueOf(HttpStatus.NOT_FOUND.value()))) { + logger.error("Caught HttpNotFoundException"); + throw new HttpNotFoundException("Not Found Exception"); + } else if (httpCode.equals(String.valueOf(HttpStatus.BAD_REQUEST.value()))) { + logger.error("Caught HttpBadRequestException"); + throw new HttpBadRequestException("Bad Request Exception"); + } else if (httpCode.equals(String.valueOf(HttpStatus.INTERNAL_SERVER_ERROR.value()))) { + logger.error("Caught HttpInternalServerErrorException"); + throw new HttpInternalServerErrorException("Internal Server Error Exception"); + } else if (httpCode.equals(String.valueOf(HttpStatus.NOT_IMPLEMENTED.value()))) { + logger.error("Caught HttpNotImplementedException"); + throw new HttpNotImplementedException("Not Implemented Exception"); + } } } diff --git a/dashboard/webapp-backend/src/main/java/org/oransc/ric/portal/dashboard/exceptions/HttpBadRequestException.java b/dashboard/webapp-backend/src/main/java/org/oransc/ric/portal/dashboard/exceptions/HttpBadRequestException.java new file mode 100644 index 00000000..db433e83 --- /dev/null +++ b/dashboard/webapp-backend/src/main/java/org/oransc/ric/portal/dashboard/exceptions/HttpBadRequestException.java @@ -0,0 +1,37 @@ +/*- + * ========================LICENSE_START================================= + * O-RAN-SC + * %% + * Copyright (C) 2019 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. + * ========================LICENSE_END=================================== + */ +package org.oransc.ric.portal.dashboard.exceptions; + +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.ResponseStatus; + +@ResponseStatus(value = HttpStatus.BAD_REQUEST) +public class HttpBadRequestException extends RuntimeException { + + /** + * + */ + private static final long serialVersionUID = -1402302710014461941L; + + public HttpBadRequestException(String message) { + super(message); + } + +} diff --git a/dashboard/webapp-backend/src/main/java/org/oransc/ric/portal/dashboard/exceptions/HttpInternalServerErrorException.java b/dashboard/webapp-backend/src/main/java/org/oransc/ric/portal/dashboard/exceptions/HttpInternalServerErrorException.java new file mode 100644 index 00000000..474fe2a7 --- /dev/null +++ b/dashboard/webapp-backend/src/main/java/org/oransc/ric/portal/dashboard/exceptions/HttpInternalServerErrorException.java @@ -0,0 +1,37 @@ +/*- + * ========================LICENSE_START================================= + * O-RAN-SC + * %% + * Copyright (C) 2019 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. + * ========================LICENSE_END=================================== + */ +package org.oransc.ric.portal.dashboard.exceptions; + +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.ResponseStatus; + +@ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR) +public class HttpInternalServerErrorException extends RuntimeException { + + /** + * + */ + private static final long serialVersionUID = 6821547799515184764L; + + public HttpInternalServerErrorException(String message) { + super(message); + } + +} diff --git a/dashboard/webapp-backend/src/main/java/org/oransc/ric/portal/dashboard/exceptions/HttpNotFoundException.java b/dashboard/webapp-backend/src/main/java/org/oransc/ric/portal/dashboard/exceptions/HttpNotFoundException.java new file mode 100644 index 00000000..48f577e4 --- /dev/null +++ b/dashboard/webapp-backend/src/main/java/org/oransc/ric/portal/dashboard/exceptions/HttpNotFoundException.java @@ -0,0 +1,37 @@ +/*- + * ========================LICENSE_START================================= + * O-RAN-SC + * %% + * Copyright (C) 2019 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. + * ========================LICENSE_END=================================== + */ +package org.oransc.ric.portal.dashboard.exceptions; + +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.ResponseStatus; + +@ResponseStatus(value = HttpStatus.NOT_FOUND) +public class HttpNotFoundException extends RuntimeException { + + /** + * + */ + private static final long serialVersionUID = -4755191883669549764L; + + public HttpNotFoundException(String message) { + super(message); + } + +} diff --git a/dashboard/webapp-backend/src/main/java/org/oransc/ric/portal/dashboard/exceptions/HttpNotImplementedException.java b/dashboard/webapp-backend/src/main/java/org/oransc/ric/portal/dashboard/exceptions/HttpNotImplementedException.java new file mode 100644 index 00000000..756012b1 --- /dev/null +++ b/dashboard/webapp-backend/src/main/java/org/oransc/ric/portal/dashboard/exceptions/HttpNotImplementedException.java @@ -0,0 +1,37 @@ +/*- + * ========================LICENSE_START================================= + * O-RAN-SC + * %% + * Copyright (C) 2019 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. + * ========================LICENSE_END=================================== + */ +package org.oransc.ric.portal.dashboard.exceptions; + +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.ResponseStatus; + +@ResponseStatus(value = HttpStatus.NOT_IMPLEMENTED) +public class HttpNotImplementedException extends RuntimeException { + + /** + * + */ + private static final long serialVersionUID = 2697666454489763009L; + + public HttpNotImplementedException(String message) { + super(message); + } + +} diff --git a/dashboard/webapp-backend/src/test/java/org/oransc/ric/portal/dashboard/config/A1ControllerMockConfiguration.java b/dashboard/webapp-backend/src/test/java/org/oransc/ric/portal/dashboard/config/A1ControllerMockConfiguration.java index b056cb50..086868de 100644 --- a/dashboard/webapp-backend/src/test/java/org/oransc/ric/portal/dashboard/config/A1ControllerMockConfiguration.java +++ b/dashboard/webapp-backend/src/test/java/org/oransc/ric/portal/dashboard/config/A1ControllerMockConfiguration.java @@ -23,7 +23,7 @@ import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.doAnswer; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; - +import com.fasterxml.jackson.core.JsonProcessingException; import java.lang.invoke.MethodHandles; import java.util.ArrayList; import java.util.HashMap; @@ -33,21 +33,22 @@ import java.util.List; import java.util.Map; import java.util.Optional; import java.util.Set; - import org.oransc.ric.a1controller.client.api.A1ControllerApi; import org.oransc.ric.a1controller.client.invoker.ApiClient; import org.oransc.ric.a1controller.client.model.InputNRRidPTidPIidPISchema; import org.oransc.ric.a1controller.client.model.InputNRRidPTidPIidSchema; import org.oransc.ric.a1controller.client.model.InputNRRidPTidSchema; import org.oransc.ric.a1controller.client.model.InputNRRidSchema; -import org.oransc.ric.a1controller.client.model.OutputDescNamePTSchema; -import org.oransc.ric.a1controller.client.model.OutputDescNamePTSchemaOutput; -import org.oransc.ric.a1controller.client.model.OutputPISchema; -import org.oransc.ric.a1controller.client.model.OutputPISchemaOutput; -import org.oransc.ric.a1controller.client.model.OutputPIidsListSchema; -import org.oransc.ric.a1controller.client.model.OutputPIidsListSchemaOutput; -import org.oransc.ric.a1controller.client.model.OutputPTidsListSchema; -import org.oransc.ric.a1controller.client.model.OutputPTidsListSchemaOutput; +import org.oransc.ric.a1controller.client.model.OutputCodeSchema; +import org.oransc.ric.a1controller.client.model.OutputCodeSchemaOutput; +import org.oransc.ric.a1controller.client.model.OutputDescNamePTCodeSchema; +import org.oransc.ric.a1controller.client.model.OutputDescNamePTCodeSchemaOutput; +import org.oransc.ric.a1controller.client.model.OutputPICodeSchema; +import org.oransc.ric.a1controller.client.model.OutputPICodeSchemaOutput; +import org.oransc.ric.a1controller.client.model.OutputPIidsListCodeSchema; +import org.oransc.ric.a1controller.client.model.OutputPIidsListCodeSchemaOutput; +import org.oransc.ric.a1controller.client.model.OutputPTidsListCodeSchema; +import org.oransc.ric.a1controller.client.model.OutputPTidsListCodeSchemaOutput; import org.oransc.ric.portal.dashboard.model.PolicyType; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -57,8 +58,6 @@ import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Profile; import org.springframework.http.HttpStatus; -import com.fasterxml.jackson.core.JsonProcessingException; - /** * Creates a mock implementation of the A1 controller client API. */ @@ -99,9 +98,10 @@ public class A1ControllerMockConfiguration { Thread.sleep(delayMs); } List types = database.getTypes(); - OutputPTidsListSchemaOutput output = new OutputPTidsListSchemaOutput(); + OutputPTidsListCodeSchemaOutput output = new OutputPTidsListCodeSchemaOutput(); output.setPolicyTypeIdList(types); - OutputPTidsListSchema outputSchema = new OutputPTidsListSchema(); + output.setCode(String.valueOf(HttpStatus.OK.value())); + OutputPTidsListCodeSchema outputSchema = new OutputPTidsListCodeSchema(); outputSchema.setOutput(output); return outputSchema; }).when(mockApi).a1ControllerGetAllPolicyTypes(any(InputNRRidSchema.class)); @@ -113,11 +113,12 @@ public class A1ControllerMockConfiguration { } InputNRRidPTidSchema input = inv.getArgument(0); PolicyType policyType = database.getPolicyType(input.getInput().getPolicyTypeId()); - OutputDescNamePTSchemaOutput type = new OutputDescNamePTSchemaOutput(); + OutputDescNamePTCodeSchemaOutput type = new OutputDescNamePTCodeSchemaOutput(); type.setName(policyType.getName()); type.setDescription(policyType.getDescription()); type.setPolicyType(database.normalize(policyType.getCreateSchema())); - OutputDescNamePTSchema outputSchema = new OutputDescNamePTSchema(); + type.setCode(String.valueOf(HttpStatus.OK.value())); + OutputDescNamePTCodeSchema outputSchema = new OutputDescNamePTCodeSchema(); outputSchema.setOutput(type); return outputSchema; }).when(mockApi).a1ControllerGetPolicyType(any(InputNRRidPTidSchema.class)); @@ -129,9 +130,10 @@ public class A1ControllerMockConfiguration { } InputNRRidPTidSchema input = inv.getArgument(0); List instances = database.getInstances(Optional.of(input.getInput().getPolicyTypeId())); - OutputPIidsListSchemaOutput instancesOutput = new OutputPIidsListSchemaOutput(); + OutputPIidsListCodeSchemaOutput instancesOutput = new OutputPIidsListCodeSchemaOutput(); instancesOutput.setPolicyInstanceIdList(instances); - OutputPIidsListSchema outputSchema = new OutputPIidsListSchema(); + instancesOutput.setCode(String.valueOf(HttpStatus.OK.value())); + OutputPIidsListCodeSchema outputSchema = new OutputPIidsListCodeSchema(); outputSchema.setOutput(instancesOutput); return outputSchema; }).when(mockApi).a1ControllerGetAllInstancesForType(any(InputNRRidPTidSchema.class)); @@ -145,9 +147,10 @@ public class A1ControllerMockConfiguration { Integer polcyTypeId = input.getInput().getPolicyTypeId(); String instanceId = input.getInput().getPolicyInstanceId(); String instance = database.normalize(database.getInstance(polcyTypeId, instanceId)); - OutputPISchemaOutput instanceOutput = new OutputPISchemaOutput(); + OutputPICodeSchemaOutput instanceOutput = new OutputPICodeSchemaOutput(); instanceOutput.setPolicyInstance(instance); - OutputPISchema outputSchema = new OutputPISchema(); + instanceOutput.setCode(String.valueOf(HttpStatus.OK.value())); + OutputPICodeSchema outputSchema = new OutputPICodeSchema(); outputSchema.setOutput(instanceOutput); return outputSchema; }).when(mockApi).a1ControllerGetPolicyInstance(any(InputNRRidPTidPIidSchema.class)); @@ -162,7 +165,11 @@ public class A1ControllerMockConfiguration { String instanceId = input.getInput().getPolicyInstanceId(); String instance = input.getInput().getPolicyInstance(); database.putInstance(polcyTypeId, instanceId, instance); - return null; + OutputCodeSchemaOutput outputCodeSchemaOutput = new OutputCodeSchemaOutput(); + outputCodeSchemaOutput.setCode(String.valueOf(HttpStatus.CREATED.value())); + OutputCodeSchema outputCodeSchema = new OutputCodeSchema(); + outputCodeSchema.setOutput(outputCodeSchemaOutput); + return outputCodeSchema; }).when(mockApi).a1ControllerCreatePolicyInstance(any(InputNRRidPTidPIidPISchema.class)); doAnswer(inv -> { @@ -174,7 +181,11 @@ public class A1ControllerMockConfiguration { Integer polcyTypeId = input.getInput().getPolicyTypeId(); String instanceId = input.getInput().getPolicyInstanceId(); database.deleteInstance(polcyTypeId, instanceId); - return null; + OutputCodeSchemaOutput outputCodeSchemaOutput = new OutputCodeSchemaOutput(); + outputCodeSchemaOutput.setCode(String.valueOf(HttpStatus.NO_CONTENT.value())); + OutputCodeSchema outputCodeSchema = new OutputCodeSchema(); + outputCodeSchema.setOutput(outputCodeSchemaOutput); + return outputCodeSchema; }).when(mockApi).a1ControllerDeletePolicyInstance(any(InputNRRidPTidPIidSchema.class)); return mockApi; diff --git a/sdnc-a1-controller/northbound/nonrt-ric-api/model/src/main/yang/NONRT-RIC-API.yang b/sdnc-a1-controller/northbound/nonrt-ric-api/model/src/main/yang/NONRT-RIC-API.yang index 806bdc1c..834d12fb 100644 --- a/sdnc-a1-controller/northbound/nonrt-ric-api/model/src/main/yang/NONRT-RIC-API.yang +++ b/sdnc-a1-controller/northbound/nonrt-ric-api/model/src/main/yang/NONRT-RIC-API.yang @@ -10,10 +10,10 @@ module A1-ADAPTER-API { revision "2019-10-02" { description - "A1 adapter for Frankfurt"; + "A1 adapter"; } -///Flattend interface using RPC + ///Flattend interface using RPC //Get an array of near-rt-ric IDs, //Each item in the returned array will be regarded as one near-rt-ric-id. @@ -22,6 +22,9 @@ module A1-ADAPTER-API { leaf-list near-rt-ric-id-list { type string; } + leaf code { + type string; + } } } @@ -37,6 +40,9 @@ module A1-ADAPTER-API { leaf health-status { type boolean; } + leaf code { + type string; + } } } @@ -53,6 +59,9 @@ module A1-ADAPTER-API { leaf-list policy-type-id-list { type uint32; } + leaf code { + type string; + } } } @@ -76,9 +85,6 @@ module A1-ADAPTER-API { } } output { - leaf status { - type string; - } leaf code { type string; } @@ -105,6 +111,9 @@ module A1-ADAPTER-API { leaf policy-type { type string; } + leaf code { + type string; + } } } @@ -118,6 +127,11 @@ module A1-ADAPTER-API { type uint32; } } + output { + leaf code { + type string; + } + } } //Get an array of string policy instance ids @@ -136,6 +150,9 @@ module A1-ADAPTER-API { leaf-list policy-instance-id-list { type string; } + leaf code { + type string; + } } } @@ -155,6 +172,11 @@ module A1-ADAPTER-API { type string; } } + output { + leaf code { + type string; + } + } } ///Get a policy instance @@ -174,6 +196,9 @@ module A1-ADAPTER-API { leaf policy-instance { type string; } + leaf code { + type string; + } } } @@ -190,6 +215,11 @@ module A1-ADAPTER-API { type string; } } + output { + leaf code { + type string; + } + } } //Get the status for a policy instance @@ -209,6 +239,9 @@ module A1-ADAPTER-API { leaf status { type string; } + leaf code { + type string; + } } } } \ No newline at end of file diff --git a/sdnc-a1-controller/northbound/nonrt-ric-api/provider/pom.xml b/sdnc-a1-controller/northbound/nonrt-ric-api/provider/pom.xml index 51bcfe75..250c0160 100644 --- a/sdnc-a1-controller/northbound/nonrt-ric-api/provider/pom.xml +++ b/sdnc-a1-controller/northbound/nonrt-ric-api/provider/pom.xml @@ -54,6 +54,10 @@ nonrt-ric-api-model ${project.version} + + com.google.code.gson + gson + org.springframework spring-context diff --git a/sdnc-a1-controller/northbound/nonrt-ric-api/provider/src/main/java/org/onap/sdnc/northbound/exceptions/NearRtRicNotFoundException.java b/sdnc-a1-controller/northbound/nonrt-ric-api/provider/src/main/java/org/onap/sdnc/northbound/exceptions/NearRtRicNotFoundException.java new file mode 100644 index 00000000..1754bfa4 --- /dev/null +++ b/sdnc-a1-controller/northbound/nonrt-ric-api/provider/src/main/java/org/onap/sdnc/northbound/exceptions/NearRtRicNotFoundException.java @@ -0,0 +1,14 @@ +package org.onap.sdnc.northbound.exceptions; + +public class NearRtRicNotFoundException extends RuntimeException { + + /** + * + */ + private static final long serialVersionUID = -4503072266424371087L; + + public NearRtRicNotFoundException(String message) { + super(message); + } + +} diff --git a/sdnc-a1-controller/northbound/nonrt-ric-api/provider/src/main/java/org/onap/sdnc/northbound/provider/NonrtRicApiProvider.java b/sdnc-a1-controller/northbound/nonrt-ric-api/provider/src/main/java/org/onap/sdnc/northbound/provider/NonrtRicApiProvider.java index 66af597e..b5e3deed 100644 --- a/sdnc-a1-controller/northbound/nonrt-ric-api/provider/src/main/java/org/onap/sdnc/northbound/provider/NonrtRicApiProvider.java +++ b/sdnc-a1-controller/northbound/nonrt-ric-api/provider/src/main/java/org/onap/sdnc/northbound/provider/NonrtRicApiProvider.java @@ -20,10 +20,10 @@ package org.onap.sdnc.northbound.provider; -import com.google.common.base.Optional; import com.google.common.util.concurrent.CheckedFuture; import com.google.common.util.concurrent.Futures; import com.google.common.util.concurrent.ListenableFuture; +import com.google.gson.Gson; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.ArrayList; @@ -34,6 +34,7 @@ import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import org.json.JSONObject; +import org.onap.sdnc.northbound.exceptions.NearRtRicNotFoundException; import org.onap.sdnc.northbound.restadpter.NearRicUrlProvider; import org.onap.sdnc.northbound.restadpter.RestAdapter; import org.onap.sdnc.northbound.restadpter.RestAdapterImpl; @@ -81,6 +82,9 @@ import org.opendaylight.yangtools.yang.common.RpcResult; import org.opendaylight.yangtools.yang.common.RpcResultBuilder; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.client.RestClientResponseException; /** * Defines a base implementation for your provider. This class overrides the generated interface @@ -100,8 +104,6 @@ public class NonrtRicApiProvider implements AutoCloseable, A1ADAPTERAPIService { protected static final String NO_SERVICE_LOGIC_ACTIVE = "No service logic active for "; private static final String NON_NULL_PARAM = "non-null"; private static final String NULL_PARAM = "null"; - private static final String RESPONSE_SUCCESS = "Success"; - private static final String RESPONSE_CODE_SUCCESS = "200"; private final Logger log = LoggerFactory.getLogger(NonrtRicApiProvider.class); private final ExecutorService executor; @@ -201,11 +203,20 @@ public class NonrtRicApiProvider implements AutoCloseable, A1ADAPTERAPIService { public ListenableFuture> createPolicyInstance( CreatePolicyInstanceInput input) { log.info("Start of createPolicyInstance"); - String uri = nearRicUrlProvider.getPolicyInstanceId(String.valueOf(input.getNearRtRicId()), - String.valueOf(input.getPolicyTypeId()), String.valueOf(input.getPolicyInstanceId())); - log.info("PUT Request input.getPolicyInstance() : {} ", input.getPolicyInstance()); - restAdapter.put(uri, input.getPolicyInstance()); CreatePolicyInstanceOutputBuilder responseBuilder = new CreatePolicyInstanceOutputBuilder(); + try { + String uri = nearRicUrlProvider.getPolicyInstanceId(String.valueOf(input.getNearRtRicId()), + String.valueOf(input.getPolicyTypeId()), String.valueOf(input.getPolicyInstanceId())); + log.info("PUT Request input.getPolicyInstance() : {} ", input.getPolicyInstance()); + ResponseEntity response = restAdapter.put(uri, input.getPolicyInstance()); + responseBuilder.setCode(response.getStatusCode().toString()); + } catch (NearRtRicNotFoundException ex) { + log.error("Caught exception: {}", ex); + responseBuilder.setCode(HttpStatus.INTERNAL_SERVER_ERROR.toString()); + } catch (RestClientResponseException ex) { + log.error("Caught exception: {}", ex); + responseBuilder.setCode(String.valueOf(ex.getRawStatusCode())); + } log.info("End of createPolicyInstance"); RpcResult rpcResult = RpcResultBuilder .status(true).withResult(responseBuilder.build()).build(); @@ -216,13 +227,20 @@ public class NonrtRicApiProvider implements AutoCloseable, A1ADAPTERAPIService { public ListenableFuture> createPolicyType( CreatePolicyTypeInput input) { log.info("Start of createPolicyType"); - String uri = nearRicUrlProvider.getPolicyTypeId(String.valueOf(input.getNearRtRicId()), - String.valueOf(input.getPolicyTypeId())); - log.info("PUT Request input.getPolicyType() : {} ", input.getPolicyType()); - restAdapter.put(uri, input.getPolicyType()); CreatePolicyTypeOutputBuilder responseBuilder = new CreatePolicyTypeOutputBuilder(); - responseBuilder.setCode(RESPONSE_CODE_SUCCESS); - responseBuilder.setStatus(RESPONSE_SUCCESS); + try { + String uri = nearRicUrlProvider.getPolicyTypeId(String.valueOf(input.getNearRtRicId()), + String.valueOf(input.getPolicyTypeId())); + log.info("PUT Request input.getPolicyType() : {} ", input.getPolicyType()); + ResponseEntity response = restAdapter.put(uri, input.getPolicyType()); + responseBuilder.setCode(response.getStatusCode().toString()); + } catch (NearRtRicNotFoundException ex) { + log.error("Caught exception: {}", ex); + responseBuilder.setCode(HttpStatus.INTERNAL_SERVER_ERROR.toString()); + } catch (RestClientResponseException ex) { + log.error("Caught exception: {}", ex); + responseBuilder.setCode(String.valueOf(ex.getRawStatusCode())); + } log.info("End of createPolicyType"); RpcResult rpcResult = RpcResultBuilder .status(true).withResult(responseBuilder.build()).build(); @@ -233,10 +251,19 @@ public class NonrtRicApiProvider implements AutoCloseable, A1ADAPTERAPIService { public ListenableFuture> deletePolicyInstance( DeletePolicyInstanceInput input) { log.info("Start of deletePolicyInstance"); - String uri = nearRicUrlProvider.getPolicyInstanceId(String.valueOf(input.getNearRtRicId()), - String.valueOf(input.getPolicyTypeId()), String.valueOf(input.getPolicyInstanceId())); - restAdapter.delete(uri); DeletePolicyInstanceOutputBuilder responseBuilder = new DeletePolicyInstanceOutputBuilder(); + try { + String uri = nearRicUrlProvider.getPolicyInstanceId(String.valueOf(input.getNearRtRicId()), + String.valueOf(input.getPolicyTypeId()), String.valueOf(input.getPolicyInstanceId())); + ResponseEntity response = restAdapter.delete(uri); + responseBuilder.setCode(response.getStatusCode().toString()); + } catch (NearRtRicNotFoundException ex) { + log.error("Caught exception: {}", ex); + responseBuilder.setCode(HttpStatus.INTERNAL_SERVER_ERROR.toString()); + } catch (RestClientResponseException ex) { + log.error("Caught exception: {}", ex); + responseBuilder.setCode(String.valueOf(ex.getRawStatusCode())); + } log.info("End of deletePolicyInstance"); RpcResult rpcResult = RpcResultBuilder .status(true).withResult(responseBuilder.build()).build(); @@ -247,10 +274,19 @@ public class NonrtRicApiProvider implements AutoCloseable, A1ADAPTERAPIService { public ListenableFuture> deletePolicyType( DeletePolicyTypeInput input) { log.info("Start of deletePolicyType"); - String uri = nearRicUrlProvider.getPolicyTypeId(String.valueOf(input.getNearRtRicId()), - String.valueOf(input.getPolicyTypeId())); - restAdapter.delete(uri); DeletePolicyTypeOutputBuilder responseBuilder = new DeletePolicyTypeOutputBuilder(); + try { + String uri = nearRicUrlProvider.getPolicyTypeId(String.valueOf(input.getNearRtRicId()), + String.valueOf(input.getPolicyTypeId())); + ResponseEntity response = restAdapter.delete(uri); + responseBuilder.setCode(response.getStatusCode().toString()); + } catch (NearRtRicNotFoundException ex) { + log.error("Caught exception: {}", ex); + responseBuilder.setCode(HttpStatus.INTERNAL_SERVER_ERROR.toString()); + } catch (RestClientResponseException ex) { + log.error("Caught exception: {}", ex); + responseBuilder.setCode(String.valueOf(ex.getRawStatusCode())); + } log.info("End of deletePolicyType"); RpcResult rpcResult = RpcResultBuilder .status(true).withResult(responseBuilder.build()).build(); @@ -261,10 +297,22 @@ public class NonrtRicApiProvider implements AutoCloseable, A1ADAPTERAPIService { public ListenableFuture> getHealthCheck( GetHealthCheckInput input) { log.info("Start of getHealthCheck"); - String uri = nearRicUrlProvider.getHealthCheck(String.valueOf(input.getNearRtRicId())); - restAdapter.get(uri, String.class); GetHealthCheckOutputBuilder responseBuilder = new GetHealthCheckOutputBuilder(); - responseBuilder.setHealthStatus(true); + try { + String uri = nearRicUrlProvider.getHealthCheck(String.valueOf(input.getNearRtRicId())); + ResponseEntity response = restAdapter.get(uri, Object.class); + responseBuilder.setHealthStatus(false); + if (response.getStatusCode().equals(HttpStatus.OK)) { + responseBuilder.setHealthStatus(true); + } + responseBuilder.setCode(response.getStatusCode().toString()); + } catch (NearRtRicNotFoundException ex) { + log.error("Caught exception: {}", ex); + responseBuilder.setCode(HttpStatus.INTERNAL_SERVER_ERROR.toString()); + } catch (RestClientResponseException ex) { + log.error("Caught exception: {}", ex); + responseBuilder.setCode(String.valueOf(ex.getRawStatusCode())); + } log.info("End of getHealthCheck"); RpcResult rpcResult = RpcResultBuilder.status(true) .withResult(responseBuilder.build()).build(); @@ -276,6 +324,7 @@ public class NonrtRicApiProvider implements AutoCloseable, A1ADAPTERAPIService { log.info("Start of getNearRTRICs"); GetNearRTRICsOutputBuilder responseBuilder = new GetNearRTRICsOutputBuilder(); responseBuilder.setNearRtRicIdList(nearRicUrlProvider.getNearRTRicIdsList()); + responseBuilder.setCode(HttpStatus.OK.toString()); log.info("End of getNearRTRICs"); RpcResult rpcResult = RpcResultBuilder.status(true) .withResult(responseBuilder.build()).build(); @@ -287,13 +336,22 @@ public class NonrtRicApiProvider implements AutoCloseable, A1ADAPTERAPIService { GetPolicyInstanceInput input) { log.info("Start of getPolicyInstance"); log.info("Policy Type Id : {}, Policy Instance Id : {}", input.getPolicyTypeId(), input.getPolicyInstanceId()); - String uri = nearRicUrlProvider.getPolicyInstanceId(String.valueOf(input.getNearRtRicId()), - String.valueOf(input.getPolicyTypeId()), String.valueOf(input.getPolicyInstanceId())); - Optional policyInstance = restAdapter.get(uri, String.class); GetPolicyInstanceOutputBuilder responseBuilder = new GetPolicyInstanceOutputBuilder(); - if (policyInstance.isPresent()) { - log.info("Response policyInstance.get() : {} ", policyInstance.get()); - responseBuilder.setPolicyInstance(policyInstance.get()); + try { + String uri = nearRicUrlProvider.getPolicyInstanceId(String.valueOf(input.getNearRtRicId()), + String.valueOf(input.getPolicyTypeId()), String.valueOf(input.getPolicyInstanceId())); + ResponseEntity response = restAdapter.get(uri, String.class); + if (response.hasBody()) { + log.info("Response getPolicyInstance : {} ", response.getBody()); + responseBuilder.setPolicyInstance(response.getBody()); + } + responseBuilder.setCode(response.getStatusCode().toString()); + } catch (NearRtRicNotFoundException ex) { + log.error("Caught exception: {}", ex); + responseBuilder.setCode(HttpStatus.INTERNAL_SERVER_ERROR.toString()); + } catch (RestClientResponseException ex) { + log.error("Caught exception: {}", ex); + responseBuilder.setCode(String.valueOf(ex.getRawStatusCode())); } log.info("End of getPolicyInstance"); RpcResult rpcResult = RpcResultBuilder @@ -305,13 +363,22 @@ public class NonrtRicApiProvider implements AutoCloseable, A1ADAPTERAPIService { public ListenableFuture> getPolicyInstances( GetPolicyInstancesInput input) { log.info("Start of getPolicyInstances"); - String uri = nearRicUrlProvider.getPolicyInstances(String.valueOf(input.getNearRtRicId()), - String.valueOf(input.getPolicyTypeId())); - Optional> policyInstances = restAdapter.get(uri, List.class); GetPolicyInstancesOutputBuilder responseBuilder = new GetPolicyInstancesOutputBuilder(); - if (policyInstances.isPresent()) { - log.info("Response policyInstances.get() : {} ", policyInstances.get()); - responseBuilder.setPolicyInstanceIdList(policyInstances.get()); + try { + String uri = nearRicUrlProvider.getPolicyInstances(String.valueOf(input.getNearRtRicId()), + String.valueOf(input.getPolicyTypeId())); + ResponseEntity> response = restAdapter.get(uri, List.class); + if (response.hasBody()) { + log.info("Response getPolicyInstances : {} ", response.getBody()); + responseBuilder.setPolicyInstanceIdList(response.getBody()); + } + responseBuilder.setCode(response.getStatusCode().toString()); + } catch (NearRtRicNotFoundException ex) { + log.error("Caught exception: {}", ex); + responseBuilder.setCode(HttpStatus.INTERNAL_SERVER_ERROR.toString()); + } catch (RestClientResponseException ex) { + log.error("Caught exception: {}", ex); + responseBuilder.setCode(String.valueOf(ex.getRawStatusCode())); } log.info("End of getPolicyInstances"); RpcResult rpcResult = RpcResultBuilder @@ -323,16 +390,25 @@ public class NonrtRicApiProvider implements AutoCloseable, A1ADAPTERAPIService { public ListenableFuture> getPolicyType(GetPolicyTypeInput input) { log.info("Start of getPolicyType"); log.info("Policy Type Id : {} ", input.getPolicyTypeId()); - String uri = nearRicUrlProvider.getPolicyTypeId(String.valueOf(input.getNearRtRicId()), - String.valueOf(input.getPolicyTypeId())); - Optional policyType = restAdapter.get(uri, String.class); GetPolicyTypeOutputBuilder responseBuilder = new GetPolicyTypeOutputBuilder(); - if (policyType.isPresent()) { - log.info("Response policyType.get() : {} ", policyType.get()); - JSONObject policyTypeObj = new JSONObject(policyType.get()); - responseBuilder.setDescription(policyTypeObj.getString("description")); - responseBuilder.setName(policyTypeObj.getString("name")); - responseBuilder.setPolicyType(policyTypeObj.getJSONObject("create_schema").toString()); + try { + String uri = nearRicUrlProvider.getPolicyTypeId(String.valueOf(input.getNearRtRicId()), + String.valueOf(input.getPolicyTypeId())); + ResponseEntity response = restAdapter.get(uri, String.class); + if (response.hasBody()) { + log.info("Response getPolicyType : {} ", response.getBody()); + JSONObject policyTypeObj = new JSONObject(response.getBody()); + responseBuilder.setDescription(policyTypeObj.getString("description")); + responseBuilder.setName(policyTypeObj.getString("name")); + responseBuilder.setPolicyType(policyTypeObj.getJSONObject("create_schema").toString()); + } + responseBuilder.setCode(response.getStatusCode().toString()); + } catch (NearRtRicNotFoundException ex) { + log.error("Caught exception: {}", ex); + responseBuilder.setCode(HttpStatus.INTERNAL_SERVER_ERROR.toString()); + } catch (RestClientResponseException ex) { + log.error("Caught exception: {}", ex); + responseBuilder.setCode(String.valueOf(ex.getRawStatusCode())); } log.info("End of getPolicyType"); RpcResult rpcResult = RpcResultBuilder.status(true) @@ -344,17 +420,26 @@ public class NonrtRicApiProvider implements AutoCloseable, A1ADAPTERAPIService { public ListenableFuture> getPolicyTypes( GetPolicyTypesInput input) { log.info("Start of getPolicyTypes"); - String uri = nearRicUrlProvider.getPolicyTypes(String.valueOf(input.getNearRtRicId())); - Optional> policyTypes = restAdapter.get(uri, List.class); GetPolicyTypesOutputBuilder responseBuilder = new GetPolicyTypesOutputBuilder(); - if (policyTypes.isPresent()) { - log.info("Response policyTypes.get() : {} ", policyTypes.get()); - List policyTypesListInteger = policyTypes.get(); - List policyTypesListLong = new ArrayList<>(); - for(Integer i : policyTypesListInteger){ - policyTypesListLong.add(i.longValue()); + try { + String uri = nearRicUrlProvider.getPolicyTypes(String.valueOf(input.getNearRtRicId())); + ResponseEntity> response = restAdapter.get(uri, List.class); + if (response.hasBody()) { + log.info("Response getPolicyTypes : {} ", response.getBody()); + List policyTypesListInteger = response.getBody(); + List policyTypesListLong = new ArrayList<>(); + for(Integer i : policyTypesListInteger){ + policyTypesListLong.add(i.longValue()); + } + responseBuilder.setPolicyTypeIdList(policyTypesListLong); } - responseBuilder.setPolicyTypeIdList(policyTypesListLong); + responseBuilder.setCode(response.getStatusCode().toString()); + } catch (NearRtRicNotFoundException ex) { + log.error("Caught exception: {}", ex); + responseBuilder.setCode(HttpStatus.INTERNAL_SERVER_ERROR.toString()); + } catch (RestClientResponseException ex) { + log.error("Caught exception: {}", ex); + responseBuilder.setCode(String.valueOf(ex.getRawStatusCode())); } log.info("End of getPolicyTypes"); RpcResult rpcResult = RpcResultBuilder.status(true) @@ -365,14 +450,24 @@ public class NonrtRicApiProvider implements AutoCloseable, A1ADAPTERAPIService { @Override public ListenableFuture> getStatus(GetStatusInput input) { log.info("Start of getStatus"); - String uri = nearRicUrlProvider.getPolicyInstanceIdStatus(String.valueOf(input.getNearRtRicId()), - String.valueOf(input.getPolicyTypeId()), String.valueOf(input.getPolicyInstanceId())); - Optional status = restAdapter.get(uri, String.class); GetStatusOutputBuilder responseBuilder = new GetStatusOutputBuilder(); - if (status.isPresent()) { - log.info("Response status.get() : {} ", status.get()); - JSONObject statusObj = new JSONObject(status.get()); - responseBuilder.setStatus(statusObj.getString("status")); + try { + String uri = nearRicUrlProvider.getPolicyInstanceIdStatus(String.valueOf(input.getNearRtRicId()), + String.valueOf(input.getPolicyTypeId()), String.valueOf(input.getPolicyInstanceId())); + ResponseEntity> response = restAdapter.get(uri, List.class); + if (response.hasBody()) { + log.info("Response getStatus : {} ", response.getBody()); + // only return the status of first handler for compliance with current yang model, ignore handler_id + JSONObject statusObj = new JSONObject(new Gson().toJson(response.getBody().get(0))); + responseBuilder.setStatus(statusObj.getString("status")); + } + responseBuilder.setCode(response.getStatusCode().toString()); + } catch (NearRtRicNotFoundException ex) { + log.error("Caught exception: {}", ex); + responseBuilder.setCode(HttpStatus.INTERNAL_SERVER_ERROR.toString()); + } catch (RestClientResponseException ex) { + log.error("Caught exception: {}", ex); + responseBuilder.setCode(String.valueOf(ex.getRawStatusCode())); } log.info("End of getStatus"); RpcResult rpcResult = diff --git a/sdnc-a1-controller/northbound/nonrt-ric-api/provider/src/main/java/org/onap/sdnc/northbound/restadpter/NearRicUrlProvider.java b/sdnc-a1-controller/northbound/nonrt-ric-api/provider/src/main/java/org/onap/sdnc/northbound/restadpter/NearRicUrlProvider.java index 14c34e4f..8c2d6923 100644 --- a/sdnc-a1-controller/northbound/nonrt-ric-api/provider/src/main/java/org/onap/sdnc/northbound/restadpter/NearRicUrlProvider.java +++ b/sdnc-a1-controller/northbound/nonrt-ric-api/provider/src/main/java/org/onap/sdnc/northbound/restadpter/NearRicUrlProvider.java @@ -27,6 +27,7 @@ import java.util.Enumeration; import java.util.HashMap; import java.util.List; import java.util.Properties; +import org.onap.sdnc.northbound.exceptions.NearRtRicNotFoundException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.web.util.UriComponentsBuilder; @@ -84,6 +85,9 @@ public class NearRicUrlProvider { * @return the base url */ public String getBaseUrl(final String nearRtRicId) { + if (!nearRicMap.containsKey(nearRtRicId)) { + throw new NearRtRicNotFoundException("NearRtRic with this ID is not known by the A1 controller"); + } String baseUrl = "http://" + nearRicMap.get(nearRtRicId) + "/a1-p/"; return UriComponentsBuilder.fromUriString(baseUrl).build().toString(); } @@ -114,7 +118,8 @@ public class NearRicUrlProvider { * @param policyTypeId Policy Type Id * @return the policy type id url */ - public String getPolicyTypeId(final String nearRtRicId, final String policyTypeId) { + public String getPolicyTypeId(final String nearRtRicId, + final String policyTypeId) { return UriComponentsBuilder.fromUriString(getBaseUrl(nearRtRicId)).pathSegment("policytypes") .pathSegment(policyTypeId).build().toString(); } @@ -125,7 +130,8 @@ public class NearRicUrlProvider { * @param policyTypeId Policy Type Id * @return the policy instances for the given policy type */ - public String getPolicyInstances(final String nearRtRicId, final String policyTypeId) { + public String getPolicyInstances(final String nearRtRicId, + final String policyTypeId) { return UriComponentsBuilder.fromUriString(getPolicyTypeId(nearRtRicId, policyTypeId)).pathSegment("policies") .build().toString(); } @@ -137,7 +143,8 @@ public class NearRicUrlProvider { * @param policyInstanceId Policy Instance Id * @return the policy instance id for the given policy type */ - public String getPolicyInstanceId(final String nearRtRicId, final String policyTypeId, final String policyInstanceId) { + public String getPolicyInstanceId(final String nearRtRicId, final String policyTypeId, + final String policyInstanceId) { return UriComponentsBuilder.fromUriString(getPolicyTypeId(nearRtRicId, policyTypeId)).pathSegment("policies") .pathSegment(policyInstanceId).build().toString(); } diff --git a/sdnc-a1-controller/northbound/nonrt-ric-api/provider/src/main/java/org/onap/sdnc/northbound/restadpter/RestAdapter.java b/sdnc-a1-controller/northbound/nonrt-ric-api/provider/src/main/java/org/onap/sdnc/northbound/restadpter/RestAdapter.java index 504c128d..8f13f22d 100644 --- a/sdnc-a1-controller/northbound/nonrt-ric-api/provider/src/main/java/org/onap/sdnc/northbound/restadpter/RestAdapter.java +++ b/sdnc-a1-controller/northbound/nonrt-ric-api/provider/src/main/java/org/onap/sdnc/northbound/restadpter/RestAdapter.java @@ -20,8 +20,7 @@ package org.onap.sdnc.northbound.restadpter; -import com.google.common.base.Optional; - +import org.springframework.http.ResponseEntity; /** * An interface to wrap the generic HTTP methods @@ -40,7 +39,7 @@ public interface RestAdapter { * @return the converted object */ - Optional get(final String url, final Class clazz); + ResponseEntity get(final String url, final Class clazz); /** * Create or update a resource by PUTting the given object to the URI. @@ -49,7 +48,7 @@ public interface RestAdapter { * @param request the String to be PUT (may be {@code null}) * @return the response code */ - Optional put(final String url, final String body); + ResponseEntity put(final String url, final String body); /** * Delete resource for the given object to the URI. @@ -57,6 +56,6 @@ public interface RestAdapter { * @param url the URL * @return the response code */ - Optional delete(final String url); + ResponseEntity delete(final String url); } diff --git a/sdnc-a1-controller/northbound/nonrt-ric-api/provider/src/main/java/org/onap/sdnc/northbound/restadpter/RestAdapterImpl.java b/sdnc-a1-controller/northbound/nonrt-ric-api/provider/src/main/java/org/onap/sdnc/northbound/restadpter/RestAdapterImpl.java index 215ebad4..c2c9522a 100644 --- a/sdnc-a1-controller/northbound/nonrt-ric-api/provider/src/main/java/org/onap/sdnc/northbound/restadpter/RestAdapterImpl.java +++ b/sdnc-a1-controller/northbound/nonrt-ric-api/provider/src/main/java/org/onap/sdnc/northbound/restadpter/RestAdapterImpl.java @@ -20,13 +20,11 @@ package org.onap.sdnc.northbound.restadpter; -import com.google.common.base.Optional; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.http.HttpEntity; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpMethod; -import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.web.client.RestTemplate; @@ -53,26 +51,23 @@ public class RestAdapterImpl implements RestAdapter { } @Override - public Optional get(String uri, Class clazz) { + public ResponseEntity get(String uri, Class clazz) { HttpEntity entity = getHttpEntity(null); - final ResponseEntity response = invokeHttpRequest(uri, HttpMethod.GET, clazz, entity); - return buildOptional(response); + return invokeHttpRequest(uri, HttpMethod.GET, clazz, entity); } @Override - public Optional put(String uri, String body) { + public ResponseEntity put(String uri, String body) { HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_JSON); HttpEntity entity = new HttpEntity(body, headers); - final ResponseEntity response = invokeHttpRequest(uri, HttpMethod.PUT, null, entity); - return buildOptional(response); + return invokeHttpRequest(uri, HttpMethod.PUT, null, entity); } @Override - public Optional delete(String uri) { + public ResponseEntity delete(String uri) { HttpEntity entity = getHttpEntity(null); - final ResponseEntity response = invokeHttpRequest(uri, HttpMethod.DELETE, null, entity); - return buildOptional(response); + return invokeHttpRequest(uri, HttpMethod.DELETE, null, entity); } @SuppressWarnings("unchecked") @@ -80,17 +75,4 @@ public class RestAdapterImpl implements RestAdapter { HttpEntity entity) { return (ResponseEntity) restTemplate.exchange(uri, httpMethod, entity, clazz); } - - private Optional buildOptional(ResponseEntity response) { - if (!response.getStatusCode().equals(HttpStatus.OK) - & !response.getStatusCode().equals(HttpStatus.CREATED) - & !response.getStatusCode().equals(HttpStatus.NO_CONTENT)) { - log.error("Failed to get the Response, Status Code = {}", response.getStatusCode()); - return Optional.absent(); - } - if (response.hasBody()) { - return Optional.of(response.getBody()); - } - return Optional.absent(); - } } diff --git a/sdnc-a1-controller/northbound/nonrt-ric-api/provider/src/test/java/org/onap/sdnc/northbound/NonrtRicApiProviderTest.java b/sdnc-a1-controller/northbound/nonrt-ric-api/provider/src/test/java/org/onap/sdnc/northbound/NonrtRicApiProviderTest.java index e67304c0..e9b86be4 100644 --- a/sdnc-a1-controller/northbound/nonrt-ric-api/provider/src/test/java/org/onap/sdnc/northbound/NonrtRicApiProviderTest.java +++ b/sdnc-a1-controller/northbound/nonrt-ric-api/provider/src/test/java/org/onap/sdnc/northbound/NonrtRicApiProviderTest.java @@ -23,8 +23,8 @@ package org.onap.sdnc.northbound; import static org.mockito.Matchers.anyObject; import static org.mockito.Matchers.eq; import static org.mockito.Mockito.when; -import com.google.common.base.Optional; import com.google.common.util.concurrent.ListenableFuture; +import com.google.gson.Gson; import java.util.ArrayList; import java.util.List; import java.util.concurrent.ExecutionException; @@ -42,10 +42,18 @@ import org.opendaylight.controller.md.sal.binding.api.DataBroker; import org.opendaylight.controller.md.sal.binding.api.NotificationPublishService; import org.opendaylight.controller.md.sal.binding.test.AbstractConcurrentDataBrokerTest; import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry; +import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev191002.CreatePolicyInstanceInputBuilder; +import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev191002.CreatePolicyInstanceOutput; import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev191002.CreatePolicyTypeInputBuilder; import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev191002.CreatePolicyTypeOutput; +import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev191002.DeletePolicyInstanceInputBuilder; +import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev191002.DeletePolicyInstanceOutput; +import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev191002.DeletePolicyTypeInputBuilder; +import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev191002.DeletePolicyTypeOutput; import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev191002.GetHealthCheckInputBuilder; import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev191002.GetHealthCheckOutput; +import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev191002.GetNearRTRICsInputBuilder; +import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev191002.GetNearRTRICsOutput; import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev191002.GetPolicyInstanceInputBuilder; import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev191002.GetPolicyInstanceOutput; import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev191002.GetPolicyInstancesInputBuilder; @@ -59,6 +67,8 @@ import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev19100 import org.opendaylight.yangtools.yang.common.RpcResult; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; /** * This class Tests all the methods in NonrtRicApiProvider @@ -93,6 +103,46 @@ public class NonrtRicApiProviderTest extends AbstractConcurrentDataBrokerTest { mockRpcProviderRegistry); } + @Test + public void testCreatePolicyInstance() throws InterruptedException, ExecutionException { + CreatePolicyInstanceInputBuilder inputBuilder = new CreatePolicyInstanceInputBuilder(); + inputBuilder.setNearRtRicId(nearRtRicId); + inputBuilder.setPolicyTypeId(policyTypeId); + inputBuilder.setPolicyInstanceId(policyTypeInstanceId); + Whitebox.setInternalState(nonrtRicApiProvider, "restAdapter", restAdapter); + String uri = nearRicUrlProvider.getPolicyInstanceId(inputBuilder.build().getNearRtRicId(), + String.valueOf(inputBuilder.build().getPolicyTypeId()), inputBuilder.getPolicyInstanceId()); + ResponseEntity createPolicyInstanceResponse = new ResponseEntity<>(HttpStatus.CREATED); + when(restAdapter.put(eq(uri), anyObject())).thenReturn(createPolicyInstanceResponse); + ListenableFuture> result = + nonrtRicApiProvider.createPolicyInstance(inputBuilder.build()); + Assert.assertEquals(String.valueOf(HttpStatus.CREATED.value()), result.get().getResult().getCode()); + } + + @Test + public void testDeletePolicyInstance() throws InterruptedException, ExecutionException { + DeletePolicyInstanceInputBuilder inputBuilder = new DeletePolicyInstanceInputBuilder(); + inputBuilder.setNearRtRicId(nearRtRicId); + inputBuilder.setPolicyTypeId(policyTypeId); + inputBuilder.setPolicyInstanceId(policyTypeInstanceId); + Whitebox.setInternalState(nonrtRicApiProvider, "restAdapter", restAdapter); + String uri = nearRicUrlProvider.getPolicyInstanceId(inputBuilder.build().getNearRtRicId(), + String.valueOf(inputBuilder.build().getPolicyTypeId()), inputBuilder.getPolicyInstanceId()); + ResponseEntity deletePolicyInstanceResponse = new ResponseEntity<>(HttpStatus.NO_CONTENT); + when(restAdapter.delete(eq(uri))).thenReturn(deletePolicyInstanceResponse); + ListenableFuture> result = + nonrtRicApiProvider.deletePolicyInstance(inputBuilder.build()); + Assert.assertEquals(String.valueOf(HttpStatus.NO_CONTENT.value()), result.get().getResult().getCode()); + } + + @Test + public void testGetNearRTRICs() throws InterruptedException, ExecutionException { + GetNearRTRICsInputBuilder inputBuilder = new GetNearRTRICsInputBuilder(); + ListenableFuture> result = + nonrtRicApiProvider.getNearRTRICs(inputBuilder.build()); + Assert.assertEquals(String.valueOf(HttpStatus.OK.value()), result.get().getResult().getCode()); + } + @Test public void testCreatePolicyType() throws InterruptedException, ExecutionException { CreatePolicyTypeInputBuilder inputBuilder = new CreatePolicyTypeInputBuilder(); @@ -101,11 +151,26 @@ public class NonrtRicApiProviderTest extends AbstractConcurrentDataBrokerTest { Whitebox.setInternalState(nonrtRicApiProvider, "restAdapter", restAdapter); String uri = nearRicUrlProvider.getPolicyTypeId(inputBuilder.build().getNearRtRicId(), String.valueOf(inputBuilder.build().getPolicyTypeId())); - Optional createPolicyTyperesponse = null; - when(restAdapter.put(eq(uri), anyObject())).thenReturn(createPolicyTyperesponse); + ResponseEntity createPolicyTypeResponse = new ResponseEntity<>(HttpStatus.CREATED); + when(restAdapter.put(eq(uri), anyObject())).thenReturn(createPolicyTypeResponse); ListenableFuture> result = nonrtRicApiProvider.createPolicyType(inputBuilder.build()); - Assert.assertEquals("Success", result.get().getResult().getStatus()); + Assert.assertEquals(String.valueOf(HttpStatus.CREATED.value()), result.get().getResult().getCode()); + } + + @Test + public void testDeletePolicyType() throws InterruptedException, ExecutionException { + DeletePolicyTypeInputBuilder inputBuilder = new DeletePolicyTypeInputBuilder(); + inputBuilder.setNearRtRicId(nearRtRicId); + inputBuilder.setPolicyTypeId(policyTypeId); + Whitebox.setInternalState(nonrtRicApiProvider, "restAdapter", restAdapter); + String uri = nearRicUrlProvider.getPolicyTypeId(inputBuilder.build().getNearRtRicId(), + String.valueOf(inputBuilder.build().getPolicyTypeId())); + ResponseEntity deletePolicyTypeResponse = new ResponseEntity<>(HttpStatus.NO_CONTENT); + when(restAdapter.delete(eq(uri))).thenReturn(deletePolicyTypeResponse); + ListenableFuture> result = + nonrtRicApiProvider.deletePolicyType(inputBuilder.build()); + Assert.assertEquals(String.valueOf(HttpStatus.NO_CONTENT.value()), result.get().getResult().getCode()); } @Test @@ -118,10 +183,12 @@ public class NonrtRicApiProviderTest extends AbstractConcurrentDataBrokerTest { String.valueOf(inputBuilder.build().getPolicyTypeId())); String policyType = "{\"name\":\"Policy type 1\",\"description\":\"PT 1\",\"policy_type_id\":1,\"create_schema\":{}}"; - when(restAdapter.get(eq(uri), anyObject())).thenReturn(Optional.of(policyType)); + ResponseEntity getPolicyTypeResponse = new ResponseEntity<>(policyType, HttpStatus.OK); + when(restAdapter.get(eq(uri), eq(String.class))).thenReturn(getPolicyTypeResponse); ListenableFuture> result = nonrtRicApiProvider.getPolicyType(inputBuilder.build()); Assert.assertEquals("Policy type 1", result.get().getResult().getName()); + Assert.assertEquals(String.valueOf(HttpStatus.OK.value()), result.get().getResult().getCode()); } @Test @@ -134,10 +201,12 @@ public class NonrtRicApiProviderTest extends AbstractConcurrentDataBrokerTest { policyTypesInteger.add(20001); List policyTypesLong = new ArrayList<>(); policyTypesLong.add(20001L); - when(restAdapter.get(eq(uri), eq(List.class))).thenReturn(Optional.of(policyTypesInteger)); + ResponseEntity getPolicyTypesResponse = new ResponseEntity<>(policyTypesInteger, HttpStatus.OK); + when(restAdapter.get(eq(uri), eq(List.class))).thenReturn(getPolicyTypesResponse); ListenableFuture> result = nonrtRicApiProvider.getPolicyTypes(inputBuilder.build()); Assert.assertEquals(policyTypesLong, result.get().getResult().getPolicyTypeIdList()); + Assert.assertEquals(String.valueOf(HttpStatus.OK.value()), result.get().getResult().getCode()); } @Test @@ -151,11 +220,12 @@ public class NonrtRicApiProviderTest extends AbstractConcurrentDataBrokerTest { String.valueOf(inputBuilder.build().getPolicyTypeId()), inputBuilder.getPolicyInstanceId()); String policyInstance = "{\"scope\":{\"ue_id\":\"2\"},\"statement\":{\"priority_level\":\"1\"},\"policy_id\":\"pi12\"}"; - when(restAdapter.get(eq(uri), eq(String.class))) - .thenReturn(Optional.of(policyInstance)); + ResponseEntity getPolicyInstanceResponse = new ResponseEntity<>(policyInstance, HttpStatus.OK); + when(restAdapter.get(eq(uri), eq(String.class))).thenReturn(getPolicyInstanceResponse); ListenableFuture> result = nonrtRicApiProvider.getPolicyInstance(inputBuilder.build()); Assert.assertEquals(policyInstance, result.get().getResult().getPolicyInstance()); + Assert.assertEquals(String.valueOf(HttpStatus.OK.value()), result.get().getResult().getCode()); } @Test @@ -168,10 +238,12 @@ public class NonrtRicApiProviderTest extends AbstractConcurrentDataBrokerTest { String.valueOf(inputBuilder.build().getPolicyTypeId())); List policyInstances = new ArrayList<>(); policyInstances.add("3d2157af-6a8f-4a7c-810f-38c2f824bf12"); - when(restAdapter.get(eq(uri), eq(List.class))).thenReturn(Optional.of(policyInstances)); + ResponseEntity getPolicyInstancesResponse = new ResponseEntity<>(policyInstances, HttpStatus.OK); + when(restAdapter.get(eq(uri), eq(List.class))).thenReturn(getPolicyInstancesResponse); ListenableFuture> result = nonrtRicApiProvider.getPolicyInstances(inputBuilder.build()); Assert.assertEquals(policyInstances, result.get().getResult().getPolicyInstanceIdList()); + Assert.assertEquals(String.valueOf(HttpStatus.OK.value()), result.get().getResult().getCode()); } @Test @@ -183,23 +255,27 @@ public class NonrtRicApiProviderTest extends AbstractConcurrentDataBrokerTest { Whitebox.setInternalState(nonrtRicApiProvider, "restAdapter", restAdapter); String uri = nearRicUrlProvider.getPolicyInstanceIdStatus(inputBuilder.build().getNearRtRicId(), String.valueOf(inputBuilder.build().getPolicyTypeId()), inputBuilder.getPolicyInstanceId()); - String policyInstanceStatus = "{\"status\":\"enforced\"}"; - when(restAdapter.get(eq(uri), eq(String.class))).thenReturn(Optional.of(policyInstanceStatus)); + String policyInstanceStatus = "[{\"handler_id\":\"NearRTRIC-1\",\"status\":\"enforced\"}]"; + ResponseEntity getStatusResponse = + new ResponseEntity<>(new Gson().fromJson(policyInstanceStatus, List.class), HttpStatus.OK); + when(restAdapter.get(eq(uri), eq(List.class))).thenReturn(getStatusResponse); ListenableFuture> result = nonrtRicApiProvider.getStatus(inputBuilder.build()); Assert.assertEquals("enforced", result.get().getResult().getStatus()); + Assert.assertEquals(String.valueOf(HttpStatus.OK.value()), result.get().getResult().getCode()); } @Test - public void testHealthCheck() throws InterruptedException, ExecutionException { + public void testGetHealthCheck() throws InterruptedException, ExecutionException { GetHealthCheckInputBuilder inputBuilder = new GetHealthCheckInputBuilder(); inputBuilder.setNearRtRicId(nearRtRicId); Whitebox.setInternalState(nonrtRicApiProvider, "restAdapter", restAdapter); String uri = nearRicUrlProvider.getHealthCheck(inputBuilder.build().getNearRtRicId()); - String healthCheckStatus = ""; - when(restAdapter.get(eq(uri), eq(String.class))).thenReturn(null); + ResponseEntity getHealthCheckResponse = new ResponseEntity<>(HttpStatus.OK); + when(restAdapter.get(eq(uri), eq(Object.class))).thenReturn(getHealthCheckResponse); ListenableFuture> result = nonrtRicApiProvider.getHealthCheck(inputBuilder.build()); Assert.assertEquals(true, result.get().getResult().isHealthStatus()); + Assert.assertEquals(String.valueOf(HttpStatus.OK.value()), result.get().getResult().getCode()); } }