Skip to content

Commit

Permalink
Fixed the issue osc-core issue opensecuritycontroller#543
Browse files Browse the repository at this point in the history
  • Loading branch information
balmukundblr committed Dec 12, 2017
1 parent df0507e commit 811aea7
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 32 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,50 +16,82 @@
*******************************************************************************/
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> {
public class DeleteDistributedApplianceRequestValidator
implements RequestValidator<BaseDeleteRequest, DistributedAppliance> {

private EntityManager em;

private EntityManager em;
public DeleteDistributedApplianceRequestValidator(EntityManager em) {
this.em = em;
}

@Override
public void validate(BaseDeleteRequest request) throws Exception {
throw new UnsupportedOperationException();
}

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

@Override
public void validate(BaseDeleteRequest request) throws Exception {
throw new UnsupportedOperationException();
}
// 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
throw new VmidcBrokerValidationException(
"Distributed Appliance entry with ID '" + request.getId() + "' is not found.");
}

@Override
public DistributedAppliance validateAndLoad(BaseDeleteRequest request) throws Exception {
DistributedAppliance da = this.em.find(DistributedAppliance.class, request.getId());
if (DistributedApplianceEntityMgr.isProtectingWorkload(da)) {
throw new VmidcBrokerValidationException(String.format(
"The distributed appliance with name '%s' and id '%s' is currently protecting a workload",
da.getName(), da.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
throw new VmidcBrokerValidationException("Distributed Appliance entry with ID '" + request.getId() + "' is not found.");
}
// DE5296->Error message to be more appropriate when deleting a DA without
// deleting the SFC
validateDAReferenceToSfcAndSecurityGroup(this.em, da);

if (DistributedApplianceEntityMgr.isProtectingWorkload(da)) {
throw new VmidcBrokerValidationException(
String.format("The distributed appliance with name '%s' and id '%s' is currently protecting a workload",
da.getName(),
da.getId()));
}
if (!da.getMarkedForDeletion() && request.isForceDelete()) {
throw new VmidcBrokerValidationException("Distributed Appilance with ID " + request.getId()
+ " is not marked for deletion and force delete operation is applicable only for entries marked for deletion.");
}

if (!da.getMarkedForDeletion() && request.isForceDelete()) {
throw new VmidcBrokerValidationException(
"Distributed Appilance with ID "
+ request.getId()
+ " is not marked for deletion and force delete operation is applicable only for entries marked for deletion.");
}
return da;
}

return da;
}
private void validateDAReferenceToSfcAndSecurityGroup(EntityManager em, DistributedAppliance da) throws Exception {
for (VirtualSystem vs : da.getVirtualSystems()) {
if (!vs.getServiceFunctionChains().isEmpty()) {
// DE5296-->Look if these SFCs are binded to any of the SG of same DA
for (ServiceFunctionChain sfc : vs.getServiceFunctionChains()) {
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 (sgNames.isEmpty() && 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()));
}
}
}
}
}
}

0 comments on commit 811aea7

Please sign in to comment.