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.
  • Loading branch information
balmukundblr committed Dec 21, 2017
1 parent df0507e commit cc54476
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,6 @@ 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 Down Expand Up @@ -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,24 @@
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.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})
public class DeleteDistributedApplianceRequestValidatorTest {

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

@Mock
SecurityGroupEntityMgr sem;

private DeleteDistributedApplianceRequestValidator validator;

@Before
Expand Down Expand Up @@ -150,6 +163,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 cc54476

Please sign in to comment.