X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=policy-agent%2Fsrc%2Fmain%2Fjava%2Forg%2Foransc%2Fpolicyagent%2Fclients%2FOscA1Client.java;h=1b7342f27ef7039a32f1ffd58e164cb9ff62d2ca;hb=d898170a0bb639b85a7f8770ddab2a3ccd181cfb;hp=273f27b89a0ad3089ffbe8f0117dd21623a5035e;hpb=934a146caf5c9d0f735f913375d55b59041b9db5;p=nonrtric.git diff --git a/policy-agent/src/main/java/org/oransc/policyagent/clients/OscA1Client.java b/policy-agent/src/main/java/org/oransc/policyagent/clients/OscA1Client.java index 273f27b8..1b7342f2 100644 --- a/policy-agent/src/main/java/org/oransc/policyagent/clients/OscA1Client.java +++ b/policy-agent/src/main/java/org/oransc/policyagent/clients/OscA1Client.java @@ -2,7 +2,7 @@ * ========================LICENSE_START================================= * O-RAN-SC * %% - * Copyright (C) 2019 Nordix Foundation + * Copyright (C) 2020 Nordix Foundation * %% * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -21,52 +21,135 @@ package org.oransc.policyagent.clients; import java.lang.invoke.MethodHandles; -import java.util.Collection; +import java.util.List; +import org.json.JSONObject; import org.oransc.policyagent.configuration.RicConfig; import org.oransc.policyagent.repository.Policy; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.web.util.UriComponentsBuilder; + +import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; public class OscA1Client implements A1Client { + private static final String URL_PREFIX = "/a1-p"; + + private static final String POLICY_TYPES = "/policytypes"; + private static final String CREATE_SCHEMA = "create_schema"; + private static final String TITLE = "title"; + + private static final String HEALTHCHECK = "/healthcheck"; + + private static final UriComponentsBuilder POLICY_TYPE_SCHEMA_URI = + UriComponentsBuilder.fromPath("/policytypes/{policy-type-name}"); + + private static final UriComponentsBuilder POLICY_URI = + UriComponentsBuilder.fromPath("/policytypes/{policy-type-name}/policies/{policy-id}"); + + private static final UriComponentsBuilder POLICY_IDS_URI = + UriComponentsBuilder.fromPath("/policytypes/{policy-type-name}/policies"); + + private static final UriComponentsBuilder POLICY_STATUS_URI = + UriComponentsBuilder.fromPath("/policytypes/{policy-type-name}/policies/{policy-id}/status"); + private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); - private final RicConfig ricConfig; + private final AsyncRestClient restClient; public OscA1Client(RicConfig ricConfig) { - this.ricConfig = ricConfig; - logger.debug("OscA1Client for ric: {}", this.ricConfig.name()); + String baseUrl = ricConfig.baseUrl() + URL_PREFIX; + this.restClient = new AsyncRestClient(baseUrl); + if (logger.isDebugEnabled()) { + logger.debug("OscA1Client for ric: {}", ricConfig.name()); + } + } + + public OscA1Client(AsyncRestClient restClient) { + this.restClient = restClient; } @Override - public Mono> getPolicyTypeIdentities() { - return Mono.error(new Exception("Not impl")); + public Mono> getPolicyTypeIdentities() { + return getPolicyTypeIds() // + .collectList(); } @Override - public Mono> getPolicyIdentities() { - return Mono.error(new Exception("Not impl")); + public Mono> getPolicyIdentities() { + return getPolicyTypeIds() // + .flatMap(this::getPolicyIdentitiesByType) // + .collectList(); } @Override public Mono getPolicyTypeSchema(String policyTypeId) { - return Mono.error(new Exception("Not impl")); + String uri = POLICY_TYPE_SCHEMA_URI.buildAndExpand(policyTypeId).toUriString(); + return restClient.get(uri) // + .flatMap(response -> getCreateSchema(response, policyTypeId)); } @Override public Mono putPolicy(Policy policy) { - return Mono.error(new Exception("Not impl")); + String uri = POLICY_URI.buildAndExpand(policy.type().name(), policy.id()).toUriString(); + return restClient.put(uri, policy.json()); } @Override - public Mono deletePolicy(String policyId) { - return Mono.error(new Exception("Not impl")); + public Mono deletePolicy(Policy policy) { + return deletePolicyById(policy.type().name(), policy.id()); } @Override public Mono getProtocolVersion() { - return Mono.error(new Exception("Not impl")); + return restClient.get(HEALTHCHECK) // + .flatMap(notUsed -> Mono.just(A1ProtocolType.OSC_V1)); + } + + @Override + public Flux deleteAllPolicies() { + return getPolicyTypeIds() // + .flatMap(this::deletePoliciesForType); } + @Override + public Mono getPolicyStatus(Policy policy) { + String uri = POLICY_STATUS_URI.buildAndExpand(policy.type().name(), policy.id()).toUriString(); + return restClient.get(uri); + + } + + private Flux getPolicyTypeIds() { + return restClient.get(POLICY_TYPES) // + .flatMapMany(JsonHelper::parseJsonArrayOfString); + } + + private Flux getPolicyIdentitiesByType(String typeId) { + return restClient.get(POLICY_IDS_URI.buildAndExpand(typeId).toUriString()) // + .flatMapMany(JsonHelper::parseJsonArrayOfString); + } + + private Mono getCreateSchema(String policyTypeResponse, String policyTypeId) { + try { + JSONObject obj = new JSONObject(policyTypeResponse); + JSONObject schemaObj = obj.getJSONObject(CREATE_SCHEMA); + schemaObj.put(TITLE, policyTypeId); + return Mono.just(schemaObj.toString()); + } catch (Exception e) { + String exceptionString = e.toString(); + logger.error("Unexpected response for policy type: {}, exception: {}", policyTypeResponse, exceptionString); + return Mono.error(e); + } + } + + private Mono deletePolicyById(String typeId, String policyId) { + String uri = POLICY_URI.buildAndExpand(typeId, policyId).toUriString(); + return restClient.delete(uri); + } + + private Flux deletePoliciesForType(String typeId) { + return getPolicyIdentitiesByType(typeId) // + .flatMap(policyId -> deletePolicyById(typeId, policyId)); + } }