Added RIC Config 44/1944/1
authorPatrikBuhr <patrik.buhr@est.tech>
Mon, 2 Dec 2019 07:41:33 +0000 (08:41 +0100)
committerPatrikBuhr <patrik.buhr@est.tech>
Thu, 5 Dec 2019 10:18:46 +0000 (11:18 +0100)
Change-Id: I96f2c33cd4adeb16561279d71380f5d984d7aaf4
Issue-ID: NONRTRIC-76
Signed-off-by: PatrikBuhr <patrik.buhr@est.tech>
12 files changed:
policy-agent/config/application_configuration.json
policy-agent/src/main/java/org/oransc/policyagent/configuration/ApplicationConfig.java
policy-agent/src/main/java/org/oransc/policyagent/configuration/ApplicationConfigLoader.java
policy-agent/src/main/java/org/oransc/policyagent/configuration/ApplicationConfigParser.java
policy-agent/src/main/java/org/oransc/policyagent/configuration/RicConfig.java [new file with mode: 0644]
policy-agent/src/main/java/org/oransc/policyagent/controllers/PolicyController.java
policy-agent/src/main/java/org/oransc/policyagent/controllers/RicInfo.java [new file with mode: 0644]
policy-agent/src/main/java/org/oransc/policyagent/repository/Policies.java [new file with mode: 0644]
policy-agent/src/main/java/org/oransc/policyagent/repository/Policy.java [new file with mode: 0644]
policy-agent/src/main/java/org/oransc/policyagent/repository/PolicyType.java [new file with mode: 0644]
policy-agent/src/main/java/org/oransc/policyagent/repository/PolicyTypes.java [new file with mode: 0644]
policy-agent/src/test/java/org/oransc/policyagent/ApplicationTest.java

index ccde671..7033c4a 100644 (file)
@@ -1,6 +1,15 @@
 {
-  "config": {
-    "//description": "Application configuration",
-    "exampleProperty": "test"
-  }
+   "config": {
+      "//description": "Application configuration",
+      "ric": [
+         {
+            "name": "ric1",
+            "baseUrl": "http://localhost:8080/",
+            "managedElementIds": [
+               "kista_1",
+               "kista_2"
+            ]
+         }
+      ]
+   }
 }
\ No newline at end of file
index c7ab0ce..79c82a7 100644 (file)
@@ -32,8 +32,10 @@ import java.io.FileInputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStreamReader;
+import java.util.Optional;
 import java.util.Properties;
 import java.util.ServiceLoader;
+import java.util.Vector;
 
 import javax.validation.constraints.NotEmpty;
 import javax.validation.constraints.NotNull;
