diff --git a/src/policy/src/main/java/eu/teraflow/policy/Serializer.java b/src/policy/src/main/java/eu/teraflow/policy/Serializer.java index 9e536450873fe9d6e31f66dd71088ae401fce64e..97243789031e872a883c65def4d01ebecb9e916d 100644 --- a/src/policy/src/main/java/eu/teraflow/policy/Serializer.java +++ b/src/policy/src/main/java/eu/teraflow/policy/Serializer.java @@ -23,6 +23,7 @@ import context.ContextOuterClass.ConfigRule_Custom; import context.ContextOuterClass.ContextId; import context.ContextOuterClass.DeviceId; import context.ContextOuterClass.DeviceOperationalStatusEnum; +import context.ContextOuterClass.Location.LocationCase; import context.ContextOuterClass.Uuid; import eu.teraflow.policy.acl.AclAction; import eu.teraflow.policy.acl.AclEntry; @@ -2109,12 +2110,14 @@ public class Serializer { final var serializedEndPointId = serialize(endPointId); final var serializedKpiSampleTypes = kpiSampleTypes.stream().map(this::serialize).collect(Collectors.toList()); - final var serializedEndPointLocation = serialize(endPointLocation); + if (endPointLocation != null) { + final var serializedEndPointLocation = serialize(endPointLocation); + builder.setEndpointLocation(serializedEndPointLocation); + } builder.setEndpointId(serializedEndPointId); builder.setEndpointType(endPointType); builder.addAllKpiSampleTypes(serializedKpiSampleTypes); - builder.setEndpointLocation(serializedEndPointLocation); return builder.build(); } @@ -2128,9 +2131,15 @@ public class Serializer { final var endPointId = deserialize(serializedEndPointId); final var kpiSampleTypes = serializedKpiSampleTypes.stream().map(this::deserialize).collect(Collectors.toList()); - final var endPointLocation = deserialize(serializedEndPointLocation); - return new EndPoint(endPointId, endPointType, kpiSampleTypes, endPointLocation); + if (serializedEndPointLocation.getLocationCase() != LocationCase.LOCATION_NOT_SET) { + final var endPointLocation = deserialize(serializedEndPointLocation); + return new EndPoint.EndPointBuilder(endPointId, endPointType, kpiSampleTypes) + .location(endPointLocation) + .build(); + } + + return new EndPoint.EndPointBuilder(endPointId, endPointType, kpiSampleTypes).build(); } public ContextOuterClass.Device serialize(Device device) { diff --git a/src/policy/src/main/java/eu/teraflow/policy/context/model/EndPoint.java b/src/policy/src/main/java/eu/teraflow/policy/context/model/EndPoint.java index a657b59218598c375cfe272f6d3dc1b2ed5bdd04..b40952b85a39226738e03261be0084acd0155b22 100644 --- a/src/policy/src/main/java/eu/teraflow/policy/context/model/EndPoint.java +++ b/src/policy/src/main/java/eu/teraflow/policy/context/model/EndPoint.java @@ -26,15 +26,11 @@ public class EndPoint { private final List<KpiSampleType> kpiSampleTypes; private final Location endPointLocation; - public EndPoint( - EndPointId endPointId, - String endPointType, - List<KpiSampleType> kpiSampleTypes, - Location endPointLocation) { - this.endPointId = endPointId; - this.endPointType = endPointType; - this.kpiSampleTypes = kpiSampleTypes; - this.endPointLocation = endPointLocation; + EndPoint(EndPointBuilder builder) { + this.endPointId = builder.endPointId; + this.endPointType = builder.endPointType; + this.kpiSampleTypes = builder.kpiSampleTypes; + this.endPointLocation = builder.endPointLocation; } public EndPointId getEndPointId() { @@ -63,4 +59,47 @@ public class EndPoint { Util.toString(kpiSampleTypes), endPointLocation); } + + public static class EndPointBuilder { + private final EndPointId endPointId; + private final String endPointType; + private final List<KpiSampleType> kpiSampleTypes; + private Location endPointLocation; + + public EndPointBuilder( + EndPointId endPointId, String endPointType, List<KpiSampleType> kpiSampleTypes) { + this.endPointId = endPointId; + this.endPointType = endPointType; + this.kpiSampleTypes = kpiSampleTypes; + } + + public EndPointBuilder location(Location endPointLocation) { + this.endPointLocation = endPointLocation; + return this; + } + + public EndPoint build() { + EndPoint endPoint = new EndPoint(this); + validateEndPointObject(endPoint); + return endPoint; + } + + private void validateEndPointObject(EndPoint endPoint) { + final var validatedEndPointId = endPoint.getEndPointId(); + final var validatedEndPointType = endPoint.getEndPointType(); + final var validatedKpiSampleTypes = endPoint.getKpiSampleTypes(); + + if (validatedEndPointId == null) { + throw new IllegalStateException("EndPoint ID cannot be null"); + } + + if (validatedEndPointType == null) { + throw new IllegalStateException("EndPoint type cannot be null"); + } + + if (validatedKpiSampleTypes == null) { + throw new IllegalStateException("Kpi sample types cannot be null"); + } + } + } } diff --git a/src/policy/src/test/java/eu/teraflow/policy/EndPointCreationTest.java b/src/policy/src/test/java/eu/teraflow/policy/EndPointCreationTest.java new file mode 100644 index 0000000000000000000000000000000000000000..4c113dc95fda5160959505bfc90c02c4bd39acf7 --- /dev/null +++ b/src/policy/src/test/java/eu/teraflow/policy/EndPointCreationTest.java @@ -0,0 +1,124 @@ +/* +* Copyright 2021-2023 H2020 TeraFlow (https://www.teraflow-h2020.eu/) +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +package eu.teraflow.policy; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; + +import eu.teraflow.policy.context.model.EndPoint; +import eu.teraflow.policy.context.model.EndPointId; +import eu.teraflow.policy.context.model.Location; +import eu.teraflow.policy.context.model.LocationTypeRegion; +import eu.teraflow.policy.context.model.TopologyId; +import eu.teraflow.policy.kpi_sample_types.model.KpiSampleType; +import io.quarkus.test.junit.QuarkusTest; +import java.util.List; +import org.junit.jupiter.api.Test; + +@QuarkusTest +class EndPointCreationTest { + + @Test + void shouldCreateEndPointObjectGivenAllAvailableFields() { + final var expectedTopologyId = new TopologyId("contextId", "id"); + final var expectedDeviceId = "expectedDeviceId"; + final var expectedId = "expectedId"; + + final var expectedEndPointId = new EndPointId(expectedTopologyId, expectedDeviceId, expectedId); + final var expectedEndPointType = "expectedEndPointType"; + final var expectedKpiSampleTypes = + List.of(KpiSampleType.BYTES_RECEIVED, KpiSampleType.BYTES_TRANSMITTED); + + final var expectedLocationType = new LocationTypeRegion("ATH"); + final var expectedEndPointLocation = new Location(expectedLocationType); + + final var expectedEndPoint = + new EndPoint.EndPointBuilder( + expectedEndPointId, expectedEndPointType, expectedKpiSampleTypes) + .location(expectedEndPointLocation) + .build(); + + assertThat(expectedEndPoint.getEndPointId()).isEqualTo(expectedEndPointId); + assertThat(expectedEndPoint.getEndPointType()).isEqualTo(expectedEndPointType); + assertThat(expectedEndPoint.getKpiSampleTypes()).isEqualTo(expectedKpiSampleTypes); + assertThat(expectedEndPoint.getEndPointLocation()).isEqualTo(expectedEndPointLocation); + } + + @Test + void shouldCreateEndPointObjectGivenAllFieldsExceptFromLocation() { + final var expectedTopologyId = new TopologyId("contextId", "id"); + final var expectedDeviceId = "expectedDeviceId"; + final var expectedId = "expectedId"; + + final var expectedEndPointId = new EndPointId(expectedTopologyId, expectedDeviceId, expectedId); + final var expectedEndPointType = "expectedEndPointType"; + final var expectedKpiSampleTypes = + List.of(KpiSampleType.BYTES_RECEIVED, KpiSampleType.BYTES_TRANSMITTED); + + final var expectedEndPoint = + new EndPoint.EndPointBuilder( + expectedEndPointId, expectedEndPointType, expectedKpiSampleTypes) + .build(); + + assertThat(expectedEndPoint.getEndPointId()).isEqualTo(expectedEndPointId); + assertThat(expectedEndPoint.getEndPointType()).isEqualTo(expectedEndPointType); + assertThat(expectedEndPoint.getKpiSampleTypes()).isEqualTo(expectedKpiSampleTypes); + } + + @Test + void shouldThrowIllegalStateExceptionDuringCreationOfEndPointObjectUponMissingEndPointId() { + final var expectedEndPointType = "expectedEndPointType"; + final var expectedKpiSampleTypes = + List.of(KpiSampleType.BYTES_RECEIVED, KpiSampleType.BYTES_TRANSMITTED); + + final var endPoint = + new EndPoint.EndPointBuilder(null, expectedEndPointType, expectedKpiSampleTypes); + + assertThatExceptionOfType(IllegalStateException.class).isThrownBy(endPoint::build); + } + + @Test + void shouldThrowIllegalStateExceptionDuringCreationOfEndPointObjectUponMissingEndPointType() { + final var expectedTopologyId = new TopologyId("contextId", "id"); + final var expectedDeviceId = "expectedDeviceId"; + final var expectedId = "expectedId"; + + final var expectedEndPointId = new EndPointId(expectedTopologyId, expectedDeviceId, expectedId); + final var expectedKpiSampleTypes = + List.of(KpiSampleType.BYTES_RECEIVED, KpiSampleType.BYTES_TRANSMITTED); + + final var endPoint = + new EndPoint.EndPointBuilder(expectedEndPointId, null, expectedKpiSampleTypes); + + assertThatExceptionOfType(IllegalStateException.class).isThrownBy(endPoint::build); + } + + @Test + void shouldThrowIllegalStateExceptionDuringCreationOfEndPointObjectUponMissingKpiSampleTypes() { + final var expectedTopologyId = new TopologyId("contextId", "id"); + final var expectedDeviceId = "expectedDeviceId"; + final var expectedId = "expectedId"; + + final var expectedEndPointId = new EndPointId(expectedTopologyId, expectedDeviceId, expectedId); + final var expectedEndPointType = "expectedEndPointType"; + + final var endPoint = + new EndPoint.EndPointBuilder(expectedEndPointId, expectedEndPointType, null); + + assertThatExceptionOfType(IllegalStateException.class).isThrownBy(endPoint::build); + } +} diff --git a/src/policy/src/test/java/eu/teraflow/policy/SerializerTest.java b/src/policy/src/test/java/eu/teraflow/policy/SerializerTest.java index 40e8ce80bacc02cca70a730f0f5a618e761d597d..fd2483a0a6ece8d2354d9b1ff9bb5233ff70cb9e 100644 --- a/src/policy/src/test/java/eu/teraflow/policy/SerializerTest.java +++ b/src/policy/src/test/java/eu/teraflow/policy/SerializerTest.java @@ -58,7 +58,7 @@ import eu.teraflow.policy.context.model.DeviceConfig; import eu.teraflow.policy.context.model.DeviceDriverEnum; import eu.teraflow.policy.context.model.DeviceOperationalStatus; import eu.teraflow.policy.context.model.Empty; -import eu.teraflow.policy.context.model.EndPoint; +import eu.teraflow.policy.context.model.EndPoint.EndPointBuilder; import eu.teraflow.policy.context.model.EndPointId; import eu.teraflow.policy.context.model.Event; import eu.teraflow.policy.context.model.EventTypeEnum; @@ -3484,8 +3484,8 @@ class SerializerTest { final var locationTypeRegion = new LocationTypeRegion("ATH"); final var location = new Location(locationTypeRegion); - - final var endPoint = new EndPoint(endPointId, endPointType, kpiSampleTypes, location); + final var endPoint = + new EndPointBuilder(endPointId, endPointType, kpiSampleTypes).location(location).build(); final var serializedEndPointId = serializer.serialize(endPointId); final var serializedKpiSampleTypes = @@ -3521,8 +3521,9 @@ class SerializerTest { final var expectedLocation = new Location(expectedLocationTypeRegion); final var expectedEndPoint = - new EndPoint( - expectedEndPointId, expectedEndPointType, expectedKpiSampleTypes, expectedLocation); + new EndPointBuilder(expectedEndPointId, expectedEndPointType, expectedKpiSampleTypes) + .location(expectedLocation) + .build(); final var serializedEndPointId = serializer.serialize(expectedEndPointId); final var serializedKpiSampleTypes = @@ -3575,7 +3576,10 @@ class SerializerTest { List.of(KpiSampleType.BYTES_RECEIVED, KpiSampleType.BYTES_TRANSMITTED); final var locationTypeRegionA = new LocationTypeRegion("ATH"); final var locationA = new Location(locationTypeRegionA); - final var endPointA = new EndPoint(endPointIdA, endPointTypeA, kpiSampleTypesA, locationA); + final var endPointA = + new EndPointBuilder(endPointIdA, endPointTypeA, kpiSampleTypesA) + .location(locationA) + .build(); final var expectedTopologyIdB = new TopologyId("contextIdB", "idB"); final var expectedDeviceIdB = "expectedDeviceIdB"; @@ -3587,7 +3591,10 @@ class SerializerTest { List.of(KpiSampleType.BYTES_RECEIVED, KpiSampleType.BYTES_TRANSMITTED); final var locationTypeRegionB = new LocationTypeRegion("ATH"); final var locationB = new Location(locationTypeRegionB); - final var endPointB = new EndPoint(endPointIdB, endPointTypeB, kpiSampleTypesB, locationB); + final var endPointB = + new EndPointBuilder(endPointIdB, endPointTypeB, kpiSampleTypesB) + .location(locationB) + .build(); final var endPoints = List.of(endPointA, endPointB); @@ -3654,7 +3661,10 @@ class SerializerTest { List.of(KpiSampleType.BYTES_RECEIVED, KpiSampleType.BYTES_TRANSMITTED); final var locationTypeRegionA = new LocationTypeRegion("ATH"); final var locationA = new Location(locationTypeRegionA); - final var endPointA = new EndPoint(endPointIdA, endPointTypeA, kpiSampleTypesA, locationA); + final var endPointA = + new EndPointBuilder(endPointIdA, endPointTypeA, kpiSampleTypesA) + .location(locationA) + .build(); final var expectedTopologyIdB = new TopologyId("contextIdB", "idB"); final var expectedDeviceIdB = "expectedDeviceIdB"; @@ -3666,7 +3676,10 @@ class SerializerTest { List.of(KpiSampleType.BYTES_RECEIVED, KpiSampleType.BYTES_TRANSMITTED); final var locationTypeRegionB = new LocationTypeRegion("ATH"); final var locationB = new Location(locationTypeRegionB); - final var endPointB = new EndPoint(endPointIdB, endPointTypeB, kpiSampleTypesB, locationB); + final var endPointB = + new EndPointBuilder(endPointIdB, endPointTypeB, kpiSampleTypesB) + .location(locationB) + .build(); final var endPoints = List.of(endPointA, endPointB); diff --git a/src/policy/target/kubernetes/kubernetes.yml b/src/policy/target/kubernetes/kubernetes.yml index b3313f771654e02f80bb7e4cadf68476afcadc75..2f4a467ee17ca48612553514935345c9b7590aa6 100644 --- a/src/policy/target/kubernetes/kubernetes.yml +++ b/src/policy/target/kubernetes/kubernetes.yml @@ -31,10 +31,6 @@ metadata: app.quarkus.io/commit-id: a25756b932a3146d95a993cf529e26e48654455e app.quarkus.io/build-timestamp: 2022-08-01 - 12:51:14 +0000 labels: - app: policyservice - app.kubernetes.io/name: policyservice - name: policyservice -spec: replicas: 1 selector: matchLabels: