From 35e7ea7bb7a6e2890542f3535aa09b11b2f079a1 Mon Sep 17 00:00:00 2001 From: RehanRaza Date: Thu, 19 Dec 2019 16:47:06 +0100 Subject: [PATCH] Modify method calls in A1 client Change-Id: I6b82687c09f238e3c60c85e6c92491c3f02ec69f Issue-ID: NONRTRIC-80 Signed-off-by: RehanRaza --- .../org/oransc/policyagent/clients/A1Client.java | 6 +- .../oransc/policyagent/clients/A1ClientImpl.java | 68 ++++++++-------------- .../org/oransc/policyagent/MockPolicyAgent.java | 15 +++-- 3 files changed, 38 insertions(+), 51 deletions(-) diff --git a/policy-agent/src/main/java/org/oransc/policyagent/clients/A1Client.java b/policy-agent/src/main/java/org/oransc/policyagent/clients/A1Client.java index bed839b7..0cc6903e 100644 --- a/policy-agent/src/main/java/org/oransc/policyagent/clients/A1Client.java +++ b/policy-agent/src/main/java/org/oransc/policyagent/clients/A1Client.java @@ -25,11 +25,11 @@ import reactor.core.publisher.Mono; public interface A1Client { - public Flux getAllPolicyTypes(String nearRtRicUrl); + public Flux getPolicyTypeIdentities(String nearRtRicUrl); - public Flux getPoliciesForType(String nearRtRicUrl, String policyTypeId); + public Flux getPolicyIdentities(String nearRtRicUrl); - public Mono getPolicy(String nearRtRicUrl, String policyId); + public Mono getPolicyType(String nearRtRicUrl, String policyTypeId); public Mono putPolicy(String nearRtRicUrl, String policyId, String policyString); 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 5a16bc27..7bbb4990 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 @@ -41,27 +41,27 @@ public class A1ClientImpl implements A1Client { } @Override - public Flux getAllPolicyTypes(String nearRtRicUrl) { - logger.debug("getAllPolicyTypes nearRtRicUrl = {}", nearRtRicUrl); + public Flux getPolicyTypeIdentities(String nearRtRicUrl) { + logger.debug("getPolicyTypeIdentities nearRtRicUrl = {}", nearRtRicUrl); AsyncRestClient client = new AsyncRestClient(getBaseUrl(nearRtRicUrl)); - Mono response = client.get("/policytypes"); - return response.flatMapMany(this::createPolicyTypesFlux); + Mono response = client.get("/policytypes/identities"); + return response.flatMapMany(this::createFlux); } @Override - public Flux getPoliciesForType(String nearRtRicUrl, String policyTypeId) { - logger.debug("getPoliciesForType nearRtRicUrl = {}, policyTypeId = {}", nearRtRicUrl, policyTypeId); + public Flux getPolicyIdentities(String nearRtRicUrl) { + logger.debug("getPolicyIdentities nearRtRicUrl = {}", nearRtRicUrl); AsyncRestClient client = new AsyncRestClient(getBaseUrl(nearRtRicUrl)); - return client.get("/policies") // - .flatMapMany(policiesString -> createPoliciesFlux(policiesString, policyTypeId)); + Mono response = client.get("/policies/identities"); + return response.flatMapMany(this::createFlux); } @Override - public Mono getPolicy(String nearRtRicUrl, String policyId) { - logger.debug("getPolicy nearRtRicUrl = {}, policyId = {}", nearRtRicUrl, policyId); + public Mono getPolicyType(String nearRtRicUrl, String policyTypeId) { + logger.debug("getPolicyType nearRtRicUrl = {}, policyTypeId = {}", nearRtRicUrl, policyTypeId); AsyncRestClient client = new AsyncRestClient(getBaseUrl(nearRtRicUrl)); - Mono response = client.get("/policies/" + policyId); - return response.flatMap(this::createPolicyMono); + Mono response = client.get("/policytypes/" + policyTypeId); + return response.flatMap(this::createMono); } @Override @@ -70,7 +70,7 @@ public class A1ClientImpl implements A1Client { policyString); AsyncRestClient client = new AsyncRestClient(getBaseUrl(nearRtRicUrl)); Mono response = client.put("/policies/" + policyId, policyString); - return response.flatMap(this::createPolicyMono); + return response.flatMap(this::createMono); } @Override @@ -80,46 +80,28 @@ public class A1ClientImpl implements A1Client { return client.delete("/policies/" + policyId); } - private Flux createPolicyTypesFlux(String policyTypesString) { + private Flux createFlux(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 Flux.fromIterable(arrayList); } catch (JSONException ex) { // invalid json return Flux.error(ex); } } - private Flux createPoliciesFlux(String policiesString, String policyTypeId) { + private Mono createMono(String inputString) { 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); - } - } - - private Mono createPolicyMono(String policyString) { - 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); - } } } diff --git a/policy-agent/src/test/java/org/oransc/policyagent/MockPolicyAgent.java b/policy-agent/src/test/java/org/oransc/policyagent/MockPolicyAgent.java index dda64269..4074b995 100644 --- a/policy-agent/src/test/java/org/oransc/policyagent/MockPolicyAgent.java +++ b/policy-agent/src/test/java/org/oransc/policyagent/MockPolicyAgent.java @@ -35,6 +35,7 @@ import org.oransc.policyagent.clients.A1Client; import org.oransc.policyagent.configuration.ApplicationConfig; import org.oransc.policyagent.repository.ImmutablePolicyType; import org.oransc.policyagent.repository.Policies; +import org.oransc.policyagent.repository.Policy; import org.oransc.policyagent.repository.PolicyType; import org.oransc.policyagent.repository.PolicyTypes; import org.oransc.policyagent.repository.Rics; @@ -80,7 +81,7 @@ public class MockPolicyAgent { } @Override - public Flux getAllPolicyTypes(String nearRtRicUrl) { + public Flux getPolicyTypeIdentities(String nearRtRicUrl) { Vector result = new Vector<>(); for (PolicyType p : this.policyTypes.getAll()) { result.add(p.name()); @@ -89,14 +90,18 @@ public class MockPolicyAgent { } @Override - public Flux getPoliciesForType(String nearRtRicUrl, String policyTypeId) { - return Flux.empty(); + public Flux getPolicyIdentities(String nearRtRicUrl) { + Vector result = new Vector<>(); + for (Policy p : this.policies.getAll()) { + result.add(p.id()); + } + return Flux.fromIterable(result); } @Override - public Mono getPolicy(String nearRtRicUrl, String policyId) { + public Mono getPolicyType(String nearRtRicUrl, String policyTypeId) { try { - return Mono.just(this.policies.get(policyId).json()); + return Mono.just(this.policyTypes.getType(policyTypeId).jsonSchema()); } catch (Exception e) { return Mono.error(e); } -- 2.16.6