From 8936c783ff4f362fbc9b96b658b5defb0701da54 Mon Sep 17 00:00:00 2001 From: PatrikBuhr Date: Wed, 12 Feb 2020 08:35:53 +0100 Subject: [PATCH] Adding validation of updated policies The ric cannot be changed. Change-Id: I24d493ce4922256ab3567f7ba5ad7e9a1b9f6588 Issue-ID: NONRTRIC-107 Signed-off-by: PatrikBuhr --- .../policyagentapi/PolicyAgentApiImpl.java | 4 ++-- .../policyagent/clients/A1ClientFactory.java | 6 ++++-- .../policyagent/controllers/PolicyController.java | 17 +++++++++++++++-- .../oransc/policyagent/controllers/RicInfo.java | 4 ++-- .../org/oransc/policyagent/ApplicationTest.java | 22 +++++++++++++++++++--- .../policyagent/clients/A1ClientFactoryTest.java | 3 +++ 6 files changed, 45 insertions(+), 11 deletions(-) diff --git a/dashboard/webapp-backend/src/main/java/org/oransc/ric/portal/dashboard/policyagentapi/PolicyAgentApiImpl.java b/dashboard/webapp-backend/src/main/java/org/oransc/ric/portal/dashboard/policyagentapi/PolicyAgentApiImpl.java index fe9c26cd..470e2df1 100644 --- a/dashboard/webapp-backend/src/main/java/org/oransc/ric/portal/dashboard/policyagentapi/PolicyAgentApiImpl.java +++ b/dashboard/webapp-backend/src/main/java/org/oransc/ric/portal/dashboard/policyagentapi/PolicyAgentApiImpl.java @@ -174,7 +174,7 @@ public class PolicyAgentApiImpl implements PolicyAgentApi { @Value.Immutable @Gson.TypeAdapters interface RicInfo { - public String name(); + public String ricName(); public Collection nodeNames(); @@ -193,7 +193,7 @@ public class PolicyAgentApiImpl implements PolicyAgentApi { List rspParsed = gson.fromJson(rsp, listType); Collection 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) { diff --git a/policy-agent/src/main/java/org/oransc/policyagent/clients/A1ClientFactory.java b/policy-agent/src/main/java/org/oransc/policyagent/clients/A1ClientFactory.java index e340e602..6598c276 100644 --- a/policy-agent/src/main/java/org/oransc/policyagent/clients/A1ClientFactory.java +++ b/policy-agent/src/main/java/org/oransc/policyagent/clients/A1ClientFactory.java @@ -45,10 +45,12 @@ public class A1ClientFactory { /** * Creates an A1 client with the correct A1 protocol for the provided Ric. * - *

It detects the protocol version by trial and error, since there is no getVersion method specified in the A1 + *

+ * It detects the protocol version by trial and error, since there is no getVersion method specified in the A1 * api yet. * - *

As a side effect it also sets the protocol version in the provided Ric. This means that after the first + *

+ * 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. 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 afd3a5bc..b102964c 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 @@ -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 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( diff --git a/policy-agent/src/main/java/org/oransc/policyagent/controllers/RicInfo.java b/policy-agent/src/main/java/org/oransc/policyagent/controllers/RicInfo.java index 73103b80..5a635716 100644 --- a/policy-agent/src/main/java/org/oransc/policyagent/controllers/RicInfo.java +++ b/policy-agent/src/main/java/org/oransc/policyagent/controllers/RicInfo.java @@ -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 managedElementIds; @@ -40,7 +40,7 @@ class RicInfo { public final Collection policyTypes; RicInfo(String name, Collection managedElementIds, Collection policyTypes) { - this.name = name; + this.ricName = name; this.managedElementIds = managedElementIds; this.policyTypes = policyTypes; } 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 38de021f..fc4030f5 100644 --- a/policy-agent/src/test/java/org/oransc/policyagent/ApplicationTest.java +++ b/policy-agent/src/test/java/org/oransc/policyagent/ApplicationTest.java @@ -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) { diff --git a/policy-agent/src/test/java/org/oransc/policyagent/clients/A1ClientFactoryTest.java b/policy-agent/src/test/java/org/oransc/policyagent/clients/A1ClientFactoryTest.java index 926485dc..b42cebfa 100644 --- a/policy-agent/src/test/java/org/oransc/policyagent/clients/A1ClientFactoryTest.java +++ b/policy-agent/src/test/java/org/oransc/policyagent/clients/A1ClientFactoryTest.java @@ -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; -- 2.16.6