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.

Did the changes as per review comments.
  • Loading branch information
balmukundblr committed Dec 28, 2017
1 parent 9814c80 commit 0abf5f1
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 3 deletions.
Original file line number Diff line number Diff line change
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 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()) {
try {
validateVSReferenceToSfcAndSecurityGroup(em, vs);
} catch (VmidcBrokerValidationException ex) {
throw new VmidcBrokerValidationException(ex.getMessage() + " with name " + da.getName());
}
}
}

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

private void validateSfcReferenceToSecurityGroup(EntityManager em, 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 distributed appliance which is currently referencing Service Function Chain '%s' and binded to a SecurityGroup(s) '%s'",
sfc.getName(), sgNames));
} else if (!sfc.getMarkedForDeletion() && sfc != null) {
throw new VmidcBrokerValidationException(String.format(
"Cannot delete distributed appliance which is currently referencing Service Function Chain '%s'",
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 Down Expand Up @@ -138,7 +149,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 +162,61 @@ public void testValidateAndLoad_WhenChainedToSfcDeleteRequest_ThrowsValidationEx
this.validator.validateAndLoad(createRequest(VALID_ID_WITH_SFC, false));
}

@Test
public void testValidateDA_WhenChainedToSfcAndSgDeleteRequest_ThrowsValidationException() throws Exception {
// Arrange
final Long VALID_ID_WITH_SFC = 4l;
DistributedAppliance da = createDA(VALID_ID_WITH_SFC, false);
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 distributed appliance which is currently referencing Service Function Chain '%s' and binded to a SecurityGroup(s) '%s'",
sfc.getName(), sg.getName()));

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

@Test
public void testValidateDA_WhenChainedToSfcDeleteRequest_ThrowsValidationException() throws Exception {
// Arrange
final Long VALID_ID_WITH_SFC = 4l;
DistributedAppliance da = createDA(VALID_ID_WITH_SFC, false);
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 distributed appliance which is currently referencing Service Function Chain '%s'",
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 0abf5f1

Please sign in to comment.