Fix startup 78/1978/1
authorelinuxhenrik <henrik.b.andersson@est.tech>
Mon, 9 Dec 2019 14:53:00 +0000 (15:53 +0100)
committerelinuxhenrik <henrik.b.andersson@est.tech>
Mon, 9 Dec 2019 15:01:03 +0000 (16:01 +0100)
Change-Id: Ie90db67e18464f03afde11c7ebaeee57a15ed4e1
Issue-ID: NONRTRIC-81
Signed-off-by: elinuxhenrik <henrik.b.andersson@est.tech>
policy-agent/src/main/java/org/oransc/policyagent/Application.java
policy-agent/src/main/java/org/oransc/policyagent/Beans.java
policy-agent/src/main/java/org/oransc/policyagent/controllers/RicRepositoryController.java
policy-agent/src/main/java/org/oransc/policyagent/repository/Ric.java
policy-agent/src/test/java/org/oransc/policyagent/ApplicationTest.java
policy-agent/src/test/java/org/oransc/policyagent/controllers/StartupServiceTest.java

index 717a734..e4b6e5a 100644 (file)
  * limitations under the License.
  * ========================LICENSE_END===================================
  */
  * limitations under the License.
  * ========================LICENSE_END===================================
  */
+
 package org.oransc.policyagent;
 
 package org.oransc.policyagent;
 
+import org.oransc.policyagent.controllers.StartupService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.CommandLineRunner;
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.context.ApplicationContext;
+import org.springframework.context.annotation.Bean;
 
 @SpringBootApplication
 public class Application {
 
 
 @SpringBootApplication
 public class Application {
 
+    @Autowired
+    private StartupService startupService;
+
     public static void main(String[] args) {
         SpringApplication.run(Application.class, args);
     }
 
     public static void main(String[] args) {
         SpringApplication.run(Application.class, args);
     }
 
+    /**
+     * Starts the se4rvice and reads the configuration.
+     *
+     * @param ctx
+     * @return
+     */
+    @Bean
+    public CommandLineRunner commandLineRunner(ApplicationContext ctx) {
+        return args -> {
+
+            startupService.startup();
+        };
+    }
+
 }
 }
