X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=policy-agent%2Fsrc%2Fmain%2Fjava%2Forg%2Foransc%2Fpolicyagent%2Fclients%2FA1ClientImpl.java;h=e576638588fe8ff6eb986a921011fb312fc2d6ea;hb=7a4a590fb0ebf8772169625cdda327da43c79c6d;hp=4f7a9ddfa6e28b8c33572a01080c4935eb087079;hpb=4a112834cf7ea69f230fde864856093ecadb9cfe;p=nonrtric.git diff --git a/policy-agent/src/main/java/org/oransc/policyagent/clients/A1ClientImpl.java b/policy-agent/src/main/java/org/oransc/policyagent/clients/A1ClientImpl.java index 4f7a9ddf..e5766385 100644 --- a/policy-agent/src/main/java/org/oransc/policyagent/clients/A1ClientImpl.java +++ b/policy-agent/src/main/java/org/oransc/policyagent/clients/A1ClientImpl.java @@ -17,19 +17,20 @@ * limitations under the License. * ========================LICENSE_END=================================== */ + package org.oransc.policyagent.clients; import java.lang.invoke.MethodHandles; import java.util.ArrayList; +import java.util.Collection; import java.util.List; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; +import org.oransc.policyagent.repository.Policy; import org.slf4j.Logger; import org.slf4j.LoggerFactory; - -import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; public class A1ClientImpl implements A1Client { @@ -39,86 +40,76 @@ public class A1ClientImpl implements A1Client { return nearRtRicUrl + "/A1-P/v1"; } + protected AsyncRestClient createClient(final String nearRtRicUrl) { + return new AsyncRestClient(getBaseUrl(nearRtRicUrl)); + } + @Override - public Flux getAllPolicyTypes(String nearRtRicUrl) { - logger.debug("getAllPolicyTypes nearRtRicUrl = {}", nearRtRicUrl); - AsyncRestClient client = new AsyncRestClient(getBaseUrl(nearRtRicUrl)); - Mono response = client.get("/policytypes"); - return response.flatMapMany(this::createPolicyTypesFlux); + public Mono> getPolicyTypeIdentities(String nearRtRicUrl) { + logger.debug("getPolicyTypeIdentities nearRtRicUrl = {}", nearRtRicUrl); + AsyncRestClient client = createClient(nearRtRicUrl); + return client.get("/policytypes/identities") // + .flatMap(this::parseJsonArrayOfString); } @Override - public Flux getPoliciesForType(String nearRtRicUrl, String policyTypeId) { - logger.debug("getPoliciesForType nearRtRicUrl = {}, policyTypeId = {}", nearRtRicUrl, policyTypeId); - AsyncRestClient client = new AsyncRestClient(getBaseUrl(nearRtRicUrl)); - return client.get("/policies") // - .flatMapMany(policiesString -> createPoliciesFlux(policiesString, policyTypeId)); + public Mono> getPolicyIdentities(String nearRtRicUrl) { + logger.debug("getPolicyIdentities nearRtRicUrl = {}", nearRtRicUrl); + AsyncRestClient client = createClient(nearRtRicUrl); + return client.get("/policies/identities") // + .flatMap(this::parseJsonArrayOfString); } @Override - public Mono getPolicy(String nearRtRicUrl, String policyId) { - logger.debug("getPolicy nearRtRicUrl = {}, policyId = {}", nearRtRicUrl, policyId); - AsyncRestClient client = new AsyncRestClient(getBaseUrl(nearRtRicUrl)); - Mono response = client.get("/policies/" + policyId); - return response.flatMap(this::createPolicyMono); + public Mono getPolicyType(String nearRtRicUrl, String policyTypeId) { + logger.debug("getPolicyType nearRtRicUrl = {}, policyTypeId = {}", nearRtRicUrl, policyTypeId); + AsyncRestClient client = createClient(nearRtRicUrl); + Mono response = client.get("/policytypes/" + policyTypeId); + return response.flatMap(this::createMono); } @Override - public Mono putPolicy(String nearRtRicUrl, String policyId, String policyString) { - logger.debug("putPolicy nearRtRicUrl = {}, policyId = {}, policyString = {}", nearRtRicUrl, policyId, - policyString); - AsyncRestClient client = new AsyncRestClient(getBaseUrl(nearRtRicUrl)); - Mono response = client.put("/policies/" + policyId, policyString); - return response.flatMap(this::createPolicyMono); + public Mono putPolicy(Policy policy) { + logger.debug("putPolicy nearRtRicUrl = {}, policyId = {}, policyString = {}", // + policy.ric().getConfig().baseUrl(), policy.id(), policy.json()); + AsyncRestClient client = createClient(policy.ric().getConfig().baseUrl()); + // TODO update when simulator is updated to include policy type + // Mono response = client.put("/policies/" + policy.id() + "?policyTypeId=" + policy.type().name(), + // policy.json()); + Mono response = client.put("/policies/" + policy.id(), policy.json()); + + return response.flatMap(this::createMono); } @Override - public Mono deletePolicy(String nearRtRicUrl, String policyId) { + public Mono deletePolicy(String nearRtRicUrl, String policyId) { logger.debug("deletePolicy nearRtRicUrl = {}, policyId = {}", nearRtRicUrl, policyId); - AsyncRestClient client = new AsyncRestClient(getBaseUrl(nearRtRicUrl)); + AsyncRestClient client = createClient(nearRtRicUrl); return client.delete("/policies/" + policyId); } - private Flux createPolicyTypesFlux(String policyTypesString) { + private Mono> parseJsonArrayOfString(String inputString) { try { - List policyTypesList = new ArrayList<>(); - JSONArray policyTypesArray = new JSONArray(policyTypesString); - for (int i = 0; i < policyTypesArray.length(); i++) { - policyTypesList.add(policyTypesArray.getJSONObject(i).toString()); + List arrayList = new ArrayList<>(); + JSONArray jsonArray = new JSONArray(inputString); + for (int i = 0; i < jsonArray.length(); i++) { + arrayList.add(jsonArray.getString(i)); } - logger.debug("A1 client: policyTypes = {}", policyTypesList); - return Flux.fromIterable(policyTypesList); + logger.debug("A1 client: received list = {}", arrayList); + return Mono.just(arrayList); } catch (JSONException ex) { // invalid json - return Flux.error(ex); - } - } - - private Flux createPoliciesFlux(String policiesString, String policyTypeId) { - try { - List policiesList = new ArrayList<>(); - JSONArray policiesArray = new JSONArray(policiesString); - for (int i = 0; i < policiesArray.length(); i++) { - JSONObject policyObject = policiesArray.getJSONObject(i); - if (policyObject.get("policyTypeId").equals(policyTypeId)) { - policiesList.add(policyObject.toString()); - } - } - logger.debug("A1 client: policies = {}", policiesList); - return Flux.fromIterable(policiesList); - } catch (JSONException ex) { // invalid json - return Flux.error(ex); + return Mono.error(ex); } } - private Mono createPolicyMono(String policyString) { + private Mono createMono(String inputString) { try { - JSONObject policyObject = new JSONObject(policyString); - String policy = policyObject.toString(); - logger.debug("A1 client: policy = {}", policy); - return Mono.just(policy); + JSONObject jsonObject = new JSONObject(inputString); + String jsonString = jsonObject.toString(); + logger.debug("A1 client: received string = {}", jsonString); + return Mono.just(jsonString); } catch (JSONException ex) { // invalid json return Mono.error(ex); - } } }