Add first version of startup 69/1969/2
authorelinuxhenrik <henrik.b.andersson@est.tech>
Fri, 6 Dec 2019 14:27:51 +0000 (15:27 +0100)
committerelinuxhenrik <henrik.b.andersson@est.tech>
Mon, 9 Dec 2019 09:50:20 +0000 (10:50 +0100)
Change-Id: I526fc8c82c5681af73e9ce21bc066ecebab75793
Issue-ID: NONRTRIC-81
Signed-off-by: elinuxhenrik <henrik.b.andersson@est.tech>
policy-agent/src/main/java/org/oransc/policyagent/Beans.java
policy-agent/src/main/java/org/oransc/policyagent/controllers/PolicyController.java
policy-agent/src/main/java/org/oransc/policyagent/controllers/StartupService.java [new file with mode: 0644]
policy-agent/src/main/java/org/oransc/policyagent/repository/Policies.java
policy-agent/src/main/java/org/oransc/policyagent/repository/Policy.java
policy-agent/src/main/java/org/oransc/policyagent/repository/Ric.java [new file with mode: 0644]
policy-agent/src/main/java/org/oransc/policyagent/repository/Rics.java [new file with mode: 0644]
policy-agent/src/test/java/org/oransc/policyagent/ApplicationTest.java
policy-agent/src/test/java/org/oransc/policyagent/controllers/StartupServiceTest.java [new file with mode: 0644]

index 7447749..4b8adf4 100644 (file)
@@ -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();
     }
 
index 4b3c9cf..e40fe8c 100644 (file)
@@ -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 (file)
index 0000000..c0a05d3
--- /dev/null
@@ -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<RicConfig> ricConfigs = applicationConfig.getRicConfigs();
+        for (RicConfig ricConfig : ricConfigs) {
+            logger.error("Ric: {}", ricConfig);
+            Ric ric = new Ric(ricConfig);
+            rics.put(ric);
+        }
+
+    }
+
+}
index cddd8a3..5f88788 100644 (file)
@@ -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;
index d76e625..0f7105a 100644 (file)
@@ -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 (file)
index 0000000..bfd8ef6
--- /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.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 (file)
index 0000000..5c286bc
--- /dev/null
@@ -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<String, Ric> rics = new HashMap<>();
+
+    public void put(Ric ric) {
+        rics.put(ric.name(), ric);
+    }
+
+    public Collection<Ric> getRics() {
+        return rics.values();
+    }
+
+    public Ric getRic(String name) {
+        return rics.get(name);
+    }
+
+    public int size() {
+        return rics.size();
+    }
+}
index d8ff2e1..c7d2e55 100644 (file)
@@ -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<String> 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<String> 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 (file)
index 0000000..5a8f42c
--- /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.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<RicConfig> ricConfigs = new Vector<>(2);
+        Vector<String> firstNodes = new Vector<String>(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());
+    }
+}