Skip to content

Commit

Permalink
Fixed the issue osc-core issue opensecuritycontroller#543
Browse files Browse the repository at this point in the history
Modified and added test cases.

Added back to original intialization

Made the changes according to the review comments.

Did required changes for unit test case.
  • Loading branch information
balmukundblr committed Dec 27, 2017
1 parent df0507e commit 239c8b6
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public static List<VirtualSystemDto> findByMcApplianceAndSwVer(EntityManager em,

List<Object[]> ls = query.getResultList();

List<VirtualSystemDto> dtoList = new ArrayList<VirtualSystemDto>();
List<VirtualSystemDto> dtoList = new ArrayList<>();

for (Object[] arr : ls) {

Expand Down Expand Up @@ -185,7 +185,7 @@ public static synchronized Long generateUniqueTag(EntityManager em, VirtualSyste
}

public static boolean isProtectingWorkload(VirtualSystem vs) {
return !vs.getServiceFunctionChains().isEmpty() ||
CollectionUtils.emptyIfNull(vs.getDeploymentSpecs()).stream().anyMatch(ds -> DeploymentSpecEntityMgr.isProtectingWorkload(ds));
return CollectionUtils.emptyIfNull(vs.getDeploymentSpecs()).stream()
.anyMatch(ds -> DeploymentSpecEntityMgr.isProtectingWorkload(ds));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,18 @@
*******************************************************************************/
package org.osc.core.broker.service.validator;

import java.util.List;
import java.util.stream.Collectors;

import javax.persistence.EntityManager;

import org.osc.core.broker.model.entities.appliance.DistributedAppliance;
import org.osc.core.broker.model.entities.appliance.VirtualSystem;
import org.osc.core.broker.model.entities.virtualization.SecurityGroup;
import org.osc.core.broker.model.entities.virtualization.ServiceFunctionChain;
import org.osc.core.broker.service.exceptions.VmidcBrokerValidationException;
import org.osc.core.broker.service.persistence.DistributedApplianceEntityMgr;
import org.osc.core.broker.service.persistence.SecurityGroupEntityMgr;
import org.osc.core.broker.service.request.BaseDeleteRequest;

public class DeleteDistributedApplianceRequestValidator implements RequestValidator<BaseDeleteRequest,DistributedAppliance> {
Expand All @@ -39,7 +46,7 @@ public void validate(BaseDeleteRequest request) throws Exception {

@Override
public DistributedAppliance validateAndLoad(BaseDeleteRequest request) throws Exception {
DistributedAppliance da = this.em.find(DistributedAppliance.class, request.getId());
DistributedAppliance da = em.find(DistributedAppliance.class, request.getId());

// entry must pre-exist in db
if (da == null) { // note: we cannot use name here in error msg since del req does not have name, only ID
Expand All @@ -53,6 +60,8 @@ public DistributedAppliance validateAndLoad(BaseDeleteRequest request) throws Ex
da.getId()));
}

validateDAReferenceToSfcAndSecurityGroup(em, da);

if (!da.getMarkedForDeletion() && request.isForceDelete()) {
throw new VmidcBrokerValidationException(
"Distributed Appilance with ID "
Expand All @@ -62,4 +71,38 @@ public DistributedAppliance validateAndLoad(BaseDeleteRequest request) throws Ex

return da;
}

private void validateDAReferenceToSfcAndSecurityGroup(EntityManager em, DistributedAppliance da) throws Exception {
for (VirtualSystem vs : da.getVirtualSystems()) {
validateVSReferenceToSfcAndSecurityGroup(em, da, vs);
}
}

private void validateVSReferenceToSfcAndSecurityGroup(EntityManager em, DistributedAppliance da, VirtualSystem vs)
throws Exception {
if (vs.getServiceFunctionChains() == null || vs.getServiceFunctionChains().isEmpty()) {
return;
}
for (ServiceFunctionChain sfc : vs.getServiceFunctionChains()) {
validateSfcReferenceToSecurityGroup(em, da, sfc);
}

}

private void validateSfcReferenceToSecurityGroup(EntityManager em, DistributedAppliance da,
ServiceFunctionChain sfc) throws Exception {
List<SecurityGroup> sgList = SecurityGroupEntityMgr.listSecurityGroupsBySfcId(em, sfc.getId());
String sgNames = sgList.stream().filter(sg -> !sg.getMarkedForDeletion()).map(sg -> sg.getName())
.collect(Collectors.joining(", "));
if (!sgNames.isEmpty()) {
throw new VmidcBrokerValidationException(String.format(
"Cannot delete deployment appliance with name '%s' which is currently referencing Service Function Chain '%s' and binded to a SecurityGroup(s) '%s'",
da.getName(), sfc.getName(), sgNames));
} else if (!sfc.getMarkedForDeletion() && sfc != null) {
throw new VmidcBrokerValidationException(String.format(
"Cannot delete deployment appliance with name '%s' which is currently referencing Service Function Chain '%s'",
da.getName(), sfc.getName()));
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
*******************************************************************************/
package org.osc.core.broker.service.request;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.persistence.EntityManager;

Expand All @@ -25,16 +27,25 @@
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.osc.core.broker.model.entities.appliance.DistributedAppliance;
import org.osc.core.broker.model.entities.appliance.TagEncapsulationType;
import org.osc.core.broker.model.entities.appliance.VirtualSystem;
import org.osc.core.broker.model.entities.virtualization.SecurityGroup;
import org.osc.core.broker.model.entities.virtualization.ServiceFunctionChain;
import org.osc.core.broker.service.exceptions.VmidcBrokerValidationException;
import org.osc.core.broker.service.persistence.DistributedApplianceEntityMgr;
import org.osc.core.broker.service.persistence.SecurityGroupEntityMgr;
import org.osc.core.broker.service.validator.DeleteDistributedApplianceRequestValidator;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

@RunWith(PowerMockRunner.class)
@PrepareForTest({SecurityGroupEntityMgr.class,DistributedApplianceEntityMgr.class})
public class DeleteDistributedApplianceRequestValidatorTest {

private static final Long VALID_ID = 1l;
Expand All @@ -56,6 +67,9 @@ public class DeleteDistributedApplianceRequestValidatorTest {
@Mock
EntityManager em;

@Mock
SecurityGroupEntityMgr sem;

private DeleteDistributedApplianceRequestValidator validator;

@Before
Expand Down Expand Up @@ -138,7 +152,8 @@ public void testValidateAndLoad_WhenChainedToSfcDeleteRequest_ThrowsValidationEx
vs.setEncapsulationType(TagEncapsulationType.VLAN);
vs.setServiceFunctionChains(Arrays.asList(sfc));
da.addVirtualSystem(vs);

PowerMockito.mockStatic(DistributedApplianceEntityMgr.class);
PowerMockito.when(DistributedApplianceEntityMgr.isProtectingWorkload(da)).thenReturn(true);
Mockito.when(this.em.find(Mockito.eq(DistributedAppliance.class), Mockito.eq(VALID_ID_WITH_SFC)))
.thenReturn(da);
this.exception.expect(VmidcBrokerValidationException.class);
Expand All @@ -150,6 +165,63 @@ public void testValidateAndLoad_WhenChainedToSfcDeleteRequest_ThrowsValidationEx
this.validator.validateAndLoad(createRequest(VALID_ID_WITH_SFC, false));
}

@Test
public void testValidateSfcToSecurityGroup_WhenDADeleteRequest_ThrowsValidationException() throws Exception {
// Arrange
final Long VALID_ID_WITH_SFC = 4l;
DistributedAppliance da = createDA(VALID_ID_WITH_SFC, false);
da.setName("DA");
List<SecurityGroup> sgList=new ArrayList<>();
SecurityGroup sg=new SecurityGroup(null,null,null);
sg.setName("sg");
sgList.add(sg);
ServiceFunctionChain sfc = new ServiceFunctionChain("sfc-1", null);
sfc.setId(VALID_ID_WITH_SFC);
VirtualSystem vs = new VirtualSystem(null);
vs.setName("vs-1");
vs.setEncapsulationType(TagEncapsulationType.VLAN);
vs.setServiceFunctionChains(Arrays.asList(sfc));

da.addVirtualSystem(vs);
PowerMockito.mockStatic(SecurityGroupEntityMgr.class);
Mockito.when(SecurityGroupEntityMgr.listSecurityGroupsBySfcId(em,VALID_ID_WITH_SFC)).thenReturn(sgList);
Mockito.when(em.find(Mockito.eq(DistributedAppliance.class), Mockito.eq(VALID_ID_WITH_SFC)))
.thenReturn(da);
exception.expect(VmidcBrokerValidationException.class);
exception.expectMessage(
String.format("Cannot delete deployment appliance with name '%s' which is currently referencing Service Function Chain '%s' and binded to a SecurityGroup(s) '%s'",
da.getName(), sfc.getName(), sg.getName()));

// Act
validator.validateAndLoad(createRequest(VALID_ID_WITH_SFC, false));
}

@Test
public void testValidateDaToSfc_WhenDADeleteRequest_ThrowsValidationException() throws Exception {
// Arrange
final Long VALID_ID_WITH_SFC = 4l;
DistributedAppliance da = createDA(VALID_ID_WITH_SFC, false);
da.setName("DA");
ServiceFunctionChain sfc = new ServiceFunctionChain("sfc-1", null);
List<SecurityGroup> sgList=new ArrayList<>();
VirtualSystem vs = new VirtualSystem(null);
vs.setName("vs-1");
vs.setEncapsulationType(TagEncapsulationType.VLAN);
vs.setServiceFunctionChains(Arrays.asList(sfc));
da.addVirtualSystem(vs);
PowerMockito.mockStatic(SecurityGroupEntityMgr.class);
Mockito.when(SecurityGroupEntityMgr.listSecurityGroupsBySfcId(em,VALID_ID_WITH_SFC)).thenReturn(sgList);
Mockito.when(em.find(Mockito.eq(DistributedAppliance.class), Mockito.eq(VALID_ID_WITH_SFC)))
.thenReturn(da);
exception.expect(VmidcBrokerValidationException.class);
exception.expectMessage(
String.format("Cannot delete deployment appliance with name '%s' which is currently referencing Service Function Chain '%s'",
da.getName(), sfc.getName()));

// Act
validator.validateAndLoad(createRequest(VALID_ID_WITH_SFC, false));
}

private static BaseDeleteRequest createRequest(Long id, boolean forceDelete) {
BaseDeleteRequest request = new BaseDeleteRequest(id);
request.setForceDelete(forceDelete);
Expand Down

0 comments on commit 239c8b6

Please sign in to comment.