Adding validation of updated policies 79/2479/2
authorPatrikBuhr <patrik.buhr@est.tech>
Wed, 12 Feb 2020 07:35:53 +0000 (08:35 +0100)
committerPatrikBuhr <patrik.buhr@est.tech>
Wed, 12 Feb 2020 09:15:14 +0000 (10:15 +0100)
The ric cannot be changed.

Change-Id: I24d493ce4922256ab3567f7ba5ad7e9a1b9f6588
Issue-ID: NONRTRIC-107
Signed-off-by: PatrikBuhr <patrik.buhr@est.tech>
dashboard/webapp-backend/src/main/java/org/oransc/ric/portal/dashboard/policyagentapi/PolicyAgentApiImpl.java
policy-agent/src/main/java/org/oransc/policyagent/clients/A1ClientFactory.java
policy-agent/src/main/java/org/oransc/policyagent/controllers/PolicyController.java
policy-agent/src/main/java/org/oransc/policyagent/controllers/RicInfo.java
policy-agent/src/test/java/org/oransc/policyagent/ApplicationTest.java
policy-agent/src/test/java/org/oransc/policyagent/clients/A1ClientFactoryTest.java

index fe9c26c..470e2df 100644 (file)
@@ -174,7 +174,7 @@ public class PolicyAgentApiImpl implements PolicyAgentApi {
     @Value.Immutable
     @Gson.TypeAdapters
     interface RicInfo {
-        public String name();
+        public String ricName();
 
         public Collection<String> nodeNames();
 
@@ -193,7 +193,7 @@ public class PolicyAgentApiImpl implements PolicyAgentApi {
             List<RicInfo> rspParsed = gson.fromJson(rsp, listType);
             Collection<String> result = new Vector<>(rspParsed.size());
             for (RicInfo ric : rspParsed) {
-                result.add(ric.name());
+                result.add(ric.ricName());
             }
             return new ResponseEntity<>(gson.toJson(result), HttpStatus.OK);
         } catch (Exception e) {
index e340e60..6598c27 100644 (file)
@@ -45,10 +45,12 @@ public class A1ClientFactory {
     /**
      * Creates an A1 client with the correct A1 protocol for the provided Ric.
      *
-     * <p>It detects the protocol version by trial and error, since there is no getVersion method specified in the A1
+     * <p>
+     * It detects the protocol version by trial and error, since there is no getVersion method specified in the A1
      * api yet.
      *
-     * <p>As a side effect it also sets the protocol version in the provided Ric. This means that after the first
+     * <p>
+     * As a side effect it also sets the protocol version in the provided Ric. This means that after the first
      * successful creation it won't have to try which protocol to use, but can create the client directly.
      *
      * @param ric The Ric to get a client for.
index afd3a5b..b102964 100644 (file)
@@ -27,10 +27,10 @@ import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiOperation;
 import io.swagger.annotations.ApiResponse;
 import io.swagger.annotations.ApiResponses;
+
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.List;
-import java.util.Vector;
 
 import org.oransc.policyagent.clients.A1ClientFactory;
 import org.oransc.policyagent.configuration.ApplicationConfig;
@@ -188,7 +188,9 @@ public class PolicyController {
                 .ownerServiceName(service) //
                 .lastModified(getTimeStampUtc()) //
                 .build();
-            return a1ClientFactory.createA1Client(ric) //
+
+            return validateModifiedPolicy(policy) //
+                .flatMap(x -> a1ClientFactory.createA1Client(ric)) //
                 .flatMap(client -> client.putPolicy(policy)) //
                 .doOnNext(notUsed -> policies.put(policy)) //
                 .flatMap(notUsed -> Mono.just(new ResponseEntity<>(HttpStatus.OK)));
@@ -196,6 +198,17 @@ public class PolicyController {
         return Mono.just(new ResponseEntity<>(HttpStatus.NOT_FOUND));
     }
 
+    private Mono<Object> validateModifiedPolicy(Policy policy) {
+        // Check that ric is not updated
+        Policy current = this.policies.get(policy.id());
+        if (current != null) {
+            if (!current.ric().name().equals(policy.ric().name())) {
+                return Mono.error(new Exception("Policy cannot change RIC or service"));
+            }
+        }
+        return Mono.just("OK");
+    }
+
     @GetMapping("/policies")
     @ApiOperation(value = "Query policies")
     @ApiResponses(
index 73103b8..5a63571 100644 (file)
@@ -31,7 +31,7 @@ import org.immutables.gson.Gson;
 @ApiModel(value = "RicInfo")
 class RicInfo {
     @ApiModelProperty(value = "identity of the ric")
-    public final String name;
+    public final String ricName;
 
     @ApiModelProperty(value = "O1 identities for managed entities")
     public final Collection<String> managedElementIds;
@@ -40,7 +40,7 @@ class RicInfo {
     public final Collection<String> policyTypes;
 
     RicInfo(String name, Collection<String> managedElementIds, Collection<String> policyTypes) {
-        this.name = name;
+        this.ricName = name;
         this.managedElementIds = managedElementIds;
         this.policyTypes = policyTypes;
     }
index 38de021..fc4030f 100644 (file)
@@ -224,22 +224,38 @@ public class ApplicationTest {
     public void testPutPolicy() throws Exception {
         reset();
         putService("service1");
+        this.addRic("ric1").setState(Ric.RicState.IDLE);
         addPolicyType("type1", "ric1");
 
-        String url = baseUrl() + "/policy?type=type1&instance=instance1&ric=ric1&service=service1";
         final String json = jsonString();
-        this.rics.getRic("ric1").setState(Ric.RicState.IDLE);
-
+        String url = baseUrl() + "/policy?type=type1&instance=instance1&ric=ric1&service=service1";
         this.restTemplate.put(url, createJsonHttpEntity(json));
         Policy policy = policies.getPolicy("instance1");
 
         assertThat(policy).isNotNull();
         assertThat(policy.id()).isEqualTo("instance1");
         assertThat(policy.ownerServiceName()).isEqualTo("service1");
+        assertThat(policy.ric().name()).isEqualTo("ric1");
 
         url = baseUrl() + "/policies";
         String rsp = this.restTemplate.getForObject(url, String.class);
         System.out.println(rsp);
+
+    }
+
+    @Test
+    public void testRefuseToUpdatePolicy() throws Exception {
+        // Test that only the json can be changed for a already created policy
+        // In this case service is attempted to be changed
+        reset();
+        this.addRic("ric1").setState(Ric.RicState.IDLE);
+        this.addRic("ricXXX").setState(Ric.RicState.IDLE);
+
+        this.addPolicy("instance1", "type1", "service1", "ric1");
+        String urlWrongRic = baseUrl() + "/policy?type=type1&instance=instance1&ric=ricXXX&service=service1";
+        this.restTemplate.put(urlWrongRic, createJsonHttpEntity(jsonString()));
+        Policy policy = policies.getPolicy("instance1");
+        assertThat(policy.ric().name()).isEqualTo("ric1"); // Not changed
     }
 
     private PolicyType addPolicyType(String policyTypeName, String ricName) {
index 926485d..b42cebf 100644 (file)
@@ -31,7 +31,9 @@ import static org.mockito.Mockito.when;
 import ch.qos.logback.classic.Level;
 import ch.qos.logback.classic.spi.ILoggingEvent;
 import ch.qos.logback.core.read.ListAppender;
+
 import java.util.Vector;
+
 import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
 import org.junit.jupiter.api.extension.ExtendWith;
@@ -42,6 +44,7 @@ import org.oransc.policyagent.configuration.ApplicationConfig;
 import org.oransc.policyagent.configuration.ImmutableRicConfig;
 import org.oransc.policyagent.repository.Ric;
 import org.oransc.policyagent.utils.LoggingUtils;
+
 import reactor.core.publisher.Mono;
 import reactor.test.StepVerifier;