Skip to content
/*
* Copyright 2022-2023 ETSI TeraFlowSDN - TFS OSG (https://tfs.etsi.org/)
*
* 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 org.etsi.tfs.policy.policy.model;
import java.util.List;
import lombok.Data;
@Data
public class PolicyRuleBase {
protected PolicyRuleBasic policyRuleBasic;
protected List<String> deviceIds;
protected Boolean isValid;
protected String exceptionMessage;
}
/*
* Copyright 2022-2023 ETSI TeraFlowSDN - TFS OSG (https://tfs.etsi.org/)
*
* 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 org.etsi.tfs.policy.policy.model;
import static com.google.common.base.Preconditions.checkArgument;
import java.util.ArrayList;
import java.util.List;
import org.etsi.tfs.policy.common.Util;
public class PolicyRuleBasic {
private String policyRuleId;
private PolicyRuleState policyRuleState;
private int priority;
private List<PolicyRuleCondition> policyRuleConditions;
private BooleanOperator booleanOperator;
private List<PolicyRuleAction> policyRuleActions;
private Boolean isValid;
private String exceptionMessage;
public PolicyRuleBasic(
String policyRuleId,
PolicyRuleState policyRuleState,
int priority,
List<PolicyRuleCondition> policyRuleConditions,
BooleanOperator booleanOperator,
List<PolicyRuleAction> policyRuleActions) {
try {
checkArgument(!policyRuleId.isBlank(), "Policy rule ID must not be empty.");
this.policyRuleId = policyRuleId;
this.policyRuleState = policyRuleState;
checkArgument(priority >= 0, "Priority value must be greater or equal than zero.");
this.priority = priority;
checkArgument(!policyRuleConditions.isEmpty(), "Policy Rule conditions cannot be empty.");
this.policyRuleConditions = policyRuleConditions;
checkArgument(
booleanOperator != BooleanOperator.POLICYRULE_CONDITION_BOOLEAN_UNDEFINED,
"Boolean operator cannot be undefined");
this.booleanOperator = booleanOperator;
checkArgument(!policyRuleActions.isEmpty(), "Policy Rule actions cannot be empty.");
this.policyRuleActions = policyRuleActions;
this.isValid = true;
} catch (Exception e) {
this.policyRuleId = "";
this.priority = 0;
this.policyRuleConditions = new ArrayList<PolicyRuleCondition>();
this.booleanOperator = BooleanOperator.POLICYRULE_CONDITION_BOOLEAN_UNDEFINED;
this.policyRuleActions = new ArrayList<PolicyRuleAction>();
this.isValid = false;
this.exceptionMessage = e.getMessage();
}
}
public boolean areArgumentsValid() {
return isValid;
}
public String getExceptionMessage() {
return exceptionMessage;
}
public String getPolicyRuleId() {
return policyRuleId;
}
public PolicyRuleState getPolicyRuleState() {
return policyRuleState;
}
public void setPolicyRuleState(PolicyRuleState state) {
this.policyRuleState = state;
}
public int getPriority() {
return priority;
}
public List<PolicyRuleCondition> getPolicyRuleConditions() {
return policyRuleConditions;
}
public BooleanOperator getBooleanOperator() {
return booleanOperator;
}
public List<PolicyRuleAction> getPolicyRuleActions() {
return policyRuleActions;
}
@Override
public String toString() {
return String.format(
"%s:{policyRuleId:\"%s\", %s, priority:%d, [%s], booleanOperator:\"%s\", [%s]}",
getClass().getSimpleName(),
policyRuleId,
policyRuleState,
priority,
Util.toString(policyRuleConditions),
booleanOperator.toString(),
Util.toString(policyRuleActions));
}
}
/*
* Copyright 2022-2023 ETSI TeraFlowSDN - TFS OSG (https://tfs.etsi.org/)
*
* 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 org.etsi.tfs.policy.policy.model;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import org.etsi.tfs.policy.monitoring.model.KpiValue;
public class PolicyRuleCondition {
private final String kpiId;
private final NumericalOperator numericalOperator;
private final KpiValue<?> kpiValue;
public PolicyRuleCondition(
String kpiId, NumericalOperator numericalOperator, KpiValue<?> kpiValue) {
checkNotNull(kpiId, "Kpi ID must not be null.");
checkArgument(!kpiId.isBlank(), "Kpi ID must not be empty.");
this.kpiId = kpiId;
checkArgument(
numericalOperator != NumericalOperator.POLICY_RULE_CONDITION_NUMERICAL_UNDEFINED,
"Numerical operator cannot be undefined");
this.numericalOperator = numericalOperator;
checkNotNull(kpiValue, "Kpi value must not be null.");
checkArgument(
isKpiValueValid(kpiValue),
"Kpi value must be: String, Float, Boolean or Integer but it was [%s].",
kpiValue.getValue().getClass().getName());
this.kpiValue = kpiValue;
}
public String getKpiId() {
return kpiId;
}
public NumericalOperator getNumericalOperator() {
return numericalOperator;
}
public KpiValue<?> getKpiValue() {
return kpiValue;
}
private boolean isKpiValueValid(KpiValue<?> kpiValue) {
final var kpiValueType = kpiValue.getValue();
if (kpiValueType instanceof String) {
return true;
}
if (kpiValueType instanceof Boolean) {
return true;
}
if (kpiValueType instanceof Integer) {
return true;
}
return kpiValueType instanceof Float;
}
@Override
public String toString() {
return String.format(
"%s:{kpiId:\"%s\", numericalOperator:\"%s\", %s}",
getClass().getSimpleName(), kpiId, numericalOperator.toString(), kpiValue);
}
}
/*
* Copyright 2022-2023 ETSI TeraFlowSDN - TFS OSG (https://tfs.etsi.org/)
*
* 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 org.etsi.tfs.policy.policy.model;
import static com.google.common.base.Preconditions.checkArgument;
import java.util.ArrayList;
import java.util.List;
import org.etsi.tfs.policy.common.Util;
public class PolicyRuleDevice extends PolicyRuleBase {
public PolicyRuleDevice(PolicyRuleBasic policyRuleBasic, List<String> deviceIds) {
try {
this.policyRuleBasic = policyRuleBasic;
checkArgument(!deviceIds.isEmpty(), "Device Ids must not be empty.");
this.deviceIds = deviceIds;
this.isValid = true;
} catch (Exception e) {
this.policyRuleBasic = policyRuleBasic;
this.deviceIds = new ArrayList<String>();
this.isValid = false;
this.exceptionMessage = e.getMessage();
}
}
public boolean areArgumentsValid() {
return isValid;
}
@Override
public String toString() {
return String.format(
"%s:{%s, [%s]}", getClass().getSimpleName(), policyRuleBasic, Util.toString(deviceIds));
}
}
/*
* Copyright 2022-2023 ETSI TeraFlowSDN - TFS OSG (https://tfs.etsi.org/)
*
* 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 org.etsi.tfs.policy.policy.model;
import static com.google.common.base.Preconditions.checkArgument;
import java.util.ArrayList;
import java.util.List;
import lombok.Data;
import org.etsi.tfs.policy.common.Util;
import org.etsi.tfs.policy.context.model.ServiceId;
@Data
public class PolicyRuleService extends PolicyRuleBase {
private ServiceId serviceId;
public PolicyRuleService(
PolicyRuleBasic policyRuleBasic, ServiceId serviceId, List<String> deviceIds) {
try {
this.policyRuleBasic = policyRuleBasic;
checkArgument(
!serviceId.getContextId().isBlank(), "Context Id of Service Id must not be empty.");
checkArgument(!serviceId.getId().isBlank(), "Service Id must not be empty.");
this.serviceId = serviceId;
// TODO If device list not empty
// checkArgument(!deviceIds.isEmpty(), "Device Ids must not be empty.");
this.deviceIds = deviceIds;
this.isValid = true;
this.exceptionMessage = "";
} catch (Exception e) {
this.policyRuleBasic = policyRuleBasic;
this.serviceId = new ServiceId("", "");
this.deviceIds = new ArrayList<String>();
this.isValid = false;
this.exceptionMessage = e.getMessage();
}
}
public boolean areArgumentsValid() {
return isValid;
}
@Override
public String toString() {
return String.format(
"%s:{%s, %s, [%s]}",
getClass().getSimpleName(), policyRuleBasic, serviceId, Util.toString(deviceIds));
}
}
/*
* Copyright 2022-2023 ETSI TeraFlowSDN - TFS OSG (https://tfs.etsi.org/)
*
* 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 org.etsi.tfs.policy.policy.model;
public class PolicyRuleState {
private PolicyRuleStateEnum policyRuleStateEnum;
private String policyRuleStateMessage;
public PolicyRuleState(PolicyRuleStateEnum policyRuleStateEnum, String policyRuleStateMessage) {
this.policyRuleStateEnum = policyRuleStateEnum;
this.policyRuleStateMessage = policyRuleStateMessage;
}
public void setRuleState(PolicyRuleStateEnum policyRuleStateEnum) {
this.policyRuleStateEnum = policyRuleStateEnum;
}
public PolicyRuleStateEnum getRuleState() {
return policyRuleStateEnum;
}
public void setPolicyRuleStateMessage(String policyRuleStateMessage) {
this.policyRuleStateMessage = policyRuleStateMessage;
}
public String getPolicyRuleStateMessage() {
return this.policyRuleStateMessage;
}
@Override
public String toString() {
return String.format(
"%s:{ruleState:\"%s\"}", getClass().getSimpleName(), policyRuleStateEnum.toString());
}
}
/*
* Copyright 2022-2023 ETSI TeraFlowSDN - TFS OSG (https://tfs.etsi.org/)
*
* 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 org.etsi.tfs.policy.policy.model;
public enum PolicyRuleStateEnum {
POLICY_UNDEFINED,
POLICY_FAILED,
POLICY_INSERTED,
POLICY_VALIDATED,
POLICY_PROVISIONED,
POLICY_ACTIVE,
POLICY_ENFORCED,
POLICY_INEFFECTIVE,
POLICY_EFFECTIVE,
POLICY_UPDATED,
POLICY_REMOVED
}
/*
* Copyright 2022-2023 ETSI TeraFlowSDN - TFS OSG (https://tfs.etsi.org/)
*
* 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 org.etsi.tfs.policy.policy.model;
public interface PolicyRuleType<T> {
public T getPolicyRuleType();
public PolicyRuleBasic getPolicyRuleBasic();
}
/*
* Copyright 2022-2023 ETSI TeraFlowSDN - TFS OSG (https://tfs.etsi.org/)
*
* 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 org.etsi.tfs.policy.policy.model;
public class PolicyRuleTypeDevice implements PolicyRuleType<PolicyRuleDevice> {
private final PolicyRuleDevice policyRuleDevice;
public PolicyRuleTypeDevice(PolicyRuleDevice policyRuleDevice) {
this.policyRuleDevice = policyRuleDevice;
}
@Override
public PolicyRuleDevice getPolicyRuleType() {
return this.policyRuleDevice;
}
@Override
public PolicyRuleBasic getPolicyRuleBasic() {
return policyRuleDevice.getPolicyRuleBasic();
}
@Override
public String toString() {
return String.format("%s:{%s}", getClass().getSimpleName(), policyRuleDevice);
}
}
/*
* Copyright 2022-2023 ETSI TeraFlowSDN - TFS OSG (https://tfs.etsi.org/)
*
* 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 org.etsi.tfs.policy.policy.model;
public class PolicyRuleTypeService implements PolicyRuleType<PolicyRuleService> {
private final PolicyRuleService policyRuleService;
public PolicyRuleTypeService(PolicyRuleService policyRuleService) {
this.policyRuleService = policyRuleService;
}
@Override
public PolicyRuleService getPolicyRuleType() {
return this.policyRuleService;
}
@Override
public PolicyRuleBasic getPolicyRuleBasic() {
return policyRuleService.getPolicyRuleBasic();
}
@Override
public String toString() {
return String.format("%s:{%s}", getClass().getSimpleName(), policyRuleService);
}
}
/*
* Copyright 2022-2023 ETSI TeraFlowSDN - TFS OSG (https://tfs.etsi.org/)
*
* 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 org.etsi.tfs.policy.policy.service;
import jakarta.inject.Singleton;
import java.util.List;
import java.util.stream.Collectors;
import org.etsi.tfs.policy.monitoring.model.KpiValue;
import org.etsi.tfs.policy.policy.model.NumericalOperator;
import org.etsi.tfs.policy.policy.model.PolicyRuleCondition;
@Singleton
public class PolicyRuleConditionFieldsGetter {
public List<String> getKpiIds(List<PolicyRuleCondition> policyRuleConditions) {
return policyRuleConditions.stream()
.map(PolicyRuleCondition::getKpiId)
.collect(Collectors.toList());
}
public List<KpiValue> getKpiValues(List<PolicyRuleCondition> policyRuleConditions) {
return policyRuleConditions.stream()
.map(PolicyRuleCondition::getKpiValue)
.collect(Collectors.toList());
}
public List<NumericalOperator> getNumericalOperators(
List<PolicyRuleCondition> policyRuleConditions) {
return policyRuleConditions.stream()
.map(PolicyRuleCondition::getNumericalOperator)
.collect(Collectors.toList());
}
}
/*
* Copyright 2022-2023 ETSI TeraFlowSDN - TFS OSG (https://tfs.etsi.org/)
*
* 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 org.etsi.tfs.policy.policy.service;
import io.smallrye.mutiny.Uni;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import java.util.ArrayList;
import java.util.List;
import org.etsi.tfs.policy.context.ContextService;
import org.etsi.tfs.policy.context.model.Device;
import org.etsi.tfs.policy.context.model.EndPointId;
import org.etsi.tfs.policy.context.model.Service;
import org.etsi.tfs.policy.context.model.ServiceId;
import org.etsi.tfs.policy.policy.model.PolicyRuleService;
import org.jboss.logging.Logger;
@ApplicationScoped
public class PolicyRuleConditionValidator {
private static final Logger LOGGER = Logger.getLogger(PolicyRuleConditionValidator.class);
private static final String INVALID_MESSAGE = "%s is invalid.";
private static final String VALID_MESSAGE = "%s is valid.";
private final ContextService contextService;
@Inject
public PolicyRuleConditionValidator(ContextService contextService) {
this.contextService = contextService;
}
public Uni<Boolean> isDeviceIdValid(String deviceId) {
return contextService
.getDevice(deviceId)
.onFailure()
.recoverWithItem((Device) null)
.onItem()
.transform(device -> checkIfDeviceIdExists(device, deviceId));
}
private boolean checkIfDeviceIdExists(Device device, String deviceId) {
if (device == null) {
return false;
}
final var deviceDeviceId = device.getDeviceId();
return deviceDeviceId.equals(deviceId);
}
public Uni<Boolean> isServiceIdValid(ServiceId serviceId, List<String> deviceIds) {
return contextService
.getService(serviceId)
.onFailure()
.recoverWithItem((Service) null)
.onItem()
.transform(service -> checkIfServiceIsValid(service, serviceId, deviceIds));
}
private boolean checkIfServiceIsValid(
Service service, ServiceId serviceId, List<String> deviceIds) {
return (checkIfServiceIdExists(service, serviceId)
&& checkIfServicesDeviceIdsExist(service, deviceIds));
}
private boolean checkIfServiceIdExists(Service service, ServiceId serviceId) {
if (service == null) {
return false;
}
final var serviceServiceIdServiceId = service.getServiceId();
final var serviceServiceIdContextId = serviceServiceIdServiceId.getContextId();
final var serviceServiceIdId = serviceServiceIdServiceId.getId();
return serviceServiceIdContextId.equals(serviceId.getContextId())
&& serviceServiceIdId.equals(serviceId.getId());
}
private boolean checkIfServicesDeviceIdsExist(Service service, List<String> deviceIds) {
if (deviceIds.isEmpty()) {
return true;
}
List<String> serviceDeviceIds = new ArrayList<>();
for (EndPointId serviceEndPointId : service.getServiceEndPointIds()) {
serviceDeviceIds.add(serviceEndPointId.getDeviceId());
}
return deviceIds.containsAll(serviceDeviceIds);
}
public Uni<Boolean> isUpdatedPolicyRuleIdValid(String updatedPolicyRuleId) {
return contextService
.getPolicyRule(updatedPolicyRuleId)
.onItem()
.ifNotNull()
.transform(
id -> {
return true;
})
.onItem()
.ifNull()
.continueWith(false);
}
public Uni<Boolean> isPolicyRuleServiceValid(String updatedPolicyRuleId, ServiceId serviceId) {
return contextService
.getPolicyRule(updatedPolicyRuleId)
.onItem()
.ifNotNull()
.transform(
policyRule -> {
var policyRuleService =
(PolicyRuleService) policyRule.getPolicyRuleType().getPolicyRuleType();
if (policyRuleService.getServiceId().getId().equals(serviceId.getId())) {
return true;
}
return false;
})
.onItem()
.ifNull()
.continueWith(false)
.onFailure()
.recoverWithItem(false);
}
}
...@@ -18,8 +18,8 @@ package org.etsi.tfs.policy.service; ...@@ -18,8 +18,8 @@ package org.etsi.tfs.policy.service;
import io.quarkus.grpc.GrpcClient; import io.quarkus.grpc.GrpcClient;
import io.smallrye.mutiny.Uni; import io.smallrye.mutiny.Uni;
import javax.enterprise.context.ApplicationScoped; import jakarta.enterprise.context.ApplicationScoped;
import javax.inject.Inject; import jakarta.inject.Inject;
import org.etsi.tfs.policy.Serializer; import org.etsi.tfs.policy.Serializer;
import org.etsi.tfs.policy.context.model.Empty; import org.etsi.tfs.policy.context.model.Empty;
import org.etsi.tfs.policy.context.model.Service; import org.etsi.tfs.policy.context.model.Service;
......
...@@ -17,8 +17,8 @@ ...@@ -17,8 +17,8 @@
package org.etsi.tfs.policy.service; package org.etsi.tfs.policy.service;
import io.smallrye.mutiny.Uni; import io.smallrye.mutiny.Uni;
import javax.enterprise.context.ApplicationScoped; import jakarta.enterprise.context.ApplicationScoped;
import javax.inject.Inject; import jakarta.inject.Inject;
import org.etsi.tfs.policy.context.model.Empty; import org.etsi.tfs.policy.context.model.Empty;
import org.etsi.tfs.policy.context.model.Service; import org.etsi.tfs.policy.context.model.Service;
import org.etsi.tfs.policy.context.model.ServiceId; import org.etsi.tfs.policy.context.model.ServiceId;
......
/*
* Copyright 2022-2023 ETSI TeraFlowSDN - TFS OSG (https://tfs.etsi.org/)
*
* 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 org.etsi.tfs.policy;
import static org.assertj.core.api.Assertions.assertThat;
import static org.etsi.tfs.policy.common.ApplicationProperties.INVALID_MESSAGE;
import static org.etsi.tfs.policy.common.ApplicationProperties.VALIDATED_POLICYRULE_STATE;
import io.quarkus.test.junit.QuarkusTest;
import io.quarkus.test.junit.mockito.InjectMock;
import io.smallrye.mutiny.Uni;
import jakarta.inject.Inject;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import org.etsi.tfs.policy.context.ContextService;
import org.etsi.tfs.policy.monitoring.MonitoringService;
import org.etsi.tfs.policy.monitoring.model.IntegerKpiValue;
import org.etsi.tfs.policy.monitoring.model.KpiValue;
import org.etsi.tfs.policy.policy.PolicyServiceImpl;
import org.etsi.tfs.policy.policy.model.BooleanOperator;
import org.etsi.tfs.policy.policy.model.NumericalOperator;
import org.etsi.tfs.policy.policy.model.PolicyRuleAction;
import org.etsi.tfs.policy.policy.model.PolicyRuleActionConfig;
import org.etsi.tfs.policy.policy.model.PolicyRuleActionEnum;
import org.etsi.tfs.policy.policy.model.PolicyRuleBasic;
import org.etsi.tfs.policy.policy.model.PolicyRuleCondition;
import org.etsi.tfs.policy.policy.model.PolicyRuleDevice;
import org.etsi.tfs.policy.policy.model.PolicyRuleState;
import org.etsi.tfs.policy.policy.model.PolicyRuleStateEnum;
import org.etsi.tfs.policy.policy.service.PolicyRuleConditionValidator;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
@QuarkusTest
class PolicyAddDeviceTest {
@Inject PolicyServiceImpl policyService;
@InjectMock PolicyRuleConditionValidator policyRuleConditionValidator;
@InjectMock ContextService contextService;
@InjectMock MonitoringService monitoringService;
static PolicyRuleBasic policyRuleBasic;
static PolicyRuleDevice policyRuleDevice;
@BeforeAll
static void init() {
String policyId = "policyRuleId";
KpiValue kpiValue = new IntegerKpiValue(100);
PolicyRuleCondition policyRuleCondition =
new PolicyRuleCondition(
"kpiId", NumericalOperator.POLICY_RULE_CONDITION_NUMERICAL_GREATER_THAN, kpiValue);
PolicyRuleActionConfig policyRuleActionConfig = new PolicyRuleActionConfig("key", "value");
PolicyRuleAction policyRuleAction =
new PolicyRuleAction(
PolicyRuleActionEnum.POLICY_RULE_ACTION_NO_ACTION,
Arrays.asList(policyRuleActionConfig));
policyRuleBasic =
new PolicyRuleBasic(
policyId,
new PolicyRuleState(PolicyRuleStateEnum.POLICY_INSERTED, "Failed due to some errors"),
1,
Arrays.asList(policyRuleCondition),
BooleanOperator.POLICYRULE_CONDITION_BOOLEAN_OR,
Arrays.asList(policyRuleAction));
List<String> deviceIds = Arrays.asList("device1", "device2");
policyRuleDevice = new PolicyRuleDevice(policyRuleBasic, deviceIds);
}
@Test
void deviceListMustNotBeEmpty()
throws ExecutionException, InterruptedException, TimeoutException {
CompletableFuture<PolicyRuleState> message = new CompletableFuture<>();
PolicyRuleDevice policyRuleDevice = new PolicyRuleDevice(policyRuleBasic, new ArrayList<>());
PolicyRuleState expectedResult =
new PolicyRuleState(PolicyRuleStateEnum.POLICY_FAILED, "Device Ids must not be empty.");
policyService
.addPolicyDevice(policyRuleDevice)
.subscribe()
.with(
item -> {
message.complete(item);
});
assertThat(message.get(5, TimeUnit.SECONDS).getPolicyRuleStateMessage())
.isEqualTo(expectedResult.getPolicyRuleStateMessage().toString());
}
@Test
void isPolicyRuleBasicValid() throws ExecutionException, InterruptedException, TimeoutException {
CompletableFuture<PolicyRuleState> message = new CompletableFuture<>();
PolicyRuleBasic policyRuleBasic =
new PolicyRuleBasic(
"policyId",
new PolicyRuleState(PolicyRuleStateEnum.POLICY_INSERTED, "Failed due to some errors"),
0,
new ArrayList<>(),
BooleanOperator.POLICYRULE_CONDITION_BOOLEAN_OR,
new ArrayList<>());
PolicyRuleDevice policyRuleDevice =
new PolicyRuleDevice(policyRuleBasic, Arrays.asList("device1", "device2"));
PolicyRuleState expectedResult =
new PolicyRuleState(
PolicyRuleStateEnum.POLICY_FAILED, "Policy Rule conditions cannot be empty.");
policyService
.addPolicyDevice(policyRuleDevice)
.subscribe()
.with(
item -> {
message.complete(item);
});
assertThat(message.get(5, TimeUnit.SECONDS).getPolicyRuleStateMessage())
.isEqualTo(expectedResult.getPolicyRuleStateMessage().toString());
}
@Test
void isPolicyRuleIdValid() throws ExecutionException, InterruptedException, TimeoutException {
CompletableFuture<PolicyRuleState> message = new CompletableFuture<>();
PolicyRuleDevice policyRuleDevice =
new PolicyRuleDevice(policyRuleBasic, Arrays.asList("device1", "device2"));
PolicyRuleState expectedResult =
new PolicyRuleState(
PolicyRuleStateEnum.POLICY_FAILED,
String.format(
INVALID_MESSAGE, policyRuleDevice.getPolicyRuleBasic().getPolicyRuleId()));
Mockito.when(policyRuleConditionValidator.isUpdatedPolicyRuleIdValid(Mockito.anyString()))
.thenReturn(Uni.createFrom().item(Boolean.FALSE));
policyService
.addPolicyDevice(policyRuleDevice)
.subscribe()
.with(
item -> {
message.complete(item);
});
assertThat(message.get(5, TimeUnit.SECONDS).getPolicyRuleStateMessage())
.isEqualTo(expectedResult.getPolicyRuleStateMessage().toString());
}
@Test
void successPolicyDevice() throws ExecutionException, InterruptedException, TimeoutException {
CompletableFuture<PolicyRuleState> message = new CompletableFuture<>();
PolicyRuleDevice policyRuleDevice =
new PolicyRuleDevice(policyRuleBasic, Arrays.asList("device1", "device2"));
PolicyRuleState expectedResult =
new PolicyRuleState(
PolicyRuleStateEnum.POLICY_VALIDATED,
VALIDATED_POLICYRULE_STATE.getPolicyRuleStateMessage());
Mockito.when(policyRuleConditionValidator.isDeviceIdValid(Mockito.anyString()))
.thenReturn(Uni.createFrom().item(Boolean.TRUE));
Mockito.when(contextService.setPolicyRule(Mockito.any()))
.thenReturn(Uni.createFrom().item("policyRuleId"));
policyService
.addPolicyDevice(policyRuleDevice)
.subscribe()
.with(
item -> {
message.complete(item);
});
assertThat(message.get(5, TimeUnit.SECONDS).getPolicyRuleStateMessage())
.isEqualTo(expectedResult.getPolicyRuleStateMessage().toString());
}
// @Test
// void failurePolicyDevice() throws ExecutionException, InterruptedException, TimeoutException
// {
// CompletableFuture<ExternalServiceFailureException> message = new CompletableFuture<>();
//
// PolicyRuleDevice policyRuleDevice =
// new PolicyRuleDevice(policyRuleBasic, Arrays.asList("device1", "device2"));
//
// // String expectedResult = "Failed1 to set policy rule for testing purposes.";
//
// PolicyRuleState expectedResult =
// new PolicyRuleState(
// PolicyRuleStateEnum.POLICY_VALIDATED,
// "Failed1 to set policy rule for testing purposes.");
//
// Mockito.when(policyRuleConditionValidator.isDeviceIdValid(Mockito.anyString()))
// .thenReturn(Uni.createFrom().item(Boolean.TRUE));
//
// Mockito.when(contextService.setPolicyRule(Mockito.any()))
// .thenReturn(
// Uni.createFrom()
// .failure(
// new ExternalServiceFailureException(
// "Failed to set policy rule for testing
// purposes.")));
//
// policyService
// .addPolicyDevice(policyRuleDevice)
// .subscribe()
// .with(
// item -> {},
// error -> {
//
// assertThat(error.getMessage()).isEqualTo(expectedResult.getPolicyRuleStateMessage());
// message.completeExceptionally(error);
// });
//
// assertThat(message.get(5, TimeUnit.SECONDS).getMessage())
// .isEqualTo(expectedResult.getPolicyRuleStateMessage().toString());
// }
}
/*
* Copyright 2022-2023 ETSI TeraFlowSDN - TFS OSG (https://tfs.etsi.org/)
*
* 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 org.etsi.tfs.policy;
import static org.assertj.core.api.Assertions.assertThat;
import static org.etsi.tfs.policy.common.ApplicationProperties.*;
import io.quarkus.test.junit.QuarkusTest;
import io.quarkus.test.junit.mockito.InjectMock;
import io.smallrye.mutiny.Uni;
import jakarta.inject.Inject;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import org.etsi.tfs.policy.context.ContextService;
import org.etsi.tfs.policy.context.model.Service;
import org.etsi.tfs.policy.context.model.ServiceId;
import org.etsi.tfs.policy.context.model.ServiceTypeEnum;
import org.etsi.tfs.policy.monitoring.MonitoringService;
import org.etsi.tfs.policy.monitoring.model.IntegerKpiValue;
import org.etsi.tfs.policy.monitoring.model.KpiValue;
import org.etsi.tfs.policy.policy.PolicyServiceImpl;
import org.etsi.tfs.policy.policy.model.BooleanOperator;
import org.etsi.tfs.policy.policy.model.NumericalOperator;
import org.etsi.tfs.policy.policy.model.PolicyRule;
import org.etsi.tfs.policy.policy.model.PolicyRuleAction;
import org.etsi.tfs.policy.policy.model.PolicyRuleActionConfig;
import org.etsi.tfs.policy.policy.model.PolicyRuleActionEnum;
import org.etsi.tfs.policy.policy.model.PolicyRuleBasic;
import org.etsi.tfs.policy.policy.model.PolicyRuleCondition;
import org.etsi.tfs.policy.policy.model.PolicyRuleService;
import org.etsi.tfs.policy.policy.model.PolicyRuleState;
import org.etsi.tfs.policy.policy.model.PolicyRuleStateEnum;
import org.etsi.tfs.policy.policy.service.PolicyRuleConditionValidator;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
@QuarkusTest
public class PolicyAddServiceTest {
@Inject PolicyServiceImpl policyService;
@InjectMock PolicyRuleConditionValidator policyRuleConditionValidator;
@InjectMock ContextService contextService;
@InjectMock MonitoringService monitoringService;
static PolicyRuleBasic policyRuleBasic;
static PolicyRuleService policyRuleService;
@BeforeAll
static void init() {
String policyId = "policyRuleId";
KpiValue kpiValue = new IntegerKpiValue(100);
PolicyRuleCondition policyRuleCondition =
new PolicyRuleCondition(
"kpiId", NumericalOperator.POLICY_RULE_CONDITION_NUMERICAL_GREATER_THAN, kpiValue);
PolicyRuleActionConfig policyRuleActionConfig = new PolicyRuleActionConfig("key", "value");
PolicyRuleAction policyRuleAction =
new PolicyRuleAction(
PolicyRuleActionEnum.POLICY_RULE_ACTION_NO_ACTION,
Arrays.asList(policyRuleActionConfig));
policyRuleBasic =
new PolicyRuleBasic(
policyId,
new PolicyRuleState(PolicyRuleStateEnum.POLICY_INSERTED, "Failed due to some errors"),
1,
Arrays.asList(policyRuleCondition),
BooleanOperator.POLICYRULE_CONDITION_BOOLEAN_OR,
Arrays.asList(policyRuleAction));
ServiceId serviceId = new ServiceId("contextId", "serviceId");
Service service = new Service(serviceId, ServiceTypeEnum.UNKNOWN, null, null, null, null, 0.0);
List<String> deviceIds = Arrays.asList("device1", "device2");
policyRuleService = new PolicyRuleService(policyRuleBasic, serviceId, deviceIds);
}
@Test
void contextOrServiceIdMustNotBeEmpty()
throws ExecutionException, InterruptedException, TimeoutException {
CompletableFuture<PolicyRuleState> message = new CompletableFuture<>();
ServiceId serviceId = new ServiceId("", "");
List<String> deviceIds = Arrays.asList("device1", "device2");
PolicyRuleService policyRuleService =
new PolicyRuleService(policyRuleBasic, serviceId, deviceIds);
PolicyRuleState expectedResult =
new PolicyRuleState(
PolicyRuleStateEnum.POLICY_FAILED, "Context Id of Service Id must not be empty.");
policyService
.addPolicyService(policyRuleService)
.subscribe()
.with(
item -> {
message.complete(item);
});
assertThat(message.get(5, TimeUnit.SECONDS).getPolicyRuleStateMessage())
.isEqualTo(expectedResult.getPolicyRuleStateMessage().toString());
}
@Test
void serviceIdMustNotBeEmpty() throws ExecutionException, InterruptedException, TimeoutException {
CompletableFuture<PolicyRuleState> message = new CompletableFuture<>();
ServiceId serviceId = new ServiceId("sdf", "");
List<String> deviceIds = Arrays.asList("device1", "device2");
PolicyRuleService policyRuleService =
new PolicyRuleService(policyRuleBasic, serviceId, deviceIds);
PolicyRuleState expectedResult =
new PolicyRuleState(PolicyRuleStateEnum.POLICY_FAILED, "Service Id must not be empty.");
policyService
.addPolicyService(policyRuleService)
.subscribe()
.with(item -> message.complete(item));
assertThat(message.get(5, TimeUnit.SECONDS).getPolicyRuleStateMessage())
.isEqualTo(expectedResult.getPolicyRuleStateMessage().toString());
}
@Test
void policyRuleIdMustNotBeEmpty()
throws ExecutionException, InterruptedException, TimeoutException {
CompletableFuture<PolicyRuleState> message = new CompletableFuture<>();
String policyId = "";
PolicyRuleBasic policyRuleBasic =
new PolicyRuleBasic(
policyId,
new PolicyRuleState(PolicyRuleStateEnum.POLICY_INSERTED, "Failed due to some errors"),
1,
new ArrayList<>(),
null,
new ArrayList<>());
ServiceId serviceId = new ServiceId("contextId", "serviceId");
Service service = new Service(serviceId, ServiceTypeEnum.UNKNOWN, null, null, null, null, 0.0);
PolicyRuleService policyRuleService =
new PolicyRuleService(policyRuleBasic, serviceId, new ArrayList<>());
PolicyRuleState expectedResult =
new PolicyRuleState(PolicyRuleStateEnum.POLICY_FAILED, "Policy rule ID must not be empty.");
policyService
.addPolicyService(policyRuleService)
.subscribe()
.with(
item -> {
message.complete(item);
});
assertThat(message.get(5, TimeUnit.SECONDS).getPolicyRuleStateMessage())
.isEqualTo(expectedResult.getPolicyRuleStateMessage().toString());
}
@Test
void checkMessageIfServiceIsNotValid()
throws ExecutionException, InterruptedException, TimeoutException {
CompletableFuture<PolicyRuleState> message = new CompletableFuture<>();
ServiceId serviceId = new ServiceId("contextId", "serviceId");
PolicyRuleService policyRuleService =
new PolicyRuleService(policyRuleBasic, serviceId, new ArrayList<>());
PolicyRuleState expectedResult =
new PolicyRuleState(PolicyRuleStateEnum.POLICY_FAILED, serviceId + " is invalid.");
Mockito.when(
policyRuleConditionValidator.isServiceIdValid(
Mockito.any(ServiceId.class), Mockito.anyList()))
.thenReturn(Uni.createFrom().item(Boolean.FALSE));
policyService
.addPolicyService(policyRuleService)
.subscribe()
.with(
item -> {
message.complete(item);
});
assertThat(message.get(5, TimeUnit.SECONDS).getPolicyRuleStateMessage())
.isEqualTo(expectedResult.getPolicyRuleStateMessage().toString());
}
@Test
void policyServiceSuccess()
throws ExecutionException, InterruptedException, TimeoutException, IOException {
CompletableFuture<PolicyRuleState> message = new CompletableFuture<>();
PolicyRuleState expectedResult =
new PolicyRuleState(
PolicyRuleStateEnum.POLICY_VALIDATED,
VALIDATED_POLICYRULE_STATE.getPolicyRuleStateMessage());
Mockito.when(
policyRuleConditionValidator.isServiceIdValid(
Mockito.any(ServiceId.class), Mockito.anyList()))
.thenReturn(Uni.createFrom().item(Boolean.TRUE));
Mockito.when(contextService.setPolicyRule(Mockito.any(PolicyRule.class)))
.thenReturn(Uni.createFrom().item("policyRuleId"));
policyService
.addPolicyService(policyRuleService)
.subscribe()
.with(
item -> {
message.complete(item);
});
assertThat(message.get(5, TimeUnit.SECONDS).getPolicyRuleStateMessage())
.isEqualTo(expectedResult.getPolicyRuleStateMessage().toString());
}
}
/*
* Copyright 2022-2023 ETSI TeraFlowSDN - TFS OSG (https://tfs.etsi.org/)
*
* 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 org.etsi.tfs.policy;
import static org.assertj.core.api.Assertions.assertThat;
import static org.etsi.tfs.policy.common.ApplicationProperties.REMOVED_POLICYRULE_STATE;
import io.quarkus.test.junit.QuarkusTest;
import io.quarkus.test.junit.mockito.InjectMock;
import io.smallrye.mutiny.Uni;
import jakarta.inject.Inject;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import org.etsi.tfs.policy.context.ContextService;
import org.etsi.tfs.policy.context.model.Service;
import org.etsi.tfs.policy.context.model.ServiceId;
import org.etsi.tfs.policy.context.model.ServiceTypeEnum;
import org.etsi.tfs.policy.monitoring.MonitoringService;
import org.etsi.tfs.policy.monitoring.model.IntegerKpiValue;
import org.etsi.tfs.policy.monitoring.model.KpiValue;
import org.etsi.tfs.policy.policy.PolicyServiceImpl;
import org.etsi.tfs.policy.policy.model.BooleanOperator;
import org.etsi.tfs.policy.policy.model.NumericalOperator;
import org.etsi.tfs.policy.policy.model.PolicyRule;
import org.etsi.tfs.policy.policy.model.PolicyRuleAction;
import org.etsi.tfs.policy.policy.model.PolicyRuleActionConfig;
import org.etsi.tfs.policy.policy.model.PolicyRuleActionEnum;
import org.etsi.tfs.policy.policy.model.PolicyRuleBasic;
import org.etsi.tfs.policy.policy.model.PolicyRuleCondition;
import org.etsi.tfs.policy.policy.model.PolicyRuleService;
import org.etsi.tfs.policy.policy.model.PolicyRuleState;
import org.etsi.tfs.policy.policy.model.PolicyRuleStateEnum;
import org.etsi.tfs.policy.policy.model.PolicyRuleType;
import org.etsi.tfs.policy.policy.model.PolicyRuleTypeService;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
@QuarkusTest
class PolicyDeleteServiceTest {
@Inject PolicyServiceImpl policyService;
@InjectMock ContextService contextService;
@InjectMock MonitoringService monitoringService;
static PolicyRuleBasic policyRuleBasic;
static PolicyRuleService policyRuleService;
@BeforeAll
static void init() {
String policyId = "policyRuleId";
KpiValue kpiValue = new IntegerKpiValue(100);
PolicyRuleCondition policyRuleCondition =
new PolicyRuleCondition(
"kpiId", NumericalOperator.POLICY_RULE_CONDITION_NUMERICAL_GREATER_THAN, kpiValue);
PolicyRuleActionConfig policyRuleActionConfig = new PolicyRuleActionConfig("key", "value");
PolicyRuleAction policyRuleAction =
new PolicyRuleAction(
PolicyRuleActionEnum.POLICY_RULE_ACTION_NO_ACTION,
Arrays.asList(policyRuleActionConfig));
policyRuleBasic =
new PolicyRuleBasic(
policyId,
new PolicyRuleState(PolicyRuleStateEnum.POLICY_INSERTED, "Failed due to some errors"),
1,
Arrays.asList(policyRuleCondition),
BooleanOperator.POLICYRULE_CONDITION_BOOLEAN_OR,
Arrays.asList(policyRuleAction));
ServiceId serviceId = new ServiceId("contextId", "serviceId");
Service service = new Service(serviceId, ServiceTypeEnum.UNKNOWN, null, null, null, null, 0.0);
List<String> deviceIds = Arrays.asList("device1", "device2");
policyRuleService = new PolicyRuleService(policyRuleBasic, serviceId, deviceIds);
}
@Test
void contextOrServiceIdMustNotBeEmpty()
throws ExecutionException, InterruptedException, TimeoutException {
CompletableFuture<PolicyRuleState> message = new CompletableFuture<>();
String policyRuleId = "";
PolicyRuleState expectedResult =
new PolicyRuleState(
PolicyRuleStateEnum.POLICY_REMOVED,
REMOVED_POLICYRULE_STATE.getPolicyRuleStateMessage());
PolicyRuleType policyRuleType = new PolicyRuleTypeService(policyRuleService);
PolicyRule policyRule = new PolicyRule(policyRuleType);
Mockito.when(contextService.getPolicyRule(Mockito.anyString()))
.thenReturn(Uni.createFrom().item(policyRule));
policyService
.deletePolicy(policyRuleId)
.subscribe()
.with(
item -> {
message.complete(item);
});
assertThat(message.get(5, TimeUnit.SECONDS).getPolicyRuleStateMessage())
.isEqualTo(expectedResult.getPolicyRuleStateMessage().toString());
}
}
/*
* Copyright 2022-2023 ETSI TeraFlowSDN - TFS OSG (https://tfs.etsi.org/)
*
* 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 org.etsi.tfs.policy;
import static org.assertj.core.api.Assertions.assertThat;
import context.ContextOuterClass;
import context.ContextOuterClass.Uuid;
import io.quarkus.grpc.GrpcClient;
import io.quarkus.test.junit.QuarkusTest;
import jakarta.inject.Inject;
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 monitoring.Monitoring.KpiId;
import org.etsi.tfs.policy.monitoring.model.FloatKpiValue;
import org.etsi.tfs.policy.monitoring.model.IntegerKpiValue;
import org.etsi.tfs.policy.policy.model.PolicyRuleActionConfig;
import org.jboss.logging.Logger;
import org.junit.jupiter.api.Test;
import policy.Policy;
import policy.Policy.PolicyRuleBasic;
import policy.Policy.PolicyRuleStateEnum;
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
class PolicyGrpcServiceTest {
private static final Logger LOGGER = Logger.getLogger(PolicyGrpcServiceTest.class);
@GrpcClient PolicyService client;
private final Serializer serializer;
@Inject
PolicyGrpcServiceTest(Serializer serializer) {
this.serializer = serializer;
}
private context.ContextOuterClass.ServiceId createContextServiceId() {
final var contextIdUuid = serializer.serializeUuid("571eabc1-0f59-48da-b608-c45876c3fa8a");
final var serviceIdUuid = serializer.serializeUuid("123456789");
context.ContextOuterClass.ContextId contextId =
context.ContextOuterClass.ContextId.newBuilder().setContextUuid(contextIdUuid).build();
return context.ContextOuterClass.ServiceId.newBuilder()
.setContextId(contextId)
.setServiceUuid(serviceIdUuid)
.build();
}
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(PolicyRuleStateEnum.POLICY_INSERTED)
.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);
PolicyRuleActionConfig policyRuleActionConfig_1 =
new PolicyRuleActionConfig("paramater1", "parameter2");
final var serializedPolicyRuleActionConfigList_1 =
serializer.serialize(policyRuleActionConfig_1);
PolicyRuleActionConfig policyRuleActionConfig_2 =
new PolicyRuleActionConfig("paramater3", "parameter4");
final var serializedPolicyRuleActionConfigList_2 =
serializer.serialize(policyRuleActionConfig_2);
final var firstExpectedPolicyRuleAction =
PolicyAction.PolicyRuleAction.newBuilder()
.setAction(PolicyAction.PolicyRuleActionEnum.POLICYRULE_ACTION_ADD_SERVICE_CONFIGRULE)
.addActionConfig(serializedPolicyRuleActionConfigList_1)
.build();
final var secondExpectedPolicyRuleAction =
PolicyAction.PolicyRuleAction.newBuilder()
.setAction(PolicyRuleActionEnum.POLICYRULE_ACTION_ADD_SERVICE_CONSTRAINT)
.addActionConfig(serializedPolicyRuleActionConfigList_2)
.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 policyRuleBasic = createPolicyRuleBasic();
// final var expectedPolicyRuleState = policyRuleBasic.getPolicyRuleState();
// final var serviceId = createContextServiceId();
// final var expectedDeviceIdUuid1 =
// serializer.serializeUuid("20db867c-772d-4872-9179-244ecafb3257");
// final var expectedDeviceId1 =
//
// ContextOuterClass.DeviceId.newBuilder().setDeviceUuid(expectedDeviceIdUuid1).build();
// final var deviceIds = List.of(expectedDeviceId1);
// final var policyRuleService =
// Policy.PolicyRuleService.newBuilder()
// .setPolicyRuleBasic(policyRuleBasic)
// .setServiceId(serviceId)
// .addAllDeviceList(deviceIds)
// .build();
// client
// .policyAddService(policyRuleService)
// .subscribe()
// .with(policyRuleState ->
// message.complete(policyRuleState.getPolicyRuleState().toString()));
// assertThat(message.get(5, TimeUnit.SECONDS))
// .isEqualTo(expectedPolicyRuleState.getPolicyRuleState().toString());
// }
// @Test
// void shouldAddPolicyDevice() throws ExecutionException, InterruptedException, TimeoutException
// {
// CompletableFuture<String> message = new CompletableFuture<>();
// final var expectedDeviceIdUuid1 =
// serializer.serializeUuid("20db867c-772d-4872-9179-244ecafb3257");
// final var expectedDeviceIdUuid2 =
// serializer.serializeUuid("095974ac-d757-412d-b317-bcf355220aa9");
// final var expectedDeviceId1 =
//
// ContextOuterClass.DeviceId.newBuilder().setDeviceUuid(expectedDeviceIdUuid1).build();
// final var expectedDeviceId2 =
//
// ContextOuterClass.DeviceId.newBuilder().setDeviceUuid(expectedDeviceIdUuid2).build();
// final var policyRuleBasic = createPolicyRuleBasic();
// final var deviceIds = List.of(expectedDeviceId1, expectedDeviceId2);
// final var expectedPolicyRuleState = policyRuleBasic.getPolicyRuleState();
// final var policyRuleDevice =
// Policy.PolicyRuleDevice.newBuilder()
// .setPolicyRuleBasic(policyRuleBasic)
// .addAllDeviceList(deviceIds)
// .build();
// client
// .policyAddDevice(policyRuleDevice)
// .subscribe()
// .with(policyRuleState ->
// message.complete(policyRuleState.getPolicyRuleState().toString()));
// assertThat(message.get(5, TimeUnit.SECONDS))
// .isEqualTo(expectedPolicyRuleState.getPolicyRuleState().toString());
// }
@Test
void shouldUpdatePolicyServiceReturnFailedState()
throws ExecutionException, InterruptedException, TimeoutException {
CompletableFuture<String> message = new CompletableFuture<>();
final var expectedPolicyRuleState =
Policy.PolicyRuleState.newBuilder()
.setPolicyRuleState(PolicyRuleStateEnum.POLICY_FAILED)
.build();
final var policyRuleBasic =
PolicyRuleBasic.newBuilder().setPolicyRuleState(expectedPolicyRuleState).build();
final var policyRuleService =
Policy.PolicyRuleService.newBuilder().setPolicyRuleBasic(policyRuleBasic).build();
client
.policyUpdateService(policyRuleService)
.subscribe()
.with(policyRuleState -> message.complete(policyRuleState.getPolicyRuleState().toString()));
assertThat(message.get(5, TimeUnit.SECONDS))
.isEqualTo(expectedPolicyRuleState.getPolicyRuleState().toString());
}
@Test
void shouldUpdatePolicyDeviceReturnFailedState()
throws ExecutionException, InterruptedException, TimeoutException {
CompletableFuture<String> message = new CompletableFuture<>();
final var expectedDeviceIdUuid =
serializer.serializeUuid("20db867c-772d-4872-9179-244ecafb3257");
final var expectedDeviceId =
ContextOuterClass.DeviceId.newBuilder().setDeviceUuid(expectedDeviceIdUuid).build();
final var expectedPolicyRuleState =
Policy.PolicyRuleState.newBuilder()
.setPolicyRuleState(PolicyRuleStateEnum.POLICY_FAILED)
.build();
final var policyRuleBasic =
PolicyRuleBasic.newBuilder().setPolicyRuleState(expectedPolicyRuleState).build();
final var deviceIds = List.of(expectedDeviceId);
final var policyRuleDevice =
Policy.PolicyRuleDevice.newBuilder()
.setPolicyRuleBasic(policyRuleBasic)
.addAllDeviceList(deviceIds)
.build();
client
.policyUpdateDevice(policyRuleDevice)
.subscribe()
.with(policyRuleState -> message.complete(policyRuleState.getPolicyRuleState().toString()));
assertThat(message.get(5, TimeUnit.SECONDS))
.isEqualTo(expectedPolicyRuleState.getPolicyRuleState().toString());
}
// TODO: Disable shouldDeletePolicy test until mock context service
// @Test
// void shouldDeletePolicy() throws ExecutionException, InterruptedException, TimeoutException
// {
// CompletableFuture<String> message = new CompletableFuture<>();
// final var uuid =
// ContextOuterClass.Uuid.newBuilder()
//
// .setUuid(UUID.fromString("0f14d0ab-9608-7862-a9e4-5ed26688389b").toString())
// .build();
// final var policyRuleId = Policy.PolicyRuleId.newBuilder().setUuid(uuid).build();
// final var expectedPolicyRuleState =
// Policy.PolicyRuleState.newBuilder()
// .setPolicyRuleState(PolicyRuleStateEnum.POLICY_REMOVED)
// .build();
// client
// .policyDelete(policyRuleId)
// .subscribe()
// .with(policyRuleState ->
// message.complete(policyRuleState.getPolicyRuleState().toString()));
// assertThat(message.get(5, TimeUnit.SECONDS))
// .isEqualTo(expectedPolicyRuleState.getPolicyRuleState().toString());
// }
@Test
void shouldGetPolicyService() throws ExecutionException, InterruptedException, TimeoutException {
CompletableFuture<String> message = new CompletableFuture<>();
final var uuid =
ContextOuterClass.Uuid.newBuilder()
.setUuid(UUID.fromString("0f14d0ab-9608-7862-a9e4-5ed26688389b").toString())
.build();
final var policyRuleId = Policy.PolicyRuleId.newBuilder().setUuid(uuid).build();
client
.getPolicyService(policyRuleId)
.subscribe()
.with(
policyRuleService -> {
LOGGER.infof(
"Getting policy with ID: %s",
policyRuleService.getPolicyRuleBasic().getPolicyRuleId().getUuid());
message.complete(
policyRuleService.getPolicyRuleBasic().getPolicyRuleId().getUuid().getUuid());
});
assertThat(message.get(5, TimeUnit.SECONDS)).isEqualTo(policyRuleId.getUuid().getUuid());
}
@Test
void shouldGetPolicyDevice() throws ExecutionException, InterruptedException, TimeoutException {
CompletableFuture<String> message = new CompletableFuture<>();
final var uuid =
ContextOuterClass.Uuid.newBuilder()
.setUuid(UUID.fromString("0f14d0ab-9608-7862-a9e4-5ed26688389b").toString())
.build();
final var policyRuleId = Policy.PolicyRuleId.newBuilder().setUuid(uuid).build();
client
.getPolicyDevice(policyRuleId)
.subscribe()
.with(
policyRuleService -> {
LOGGER.infof(
"Getting policy with ID: %s",
policyRuleService.getPolicyRuleBasic().getPolicyRuleId().getUuid());
message.complete(
policyRuleService.getPolicyRuleBasic().getPolicyRuleId().getUuid().getUuid());
});
assertThat(message.get(5, TimeUnit.SECONDS)).isEqualTo(policyRuleId.getUuid().getUuid());
}
@Test
void shouldGetPolicyByServiceId()
throws ExecutionException, InterruptedException, TimeoutException {
CompletableFuture<String> message = new CompletableFuture<>();
final var uuid =
ContextOuterClass.Uuid.newBuilder()
.setUuid(UUID.fromString("0f14d0ab-9608-7862-a9e4-5ed26688389b").toString())
.build();
final var serviceId = ContextOuterClass.ServiceId.newBuilder().setServiceUuid(uuid).build();
client
.getPolicyByServiceId(serviceId)
.subscribe()
.with(
policyRuleList -> {
LOGGER.infof("Getting policyRuleList with ID: %s", policyRuleList);
message.complete(policyRuleList.toString());
});
assertThat(message.get(5, TimeUnit.SECONDS)).isEmpty();
}
}
...@@ -23,17 +23,17 @@ import io.quarkus.test.junit.QuarkusTest; ...@@ -23,17 +23,17 @@ import io.quarkus.test.junit.QuarkusTest;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.UUID; import java.util.UUID;
import org.etsi.tfs.policy.model.BooleanOperator;
import org.etsi.tfs.policy.model.NumericalOperator;
import org.etsi.tfs.policy.model.PolicyRuleAction;
import org.etsi.tfs.policy.model.PolicyRuleActionConfig;
import org.etsi.tfs.policy.model.PolicyRuleActionEnum;
import org.etsi.tfs.policy.model.PolicyRuleBasic;
import org.etsi.tfs.policy.model.PolicyRuleCondition;
import org.etsi.tfs.policy.model.PolicyRuleState;
import org.etsi.tfs.policy.model.PolicyRuleStateEnum;
import org.etsi.tfs.policy.monitoring.model.IntegerKpiValue; import org.etsi.tfs.policy.monitoring.model.IntegerKpiValue;
import org.etsi.tfs.policy.monitoring.model.KpiValue; import org.etsi.tfs.policy.monitoring.model.KpiValue;
import org.etsi.tfs.policy.policy.model.BooleanOperator;
import org.etsi.tfs.policy.policy.model.NumericalOperator;
import org.etsi.tfs.policy.policy.model.PolicyRuleAction;
import org.etsi.tfs.policy.policy.model.PolicyRuleActionConfig;
import org.etsi.tfs.policy.policy.model.PolicyRuleActionEnum;
import org.etsi.tfs.policy.policy.model.PolicyRuleBasic;
import org.etsi.tfs.policy.policy.model.PolicyRuleCondition;
import org.etsi.tfs.policy.policy.model.PolicyRuleState;
import org.etsi.tfs.policy.policy.model.PolicyRuleStateEnum;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
// TODO: Revisit PolicyRuleBasicValidationTest cases after handling exceptions in PolicyRuleBasic // TODO: Revisit PolicyRuleBasicValidationTest cases after handling exceptions in PolicyRuleBasic
......
...@@ -21,13 +21,13 @@ import static org.assertj.core.api.Assertions.assertThatExceptionOfType; ...@@ -21,13 +21,13 @@ import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import io.quarkus.test.junit.QuarkusTest; import io.quarkus.test.junit.QuarkusTest;
import java.util.stream.Stream; import java.util.stream.Stream;
import org.etsi.tfs.policy.model.NumericalOperator;
import org.etsi.tfs.policy.model.PolicyRuleCondition;
import org.etsi.tfs.policy.monitoring.model.BooleanKpiValue; import org.etsi.tfs.policy.monitoring.model.BooleanKpiValue;
import org.etsi.tfs.policy.monitoring.model.FloatKpiValue; import org.etsi.tfs.policy.monitoring.model.FloatKpiValue;
import org.etsi.tfs.policy.monitoring.model.IntegerKpiValue; import org.etsi.tfs.policy.monitoring.model.IntegerKpiValue;
import org.etsi.tfs.policy.monitoring.model.KpiValue; import org.etsi.tfs.policy.monitoring.model.KpiValue;
import org.etsi.tfs.policy.monitoring.model.StringKpiValue; import org.etsi.tfs.policy.monitoring.model.StringKpiValue;
import org.etsi.tfs.policy.policy.model.NumericalOperator;
import org.etsi.tfs.policy.policy.model.PolicyRuleCondition;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.Arguments;
......