@Test public void should_remove_route_on_deletion() throws Exception { String testId = "testDelRouteBinding"; final DeleteServiceInstanceBindingRequest deleteRequest = prepareDeleteAppRouteTest(testId, testId, testId); when(bindingRepository.findOne(testId)).thenReturn(BeanGenerator.createRouteBinding(testId)); bindingService.deleteServiceInstanceBinding(deleteRequest); verify(bindingRepository, times(1)).delete(testId); }
@Override public void deleteServiceInstanceBinding(DeleteServiceInstanceBindingRequest request) throws ServiceBrokerException { final String bindingId = request.getBindingId(); final String serviceId = request.getServiceInstanceId(); log.debug("deleteServiceInstanceBinding - {} on service {}", bindingId, serviceId); final Binding binding = bindingRepository.findOne(bindingId); if (binding == null) { log.error("Trying to delete unknown binding {}, letting it pass", bindingId); return; } if (binding.getResourceType() == Application) { log.info("Unbinding app {} (binding {})", binding.getResourceId(), bindingId); final String appId = binding.getResourceId(); SpaceEnrollerConfig serviceInstance = spaceEnrollerConfigRepository.findOne(request.getServiceInstanceId()); log.debug("serviceInstance {}", serviceInstance); try { List<String> mappedRouteIds = cfApi.listApplicationRoutes(appId); if (mappedRouteIds.size() > 0) { List<Binding> linkedRouteBindings = bindingRepository.findByResourceIdAndType(mappedRouteIds, Route); if (linkedRouteBindings.size() > 0) { linkedRouteBindings.stream() .filter(linkedRouteBinding -> linkedRouteBinding.getServiceInstanceId().equals(serviceId)) .forEach(linkedRouteBinding -> { log.debug("detected associated route binding {}, cleaning it", linkedRouteBinding .getServiceBindingId()); try { cfApi.unbind(linkedRouteBinding.getServiceBindingId()); } catch (CloudFoundryException e) { log.error("Autosleep was unable to clear related route binding {}.", linkedRouteBinding.getServiceBindingId()); bindingRepository.delete(linkedRouteBinding.getServiceBindingId()); } }); } } applicationLocker.executeThreadSafe(appId, () -> { log.debug("deleteServiceInstanceBinding on app {}", appId); ApplicationInfo appInfo = appRepository.findOne(appId); if (appInfo != null) { appInfo.getEnrollmentState().updateEnrollment(serviceInstance.getId(), serviceInstance.getEnrollment() != Enrollment.forced && serviceInstance.getEnrollment() != Enrollment.transient_opt_out); if (appInfo.getEnrollmentState().getStates().isEmpty()) { appRepository.delete(appId); applicationLocker.removeApplication(appId); } else { appRepository.save(appInfo); } } else { log.error("Deleting a binding with no related application info. " + "This should never happen."); } bindingRepository.delete(bindingId); }); } catch (CloudFoundryException e) { throw new ServiceBrokerException("Couldn't clean related app bindings", e); } } else if (binding.getResourceType() == Route) { log.info("Unbinding route {} (binding {})", binding.getResourceId(), bindingId); bindingRepository.delete(bindingId); } }
AutosleepBindingService implements ServiceInstanceBindingService { @Override public void deleteServiceInstanceBinding(DeleteServiceInstanceBindingRequest request) throws ServiceBrokerException { final String bindingId = request.getBindingId(); final String serviceId = request.getServiceInstanceId(); log.debug("deleteServiceInstanceBinding - {} on service {}", bindingId, serviceId); final Binding binding = bindingRepository.findOne(bindingId); if (binding == null) { log.error("Trying to delete unknown binding {}, letting it pass", bindingId); return; } if (binding.getResourceType() == Application) { log.info("Unbinding app {} (binding {})", binding.getResourceId(), bindingId); final String appId = binding.getResourceId(); SpaceEnrollerConfig serviceInstance = spaceEnrollerConfigRepository.findOne(request.getServiceInstanceId()); log.debug("serviceInstance {}", serviceInstance); try { List<String> mappedRouteIds = cfApi.listApplicationRoutes(appId); if (mappedRouteIds.size() > 0) { List<Binding> linkedRouteBindings = bindingRepository.findByResourceIdAndType(mappedRouteIds, Route); if (linkedRouteBindings.size() > 0) { linkedRouteBindings.stream() .filter(linkedRouteBinding -> linkedRouteBinding.getServiceInstanceId().equals(serviceId)) .forEach(linkedRouteBinding -> { log.debug("detected associated route binding {}, cleaning it", linkedRouteBinding .getServiceBindingId()); try { cfApi.unbind(linkedRouteBinding.getServiceBindingId()); } catch (CloudFoundryException e) { log.error("Autosleep was unable to clear related route binding {}.", linkedRouteBinding.getServiceBindingId()); bindingRepository.delete(linkedRouteBinding.getServiceBindingId()); } }); } } applicationLocker.executeThreadSafe(appId, () -> { log.debug("deleteServiceInstanceBinding on app {}", appId); ApplicationInfo appInfo = appRepository.findOne(appId); if (appInfo != null) { appInfo.getEnrollmentState().updateEnrollment(serviceInstance.getId(), serviceInstance.getEnrollment() != Enrollment.forced && serviceInstance.getEnrollment() != Enrollment.transient_opt_out); if (appInfo.getEnrollmentState().getStates().isEmpty()) { appRepository.delete(appId); applicationLocker.removeApplication(appId); } else { appRepository.save(appInfo); } } else { log.error("Deleting a binding with no related application info. " + "This should never happen."); } bindingRepository.delete(bindingId); }); } catch (CloudFoundryException e) { throw new ServiceBrokerException("Couldn't clean related app bindings", e); } } else if (binding.getResourceType() == Route) { log.info("Unbinding route {} (binding {})", binding.getResourceId(), bindingId); bindingRepository.delete(bindingId); } } }
AutosleepBindingService implements ServiceInstanceBindingService { @Override public void deleteServiceInstanceBinding(DeleteServiceInstanceBindingRequest request) throws ServiceBrokerException { final String bindingId = request.getBindingId(); final String serviceId = request.getServiceInstanceId(); log.debug("deleteServiceInstanceBinding - {} on service {}", bindingId, serviceId); final Binding binding = bindingRepository.findOne(bindingId); if (binding == null) { log.error("Trying to delete unknown binding {}, letting it pass", bindingId); return; } if (binding.getResourceType() == Application) { log.info("Unbinding app {} (binding {})", binding.getResourceId(), bindingId); final String appId = binding.getResourceId(); SpaceEnrollerConfig serviceInstance = spaceEnrollerConfigRepository.findOne(request.getServiceInstanceId()); log.debug("serviceInstance {}", serviceInstance); try { List<String> mappedRouteIds = cfApi.listApplicationRoutes(appId); if (mappedRouteIds.size() > 0) { List<Binding> linkedRouteBindings = bindingRepository.findByResourceIdAndType(mappedRouteIds, Route); if (linkedRouteBindings.size() > 0) { linkedRouteBindings.stream() .filter(linkedRouteBinding -> linkedRouteBinding.getServiceInstanceId().equals(serviceId)) .forEach(linkedRouteBinding -> { log.debug("detected associated route binding {}, cleaning it", linkedRouteBinding .getServiceBindingId()); try { cfApi.unbind(linkedRouteBinding.getServiceBindingId()); } catch (CloudFoundryException e) { log.error("Autosleep was unable to clear related route binding {}.", linkedRouteBinding.getServiceBindingId()); bindingRepository.delete(linkedRouteBinding.getServiceBindingId()); } }); } } applicationLocker.executeThreadSafe(appId, () -> { log.debug("deleteServiceInstanceBinding on app {}", appId); ApplicationInfo appInfo = appRepository.findOne(appId); if (appInfo != null) { appInfo.getEnrollmentState().updateEnrollment(serviceInstance.getId(), serviceInstance.getEnrollment() != Enrollment.forced && serviceInstance.getEnrollment() != Enrollment.transient_opt_out); if (appInfo.getEnrollmentState().getStates().isEmpty()) { appRepository.delete(appId); applicationLocker.removeApplication(appId); } else { appRepository.save(appInfo); } } else { log.error("Deleting a binding with no related application info. " + "This should never happen."); } bindingRepository.delete(bindingId); }); } catch (CloudFoundryException e) { throw new ServiceBrokerException("Couldn't clean related app bindings", e); } } else if (binding.getResourceType() == Route) { log.info("Unbinding route {} (binding {})", binding.getResourceId(), bindingId); bindingRepository.delete(bindingId); } }  }
AutosleepBindingService implements ServiceInstanceBindingService { @Override public void deleteServiceInstanceBinding(DeleteServiceInstanceBindingRequest request) throws ServiceBrokerException { final String bindingId = request.getBindingId(); final String serviceId = request.getServiceInstanceId(); log.debug("deleteServiceInstanceBinding - {} on service {}", bindingId, serviceId); final Binding binding = bindingRepository.findOne(bindingId); if (binding == null) { log.error("Trying to delete unknown binding {}, letting it pass", bindingId); return; } if (binding.getResourceType() == Application) { log.info("Unbinding app {} (binding {})", binding.getResourceId(), bindingId); final String appId = binding.getResourceId(); SpaceEnrollerConfig serviceInstance = spaceEnrollerConfigRepository.findOne(request.getServiceInstanceId()); log.debug("serviceInstance {}", serviceInstance); try { List<String> mappedRouteIds = cfApi.listApplicationRoutes(appId); if (mappedRouteIds.size() > 0) { List<Binding> linkedRouteBindings = bindingRepository.findByResourceIdAndType(mappedRouteIds, Route); if (linkedRouteBindings.size() > 0) { linkedRouteBindings.stream() .filter(linkedRouteBinding -> linkedRouteBinding.getServiceInstanceId().equals(serviceId)) .forEach(linkedRouteBinding -> { log.debug("detected associated route binding {}, cleaning it", linkedRouteBinding .getServiceBindingId()); try { cfApi.unbind(linkedRouteBinding.getServiceBindingId()); } catch (CloudFoundryException e) { log.error("Autosleep was unable to clear related route binding {}.", linkedRouteBinding.getServiceBindingId()); bindingRepository.delete(linkedRouteBinding.getServiceBindingId()); } }); } } applicationLocker.executeThreadSafe(appId, () -> { log.debug("deleteServiceInstanceBinding on app {}", appId); ApplicationInfo appInfo = appRepository.findOne(appId); if (appInfo != null) { appInfo.getEnrollmentState().updateEnrollment(serviceInstance.getId(), serviceInstance.getEnrollment() != Enrollment.forced && serviceInstance.getEnrollment() != Enrollment.transient_opt_out); if (appInfo.getEnrollmentState().getStates().isEmpty()) { appRepository.delete(appId); applicationLocker.removeApplication(appId); } else { appRepository.save(appInfo); } } else { log.error("Deleting a binding with no related application info. " + "This should never happen."); } bindingRepository.delete(bindingId); }); } catch (CloudFoundryException e) { throw new ServiceBrokerException("Couldn't clean related app bindings", e); } } else if (binding.getResourceType() == Route) { log.info("Unbinding route {} (binding {})", binding.getResourceId(), bindingId); bindingRepository.delete(bindingId); } }  @Override CreateServiceInstanceBindingResponse createServiceInstanceBinding(
            CreateServiceInstanceBindingRequest request); @Override void deleteServiceInstanceBinding(DeleteServiceInstanceBindingRequest request); }
