From def3c3e28fb8616a444ad3caaf2f789292402a02 Mon Sep 17 00:00:00 2001 From: PatrikBuhr Date: Mon, 2 Dec 2019 08:41:33 +0100 Subject: [PATCH] Added RIC Config Change-Id: I96f2c33cd4adeb16561279d71380f5d984d7aaf4 Issue-ID: NONRTRIC-76 Signed-off-by: PatrikBuhr --- policy-agent/config/application_configuration.json | 17 +++++-- .../configuration/ApplicationConfig.java | 22 +++++++-- .../configuration/ApplicationConfigLoader.java | 2 +- .../configuration/ApplicationConfigParser.java | 32 +++++++++++-- .../policyagent/configuration/RicConfig.java | 35 ++++++++++++++ .../policyagent/controllers/PolicyController.java | 54 ++++++++++++++++++++-- .../oransc/policyagent/controllers/RicInfo.java | 33 +++++++++++++ .../oransc/policyagent/repository/Policies.java | 46 ++++++++++++++++++ .../org/oransc/policyagent/repository/Policy.java | 38 +++++++++++++++ .../oransc/policyagent/repository/PolicyType.java | 32 +++++++++++++ .../oransc/policyagent/repository/PolicyTypes.java | 54 ++++++++++++++++++++++ .../org/oransc/policyagent/ApplicationTest.java | 8 ++++ 12 files changed, 357 insertions(+), 16 deletions(-) create mode 100644 policy-agent/src/main/java/org/oransc/policyagent/configuration/RicConfig.java create mode 100644 policy-agent/src/main/java/org/oransc/policyagent/controllers/RicInfo.java create mode 100644 policy-agent/src/main/java/org/oransc/policyagent/repository/Policies.java create mode 100644 policy-agent/src/main/java/org/oransc/policyagent/repository/Policy.java create mode 100644 policy-agent/src/main/java/org/oransc/policyagent/repository/PolicyType.java create mode 100644 policy-agent/src/main/java/org/oransc/policyagent/repository/PolicyTypes.java diff --git a/policy-agent/config/application_configuration.json b/policy-agent/config/application_configuration.json index ccde671e..7033c4aa 100644 --- a/policy-agent/config/application_configuration.json +++ b/policy-agent/config/application_configuration.json @@ -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 diff --git a/policy-agent/src/main/java/org/oransc/policyagent/configuration/ApplicationConfig.java b/policy-agent/src/main/java/org/oransc/policyagent/configuration/ApplicationConfig.java index c7ab0ce5..79c82a78 100644 --- a/policy-agent/src/main/java/org/oransc/policyagent/configuration/ApplicationConfig.java +++ b/policy-agent/src/main/java/org/oransc/policyagent/configuration/ApplicationConfig.java @@ -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 ricConfigs; + @Autowired public ApplicationConfig() { } @@ -68,9 +72,20 @@ public class ApplicationConfig { this.filepath = filepath; } - /** - * Reads the cloud configuration. - */ + public Vector getRicConfigs() { + return this.ricConfigs; + } + + public Optional 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); diff --git a/policy-agent/src/main/java/org/oransc/policyagent/configuration/ApplicationConfigLoader.java b/policy-agent/src/main/java/org/oransc/policyagent/configuration/ApplicationConfigLoader.java index f3d6d303..24473f6f 100644 --- a/policy-agent/src/main/java/org/oransc/policyagent/configuration/ApplicationConfigLoader.java +++ b/policy-agent/src/main/java/org/oransc/policyagent/configuration/ApplicationConfigLoader.java @@ -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> scheduledFutureList = new ArrayList<>(); diff --git a/policy-agent/src/main/java/org/oransc/policyagent/configuration/ApplicationConfigParser.java b/policy-agent/src/main/java/org/oransc/policyagent/configuration/ApplicationConfigParser.java index 487c3a99..86f4e592 100644 --- a/policy-agent/src/main/java/org/oransc/policyagent/configuration/ApplicationConfigParser.java +++ b/policy-agent/src/main/java/org/oransc/policyagent/configuration/ApplicationConfigParser.java @@ -20,23 +20,43 @@ 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; 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 getRicConfigs() { + return this.ricConfig; + } + + private Vector parseRics(JsonObject config) throws ServiceException { + Vector result = new Vector(); + 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 index 00000000..ad7fe6f5 --- /dev/null +++ b/policy-agent/src/main/java/org/oransc/policyagent/configuration/RicConfig.java @@ -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 managedElementIds(); +} diff --git a/policy-agent/src/main/java/org/oransc/policyagent/controllers/PolicyController.java b/policy-agent/src/main/java/org/oransc/policyagent/controllers/PolicyController.java index ebe833b9..a507a2b1 100644 --- a/policy-agent/src/main/java/org/oransc/policyagent/controllers/PolicyController.java +++ b/policy-agent/src/main/java/org/oransc/policyagent/controllers/PolicyController.java @@ -19,16 +19,21 @@ */ 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> getStatus(@RequestHeader HttpHeaders headers) { + public Mono> getStatus() { Mono> 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 getRics( + @RequestParam(name = "managedElementId", required = false, defaultValue = "") String managedElementId) { + Vector result = new Vector(); + Vector 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 getRicConfigs(String managedElementId) { + if (managedElementId.equals("")) { + return this.appConfig.getRicConfigs(); + } + + Vector result = new Vector(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 index 00000000..843153ae --- /dev/null +++ b/policy-agent/src/main/java/org/oransc/policyagent/controllers/RicInfo.java @@ -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 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 index 00000000..359cd227 --- /dev/null +++ b/policy-agent/src/main/java/org/oransc/policyagent/repository/Policies.java @@ -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 policies = new HashMap(); + + @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 index 00000000..d76e6255 --- /dev/null +++ b/policy-agent/src/main/java/org/oransc/policyagent/repository/Policy.java @@ -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 index 00000000..184d3ec3 --- /dev/null +++ b/policy-agent/src/main/java/org/oransc/policyagent/repository/PolicyType.java @@ -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 index 00000000..47b815be --- /dev/null +++ b/policy-agent/src/main/java/org/oransc/policyagent/repository/PolicyTypes.java @@ -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 types = new HashMap(); + + @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); + } + +} diff --git a/policy-agent/src/test/java/org/oransc/policyagent/ApplicationTest.java b/policy-agent/src/test/java/org/oransc/policyagent/ApplicationTest.java index 32d7fe64..1cbd0b66 100644 --- a/policy-agent/src/test/java/org/oransc/policyagent/ApplicationTest.java +++ b/policy-agent/src/test/java/org/oransc/policyagent/ApplicationTest.java @@ -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"); + } + } -- 2.16.6