Skip to content
Snippets Groups Projects
Commit 70662d96 authored by Fotis Soldatos's avatar Fotis Soldatos
Browse files

Merge branch 'policy/implement-policyService-interface' into 'develop'

refactor(policy): refactor PolicyService & PolicyGateway interfaces

See merge request teraflow-h2020/controller!149
parents e8b83d84 e7a5f906
No related branches found
No related tags found
1 merge request!54Release 2.0.0
......@@ -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
......
......@@ -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 {
......
......@@ -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);
}
}
......@@ -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();
......
......@@ -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
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment