Add A1 Client in Policy Agent 10/2010/1
authorRehanRaza <muhammad.rehan.raza@est.tech>
Thu, 12 Dec 2019 10:05:55 +0000 (11:05 +0100)
committerRehanRaza <muhammad.rehan.raza@est.tech>
Thu, 12 Dec 2019 10:20:10 +0000 (11:20 +0100)
Change-Id: I9b10f5c0dbd1d8d0aef748c24a884b8671ef8863
Issue-ID: NONRTRIC-80
Signed-off-by: RehanRaza <muhammad.rehan.raza@est.tech>
policy-agent/pom.xml
policy-agent/src/main/java/org/oransc/policyagent/clients/A1Client.java [new file with mode: 0644]
policy-agent/src/main/java/org/oransc/policyagent/clients/AsyncRestClient.java [new file with mode: 0644]

index cca5bf6..2624596 100644 (file)
             <artifactId>gson</artifactId>
             <version>${immutable.version}</version>
         </dependency>
+        <dependency>
+            <groupId>org.json</groupId>
+            <artifactId>json</artifactId>
+            <version>20180130</version>
+        </dependency>
         <!--TEST -->
         <dependency>
             <groupId>org.springframework.boot</groupId>
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
new file mode 100644 (file)
index 0000000..39fbc37
--- /dev/null
@@ -0,0 +1,105 @@
+/*-
+ * ========================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.policyagent.clients;
+
+import java.lang.invoke.MethodHandles;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.json.JSONArray;
+import org.json.JSONObject;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import reactor.core.publisher.Flux;
+import reactor.core.publisher.Mono;
+
+public class A1Client {
+    private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
+
+    public String getBaseUrl(final String nearRtRicUrl) {
+        return nearRtRicUrl + "/A1-P/v1";
+    }
+
+    public Flux<String> getAllPolicyTypes(String nearRtRicUrl) {
+        logger.debug("getAllPolicyTypes nearRtRicUrl = {}", nearRtRicUrl);
+        AsyncRestClient client = new AsyncRestClient(getBaseUrl(nearRtRicUrl));
+        Mono<String> response = client.get("/policytypes");
+        return response.flatMapMany(this::createPolicyTypesFlux);
+    }
+
+    public Flux<String> getPoliciesForType(String nearRtRicUrl, String policyTypeId) {
+        logger.debug("getPoliciesForType nearRtRicUrl = {}, policyTypeId = {}", nearRtRicUrl, policyTypeId);
+        AsyncRestClient client = new AsyncRestClient(getBaseUrl(nearRtRicUrl));
+        Mono<String> response = client.get("/policies");
+        return response.flatMapMany(policiesString -> createPoliciesFlux(policiesString, policyTypeId));
+    }
+
+    public Mono<String> getPolicy(String nearRtRicUrl, String policyId) {
+        logger.debug("getPolicy nearRtRicUrl = {}, policyId = {}", nearRtRicUrl, policyId);
+        AsyncRestClient client = new AsyncRestClient(getBaseUrl(nearRtRicUrl));
+        Mono<String> response = client.get("/policies/" + policyId);
+        return response.flatMap(this::createPolicyMono);
+    }
+
+    public Mono<String> putPolicy(String nearRtRicUrl, String policyId, String policyString) {
+        logger.debug("putPolicy nearRtRicUrl = {}, policyId = {}, policyString = {}", nearRtRicUrl, policyId,
+            policyString);
+        AsyncRestClient client = new AsyncRestClient(getBaseUrl(nearRtRicUrl));
+        Mono<String> response = client.put("/policies/" + policyId, policyString);
+        return response.flatMap(this::createPolicyMono);
+    }
+
+    public Mono<Void> deletePolicy(String nearRtRicUrl, String policyId) {
+        logger.debug("deletePolicy nearRtRicUrl = {}, policyId = {}", nearRtRicUrl, policyId);
+        AsyncRestClient client = new AsyncRestClient(getBaseUrl(nearRtRicUrl));
+        return client.delete("/policies/" + policyId);
+    }
+
+    private Flux<String> createPolicyTypesFlux(String policyTypesString) {
+        List<String> policyTypesList = new ArrayList<>();
+        JSONArray policyTypesArray = new JSONArray(policyTypesString);
+        for (int i = 0; i < policyTypesArray.length(); i++) {
+            policyTypesList.add(policyTypesArray.getJSONObject(i).toString());
+        }
+        logger.debug("A1 client: policyTypes = {}", policyTypesList);
+        return Flux.fromIterable(policyTypesList);
+    }
+
+    private Flux<String> createPoliciesFlux(String policiesString, String policyTypeId) {
+        List<String> 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);
+    }
+
+    private Mono<String> createPolicyMono(String policyString) {
+        // remove white-spaces
+        policyString = policyString.replaceAll("\\s+", "");
+        logger.debug("A1 client: policy = {}", policyString);
+        return Mono.just(policyString);
+    }
+}
diff --git a/policy-agent/src/main/java/org/oransc/policyagent/clients/AsyncRestClient.java b/policy-agent/src/main/java/org/oransc/policyagent/clients/AsyncRestClient.java
new file mode 100644 (file)
index 0000000..c69afb2
--- /dev/null
@@ -0,0 +1,55 @@
+/*-
+ * ========================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.policyagent.clients;
+
+import org.springframework.http.MediaType;
+import org.springframework.web.reactive.function.client.WebClient;
+import reactor.core.publisher.Mono;
+
+public class AsyncRestClient {
+    private final WebClient client;
+
+    public AsyncRestClient(String baseUrl) {
+        this.client = WebClient.create(baseUrl);
+    }
+
+    public Mono<String> put(String uri, String body) {
+        return client.put() //
+            .uri(uri) //
+            .contentType(MediaType.APPLICATION_JSON) //
+            .syncBody(body) //
+            .retrieve() //
+            .bodyToMono(String.class);
+    }
+
+    public Mono<String> get(String uri) {
+        return client.get() //
+            .uri(uri) //
+            .retrieve() //
+            .bodyToMono(String.class);
+    }
+
+    public Mono<Void> delete(String uri) {
+        return client.delete() //
+            .uri(uri) //
+            .retrieve() //
+            .bodyToMono(Void.class);
+    }
+}