index cf88479..c8f774c 100644 (file)
@@ -29,8 +29,6 @@ import org.springframework.context.annotation.Configuration;
 
 @Configuration
 public class Beans {
 
 @Configuration
 public class Beans {
-    private Rics rics = new Rics();
-
     @Bean
     public Policies getPolicies() {
         return new Policies();
     @Bean
     public Policies getPolicies() {
         return new Policies();
@@ -43,7 +41,7 @@ public class Beans {
 
     @Bean
     public Rics getRics() {
 
     @Bean
     public Rics getRics() {
-        return rics;
+        return new Rics();
     }
 
     @Bean
     }
 
     @Bean
index 34fc4c1..7c63dcd 100644 (file)
  * limitations under the License.
  * ========================LICENSE_END===================================
  */
  * limitations under the License.
  * ========================LICENSE_END===================================
  */
+
 package org.oransc.policyagent.controllers;
 
 import com.google.gson.Gson;
 import com.google.gson.GsonBuilder;
 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 io.swagger.annotations.ApiOperation;
 import io.swagger.annotations.ApiResponse;
 import io.swagger.annotations.ApiResponses;
-
 import java.util.Optional;
 import java.util.Vector;
 import java.util.Optional;
 import java.util.Vector;
-
 import org.oransc.policyagent.configuration.ApplicationConfig;
 import org.oransc.policyagent.configuration.RicConfig;
 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.springframework.beans.factory.annotation.Autowired;
 import org.springframework.http.HttpStatus;
 import org.springframework.http.ResponseEntity;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.http.HttpStatus;
 import org.springframework.http.ResponseEntity;
@@ -42,13 +42,17 @@ import org.springframework.web.bind.annotation.RestController;
 public class RicRepositoryController {
 
     private final ApplicationConfig appConfig;
 public class RicRepositoryController {
 
     private final ApplicationConfig appConfig;
+
+    @Autowired
+    private Rics rics;
+
     private static Gson gson = new GsonBuilder() //
         .serializeNulls() //
         .create(); //
 
     @Autowired
     private static Gson gson = new GsonBuilder() //
         .serializeNulls() //
         .create(); //
 
     @Autowired
-    RicRepositoryController(ApplicationConfig config) {
-        this.appConfig = config;
+    RicRepositoryController(ApplicationConfig appConfig) {
+        this.appConfig = appConfig;
     }
 
     /**
     }
 
     /**
@@ -84,14 +88,14 @@ public class RicRepositoryController {
             @ApiResponse(code = 200, message = "OK") //
         })
     public ResponseEntity<String> getRics() {
             @ApiResponse(code = 200, message = "OK") //
         })
     public ResponseEntity<String> getRics() {
-        Vector<RicInfo> result = new Vector<RicInfo>();
-        for (RicConfig ricConfig : this.appConfig.getRicConfigs()) {
-            RicInfo ric = ImmutableRicInfo.builder() //
-                .managedElementIds(ricConfig.managedElementIds()) //
-                .name(ricConfig.name()) //
-                .build();
-            result.add(ric);
+        Vector<RicInfo> result = new Vector<>();
+        for (Ric ric : rics.getRics()) {
+            result.add(ImmutableRicInfo.builder() //
+                .name(ric.name()) //
+                .managedElementIds(ric.getManagedNodes()) //
+                .build());
         }
         }
+
         return new ResponseEntity<>(gson.toJson(result), HttpStatus.OK);
     }
 
         return new ResponseEntity<>(gson.toJson(result), HttpStatus.OK);
     }
 
index bfd8ef6..ffe493d 100644 (file)
@@ -20,8 +20,8 @@
 
 package org.oransc.policyagent.repository;
 
 
 package org.oransc.policyagent.repository;
 
+import java.util.Vector;
 import org.oransc.policyagent.configuration.RicConfig;
 import org.oransc.policyagent.configuration.RicConfig;
-import org.oransc.policyagent.repository.Ric.RicState;
 
 /**
  * Represents the dynamic information about a NearRealtime-RIC.
 
 /**
  * Represents the dynamic information about a NearRealtime-RIC.
@@ -51,6 +51,15 @@ public class Ric {
         state = newState;
     }
 
         state = newState;
     }
 
+    /**
+     * Gets the nodes managed by this Ric.
+     *
+     * @return a vector containing the nodes managed by this Ric.
+     */
+    public Vector<String> getManagedNodes() {
+        return ricConfig.managedElementIds();
+    }
+
     /**
      * Determines if the given node is managed by this Ric.
      *
     /**
      * Determines if the given node is managed by this Ric.
      *
index 8bcd99a..beef586 100644 (file)
@@ -24,12 +24,9 @@ import static org.assertj.core.api.Assertions.assertThat;
 import static org.junit.Assert.assertFalse;
 
 import java.net.URL;
 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.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;
 import org.oransc.policyagent.exceptions.ServiceException;
 import org.oransc.policyagent.repository.ImmutablePolicy;
 import org.oransc.policyagent.repository.ImmutablePolicyType;
@@ -37,7 +34,6 @@ 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.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.oransc.policyagent.repository.Rics;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.boot.test.context.SpringBootTest;
@@ -114,7 +110,6 @@ public class ApplicationTest {
         String url = "http://localhost:" + port + "/policy?type=type1&instance=instance1&ric=ric1&service=service1";
         String json = "{}";
         addPolicyType("type1");
         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);
 
 
         this.restTemplate.put(url, json);
 
@@ -125,19 +120,6 @@ public class ApplicationTest {
         assertThat(policy.ownerServiceName()).isEqualTo("service1");
     }
 
         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("") //
     private PolicyType addPolicyType(String name) {
         PolicyType type = ImmutablePolicyType.builder() //
             .jsonSchema("") //
@@ -177,7 +159,6 @@ public class ApplicationTest {
     @Test
     public void getPolicies() throws Exception {
         String url = "http://localhost:" + port + "/policies";
     @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");
 
         addPolicy("id1", "type1", "service1");
         addPolicy("id2", "type2", "service2");
 
index 5a8f42c..5900f14 100644 (file)
@@ -36,7 +36,7 @@ public class StartupServiceTest {
 
     @Test
     public void startup_allOk() {
 
     @Test
     public void startup_allOk() {
-        appConfigMock = mock(ApplicationConfig.class);
+        ApplicationConfig appConfigMock = mock(ApplicationConfig.class);
         Vector<RicConfig> ricConfigs = new Vector<>(2);
         Vector<String> firstNodes = new Vector<String>(1);
         firstNodes.add("nodeA");
         Vector<RicConfig> ricConfigs = new Vector<>(2);
         Vector<String> firstNodes = new Vector<String>(1);
         firstNodes.add("nodeA");