policy_info_list:
$ref: '#/components/examples/policy_info_list'
schema:
- type: object
+ $ref: '#/components/schemas/policy_info_list'
description: Policies
"404":
content:
content:
application/json:
schema:
- type: object
+ $ref: '#/components/schemas/status_info'
examples:
status_info:
$ref: '#/components/examples/status_info'
content:
application/json:
schema:
- type: object
+ $ref: '#/components/schemas/ric_info'
examples:
ric_info:
$ref: '#/components/examples/ric_info'
policy_type_id_list:
$ref: '#/components/examples/policy_type_id_list'
schema:
- type: object
+ $ref: '#/components/schemas/policy_type_id_list'
description: Policy type IDs
"404":
content:
content:
application/json:
schema:
- type: object
+ $ref: '#/components/schemas/policy_info'
examples:
policy_info:
$ref: '#/components/examples/policy_info'
content:
application/json:
schema:
- type: object
+ type: string
description: Configuration
"404":
content:
content:
application/json:
schema:
- type: object
+ $ref: '#/components/schemas/ric_info_list'
examples:
ric_info_list:
$ref: '#/components/examples/ric_info_list'
content:
application/json:
schema:
- type: object
+ $ref: '#/components/schemas/service_status_list'
examples:
service_status_list:
$ref: '#/components/examples/service_status_list'
content:
application/json:
schema:
- type: object
+ $ref: '#/components/schemas/policy_type_definition'
examples:
policy_type_definition:
$ref: '#/components/examples/policy_type_definition'
policy_id_list:
$ref: '#/components/examples/policy_id_list'
schema:
- type: object
+ $ref: '#/components/schemas/policy_id_list'
description: Policy identities
"404":
content:
policy_status_info:
$ref: '#/components/examples/policy_status_info'
schema:
- type: object
+ $ref: '#/components/schemas/policy_status_info'
description: Policy status
"404":
content:
import org.onap.ccsdk.oran.a1policymanagementservice.configuration.ApplicationConfigParser;
import org.onap.ccsdk.oran.a1policymanagementservice.configuration.ConfigurationFile;
import org.onap.ccsdk.oran.a1policymanagementservice.controllers.api.v2.ConfigurationApi;
+import org.onap.ccsdk.oran.a1policymanagementservice.exceptions.ServiceException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
logger.warn("Configuration file not written, {}.", ioe.getMessage());
return ErrorResponse.createMono("Internal error when writing the configuration.",
HttpStatus.INTERNAL_SERVER_ERROR);
- } catch (Exception e) {
+ } catch (ServiceException e) {
return ErrorResponse.createMono(e, HttpStatus.BAD_REQUEST);
}
})
- .onErrorResume(error -> {
- return ErrorResponse.createMono(error.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
- });
+ .doOnError(error -> logger.error(error.getMessage()));
}
@Override
- public Mono<ResponseEntity<Object>> getConfiguration(final ServerWebExchange exchange) {
- try {
+ public Mono<ResponseEntity<String>> getConfiguration(final ServerWebExchange exchange) throws ServiceException {
Optional<JsonObject> rootObject = configurationFile.readFile();
if (rootObject.isPresent()) {
return Mono.just(new ResponseEntity<>(rootObject.get().toString(), HttpStatus.OK));
} else {
- return ErrorResponse.createMono("File does not exist", HttpStatus.NOT_FOUND);
+ throw new ServiceException("File does not exist", HttpStatus.NOT_FOUND);
}
- } catch (Exception e) {
- return ErrorResponse.createMono(e, HttpStatus.INTERNAL_SERVER_ERROR);
- }
}
-
}
return createMono(e.toString(), code);
}
- static ResponseEntity<Object> create(String text, HttpStatusCode code) {
+ public static ResponseEntity<Object> create(String text, HttpStatusCode code) {
logger.debug("Error response: {}, {}", code, text);
ErrorInfo p = new ErrorInfo(text, code.value());
String json = gson.toJson(p);
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
-import java.util.Map;
import lombok.Getter;
.create(); //
@Override
- public Mono<ResponseEntity<Object>> getPolicyTypeDefinition(String policyTypeId, ServerWebExchange exchange)
+ public Mono<ResponseEntity<PolicyTypeDefinition>> getPolicyTypeDefinition(String policyTypeId, ServerWebExchange exchange)
throws EntityNotFoundException, JsonProcessingException {
PolicyType type = policyTypes.getType(policyTypeId);
JsonNode node = objectMapper.readTree(type.getSchema());
}
@Override
- public Mono<ResponseEntity<Object>> getPolicyTypes(String ricId, String typeName, String compatibleWithVersion, ServerWebExchange exchange) throws Exception {
+ public Mono<ResponseEntity<PolicyTypeIdList>> getPolicyTypes(String ricId, String typeName, String compatibleWithVersion, ServerWebExchange exchange) throws Exception {
if (compatibleWithVersion != null && typeName == null) {
throw new ServiceException("Parameter " + Consts.COMPATIBLE_WITH_VERSION_PARAM + " can only be used when "
+ Consts.TYPE_NAME_PARAM + " is given", HttpStatus.BAD_REQUEST);
@Override
- public Mono<ResponseEntity<Object>> getPolicy(String policyId, final ServerWebExchange exchange)
+ public Mono<ResponseEntity<PolicyInfo>> getPolicy(String policyId, final ServerWebExchange exchange)
throws EntityNotFoundException {
Policy policy = policies.getPolicy(policyId);
return authorization.doAccessControl(exchange.getRequest().getHeaders().toSingleValueMap(), policy, AccessType.READ) //
- .map(x -> new ResponseEntity<>((Object) toPolicyInfo(policy), HttpStatus.OK)) //
- .onErrorResume(this::handleException);
+ .map(x -> new ResponseEntity<>(toPolicyInfo(policy), HttpStatus.OK)) //
+ .doOnError(error -> logger.error(error.getMessage()));
}
@Override
.flatMap(tuple -> {
Ric ric = tuple.getT1();
PolicyType type = tuple.getT2();
-
+ keepServiceAlive(policyInfoValue.getServiceId());
Policy policy = Policy.builder()
.id(policyInfoValue.getPolicyId())
.json(jsonString)
}
@Override
- public Mono<ResponseEntity<Object>> getPolicyInstances(String policyTypeId, String ricId, String serviceId, String typeName, ServerWebExchange exchange) throws Exception {
+ public Mono<ResponseEntity<PolicyInfoList>> getPolicyInstances(String policyTypeId, String ricId, String serviceId, String typeName, ServerWebExchange exchange) throws Exception {
if ((policyTypeId != null && this.policyTypes.get(policyTypeId) == null)) {
throw new EntityNotFoundException("Policy type identity not found");
}
.doOnError(e -> logger.debug("Unauthorized to read policy: {}", e.getMessage()))
.onErrorResume(e -> Mono.empty())
.collectList()
- .map(authPolicies -> new ResponseEntity<>((Object) policiesToJson(authPolicies), HttpStatus.OK))
- .onErrorResume(this::handleException);
+ .map(authPolicies -> new ResponseEntity<>(policiesToJson(authPolicies), HttpStatus.OK))
+ .doOnError(error -> logger.error(error.getMessage()));
}
@Override
- public Mono<ResponseEntity<Object>> getPolicyIds(String policyTypeId, String ricId, String serviceId, String typeName, ServerWebExchange exchange) throws Exception {
+ public Mono<ResponseEntity<PolicyIdList>> getPolicyIds(String policyTypeId, String ricId, String serviceId, String typeName, ServerWebExchange exchange) throws Exception {
if ((policyTypeId != null && this.policyTypes.get(policyTypeId) == null)) {
throw new EntityNotFoundException("Policy type not found");
}
.doOnError(e -> logger.debug("Unauthorized to read policy: {}", e.getMessage()))
.onErrorResume(e -> Mono.empty())
.collectList()
- .map(authPolicies -> new ResponseEntity<>((Object)toPolicyIdsJson(authPolicies), HttpStatus.OK))
- .onErrorResume(this::handleException);
+ .map(authPolicies -> new ResponseEntity<>(toPolicyIdsJson(authPolicies), HttpStatus.OK))
+ .doOnError(error -> logger.error(error.getMessage()));
}
@Override
- public Mono<ResponseEntity<Object>> getPolicyStatus(String policyId, ServerWebExchange exchange) throws Exception {
+ public Mono<ResponseEntity<PolicyStatusInfo>> getPolicyStatus(String policyId, ServerWebExchange exchange) throws Exception {
Policy policy = policies.getPolicy(policyId);
return authorization.doAccessControl(exchange.getRequest().getHeaders().toSingleValueMap(), policy, AccessType.READ) //
.flatMap(notUsed -> a1ClientFactory.createA1Client(policy.getRic())) //
.flatMap(client -> client.getPolicyStatus(policy).onErrorResume(e -> Mono.just("{}"))) //
.flatMap(status -> createPolicyStatus(policy, status))
- .onErrorResume(this::handleException);
+ .doOnError(error -> logger.error(error.getMessage()));
}
- private Mono<ResponseEntity<Object>> createPolicyStatus(Policy policy, String statusFromNearRic) {
+ private Mono<ResponseEntity<PolicyStatusInfo>> createPolicyStatus(Policy policy, String statusFromNearRic) {
- try {
PolicyStatusInfo policyStatusInfo = new PolicyStatusInfo();
policyStatusInfo.setLastModified(policy.getLastModified().toString());
policyStatusInfo.setStatus(fromJson(statusFromNearRic));
- String policyStatusInfoAsString = objectMapper.writeValueAsString(policyStatusInfo);
- return Mono.just(new ResponseEntity<>(policyStatusInfoAsString, HttpStatus.OK));
- } catch (JsonProcessingException ex) {
- throw new RuntimeException(ex);
- }
+ return Mono.just(new ResponseEntity<>(policyStatusInfo, HttpStatus.OK));
}
private void keepServiceAlive(String name) {
}
private PolicyInfo toPolicyInfo(Policy policy) {
- PolicyInfo policyInfo = new PolicyInfo()
- .policyId(policy.getId())
- .policyData(gson.fromJson(policy.getJson(), Map.class))
- .ricId(policy.getRic().id())
- .policytypeId(policy.getType().getId())
- .serviceId(policy.getOwnerServiceId())
- ._transient(policy.isTransient());
- if (!policy.getStatusNotificationUri().isEmpty()) {
- policyInfo.setStatusNotificationUri(policy.getStatusNotificationUri());
- }
- return policyInfo;
- }
-
- private String toPolicyInfoString(Policy policy) {
-
- try {
- return objectMapper.writeValueAsString(toPolicyInfo(policy));
- } catch (JsonProcessingException ex) {
- throw new RuntimeException(ex);
- }
+ try {
+ PolicyInfo policyInfo = new PolicyInfo()
+ .policyId(policy.getId())
+ .policyData(objectMapper.readTree(policy.getJson()))
+ .ricId(policy.getRic().id())
+ .policytypeId(policy.getType().getId())
+ .serviceId(policy.getOwnerServiceId())
+ ._transient(policy.isTransient());
+ if (!policy.getStatusNotificationUri().isEmpty()) {
+ policyInfo.setStatusNotificationUri(policy.getStatusNotificationUri());
+ }
+ return policyInfo;
+ } catch (JsonProcessingException ex) {
+ throw new RuntimeException(ex);
+ }
}
- private String policiesToJson(Collection<Policy> policies) {
+ private PolicyInfoList policiesToJson(Collection<Policy> policies) {
- try {
List<PolicyInfo> policiesList = new ArrayList<>(policies.size());
PolicyInfoList policyInfoList = new PolicyInfoList();
for (Policy policy : policies) {
policiesList.add(toPolicyInfo(policy));
}
policyInfoList.setPolicies(policiesList);
- return objectMapper.writeValueAsString(policyInfoList);
- } catch(JsonProcessingException ex) {
- throw new RuntimeException(ex);
- }
+ return policyInfoList;
}
private Object fromJson(String jsonStr) {
return gson.fromJson(jsonStr, Object.class);
}
- private String toPolicyTypeIdsJson(Collection<PolicyType> policyTypes) throws JsonProcessingException {
+ private PolicyTypeIdList toPolicyTypeIdsJson(Collection<PolicyType> policyTypes) {
+ List<String> policyTypeList = new ArrayList<>(policyTypes.size());
PolicyTypeIdList idList = new PolicyTypeIdList();
for (PolicyType policyType : policyTypes) {
- idList.addPolicytypeIdsItem(policyType.getId());
+ policyTypeList.add(policyType.getId());
}
-
- return objectMapper.writeValueAsString(idList);
+ idList.setPolicytypeIds(policyTypeList);
+ return idList;
}
- private String toPolicyIdsJson(Collection<Policy> policies) {
+ private PolicyIdList toPolicyIdsJson(Collection<Policy> policies) {
- try {
List<String> policyIds = new ArrayList<>(policies.size());
PolicyIdList idList = new PolicyIdList();
for (Policy policy : policies) {
policyIds.add(policy.getId());
}
idList.setPolicyIds(policyIds);
- return objectMapper.writeValueAsString(idList);
- } catch (JsonProcessingException ex) {
- throw new RuntimeException(ex);
- }
+ return idList;
}
}
* ========================LICENSE_START=================================
* ONAP : ccsdk oran
* ======================================================================
- * Copyright (C) 2019-2020 Nordix Foundation. All rights reserved.
+ * Copyright (C) 2019-2023 Nordix Foundation. All rights reserved.
* ======================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
"Either a Near-RT RIC identity or a Managed Element identity can be specified.<br>" //
+ "The intention with Managed Element identity is the ID used in O1 for accessing the traffical element (such as the ID of CU).";
@Override
- public Mono<ResponseEntity<Object>> getRic(
+ public Mono<ResponseEntity<RicInfo>> getRic(
final String managedElementId, final String ricId, final ServerWebExchange exchange)
throws Exception {
if (managedElementId != null && ricId != null) {
throw new InvalidRequestException("Give one query parameter");
} else if (managedElementId != null) {
Ric ric = this.rics.lookupRicForManagedElement(managedElementId);
- return Mono.just(new ResponseEntity<>(objectMapper.writeValueAsString(toRicInfo(ric)), HttpStatus.OK));
+ return Mono.just(new ResponseEntity<>(toRicInfo(ric), HttpStatus.OK));
} else if (ricId != null) {
RicInfo info = toRicInfo(this.rics.getRic(ricId));
- return Mono.just(new ResponseEntity<>(objectMapper.writeValueAsString(info), HttpStatus.OK));
+ return Mono.just(new ResponseEntity<>(info, HttpStatus.OK));
} else {
throw new InvalidRequestException("Give one query parameter");
}
"The call returns all Near-RT RICs that supports a given policy type identity";
@Override
- public Mono<ResponseEntity<Object>> getRics(final String supportingPolicyType, final ServerWebExchange exchange)
+ public Mono<ResponseEntity<RicInfoList>> getRics(final String supportingPolicyType, final ServerWebExchange exchange)
throws Exception {
if ((supportingPolicyType != null) && (this.types.get(supportingPolicyType) == null)) {
throw new EntityNotFoundException("Policy type not found");
}
}
- return Mono.just(new ResponseEntity<>(objectMapper.writeValueAsString(new RicInfoList().rics(result)), HttpStatus.OK));
+ return Mono.just(new ResponseEntity<>(new RicInfoList().rics(result), HttpStatus.OK));
}
private RicInfo.StateEnum toRicState(Ric.RicState state) {
"Either information about a registered service with given identity or all registered services are returned.";
@Override
- public Mono<ResponseEntity<Object>> getServices(final String name, final ServerWebExchange exchange) throws Exception {
+ public Mono<ResponseEntity<ServiceStatusList>> getServices(final String name, final ServerWebExchange exchange) throws Exception {
if (name != null && this.services.get(name) == null) {
- return ErrorResponse.createMono("Service not found", HttpStatus.NOT_FOUND);
+ throw new ServiceException("Service not found", HttpStatus.NOT_FOUND);
}
List<ServiceStatus> servicesStatus = new ArrayList<>();
servicesStatus.add(toServiceStatus(s));
}
}
- String res = objectMapper.writeValueAsString(new ServiceStatusList().serviceList(servicesStatus));
- return Mono.just(new ResponseEntity<>(res, HttpStatus.OK));
+ return Mono.just(new ResponseEntity<>(new ServiceStatusList().serviceList(servicesStatus), HttpStatus.OK));
}
private ServiceStatus toServiceStatus(Service s) {
}
@Override
- @PutMapping(Consts.V2_API_ROOT + "/services/{service_id}/keepalive")
- public Mono<ResponseEntity<Object>> keepAliveService(final String serviceId, final ServerWebExchange exchange) {
- try {
+ public Mono<ResponseEntity<Object>> keepAliveService(final String serviceId, final ServerWebExchange exchange) throws ServiceException {
+
services.getService(serviceId).keepAlive();
return Mono.just(new ResponseEntity<>(HttpStatus.OK));
- } catch (ServiceException e) {
- return ErrorResponse.createMono(e, HttpStatus.NOT_FOUND);
- }
}
private Service removeService(String name) throws ServiceException {
package org.onap.ccsdk.oran.a1policymanagementservice.controllers.v2;
-import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.onap.ccsdk.oran.a1policymanagementservice.controllers.api.v2.HealthCheckApi;
+import org.onap.ccsdk.oran.a1policymanagementservice.models.v2.StatusInfo;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RestController;
public static final String API_NAME = "Health Check";
public static final String API_DESCRIPTION = "";
- @Schema(name = "status_info_v2")
- class StatusInfo {
- @Schema(description = "status text")
- public final String status;
-
- StatusInfo(String status) {
- this.status = status;
- }
- }
-
@Override
- public Mono<ResponseEntity<Object>> getStatus(final ServerWebExchange exchange) {
- StatusInfo info = new StatusInfo("success");
+ public Mono<ResponseEntity<StatusInfo>> getStatus(final ServerWebExchange exchange) {
+ StatusInfo info = new StatusInfo().status("success");
return Mono.just(new ResponseEntity<>(info, HttpStatus.OK));
}
* ========================LICENSE_START=================================
* ONAP : ccsdk oran
* ======================================================================
- * Copyright (C) 2019-2020 Nordix Foundation. All rights reserved.
+ * Copyright (C) 2019-2023 Nordix Foundation. All rights reserved.
* ======================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
import java.lang.invoke.MethodHandles;
import org.onap.ccsdk.oran.a1policymanagementservice.controllers.v2.ErrorResponse;
+import org.onap.ccsdk.oran.a1policymanagementservice.controllers.v2.PolicyController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
loggerx.error("Runtime exception {}", ex.getMessage());
return ErrorResponse.create(ex, HttpStatus.INTERNAL_SERVER_ERROR);
}
-}
+
+ @ExceptionHandler(PolicyController.RejectionException.class)
+ public final ResponseEntity<Object> handleRejectionException(PolicyController.RejectionException ex) {
+ loggerx.error("Rejection exception {}", ex.getMessage());
+ return ErrorResponse.create(ex, ex.getStatus());
+ }
+}
\ No newline at end of file
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.http.HttpStatus;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
public synchronized Service getService(String name) throws ServiceException {
Service service = registeredServices.get(name);
if (service == null) {
- throw new ServiceException("Could not find service: " + name);
+ throw new ServiceException("Could not find service: " + name, HttpStatus.NOT_FOUND);
}
return service;
}
import static org.assertj.core.api.Assertions.assertThat;
import static org.awaitility.Awaitility.await;
-import static org.junit.jupiter.api.Assertions.assertEquals;
-import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doReturn;
import org.onap.ccsdk.oran.a1policymanagementservice.controllers.authorization.PolicyAuthorizationRequest.Input.AccessType;
import org.onap.ccsdk.oran.a1policymanagementservice.exceptions.ServiceException;
import org.onap.ccsdk.oran.a1policymanagementservice.models.v2.RicInfo;
-import org.onap.ccsdk.oran.a1policymanagementservice.models.v2.PolicyTypeDefinition;
import org.onap.ccsdk.oran.a1policymanagementservice.models.v2.PolicyTypeIdList;
import org.onap.ccsdk.oran.a1policymanagementservice.models.v2.PolicyInfo;
import org.onap.ccsdk.oran.a1policymanagementservice.models.v2.PolicyInfoList;
{
String response = restClient().get(url).block();
PolicyInfo policyInfo = objectMapper.readValue(response, PolicyInfo.class);
- String policyData = gson.toJson(policyInfo.getPolicyData());
- assertThat(policyData).isEqualTo(policy.getJson());
+ String expectedResponse = "{\"ric_id\":\"ric1\",\"service_id\":\"service1\",\"policy_id\":\"id\",\"policy_data\":{\"servingCellNrcgi\":\"1\"},\"status_notification_uri\":\"/policy-status?id=XXX\",\"policytype_id\":\"typeName\",\"transient\":false}";
+ assertEquals(objectMapper.readTree(expectedResponse), objectMapper.readTree(response));
}
{
policies.remove(policy);
@Test
@DisplayName("test Get Service Status")
- void testGetServiceStatus() throws Exception {
+ void testGetServiceStatus() {
String url = "/status";
String rsp = restClient().get(url).block();
assertThat(rsp).contains("success");
}
private Policy addPolicy(String id, String typeName, String service, String ric) throws ServiceException {
- addRic(ric);
- Policy policy = Policy.builder()
- .id(id)
- .json(gson.toJson(jsonString()))
- .ownerServiceId(service)
- .ric(rics.getRic(ric))
- .type(addPolicyType(typeName, ric))
- .lastModified(Instant.now())
- .isTransient(false)
- .statusNotificationUri("/policy-status?id=XXX")
- .build();
- policies.put(policy);
- return policy;
+ try {
+ addRic(ric);
+ Policy policy = Policy.builder()
+ .id(id)
+ .json(objectMapper.writeValueAsString(jsonString()))
+ .ownerServiceId(service)
+ .ric(rics.getRic(ric))
+ .type(addPolicyType(typeName, ric))
+ .lastModified(Instant.now())
+ .isTransient(false)
+ .statusNotificationUri("/policy-status?id=XXX")
+ .build();
+ policies.put(policy);
+ return policy;
+ } catch (JsonProcessingException ex) {
+ throw new RuntimeException(ex);
+ }
}
private Policy addPolicy(String id, String typeName, String service) throws ServiceException {
String body = createServiceJson(name, keepAliveIntervalSeconds);
ResponseEntity<String> resp = restClient().putForEntity(url, body).block();
if (expectedStatus != null) {
+ assertNotNull(resp);
assertEquals(expectedStatus, resp.getStatusCode(), "");
}
}
return true;
}
- private MockA1Client getA1Client(String ricId) throws ServiceException {
+ private MockA1Client getA1Client(String ricId) {
return a1ClientFactory.getOrCreateA1Client(ricId);
}