AutosleepBindingService implements ServiceInstanceBindingService { @Override public void deleteServiceInstanceBinding(DeleteServiceInstanceBindingRequest request) throws ServiceBrokerException { final String bindingId = request.getBindingId(); final String serviceId = request.getServiceInstanceId(); log.debug("deleteServiceInstanceBinding - {} on service {}", bindingId, serviceId); final Binding binding = bindingRepository.findOne(bindingId); if (binding == null) { log.error("Trying to delete unknown binding {}, letting it pass", bindingId); return; } if (binding.getResourceType() == Application) { log.info("Unbinding app {} (binding {})", binding.getResourceId(), bindingId); final String appId = binding.getResourceId(); SpaceEnrollerConfig serviceInstance = spaceEnrollerConfigRepository.findOne(request.getServiceInstanceId()); log.debug("serviceInstance {}", serviceInstance); try { List<String> mappedRouteIds = cfApi.listApplicationRoutes(appId); if (mappedRouteIds.size() > 0) { List<Binding> linkedRouteBindings = bindingRepository.findByResourceIdAndType(mappedRouteIds, Route); if (linkedRouteBindings.size() > 0) { linkedRouteBindings.stream() .filter(linkedRouteBinding -> linkedRouteBinding.getServiceInstanceId().equals(serviceId)) .forEach(linkedRouteBinding -> { log.debug("detected associated route binding {}, cleaning it", linkedRouteBinding .getServiceBindingId()); try { cfApi.unbind(linkedRouteBinding.getServiceBindingId()); } catch (CloudFoundryException e) { log.error("Autosleep was unable to clear related route binding {}.", linkedRouteBinding.getServiceBindingId()); bindingRepository.delete(linkedRouteBinding.getServiceBindingId()); } }); } } applicationLocker.executeThreadSafe(appId, () -> { log.debug("deleteServiceInstanceBinding on app {}", appId); ApplicationInfo appInfo = appRepository.findOne(appId); if (appInfo != null) { appInfo.getEnrollmentState().updateEnrollment(serviceInstance.getId(), serviceInstance.getEnrollment() != Enrollment.forced && serviceInstance.getEnrollment() != Enrollment.transient_opt_out); if (appInfo.getEnrollmentState().getStates().isEmpty()) { appRepository.delete(appId); applicationLocker.removeApplication(appId); } else { appRepository.save(appInfo); } } else { log.error("Deleting a binding with no related application info. " + "This should never happen."); } bindingRepository.delete(bindingId); }); } catch (CloudFoundryException e) { throw new ServiceBrokerException("Couldn't clean related app bindings", e); } } else if (binding.getResourceType() == Route) { log.info("Unbinding route {} (binding {})", binding.getResourceId(), bindingId); bindingRepository.delete(bindingId); } }  @Override CreateServiceInstanceBindingResponse createServiceInstanceBinding(
            CreateServiceInstanceBindingRequest request); @Override void deleteServiceInstanceBinding(DeleteServiceInstanceBindingRequest request);  }