From 63036ab25f7878781e74b2ca0706b28451f59f27 Mon Sep 17 00:00:00 2001 From: elinuxhenrik Date: Fri, 6 Dec 2019 15:27:51 +0100 Subject: [PATCH] Add first version of startup Change-Id: I526fc8c82c5681af73e9ce21bc066ecebab75793 Issue-ID: NONRTRIC-81 Signed-off-by: elinuxhenrik --- .../main/java/org/oransc/policyagent/Beans.java | 12 ++- .../policyagent/controllers/PolicyController.java | 18 +++- .../policyagent/controllers/StartupService.java | 63 +++++++++++++ .../oransc/policyagent/repository/Policies.java | 1 - .../org/oransc/policyagent/repository/Policy.java | 3 +- .../org/oransc/policyagent/repository/Ric.java | 105 +++++++++++++++++++++ .../org/oransc/policyagent/repository/Rics.java | 48 ++++++++++ .../org/oransc/policyagent/ApplicationTest.java | 52 ++++++---- .../controllers/StartupServiceTest.java | 55 +++++++++++ 9 files changed, 330 insertions(+), 27 deletions(-) create mode 100644 policy-agent/src/main/java/org/oransc/policyagent/controllers/StartupService.java create mode 100644 policy-agent/src/main/java/org/oransc/policyagent/repository/Ric.java create mode 100644 policy-agent/src/main/java/org/oransc/policyagent/repository/Rics.java create mode 100644 policy-agent/src/test/java/org/oransc/policyagent/controllers/StartupServiceTest.java diff --git a/policy-agent/src/main/java/org/oransc/policyagent/Beans.java b/policy-agent/src/main/java/org/oransc/policyagent/Beans.java index 74477491..4b8adf40 100644 --- a/policy-agent/src/main/java/org/oransc/policyagent/Beans.java +++ b/policy-agent/src/main/java/org/oransc/policyagent/Beans.java @@ -22,24 +22,30 @@ package org.oransc.policyagent; import org.oransc.policyagent.configuration.ApplicationConfig; import org.oransc.policyagent.repository.Policies; import org.oransc.policyagent.repository.PolicyTypes; +import org.oransc.policyagent.repository.Rics; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class Beans { + private Rics rics = new Rics(); @Bean - Policies getPolicies() { + public Policies getPolicies() { return new Policies(); } @Bean - PolicyTypes getPolicyTypes() { + public PolicyTypes getPolicyTypes() { return new PolicyTypes(); } @Bean - ApplicationConfig getApplicationConfig() { + public Rics getRics() { + return rics; + } + @Bean + public ApplicationConfig getApplicationConfig() { return new ApplicationConfig(); } 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 4b3c9cf7..e40fe8c5 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 @@ -20,6 +20,7 @@ package org.oransc.policyagent.controllers; import com.google.gson.Gson; +import org.oransc.policyagent.Beans; import com.google.gson.GsonBuilder; import java.util.Collection; @@ -31,6 +32,8 @@ import org.oransc.policyagent.repository.ImmutablePolicy; import org.oransc.policyagent.repository.Policies; import org.oransc.policyagent.repository.Policy; import org.oransc.policyagent.repository.PolicyTypes; +import org.oransc.policyagent.repository.Ric; +import org.oransc.policyagent.repository.Rics; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; @@ -43,7 +46,9 @@ import org.springframework.web.bind.annotation.RestController; @RestController public class PolicyController { + private final Beans beans; private final ApplicationConfig appConfig; + private final Rics rics; private final PolicyTypes types; private final Policies policies; private static Gson gson = new GsonBuilder() // @@ -51,10 +56,12 @@ public class PolicyController { .create(); // @Autowired - PolicyController(ApplicationConfig config, PolicyTypes types, Policies policies) { - this.appConfig = config; - this.types = types; - this.policies = policies; + PolicyController(Beans beans) { + this.beans = beans; + this.appConfig = beans.getApplicationConfig(); + this.rics = beans.getRics(); + this.types = beans.getPolicyTypes(); + this.policies = beans.getPolicies(); } @GetMapping("/policy") @@ -133,11 +140,12 @@ public class PolicyController { @RequestBody String jsonBody) { try { + Ric ricObj = rics.getRic(ric); Policy policy = ImmutablePolicy.builder() // .id(instanceId) // .json(jsonBody) // .type(types.getType(type)) // - .ric(appConfig.getRic(ric)) // + .ric(ricObj) // .ownerServiceName(service) // .build(); policies.put(policy); diff --git a/policy-agent/src/main/java/org/oransc/policyagent/controllers/StartupService.java b/policy-agent/src/main/java/org/oransc/policyagent/controllers/StartupService.java new file mode 100644 index 00000000..c0a05d3a --- /dev/null +++ b/policy-agent/src/main/java/org/oransc/policyagent/controllers/StartupService.java @@ -0,0 +1,63 @@ +/*- + * ========================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.oransc.policyagent.configuration.ApplicationConfig; +import org.oransc.policyagent.configuration.RicConfig; +import org.oransc.policyagent.repository.Ric; +import org.oransc.policyagent.repository.Rics; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +/** + * Loads information about RealTime-RICs at startup. + */ +@Service("startupService") +public class StartupService { + + private static final Logger logger = LoggerFactory.getLogger(StartupService.class); + + @Autowired + ApplicationConfig applicationConfig; + + @Autowired + private Rics rics; + + StartupService(ApplicationConfig appConfig, Rics rics) { + this.applicationConfig = appConfig; + this.rics = rics; + } + + public void startup() { + applicationConfig.initialize(); + Vector ricConfigs = applicationConfig.getRicConfigs(); + for (RicConfig ricConfig : ricConfigs) { + logger.error("Ric: {}", ricConfig); + Ric ric = new Ric(ricConfig); + rics.put(ric); + } + + } + +} 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 index cddd8a37..5f887889 100644 --- a/policy-agent/src/main/java/org/oransc/policyagent/repository/Policies.java +++ b/policy-agent/src/main/java/org/oransc/policyagent/repository/Policies.java @@ -24,7 +24,6 @@ import java.util.Collection; import java.util.HashMap; import java.util.Map; import java.util.Vector; - import org.oransc.policyagent.exceptions.ServiceException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; 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 index d76e6255..0f7105a7 100644 --- a/policy-agent/src/main/java/org/oransc/policyagent/repository/Policy.java +++ b/policy-agent/src/main/java/org/oransc/policyagent/repository/Policy.java @@ -21,7 +21,6 @@ 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 @@ -32,7 +31,7 @@ public interface Policy { public String ownerServiceName(); - public RicConfig ric(); + public Ric ric(); public PolicyType type(); } diff --git a/policy-agent/src/main/java/org/oransc/policyagent/repository/Ric.java b/policy-agent/src/main/java/org/oransc/policyagent/repository/Ric.java new file mode 100644 index 00000000..bfd8ef65 --- /dev/null +++ b/policy-agent/src/main/java/org/oransc/policyagent/repository/Ric.java @@ -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.repository; + +import org.oransc.policyagent.configuration.RicConfig; +import org.oransc.policyagent.repository.Ric.RicState; + +/** + * Represents the dynamic information about a NearRealtime-RIC. + */ +public class Ric { + private final RicConfig ricConfig; + private RicState state = RicState.NOT_INITIATED; + + /** + * Creates the Ric. Initial state is {@link RicState.NOT_INITIATED}. + * + * @param ricConfig The {@link RicConfig} for this Ric. + */ + public Ric(RicConfig ricConfig) { + this.ricConfig = ricConfig; + } + + public String name() { + return ricConfig.name(); + } + + public RicState state() { + return state; + } + + public void setState(RicState newState) { + state = newState; + } + + /** + * Determines if the given node is managed by this Ric. + * + * @param nodeName the node name to check. + * @return true if the given node is managed by this Ric. + */ + public boolean isManaging(String nodeName) { + return ricConfig.managedElementIds().contains(nodeName); + } + + /** + * Adds the given node as managed by this Ric. + * + * @param nodeName the node to add. + */ + public void addManagedNode(String nodeName) { + if (!ricConfig.managedElementIds().contains(nodeName)) { + ricConfig.managedElementIds().add(nodeName); + } + } + + /** + * Removes the given node as managed by this Ric. + * + * @param nodeName the node to remove. + */ + public void removeManagedNode(String nodeName) { + ricConfig.managedElementIds().remove(nodeName); + } + + /** + * Represents the states possible for a Ric. + */ + public static enum RicState { + /** + * The Ric has not been initiated yet. + */ + NOT_INITIATED, + /** + * The Ric is working fine. + */ + ACTIVE, + /** + * Something is wrong with the Ric. + */ + FAULTY, + /** + * The node is unreachable at the moment. + */ + UNREACHABLE + } +} diff --git a/policy-agent/src/main/java/org/oransc/policyagent/repository/Rics.java b/policy-agent/src/main/java/org/oransc/policyagent/repository/Rics.java new file mode 100644 index 00000000..5c286bc8 --- /dev/null +++ b/policy-agent/src/main/java/org/oransc/policyagent/repository/Rics.java @@ -0,0 +1,48 @@ +/*- + * ========================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.Collection; +import java.util.HashMap; +import java.util.Map; + +/** + * Dynamic representation of all Rics in the system. + */ +public class Rics { + Map rics = new HashMap<>(); + + public void put(Ric ric) { + rics.put(ric.name(), ric); + } + + public Collection getRics() { + return rics.values(); + } + + public Ric getRic(String name) { + return rics.get(name); + } + + public int size() { + return rics.size(); + } +} 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 d8ff2e1f..c7d2e557 100644 --- a/policy-agent/src/test/java/org/oransc/policyagent/ApplicationTest.java +++ b/policy-agent/src/test/java/org/oransc/policyagent/ApplicationTest.java @@ -23,10 +23,12 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.junit.Assert.assertFalse; import java.net.URL; - +import java.util.Vector; import org.junit.Test; import org.junit.runner.RunWith; import org.oransc.policyagent.configuration.ApplicationConfig; +import org.oransc.policyagent.configuration.ImmutableRicConfig; +import org.oransc.policyagent.configuration.RicConfig; import org.oransc.policyagent.exceptions.ServiceException; import org.oransc.policyagent.repository.ImmutablePolicy; import org.oransc.policyagent.repository.ImmutablePolicyType; @@ -34,6 +36,8 @@ 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.Ric; +import org.oransc.policyagent.repository.Rics; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; @@ -50,15 +54,10 @@ import org.springframework.web.client.RestTemplate; public class ApplicationTest { @Autowired - private Policies policies; - - @Autowired - private PolicyTypes policyTypes; - - @Autowired - ApplicationConfig appConfig; + private Beans beans; static class MockApplicationConfig extends ApplicationConfig { + @Override public void initialize() { URL url = MockApplicationConfig.class.getClassLoader().getResource("test_application_configuration.json"); loadConfigurationFromFile(url.getFile()); @@ -68,17 +67,22 @@ public class ApplicationTest { @TestConfiguration static class Beans { @Bean - Policies getPolicies() { + public Rics getRics() { + return new Rics(); + } + + @Bean + public Policies getPolicies() { return new Policies(); } @Bean - PolicyTypes getPolicyTypes() { + public PolicyTypes getPolicyTypes() { return new PolicyTypes(); } @Bean - ApplicationConfig getApplicationConfig() { + public ApplicationConfig getApplicationConfig() { return new MockApplicationConfig(); } } @@ -109,22 +113,37 @@ public class ApplicationTest { String url = "http://localhost:" + port + "/policy?type=type1&instance=instance1&ric=ric1&service=service1"; String json = "{}"; addPolicyType("type1"); + addRic(beans.getRics(), "ric1", url); this.restTemplate.put(url, json); - Policy policy = this.policies.get("instance1"); + + Policy policy = beans.getPolicies().get("instance1"); assertThat(policy).isNotNull(); assertThat(policy.id()).isEqualTo("instance1"); assertThat(policy.ownerServiceName()).isEqualTo("service1"); } + private void addRic(Rics rics, String ric, String url) { + Vector nodeNames = new Vector<>(1); + nodeNames.add("node1"); + RicConfig ricConfig = ImmutableRicConfig.builder() // + .name(ric) // + .baseUrl(url) // + .managedElementIds(nodeNames) // + .build(); + Ric ricObj = new Ric(ricConfig); + + rics.put(ricObj); + } + private PolicyType addPolicyType(String name) { PolicyType type = ImmutablePolicyType.builder() // .jsonSchema("") // .name(name) // .build(); - policyTypes.put(type); + beans.getPolicyTypes().put(type); return type; } @@ -132,10 +151,10 @@ public class ApplicationTest { Policy p = ImmutablePolicy.builder().id(id) // .json("{}") // .ownerServiceName(service) // - .ric(appConfig.getRic("ric1")) // + .ric(beans.getRics().getRic("ric1")) // .type(addPolicyType(typeName)) // .build(); - this.policies.put(p); + beans.getPolicies().put(p); return p; } @@ -148,7 +167,7 @@ public class ApplicationTest { assertThat(rsp).isEqualTo(policy.json()); } { - this.policies.remove(policy); + beans.getPolicies().remove(policy); ResponseEntity rsp = this.restTemplate.getForEntity(url, String.class); assertThat(rsp.getStatusCodeValue()).isEqualTo(HttpStatus.NO_CONTENT.value()); } @@ -157,6 +176,7 @@ public class ApplicationTest { @Test public void getPolicies() throws Exception { String url = "http://localhost:" + port + "/policies"; + addRic(beans.getRics(), "ric1", url); addPolicy("id1", "type1", "service1"); addPolicy("id2", "type2", "service2"); diff --git a/policy-agent/src/test/java/org/oransc/policyagent/controllers/StartupServiceTest.java b/policy-agent/src/test/java/org/oransc/policyagent/controllers/StartupServiceTest.java new file mode 100644 index 00000000..5a8f42c8 --- /dev/null +++ b/policy-agent/src/test/java/org/oransc/policyagent/controllers/StartupServiceTest.java @@ -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.controllers; + +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import java.util.Vector; +import org.junit.Test; +import org.oransc.policyagent.configuration.ApplicationConfig; +import org.oransc.policyagent.configuration.ImmutableRicConfig; +import org.oransc.policyagent.configuration.RicConfig; +import org.oransc.policyagent.repository.Rics; + +public class StartupServiceTest { + ApplicationConfig appConfigMock; + + @Test + public void startup_allOk() { + appConfigMock = mock(ApplicationConfig.class); + Vector ricConfigs = new Vector<>(2); + Vector firstNodes = new Vector(1); + firstNodes.add("nodeA"); + ricConfigs.add(ImmutableRicConfig.builder().name("first").managedElementIds(firstNodes).baseUrl("url").build()); + when(appConfigMock.getRicConfigs()).thenReturn(ricConfigs); + + Rics rics = new Rics(); + + StartupService serviceUnderTest = new StartupService(appConfigMock, rics); + + serviceUnderTest.startup(); + + assertEquals("Correct nymber of Rics not added to Rics", 1, rics.size()); + assertEquals("Not correct Ric added to Rics", "first", rics.getRic("first").name()); + } +} -- 2.16.6