Skip to content

Commit

Permalink
support service policies to be linked to multiple service flavors
Browse files Browse the repository at this point in the history
  • Loading branch information
baixinsui committed Jan 26, 2024
1 parent 0d9a7d6 commit 82097e2
Show file tree
Hide file tree
Showing 11 changed files with 150 additions and 116 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
*/
@Data
@Table(name = "SERVICE_POLICY", uniqueConstraints = {
@UniqueConstraint(columnNames = {"SERVICE_TEMPLATE_ID", "FLAVOR_NAME", "POLICY"})
@UniqueConstraint(columnNames = {"SERVICE_TEMPLATE_ID", "POLICY"})
})
@Entity
@EqualsAndHashCode(callSuper = true)
Expand Down Expand Up @@ -55,8 +55,8 @@ public class ServicePolicyEntity extends CreateModifiedTime {
@JsonIgnoreProperties({"servicePolicyEntity"})
private ServiceTemplateEntity serviceTemplate;

@Column(name = "FLAVOR_NAME")
private String flavorName;
@Column(name = "FLAVOR_NAMES")
private String flavorNames;

/**
* Is the policy enabled.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
class ServicePolicyEntityTest {

final UUID id = UUID.fromString("0ebfecbc-3907-45c7-b9c6-36d42ec0efa1");
final String flavorName = "flavorName";
final String flavorNames = "flavor1,flavor2";
final String policy = "policy";
final Boolean enabled = false;
@Mock
Expand All @@ -29,7 +29,7 @@ class ServicePolicyEntityTest {
void setUp() {
test = new ServicePolicyEntity();
test.setServiceTemplate(mockServiceTemplate);
test.setFlavorName(flavorName);
test.setFlavorNames(flavorNames);
test.setEnabled(enabled);
test.setPolicy(policy);
test.setId(id);
Expand All @@ -40,7 +40,7 @@ void testGetters() {
assertThat(test.getId()).isEqualTo(id);
assertThat(test.getPolicy()).isEqualTo(policy);
assertThat(test.getServiceTemplate()).isEqualTo(mockServiceTemplate);
assertThat(test.getFlavorName()).isEqualTo(flavorName);
assertThat(test.getFlavorNames()).isEqualTo(flavorNames);
assertThat(test.getEnabled()).isEqualTo(enabled);
}

Expand Down Expand Up @@ -84,8 +84,9 @@ void testHashCode() {
@Test
void testToString() {
String result = String.format(
"ServicePolicyEntity(id=%s, policy=%s, serviceTemplate=%s, flavorName=%s, enabled=%s)",
id, policy, mockServiceTemplate, flavorName, enabled);
"ServicePolicyEntity(id=%s, policy=%s, serviceTemplate=%s, flavorNames=%s, "
+ "enabled=%s)",
id, policy, mockServiceTemplate, flavorNames, enabled);

assertThat(test.toString()).isEqualTo(result);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
import org.eclipse.xpanse.modules.models.policy.userpolicy.UserPolicyQueryRequest;
import org.eclipse.xpanse.modules.orchestrator.deployment.DeployTask;
import org.eclipse.xpanse.modules.policy.PolicyManager;
import org.eclipse.xpanse.modules.policy.ServicePolicyManager;
import org.eclipse.xpanse.modules.policy.UserPolicyManager;
import org.eclipse.xpanse.modules.policy.policyman.generated.model.EvalResult;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

Expand All @@ -41,6 +41,8 @@ public class PolicyValidator {
@Resource
private UserPolicyManager userPolicyManager;
@Resource
private ServicePolicyManager servicePolicyManager;
@Resource
private DatabaseServiceTemplateStorage serviceTemplateStorage;
@Resource
private DeployerKindManager deployerKindManager;
Expand All @@ -53,13 +55,9 @@ private List<ServicePolicy> getServicePolicies(UUID serviceTemplateId) {
return existedServiceTemplate.getServicePolicyList().stream()
.filter(servicePolicyEntity -> servicePolicyEntity.getEnabled()
&& StringUtils.isNotBlank(servicePolicyEntity.getPolicy()))
.map(servicePolicyEntity -> {
ServicePolicy servicePolicy = new ServicePolicy();
BeanUtils.copyProperties(servicePolicyEntity, servicePolicy);
servicePolicy.setServiceTemplateId(
servicePolicyEntity.getServiceTemplate().getId());
return servicePolicy;
}).toList();
.map(servicePolicyEntity ->
servicePolicyManager.conventToServicePolicy(servicePolicyEntity))
.toList();
}
return Collections.emptyList();
}
Expand Down Expand Up @@ -105,9 +103,9 @@ private void evaluateDeploymentPlanWithServicePolicies(List<ServicePolicy> servi
if (!CollectionUtils.isEmpty(servicePolicies)) {
List<String> policyList = new ArrayList<>();
for (ServicePolicy servicePolicy : servicePolicies) {
if (StringUtils.isBlank(servicePolicy.getFlavorName())) {
if (CollectionUtils.isEmpty(servicePolicy.getFlavorNameList())) {
policyList.add(servicePolicy.getPolicy());
} else if (StringUtils.equals(flavorName, servicePolicy.getFlavorName())) {
} else if (servicePolicy.getFlavorNameList().contains(flavorName)) {
policyList.add(servicePolicy.getPolicy());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import java.time.OffsetDateTime;
import java.util.List;
import java.util.UUID;
import lombok.Data;

Expand Down Expand Up @@ -45,10 +46,13 @@ public class ServicePolicy {
@Schema(description = "The id of registered service template which the policy belongs to.")
private UUID serviceTemplateId;

/**
* The flavor name list which the policy belongs to.
*/
@Schema(description =
"The flavor name which the policy belongs to. If a flavor is not provided, then the "
"The flavor name list which the policy belongs to. If the list is empty, then the "
+ "policy will be executed for during service deployment of all flavors.")
private String flavorName;
private List<String> flavorNameList;

/**
* Is the policy enabled.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import java.util.List;
import java.util.UUID;
import lombok.Data;

Expand All @@ -26,11 +27,13 @@ public class ServicePolicyCreateRequest {
@Schema(description = "The id of registered service template which the policy belongs to.")
private UUID serviceTemplateId;


/**
* The flavor name list which the policy belongs to.
*/
@Schema(description =
"The flavor name which the policy belongs to. If a flavor is not provided, then the "
"The flavor name list which the policy belongs to. If the list is empty, then the "
+ "policy will be executed for during service deployment of all flavors.")
private String flavorName;
private List<String> flavorNameList;

/**
* The policy.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotNull;
import java.util.List;
import java.util.UUID;
import lombok.Data;

Expand All @@ -25,10 +26,12 @@ public class ServicePolicyUpdateRequest {
private UUID id;

/**
* The flavor name of the policy.
* The flavor name list which the policy belongs to.
*/
@Schema(description = "The flavor name of the policy.")
private String flavorName;
@Schema(description =
"The flavor name list which the policy belongs to. If the list is empty, then the "
+ "policy will be executed for during service deployment of all flavors.")
private List<String> flavorNameList;

/**
* The policy.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static org.assertj.core.api.Assertions.assertThat;

import java.util.List;
import java.util.UUID;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand All @@ -10,7 +11,7 @@
class ServicePolicyCreateRequestTest {

final UUID serviceTemplateId = UUID.fromString("3a74cadd-d55b-4504-8fdf-56f1b6aae79c");
final String flavorName = "flavorName";
final List<String> flavorNameList = List.of("flavor1", "flavor2");
final String policy = "policy";
final Boolean enabled = false;
private ServicePolicyCreateRequest test;
Expand All @@ -19,7 +20,7 @@ class ServicePolicyCreateRequestTest {
void setUp() {
test = new ServicePolicyCreateRequest();
test.setServiceTemplateId(serviceTemplateId);
test.setFlavorName(flavorName);
test.setFlavorNameList(flavorNameList);
test.setEnabled(enabled);
test.setPolicy(policy);
}
Expand All @@ -28,7 +29,7 @@ void setUp() {
void testGetters() {
assertThat(test.getPolicy()).isEqualTo(policy);
assertThat(test.getServiceTemplateId()).isEqualTo(serviceTemplateId);
assertThat(test.getFlavorName()).isEqualTo(flavorName);
assertThat(test.getFlavorNameList()).isEqualTo(flavorNameList);
assertThat(test.getEnabled()).isEqualTo(enabled);
}

Expand Down Expand Up @@ -56,8 +57,8 @@ void testHashCode() {
@Test
void testToString() {
String result = String.format(
"ServicePolicyCreateRequest(serviceTemplateId=%s, flavorName=%s, policy=%s, "
+ "enabled=%s)", serviceTemplateId, flavorName, policy, enabled);
"ServicePolicyCreateRequest(serviceTemplateId=%s, flavorNameList=%s, policy=%s, "
+ "enabled=%s)", serviceTemplateId, flavorNameList, policy, enabled);
assertThat(test.toString()).isEqualTo(result);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.util.List;
import java.util.UUID;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand All @@ -14,7 +15,7 @@ class ServicePolicyTest {

final UUID id = UUID.fromString("0ebfecbc-3907-45c7-b9c6-36d42ec0efa1");
final UUID serviceTemplateId = UUID.fromString("3a74cadd-d55b-4504-8fdf-56f1b6aae79c");
final String flavorName = "flavorName";
final List<String> flavorNameList = List.of("flavor1", "flavor2");
final String policy = "policy";
final Boolean enabled = false;
private ServicePolicy test;
Expand All @@ -23,7 +24,7 @@ class ServicePolicyTest {
void setUp() {
test = new ServicePolicy();
test.setServiceTemplateId(serviceTemplateId);
test.setFlavorName(flavorName);
test.setFlavorNameList(flavorNameList);
test.setEnabled(enabled);
test.setPolicy(policy);
test.setId(id);
Expand All @@ -33,7 +34,7 @@ void setUp() {
void testGetters() {
assertThat(test.getId()).isEqualTo(id);
assertThat(test.getPolicy()).isEqualTo(policy);
assertThat(test.getFlavorName()).isEqualTo(flavorName);
assertThat(test.getFlavorNameList()).isEqualTo(flavorNameList);
assertThat(test.getServiceTemplateId()).isEqualTo(serviceTemplateId);
assertThat(test.getEnabled()).isEqualTo(enabled);
}
Expand Down Expand Up @@ -78,9 +79,9 @@ void testHashCode() {
@Test
void testToString() {
String result = String.format(
"ServicePolicy(id=%s, policy=%s, serviceTemplateId=%s, flavorName=%s, enabled=%s, "
+ "createTime=%s, lastModifiedTime=%s)", id, policy, serviceTemplateId,
flavorName, enabled, null, null);
"ServicePolicy(id=%s, policy=%s, serviceTemplateId=%s, flavorNameList=%s, "
+ "enabled=%s, createTime=%s, lastModifiedTime=%s)",
id, policy, serviceTemplateId, flavorNameList, enabled, null, null);

assertThat(test.toString()).isEqualTo(result);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static org.assertj.core.api.Assertions.assertThat;

import java.util.List;
import java.util.UUID;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand All @@ -10,7 +11,7 @@
class ServicePolicyUpdateRequestTest {

final UUID id = UUID.fromString("0ebfecbc-3907-45c7-b9c6-36d42ec0efa1");
final String flavorName = "flavorName";
final List<String> flavorNameList = List.of("flavor1","flavor2");
final String policy = "policy";
final Boolean enabled = false;
private ServicePolicyUpdateRequest test;
Expand All @@ -19,15 +20,15 @@ class ServicePolicyUpdateRequestTest {
void setUp() {
test = new ServicePolicyUpdateRequest();
test.setEnabled(enabled);
test.setFlavorName(flavorName);
test.setFlavorNameList(flavorNameList);
test.setPolicy(policy);
test.setId(id);
}

@Test
void testGetters() {
assertThat(test.getId()).isEqualTo(id);
assertThat(test.getFlavorName()).isEqualTo(flavorName);
assertThat(test.getFlavorNameList()).isEqualTo(flavorNameList);
assertThat(test.getPolicy()).isEqualTo(policy);
assertThat(test.getEnabled()).isEqualTo(enabled);
}
Expand Down Expand Up @@ -55,9 +56,9 @@ void testHashCode() {

@Test
void testToString() {
String result = String.format("ServicePolicyUpdateRequest(id=%s, flavorName=%s, "
String result = String.format("ServicePolicyUpdateRequest(id=%s, flavorNameList=%s, "
+ "policy=%s, enabled=%s)",
id, flavorName, policy, enabled);
id, flavorNameList, policy, enabled);
assertThat(test.toString()).isEqualTo(result);
}
}

0 comments on commit 82097e2

Please sign in to comment.