@@ -60,6 +62,8 @@ public class ApplicationConfig {
     @NotEmpty
     private String filepath;
 
+    private Vector<RicConfig> ricConfigs;
+
     @Autowired
     public ApplicationConfig() {
     }
@@ -68,9 +72,20 @@ public class ApplicationConfig {
         this.filepath = filepath;
     }
 
-    /**
-     * Reads the cloud configuration.
-     */
+    public Vector<RicConfig> getRicConfigs() {
+        return this.ricConfigs;
+    }
+
+    public Optional<RicConfig> getRicConfig(String managedElementId) {
+        for (RicConfig ricConfig : getRicConfigs()) {
+            if (ricConfig.managedElementIds().contains(managedElementId)) {
+                return Optional.of(ricConfig);
+
+            }
+        }
+        return Optional.empty();
+    }
+
     public void initialize() {
         loadConfigurationFromFile();
     }
@@ -94,6 +109,7 @@ public class ApplicationConfig {
             }
             ApplicationConfigParser appParser = new ApplicationConfigParser();
             appParser.parse(rootObject);
+            this.ricConfigs = appParser.getRicConfigs();
             logger.info("Local configuration file loaded: {}", filepath);
         } catch (JsonSyntaxException | ServiceException | IOException e) {
             logger.trace("Local configuration file not loaded: {}", filepath, e);
index f3d6d30..24473f6 100644 (file)
@@ -41,7 +41,7 @@ import reactor.core.publisher.Mono;
 
 @Configuration
 @EnableScheduling
-public class ApplicationConfigLoader {
+class ApplicationConfigLoader {
 
     private static final Logger logger = LoggerFactory.getLogger(ApplicationConfigLoader.class);
     private static List<ScheduledFuture<?>> scheduledFutureList = new ArrayList<>();
index 487c3a9..86f4e59 100644 (file)
 
 package org.oransc.policyagent.configuration;
 
+import com.google.gson.Gson;
+import com.google.gson.GsonBuilder;
+import com.google.gson.JsonArray;
 import com.google.gson.JsonElement;
 import com.google.gson.JsonObject;
 
+import java.util.Vector;
+
 import org.oransc.policyagent.exceptions.ServiceException;
 
-public class ApplicationConfigParser {
+class ApplicationConfigParser {
 
     private static final String CONFIG = "config";
+    private static Gson gson = new GsonBuilder() //
+        .serializeNulls() //
+        .create(); //
+
+    private Vector<RicConfig> ricConfig;
 
     public ApplicationConfigParser() {
     }
 
-    String example;
-
     public void parse(JsonObject root) throws ServiceException {
         JsonObject config = root.getAsJsonObject(CONFIG);
-        example = getAsString(config, "exampleProperty");
+        ricConfig = parseRics(config);
+    }
+
+    public Vector<RicConfig> getRicConfigs() {
+        return this.ricConfig;
+    }
+
+    private Vector<RicConfig> parseRics(JsonObject config) throws ServiceException {
+        Vector<RicConfig> result = new Vector<RicConfig>();
+        for (JsonElement ricElem : getAsJsonArray(config, "ric")) {
+            result.add(gson.fromJson(ricElem.getAsJsonObject(), ImmutableRicConfig.class));
+        }
+        return result;
     }
 
     private static JsonElement get(JsonObject obj, String memberName) throws ServiceException {
@@ -55,4 +75,8 @@ public class ApplicationConfigParser {
         return get(obj, memberName).getAsJsonObject();
     }
 
+    private JsonArray getAsJsonArray(JsonObject obj, String memberName) throws ServiceException {
+        return get(obj, memberName).getAsJsonArray();
+    }
+
 }
diff --git a/policy-agent/src/main/java/org/oransc/policyagent/configuration/RicConfig.java b/policy-agent/src/main/java/org/oransc/policyagent/configuration/RicConfig.java
new file mode 100644 (file)
index 0000000..ad7fe6f
--- /dev/null
@@ -0,0 +1,35 @@
+/*-
+ * ========================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.configuration;
+
+import java.util.Vector;
+
+import org.immutables.gson.Gson;
+import org.immutables.value.Value;
+
+@Value.Immutable
+@Gson.TypeAdapters
+public interface RicConfig {
+    public String name();
+
+    public String baseUrl();
+
+    public Vector<String> managedElementIds();
+}
index ebe833b..a507a2b 100644 (file)
  */
 package org.oransc.policyagent.controllers;
 
+import com.google.gson.Gson;
+import com.google.gson.GsonBuilder;
+
 import io.swagger.annotations.ApiOperation;
 import io.swagger.annotations.ApiResponse;
 import io.swagger.annotations.ApiResponses;
 
-import java.net.http.HttpHeaders;
+import java.util.Vector;
 
+import org.oransc.policyagent.configuration.ApplicationConfig;
+import org.oransc.policyagent.configuration.RicConfig;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.http.HttpStatus;
 import org.springframework.http.ResponseEntity;
 import org.springframework.web.bind.annotation.GetMapping;
-import org.springframework.web.bind.annotation.RequestHeader;
 import org.springframework.web.bind.annotation.RequestParam;
 import org.springframework.web.bind.annotation.RestController;
 import reactor.core.publisher.Mono;
@@ -36,6 +41,16 @@ import reactor.core.publisher.Mono;
 @RestController
 public class PolicyController {
 
+    private final ApplicationConfig appConfig;
+    private static Gson gson = new GsonBuilder() //
+        .serializeNulls() //
+        .create(); //
+
+    @Autowired
+    PolicyController(ApplicationConfig config) {
+        this.appConfig = config;
+    }
+
     // http://localhost:8080/policy?type=type3&instance=xxx
     @GetMapping("/policy")
     public String getPolicy(@RequestParam(name = "type", required = false, defaultValue = "type1") String typeName,
@@ -50,7 +65,7 @@ public class PolicyController {
     }
 
     @GetMapping("/status")
-    @ApiOperation(value = "Returns status and statistics of DATAFILE service")
+    @ApiOperation(value = "Returns status and statistics of the service")
     @ApiResponses(
         value = { //
             @ApiResponse(code = 200, message = "DATAFILE service is living"),
@@ -58,9 +73,40 @@ public class PolicyController {
             @ApiResponse(code = 403, message = "Accessing the resource you were trying to reach is forbidden"),
             @ApiResponse(code = 404, message = "The resource you were trying to reach is not found") //
         })
-    public Mono<ResponseEntity<String>> getStatus(@RequestHeader HttpHeaders headers) {
+    public Mono<ResponseEntity<String>> getStatus() {
         Mono<ResponseEntity<String>> response = Mono.just(new ResponseEntity<>("hunky dory", HttpStatus.OK));
         return response;
     }
 
+    // http://localhost:8080/rics?managedElementId=kista_1
+    @GetMapping("/rics")
+    @ApiOperation(value = "Returns defined NearRT RIC:s")
+    public ResponseEntity<String> getRics(
+        @RequestParam(name = "managedElementId", required = false, defaultValue = "") String managedElementId) {
+        Vector<RicInfo> result = new Vector<RicInfo>();
+        Vector<RicConfig> config = getRicConfigs(managedElementId);
+
+        for (RicConfig ricConfig : config) {
+            RicInfo ric = ImmutableRicInfo.builder() //
+                .managedElementIds(ricConfig.managedElementIds()) //
+                .name(ricConfig.name()) //
+                .build();
+            result.add(ric);
+        }
+
+        return new ResponseEntity<>(gson.toJson(result), HttpStatus.OK);
+    }
+
+    private Vector<RicConfig> getRicConfigs(String managedElementId) {
+        if (managedElementId.equals("")) {
+            return this.appConfig.getRicConfigs();
+        }
+
+        Vector<RicConfig> result = new Vector<RicConfig>(1);
+        appConfig.getRicConfig(managedElementId).ifPresent((config) -> {
+            result.add(config);
+        });
+        return result;
+    }
+
 }
diff --git a/policy-agent/src/main/java/org/oransc/policyagent/controllers/RicInfo.java b/policy-agent/src/main/java/org/oransc/policyagent/controllers/RicInfo.java
new file mode 100644 (file)
index 0000000..843153a
--- /dev/null
@@ -0,0 +1,33 @@
+/*-
+ * ========================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.controllers;
+
+import java.util.Vector;
+
+import org.immutables.gson.Gson;
+import org.immutables.value.Value;
+
+@Value.Immutable
+@Gson.TypeAdapters
+public interface RicInfo {
+    public String name();
+
+    public Vector<String> managedElementIds();
+}
diff --git a/policy-agent/src/main/java/org/oransc/policyagent/repository/Policies.java b/policy-agent/src/main/java/org/oransc/policyagent/repository/Policies.java
new file mode 100644 (file)
index 0000000..359cd22
--- /dev/null
@@ -0,0 +1,46 @@
+/*-
+ * ========================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.repository;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.oransc.policyagent.configuration.RicConfig;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+
+@Component
+public class Policies {
+    private static final Logger logger = LoggerFactory.getLogger(Policies.class);
+
+    private Map<String, Policy> policies = new HashMap<String, Policy>();
+
+    @Autowired
+    public Policies() {
+    }
+
+    public synchronized void put(String id, Policy policy) {
+        policies.put(id, policy);
+    }
+
+}
diff --git a/policy-agent/src/main/java/org/oransc/policyagent/repository/Policy.java b/policy-agent/src/main/java/org/oransc/policyagent/repository/Policy.java
new file mode 100644 (file)
index 0000000..d76e625
--- /dev/null
@@ -0,0 +1,38 @@
+/*-
+ * ========================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.repository;
+
+import org.immutables.gson.Gson;
+import org.immutables.value.Value;
+import org.oransc.policyagent.configuration.RicConfig;
+
+@Value.Immutable
+@Gson.TypeAdapters
+public interface Policy {
+    public String id();
+
+    public String json();
+
+    public String ownerServiceName();
+
+    public RicConfig ric();
+
+    public PolicyType type();
+}
diff --git a/policy-agent/src/main/java/org/oransc/policyagent/repository/PolicyType.java b/policy-agent/src/main/java/org/oransc/policyagent/repository/PolicyType.java
new file mode 100644 (file)
index 0000000..184d3ec
--- /dev/null
@@ -0,0 +1,32 @@
+/*-
+ * ========================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.repository;
+
+import org.immutables.gson.Gson;
+import org.immutables.value.Value;
+
+@Value.Immutable
+@Gson.TypeAdapters
+public interface PolicyType {
+    public String name();
+
+    public String jsonSchema();
+
+}
diff --git a/policy-agent/src/main/java/org/oransc/policyagent/repository/PolicyTypes.java b/policy-agent/src/main/java/org/oransc/policyagent/repository/PolicyTypes.java
new file mode 100644 (file)
index 0000000..47b815b
--- /dev/null
@@ -0,0 +1,54 @@
+/*-
+ * ========================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.repository;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.oransc.policyagent.exceptions.ServiceException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+
+@Component
+public class PolicyTypes {
+    private static final Logger logger = LoggerFactory.getLogger(PolicyTypes.class);
+
+    private Map<String, PolicyType> types = new HashMap<String, PolicyType>();
+
+    @Autowired
+    public PolicyTypes() {
+    }
+
+    public synchronized PolicyType getType(String name) throws ServiceException {
+        PolicyType t = types.get(name);
+        if (t == null) {
+            throw new ServiceException("Could not find type: " + name);
+        }
+        return t;
+    }
+
+    public synchronized void putType(String name, PolicyType type) {
+        types.put(name, type);
+    }
+
+}
index 32d7fe6..1cbd0b6 100644 (file)
@@ -46,4 +46,12 @@ public class ApplicationTest {
         assertThat(rsp).contains("type3");
     }
 
+    @Test
+    public void getRics() throws Exception {
+        String cmd = "/rics";
+        String rsp = this.restTemplate.getForObject("http://localhost:" + port + cmd, String.class);
+        System.out.println("*** rsp " + rsp);
+        assertThat(rsp).contains("kista_1");
+    }
+
 }