diff --git a/src/policy/src/main/java/eu/teraflow/policy/PolicyGatewayImpl.java b/src/policy/src/main/java/eu/teraflow/policy/PolicyGatewayImpl.java index 642fabcba92c82cab8cc0c43dca805aa95231df6..c76f8c8c402d454af88349ce14ae53f0a0c69f3d 100644 --- a/src/policy/src/main/java/eu/teraflow/policy/PolicyGatewayImpl.java +++ b/src/policy/src/main/java/eu/teraflow/policy/PolicyGatewayImpl.java @@ -33,32 +33,32 @@ import policy.Policy.RuleState; public class PolicyGatewayImpl implements PolicyGateway { private final PolicyService policyService; + private final Serializer serializer; @Inject - public PolicyGatewayImpl(PolicyService policyService) { + public PolicyGatewayImpl(PolicyService policyService, Serializer serializer) { this.policyService = policyService; + this.serializer = serializer; } @Override public Uni<PolicyRuleState> policyAddService(PolicyRuleService request) { - return Uni.createFrom() - .item( - () -> - Policy.PolicyRuleState.newBuilder() - .setPolicyRuleState( - request.getPolicyRuleBasic().getPolicyRuleState().getPolicyRuleState()) - .build()); + final var policyRuleService = serializer.deserialize(request); + + return policyService + .addPolicyService(policyRuleService) + .onItem() + .transform(serializer::serialize); } @Override public Uni<PolicyRuleState> policyAddDevice(PolicyRuleDevice request) { - return Uni.createFrom() - .item( - () -> - Policy.PolicyRuleState.newBuilder() - .setPolicyRuleState( - request.getPolicyRuleBasic().getPolicyRuleState().getPolicyRuleState()) - .build()); + final var policyRuleDevice = serializer.deserialize(request); + + return policyService + .addPolicyDevice(policyRuleDevice) + .onItem() + .transform(serializer::serialize); } @Override diff --git a/src/policy/src/main/java/eu/teraflow/policy/PolicyService.java b/src/policy/src/main/java/eu/teraflow/policy/PolicyService.java index 14ffbb41ef60a438990bfe59e1d9539b48b51d75..dcaf43b902c95471cff2c020f0fdc5659c59e6a1 100644 --- a/src/policy/src/main/java/eu/teraflow/policy/PolicyService.java +++ b/src/policy/src/main/java/eu/teraflow/policy/PolicyService.java @@ -16,10 +16,10 @@ package eu.teraflow.policy; +import eu.teraflow.policy.model.PolicyRuleDevice; +import eu.teraflow.policy.model.PolicyRuleService; import eu.teraflow.policy.model.PolicyRuleState; import io.smallrye.mutiny.Uni; -import policy.Policy.PolicyRuleDevice; -import policy.Policy.PolicyRuleService; public interface PolicyService { diff --git a/src/policy/src/main/java/eu/teraflow/policy/PolicyServiceImpl.java b/src/policy/src/main/java/eu/teraflow/policy/PolicyServiceImpl.java index c9645c5e30b1b4380bb9c0004a8b66f734f279d1..66994625ddb2eb6a52e1d0a99acd52a2cd1cc2f2 100644 --- a/src/policy/src/main/java/eu/teraflow/policy/PolicyServiceImpl.java +++ b/src/policy/src/main/java/eu/teraflow/policy/PolicyServiceImpl.java @@ -16,25 +16,48 @@ package eu.teraflow.policy; +import eu.teraflow.policy.context.ContextService; +import eu.teraflow.policy.model.PolicyRuleDevice; +import eu.teraflow.policy.model.PolicyRuleService; import eu.teraflow.policy.model.PolicyRuleState; +import eu.teraflow.policy.model.RuleState; +import eu.teraflow.policy.monitoring.MonitoringService; +import eu.teraflow.policy.service.ServiceService; import io.smallrye.mutiny.Uni; import javax.enterprise.context.ApplicationScoped; +import javax.inject.Inject; import org.jboss.logging.Logger; -import policy.Policy.PolicyRuleDevice; -import policy.Policy.PolicyRuleService; @ApplicationScoped public class PolicyServiceImpl implements PolicyService { private static final Logger LOGGER = Logger.getLogger(PolicyServiceImpl.class); + private final ContextService contextService; + private final MonitoringService monitoringService; + private final ServiceService serviceService; + + @Inject + public PolicyServiceImpl( + ContextService contextService, + MonitoringService monitoringService, + ServiceService serviceService) { + this.contextService = contextService; + this.monitoringService = monitoringService; + this.serviceService = serviceService; + } + @Override public Uni<PolicyRuleState> addPolicyService(PolicyRuleService policyRuleService) { - return null; + final var policyRuleState = new PolicyRuleState(RuleState.POLICY_VALIDATED); + + return Uni.createFrom().item(policyRuleState); } @Override public Uni<PolicyRuleState> addPolicyDevice(PolicyRuleDevice policyRuleDevice) { - return null; + final var policyRuleState = new PolicyRuleState(RuleState.POLICY_VALIDATED); + + return Uni.createFrom().item(policyRuleState); } } diff --git a/src/policy/src/test/java/eu/teraflow/policy/PolicyServiceTest.java b/src/policy/src/test/java/eu/teraflow/policy/PolicyServiceTest.java index bf1af532f06accf9c0410548ad7f4aa85757dbb1..7e3044c1d2212d6bf8726c55673ff84e5d174f4f 100644 --- a/src/policy/src/test/java/eu/teraflow/policy/PolicyServiceTest.java +++ b/src/policy/src/test/java/eu/teraflow/policy/PolicyServiceTest.java @@ -19,18 +19,30 @@ package eu.teraflow.policy; import static org.assertj.core.api.Assertions.assertThat; import context.ContextOuterClass; +import context.ContextOuterClass.Uuid; +import eu.teraflow.policy.monitoring.model.FloatKpiValue; +import eu.teraflow.policy.monitoring.model.IntegerKpiValue; import io.quarkus.grpc.GrpcClient; import io.quarkus.test.junit.QuarkusTest; +import java.util.List; import java.util.UUID; import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; +import javax.inject.Inject; +import monitoring.Monitoring.KpiId; import org.jboss.logging.Logger; import org.junit.jupiter.api.Test; import policy.Policy; import policy.Policy.PolicyRuleBasic; import policy.Policy.RuleState; +import policy.PolicyAction; +import policy.PolicyAction.PolicyRuleActionEnum; +import policy.PolicyCondition; +import policy.PolicyCondition.BooleanOperator; +import policy.PolicyCondition.NumericalOperator; +import policy.PolicyCondition.PolicyRuleCondition; import policy.PolicyService; @QuarkusTest @@ -38,16 +50,84 @@ class PolicyServiceTest { private static final Logger LOGGER = Logger.getLogger(PolicyServiceTest.class); @GrpcClient PolicyService client; + private final Serializer serializer; + + @Inject + PolicyServiceTest(Serializer serializer) { + this.serializer = serializer; + } + + private PolicyRuleBasic createPolicyRuleBasic() { + final var expectedPolicyRuleIdUuid = + serializer.serializeUuid("571eabc1-0f59-48da-b608-c45876c3fa8a"); + + final var expectedPolicyRuleId = + Policy.PolicyRuleId.newBuilder().setUuid(expectedPolicyRuleIdUuid).build(); + + final var expectedPolicyRuleState = + Policy.PolicyRuleState.newBuilder().setPolicyRuleState(RuleState.POLICY_VALIDATED).build(); + + final var expectedFirstKpiValue = new IntegerKpiValue(22); + final var expectedSecondKpiValue = new FloatKpiValue(69.1f); + + final var serializedExpectedFirstKpiValue = serializer.serialize(expectedFirstKpiValue); + final var serializedExpectedSecondKpiValue = serializer.serialize(expectedSecondKpiValue); + + final var firstExpectedPolicyRuleCondition = + PolicyRuleCondition.newBuilder() + .setKpiId( + KpiId.newBuilder() + .setKpiId( + Uuid.newBuilder().setUuid("79e49ba3-a7b4-4b4b-8aaa-28b05c6f888e").build())) + .setNumericalOperator(NumericalOperator.POLICYRULE_CONDITION_NUMERICAL_EQUAL) + .setKpiValue(serializedExpectedFirstKpiValue) + .build(); + + final var secondExpectedPolicyRuleCondition = + PolicyCondition.PolicyRuleCondition.newBuilder() + .setKpiId( + KpiId.newBuilder() + .setKpiId( + Uuid.newBuilder().setUuid("eae900e5-2703-467d-82f2-97aae8b55c15").build())) + .setNumericalOperator(NumericalOperator.POLICYRULE_CONDITION_NUMERICAL_GREATER_THAN) + .setKpiValue(serializedExpectedSecondKpiValue) + .build(); + + final var expectedPolicyRuleConditions = + List.of(firstExpectedPolicyRuleCondition, secondExpectedPolicyRuleCondition); + + final var firstExpectedPolicyRuleAction = + PolicyAction.PolicyRuleAction.newBuilder() + .setAction(PolicyAction.PolicyRuleActionEnum.POLICYRULE_ACTION_ADD_SERVICE_CONFIGRULE) + .addAllParameters(List.of("parameter1", "parameter2")) + .build(); + + final var secondExpectedPolicyRuleAction = + PolicyAction.PolicyRuleAction.newBuilder() + .setAction(PolicyRuleActionEnum.POLICYRULE_ACTION_ADD_SERVICE_CONSTRAINT) + .addAllParameters(List.of("parameter3", "parameter4")) + .build(); + + final var expectedPolicyRuleActions = + List.of(firstExpectedPolicyRuleAction, secondExpectedPolicyRuleAction); + + return PolicyRuleBasic.newBuilder() + .setPolicyRuleId(expectedPolicyRuleId) + .setPolicyRuleState(expectedPolicyRuleState) + .addAllConditionList(expectedPolicyRuleConditions) + .setBooleanOperator(BooleanOperator.POLICYRULE_CONDITION_BOOLEAN_OR) + .addAllActionList(expectedPolicyRuleActions) + .build(); + } @Test void shouldAddPolicyService() throws ExecutionException, InterruptedException, TimeoutException { CompletableFuture<String> message = new CompletableFuture<>(); - final var expectedPolicyRuleState = - Policy.PolicyRuleState.newBuilder().setPolicyRuleState(RuleState.POLICY_ACTIVE).build(); + final var policyRuleBasic = createPolicyRuleBasic(); + + final var expectedPolicyRuleState = policyRuleBasic.getPolicyRuleState(); - final var policyRuleBasic = - PolicyRuleBasic.newBuilder().setPolicyRuleState(expectedPolicyRuleState).build(); final var policyRuleService = Policy.PolicyRuleService.newBuilder().setPolicyRuleBasic(policyRuleBasic).build(); @@ -64,11 +144,9 @@ class PolicyServiceTest { void shouldAddPolicyDevice() throws ExecutionException, InterruptedException, TimeoutException { CompletableFuture<String> message = new CompletableFuture<>(); - final var expectedPolicyRuleState = - Policy.PolicyRuleState.newBuilder().setPolicyRuleState(RuleState.POLICY_EFFECTIVE).build(); + final var policyRuleBasic = createPolicyRuleBasic(); + final var expectedPolicyRuleState = policyRuleBasic.getPolicyRuleState(); - final var policyRuleBasic = - PolicyRuleBasic.newBuilder().setPolicyRuleState(expectedPolicyRuleState).build(); final var policyRuleDevice = Policy.PolicyRuleDevice.newBuilder().setPolicyRuleBasic(policyRuleBasic).build(); diff --git a/src/policy/target/kubernetes/kubernetes.yml b/src/policy/target/kubernetes/kubernetes.yml index d70d05ad91cface20dd5ad1fc15ec1578a0767ca..5af647628b4e2e24a566865502d355afb98550c2 100644 --- a/src/policy/target/kubernetes/kubernetes.yml +++ b/src/policy/target/kubernetes/kubernetes.yml @@ -3,8 +3,8 @@ apiVersion: v1 kind: Service metadata: annotations: - app.quarkus.io/commit-id: 4a11d9130e05e969e9370636484943e1fe2f8bd1 - app.quarkus.io/build-timestamp: 2022-07-27 - 12:54:10 +0000 + app.quarkus.io/commit-id: e8b83d84e122dbca908952148fffda42dd975765 + app.quarkus.io/build-timestamp: 2022-07-28 - 09:50:15 +0000 labels: app.kubernetes.io/name: policyservice app: policyservice @@ -25,8 +25,8 @@ apiVersion: apps/v1 kind: Deployment metadata: annotations: - app.quarkus.io/commit-id: 4a11d9130e05e969e9370636484943e1fe2f8bd1 - app.quarkus.io/build-timestamp: 2022-07-27 - 12:54:10 +0000 + app.quarkus.io/commit-id: e8b83d84e122dbca908952148fffda42dd975765 + app.quarkus.io/build-timestamp: 2022-07-28 - 09:50:15 +0000 labels: app: policyservice app.kubernetes.io/name: policyservice @@ -39,8 +39,8 @@ spec: template: metadata: annotations: - app.quarkus.io/commit-id: 4a11d9130e05e969e9370636484943e1fe2f8bd1 - app.quarkus.io/build-timestamp: 2022-07-27 - 12:54:10 +0000 + app.quarkus.io/commit-id: e8b83d84e122dbca908952148fffda42dd975765 + app.quarkus.io/build-timestamp: 2022-07-28 - 09:50:15 +0000 labels: app: policyservice app.kubernetes.io/name: policyservice