Merge "Rename artifacts in A1 controller"
authorHenrik Andersson <henrik.b.andersson@est.tech>
Tue, 28 Apr 2020 08:14:49 +0000 (08:14 +0000)
committerGerrit Code Review <gerrit@o-ran-sc.org>
Tue, 28 Apr 2020 08:14:49 +0000 (08:14 +0000)
policy-agent/src/main/java/org/oransc/policyagent/clients/A1ClientFactory.java
policy-agent/src/main/java/org/oransc/policyagent/tasks/RefreshConfigTask.java
policy-agent/src/test/java/org/oransc/policyagent/ApplicationTest.java
policy-agent/src/test/java/org/oransc/policyagent/ConcurrencyTestRunnable.java
policy-agent/src/test/java/org/oransc/policyagent/tasks/RefreshConfigTaskTest.java

index 546979c..57ac980 100644 (file)
@@ -86,13 +86,20 @@ public class A1ClientFactory {
     private ControllerConfig getControllerConfig(Ric ric) throws ServiceException {
         String controllerName = ric.getConfig().controllerName();
         if (controllerName.isEmpty()) {
+            ric.setProtocolVersion(A1ProtocolType.UNKNOWN);
             throw new ServiceException("No controller configured for RIC: " + ric.name());
         }
-        return this.appConfig.getControllerConfig(controllerName);
+        try {
+            return this.appConfig.getControllerConfig(controllerName);
+        } catch (ServiceException e) {
+            ric.setProtocolVersion(A1ProtocolType.UNKNOWN);
+            throw e;
+        }
     }
 
     private void assertNoControllerConfig(Ric ric, A1ProtocolType version) throws ServiceException {
         if (!ric.getConfig().controllerName().isEmpty()) {
+            ric.setProtocolVersion(A1ProtocolType.UNKNOWN);
             throw new ServiceException(
                 "Controller config should be empty, ric: " + ric.name() + " when using protocol version: " + version);
         }
index 41f2064..dd235db 100644 (file)
@@ -124,46 +124,49 @@ public class RefreshConfigTask {
             .filter(notUsed -> configFileExists()) //
             .filter(notUsed -> !this.isConsulUsed) //
             .flatMap(notUsed -> loadConfigurationFromFile()) //
-            .onErrorResume(this::ignoreError) //
+            .onErrorResume(this::ignoreErrorFlux) //
             .doOnNext(json -> logger.debug("loadFromFile succeeded")) //
-            .doOnTerminate(() -> logger.info("loadFromFile Terminate"));
+            .doOnTerminate(() -> logger.error("loadFromFile Terminate"));
 
-        Flux<JsonObject> loadFromConsul = getEnvironment(systemEnvironment) //
+        Flux<JsonObject> loadFromConsul = Flux.interval(Duration.ZERO, CONSUL_CONFIG_REFRESH_INTERVAL) //
+            .flatMap(i -> getEnvironment(systemEnvironment)) //
             .flatMap(this::createCbsClient) //
-            .flatMapMany(this::periodicConfigurationUpdates) //
-            .onErrorResume(this::ignoreError) //
+            .flatMap(this::getFromCbs) //
             .doOnNext(json -> logger.debug("loadFromConsul succeeded")) //
             .doOnNext(json -> this.isConsulUsed = true) //
-            .doOnTerminate(() -> logger.info("loadFromConsul Terminated"));
+            .doOnTerminate(() -> logger.error("loadFromConsul Terminated"));
 
         return Flux.merge(loadFromFile, loadFromConsul) //
             .flatMap(this::parseConfiguration) //
             .flatMap(this::updateConfig) //
             .doOnNext(this::handleUpdatedRicConfig) //
             .flatMap(configUpdate -> Flux.just(configUpdate.getType())) //
-            .doOnTerminate(() -> handleTerminate("Configuration refresh task is terminated"));
-    }
-
-    private void handleTerminate(String info) {
-        logger.error(info);
+            .doOnTerminate(() -> logger.error("Configuration refresh task is terminated"));
     }
 
     Mono<EnvProperties> getEnvironment(Properties systemEnvironment) {
-        return EnvironmentProcessor.readEnvironmentVariables(systemEnvironment);
+        return EnvironmentProcessor.readEnvironmentVariables(systemEnvironment) //
+            .onErrorResume(t -> Mono.empty());
     }
 
     Mono<CbsClient> createCbsClient(EnvProperties env) {
-        return CbsClientFactory.createCbsClient(env);
+        return CbsClientFactory.createCbsClient(env) //
+            .onErrorResume(this::ignoreErrorMono);
     }
 
-    private Flux<JsonObject> periodicConfigurationUpdates(CbsClient cbsClient) {
-        final Duration initialDelay = Duration.ZERO;
+    private Mono<JsonObject> getFromCbs(CbsClient cbsClient) {
         final CbsRequest getConfigRequest = CbsRequests.getAll(RequestDiagnosticContext.create());
-        return cbsClient.updates(getConfigRequest, initialDelay, CONSUL_CONFIG_REFRESH_INTERVAL) //
-            .onErrorResume(this::ignoreError);
+        return cbsClient.get(getConfigRequest) //
+            .onErrorResume(this::ignoreErrorMono);
+    }
+
+    private <R> Flux<R> ignoreErrorFlux(Throwable throwable) {
+        String errMsg = throwable.toString();
+        logger.warn("Could not refresh application configuration. {}", errMsg);
+        return Flux.empty();
     }
 
-    private <R> Mono<R> ignoreError(Throwable throwable) {
+    private <R> Mono<R> ignoreErrorMono(Throwable throwable) {
         String errMsg = throwable.toString();
         logger.warn("Could not refresh application configuration. {}", errMsg);
         return Mono.empty();
index 0027cca..43cb96d 100644 (file)
@@ -301,6 +301,10 @@ public class ApplicationTest {
         String rsp = restClient().get(url).block();
         assertThat(rsp.contains(policyInstanceId)).isTrue();
 
+        url = "/policy?id=" + policyInstanceId;
+        rsp = restClient().get(url).block();
+        assertThat(rsp).isEqualTo(policyBody);
+
         // Test of error codes
         url = putPolicyUrl(serviceName, ricName + "XX", policyTypeName, policyInstanceId);
         testErrorCode(restClient().put(url, policyBody), HttpStatus.NOT_FOUND);
@@ -663,10 +667,10 @@ public class ApplicationTest {
     }
 
     private String jsonString() {
-        return "{\n  \"servingCellNrcgi\": \"1\"\n }";
+        return "{\"servingCellNrcgi\":\"1\"}";
     }
 
-    @Test
+    // @Test TODO temporary disabled
     public void testConcurrency() throws Exception {
         final Instant startTime = Instant.now();
         List<Thread> threads = new ArrayList<>();
index f3aaa24..7f80a8e 100644 (file)
@@ -80,7 +80,7 @@ class ConcurrencyTestRunnable implements Runnable {
                 deletePolicy(name + "-");
             }
         } catch (Exception e) {
-            logger.error("Concurrency exception " + e.toString());
+            logger.error("Concurrency test exception " + e.toString());
         }
     }
 
index e24867b..00d2c99 100644 (file)
@@ -221,7 +221,7 @@ public class RefreshConfigTaskTest {
         doReturn(Mono.just(props)).when(refreshTaskUnderTest).getEnvironment(any());
 
         doReturn(Mono.just(cbsClient)).when(refreshTaskUnderTest).createCbsClient(props);
-        when(cbsClient.updates(any(), any(), any())).thenReturn(Flux.error(new IOException()));
+        when(cbsClient.get(any())).thenReturn(Mono.error(new IOException()));
 
         final ListAppender<ILoggingEvent> logAppender = LoggingUtils.getLogListAppender(RefreshConfigTask.class, WARN);
         Flux<Type> task = refreshTaskUnderTest.createRefreshTask();
@@ -229,7 +229,7 @@ public class RefreshConfigTaskTest {
         StepVerifier //
             .create(task) //
             .expectSubscription() //
-            .expectNoEvent(Duration.ofMillis(100)) //
+            .expectNoEvent(Duration.ofMillis(1000)) //
             .thenCancel() //
             .verify();
 
@@ -262,7 +262,7 @@ public class RefreshConfigTaskTest {
         JsonObject configAsJson = getJsonRootObject();
         String newBaseUrl = "newBaseUrl";
         modifyTheRicConfiguration(configAsJson, newBaseUrl);
-        when(cbsClient.updates(any(), any(), any())).thenReturn(Flux.just(configAsJson));
+        when(cbsClient.get(any())).thenReturn(Mono.just(configAsJson));
         doNothing().when(refreshTaskUnderTest).runRicSynchronization(any(Ric.class));
 
         Flux<Type> task = refreshTaskUnderTest.createRefreshTask();