Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
SIROCCO Copyright (C) 2011 France Telecom Contact: sirocco@ow2.org This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA $Id$
  
  
  package org.ow2.sirocco.cloudmanager.core.impl;
  
  import java.util.Date;
  import java.util.HashMap;
  import java.util.List;
  import java.util.Map;
  import java.util.Set;
  
  import  javax.ejb.EJB;
  import  javax.ejb.EJBContext;
  import  javax.ejb.Local;
  import  javax.ejb.Remote;
  import  javax.ejb.Stateless;
  import  javax.inject.Inject;
  
 
 @Stateless
 @Remote(IRemoteSystemManager.class)
 @Local(ISystemManager.class)
 @SuppressWarnings("unused")
 public class SystemManager implements ISystemManager {
 
     private static Logger logger = LoggerFactory.getLogger(SystemManager.class.getName());
 
     private static String CREATE_ACTION = "system creation";
 
     private static String START_ACTION = "system start";
 
     private static String STOP_ACTION = "system stop";
 
     private static String SUSPEND_ACTION = "system suspend";
 
     private static String PAUSE_ACTION = "system pause";
 
     private static String RESTART_ACTION = "system restart";
 
     private static String DELETE_ACTION = "system delete";
 
     private static String REMOVE_ENTITY_ACTION = "removeEntityFromSystem";
 
     private static String ADD_MACHINE_ACTION = "addMachineToSystem";
 
     private static String REMOVE_MACHINE_ACTION = "removeMachineFromSystem";
 
     private static String UPDATE_MACHINE_ACTION = "updateMachineFromSystem";
 
     private static String ADD_CREDENTIAL_ACTION = "addCredentialToSystem";
 
     private static String REMOVE_CREDENTIAL_ACTION = "removeCredentialFromSystem";
 
     private static String UPDATE_CREDENTIAL_ACTION = "updateCredentialFromSystem";
 
     private static String ADD_SYSTEM_ACTION = "addSystemToSystem";
 
     private static String REMOVE_SYSTEM_ACTION = "removeSystemFromSystem";
 
     private static String UPDATE_SYSTEM_ACTION = "updateSystemFromSystem";
 
     private static String ADD_VOLUME_ACTION = "addVolumeToSystem";
 
     private static String REMOVE_VOLUME_ACTION = "removeVolumeFromSystem";
 
     private static String UPDATE_VOLUME_ACTION = "updateVolumeFromSystem";
 
     private static String ADD_NETWORK_ACTION = "addNetworkToSystem";
 
     private static String REMOVE_NETWORK_ACTION = "removeNetworkFromSystem";
 
     private static String UPDATE_NETWORK_ACTION = "updateNetworkFromSystem";
 
     private static String HANDLED_JOB = "handled";
 
     private static String PROP_SYSTEM_SUPPORTED_IN_CONNECTOR = "_SystemSupportedInConnector";
 
     private static String PROP_JOB_DETAILED_ACTION = "_JobDetailedAction";
 
     private static String PROP_JOB_DETAILED_SUB_ACTION = "_JobDetailedSubAction";
 
     private static String PROP_JOB_COLLECTION_ID = "_JobCollectionId";
 
     @PersistenceContext(unitName = "persistence-unit/main", type = .)
     private EntityManager em;
 
     @Resource
     private EJBContext ctx;
 
     @Inject
     @OSGiService(dynamic = true)
 
     @EJB
     private IUserManager userManager;
 
     @EJB
     private IMachineManager machineManager;
 
     @EJB
     private IVolumeManager volumeManager;
 
     @EJB
 
     @EJB
     private INetworkManager networkManager;
 
     @EJB
     private IJobManager jobManager;
 
     @EJB
 
     private User getUser() throws CloudProviderException {
         String username = this..getCallerPrincipal().getName();
         return this..getUserByUsername(username);
     }
 
     private boolean isSystemSupportedInConnector(final ICloudProviderConnector connector) {
         boolean isSystemSupportedInConnector = false;
 
         if (connector.getClass().getName().equals("org.ow2.sirocco.cloudmanager.connector.mock.MockCloudProviderConnector")) {
             try {
                 if ((Booleanthis.getConfiguration("mockConnectorImplementsSystem")) {
                     isSystemSupportedInConnector = true;
                 } else {
                     isSystemSupportedInConnector = false;
                 }
             } catch (CloudProviderException e) {
                 ..warn("no parameter found for mockConnectorImplementsSystem");
                 isSystemSupportedInConnector = false;
             }
         } else {
             try {
                 ISystemService sysServ = connector.getSystemService();
                 isSystemSupportedInConnector = true;
             } catch (ConnectorException e1) {
                 isSystemSupportedInConnector = false;
             }
         }
 
         return isSystemSupportedInConnector;
 
     }
 
     @Override
     public void setConfiguration(final String paramNamefinal Object paramValuethrows CloudProviderException {
 
         SiroccoConfiguration config = null;
         try {
             config = (SiroccoConfigurationthis..createQuery("FROM " + SiroccoConfiguration.class.getName())
                 .getSingleResult();
         } catch (NoResultException e) {
             config = null;
         }
 
         if (config == null) {
             config = new SiroccoConfiguration();
             this..persist(config);
             // this.em.flush();
         }
         if (paramName.equals("mockConnectorImplementsSystem") && paramValue instanceof Boolean) {
             config.setMockConnectorImplementsSystem((BooleanparamValue);
         } else {
             throw new CloudProviderException("no parameter found for " + paramName);
         }
 
     }
 
     @Override
     public Object getConfiguration(final String paramNamethrows CloudProviderException {
 
         SiroccoConfiguration config = null;
         try {
             config = (SiroccoConfigurationthis..createQuery("FROM " + SiroccoConfiguration.class.getName())
                 .getSingleResult();
         } catch (NoResultException e) {
             config = null;
         }
 
         if (config == null) {
             config = new SiroccoConfiguration();
             this..persist(config);
             // this.em.flush();
         }
         if (paramName.equals("mockConnectorImplementsSystem")) {
             return config.isMockConnectorImplementsSystem();
         } else {
             throw new CloudProviderException("no parameter found for " + paramName);
         }
 
     }
 
     private CloudCollectionItem createCollection(final Class<? extends CloudCollectionItementityType,
         final CloudResource resourcefinal org.ow2.sirocco.cloudmanager.model.cimi.CloudCollectionItem.State state)
         throws CloudProviderException {
 
         CloudCollectionItem sc = null;
         try {
             sc = entityType.newInstance();
         } catch (InstantiationException e) {
             new CloudProviderException("InstantiationException in createCollection for type " + entityType);
         } catch (IllegalAccessException e) {
             new CloudProviderException("IllegalAccessException in createCollection for type " + entityType);
         }
         sc.setResource(resource);
         sc.setState(state);
         sc.setCreated(new Date());
         sc.setProperties(new HashMap<StringString>());
         return sc;
     }
 
     private CloudCollectionItem updateCollectionFromProvider(final CloudCollectionItem providerEntity,
         final CloudResource resourcefinal org.ow2.sirocco.cloudmanager.model.cimi.CloudCollectionItem.State state)
         throws CloudProviderException {
 
         CloudCollectionItem sc = providerEntity;
 
         sc.setResource(resource);
         sc.setState(state);
         sc.setCreated(new Date());
         sc.setProperties(new HashMap<StringString>());
         return sc;
     }
 
     @Override
     public Job createSystem(final SystemCreate systemCreatethrows CloudProviderException {
 
         // this.checkQuota(userManager.getUserByUsername(this.user), system);
 
         // creation of entities in the base
         System system = new System();
         system.setCreated(new Date());
         system.setDescription(systemCreate.getDescription());
         system.setLocation(null);
         system.setName(systemCreate.getName());
         system.setProperties(systemCreate.getProperties() == null ? new HashMap<StringString>()
             : new HashMap<StringString>(systemCreate.getProperties()));
         system.setState(.);
         system.setUser(this.getUser());
         this..persist(system);
         // this.em.flush();
 
         // creation of main system job
         Job parentJob = this.createJob("add"system);
         parentJob.setTargetResource(system);
         List<CloudResourceaffectedResources = new ArrayList<CloudResource>();
         affectedResources.add(system);
         parentJob.setAffectedResources(affectedResources);
         this..persist(parentJob);
         this..flush();
 
         // creating credentials if necessary
         // iterating through descriptors
         Set<ComponentDescriptorcomponentDescriptorsCred = systemCreate.getSystemTemplate().getComponentDescriptors();
 
         Iterator<ComponentDescriptoriterCred = componentDescriptorsCred.iterator();
         List<SystemCredentialscreds = new ArrayList<SystemCredentials>();
 
         while (iterCred.hasNext()) {
             ComponentDescriptor cd = iterCred.next();
             if (cd.getComponentType() == .) {
                 // creating new credentials
 
                 for (int i = 0; i < cd.getComponentQuantity(); i++) {
                     CredentialsCreate cc = new CredentialsCreate();
                     if (cd.getComponentQuantity() > 1) {
                         String name = cd.getName() == null ? "" : cd.getName();
                         cc.setName(name + new Integer(i + 1).toString());
                     } else {
                         cc.setName(cd.getName());
                     }
                     CredentialsTemplate ct = (CredentialsTemplatecd.getComponentTemplate();
                     cc.setCredentialTemplate(ct);
                     cc.setDescription(cd.getDescription());
                     cc.setProperties(cd.getProperties() == null ? new HashMap<StringString>() : new HashMap<StringString>(
                         cd.getProperties()));
 
                     // no job for credentials!
                     Credentials c = this..createCredentials(cc);
                     SystemCredentials sc = (SystemCredentialsthis.createCollection(SystemCredentials.classc,
                         ..);
                     this..persist(sc);
                     creds.add(sc);
                 }
             }
         }
         system.setCredentials(creds);
 
         // resolve MachineTemplate credential component references if any
         for (ComponentDescriptor component : systemCreate.getSystemTemplate().getComponentDescriptors()) {
             if (component.getComponentType() == .) {
                 MachineTemplate machineTemplate = (MachineTemplatecomponent.getComponentTemplate();
                 if (machineTemplate.getSystemCredentialName() != null) {
                     for (SystemCredentials sysCred : creds) {
                         Credentials cred = (CredentialssysCred.getResource();
                         if (cred.getName() != null && cred.getName().equals(machineTemplate.getSystemCredentialName())) {
                             machineTemplate.setCredential(cred);
                             break;
                         }
                     }
                 }
             }
         }
 
         // ICloudProviderConnector connector = this.getCloudProviderConnector();
         // if (connector == null) {
         // throw new CloudProviderException("no connector found");
         // }
         // CloudProviderAccount cpa =
         // this.selectCloudProviderAccount(this.selectCloudProvider());
 
         Placement placement = this..placeResource(systemCreate.getProperties());
         ICloudProviderConnector connector = this.getCloudProviderConnector(placement.getAccount(), placement.getLocation());
         if (connector == null) {
             throw new CloudProviderException("Cannot retrieve cloud provider connector "
                 + placement.getAccount().getCloudProvider().getCloudProviderType());
         }
 
         ..info("cpa id: " + placement.getAccount().getId());
         system.setCloudProviderAccount(placement.getAccount());
         system.setLocation(placement.getLocation());
 
         // this.em.flush();
 
         if (this.isSystemSupportedInConnector(connector)) {
 
             // sending command to selected connector
             Job job = null;
             try {
                 job = connector.getSystemService().createSystem(systemCreate);
             } catch (ConnectorException e) {
                 ..info("system creation failed"e);
                 throw new CloudProviderException("system creation failed");
             }
             // job returned by connector is a copy of the real connector job
             // so we can directly persist it
             job.setDescription("System creation");
             job.setUser(this.getUser());
             this..persist(job);
 
             system.setProviderAssignedId(job.getTargetResource().getProviderAssignedId());
 
             job.setTargetResource(system);
             job.setParentJob(parentJob);
 
             this.setJobProperty(parentJob."ok");
 
             // Ask for connector to notify when job completes
             try {
                 UtilsForManagers.emitJobListenerMessage(job.getProviderAssignedId(), this.);
             } catch (Exception e) {
                 throw new ServiceUnavailableException(e.getMessage());
             }
             this.relConnector(system.getCloudProviderAccount(), connector);
 
         } else {
             // implementation when System is not supported by underlying
             // connector
             Set<ComponentDescriptorcomponentDescriptors = systemCreate.getSystemTemplate().getComponentDescriptors();
 
             // iterating through descriptors
             Iterator<ComponentDescriptoriter = componentDescriptors.iterator();
             while (iter.hasNext()) {
                 ComponentDescriptor cd = iter.next();
 
                 if (cd.getComponentType() == .) {
                     // creating new machines
                     for (int i = 0; i < cd.getComponentQuantity(); i++) {
                         MachineCreate mc = new MachineCreate();
                         if (cd.getComponentQuantity() > 1) {
                             String name = cd.getName() == null ? "" : cd.getName();
                             mc.setName(name + new Integer(i + 1).toString());
                         } else {
                             mc.setName(cd.getName());
                         }
                         MachineTemplate mt = (MachineTemplatecd.getComponentTemplate();
                         mc.setMachineTemplate(mt);
                         mc.setDescription(cd.getDescription());
                         Map<StringStringprops = cd.getProperties() == null ? new HashMap<StringString>()
                             : new HashMap<StringString>(cd.getProperties());
 
                         props.put("provider"placement.getAccount().getCloudProvider().getCloudProviderType());
                         if (placement.getLocation() != null) {
                             props.put("location"placement.getLocation().getCountryName());
                         }
                         mc.setProperties(props);
 
                         Job j = this..createMachine(mc);
                         j.setParentJob(parentJob);
 
                         SystemMachine sc = (SystemMachinethis.createCollection(SystemMachine.classj.getTargetResource(),
                             ..);
                         this..persist(sc);
                     }
                 }
                 if (cd.getComponentType() == .) {
                     // creating new volumes
                     for (int i = 0; i < cd.getComponentQuantity(); i++) {
                         VolumeCreate vc = new VolumeCreate();
                         if (cd.getComponentQuantity() > 1) {
                             String name = cd.getName() == null ? "" : cd.getName();
                             vc.setName(name + new Integer(i + 1).toString());
                         } else {
                             vc.setName(cd.getName());
                         }
                         VolumeTemplate vt = (VolumeTemplatecd.getComponentTemplate();
                         vc.setVolumeTemplate(vt);
                         vc.setDescription(cd.getDescription());
                         Map<StringStringprops = cd.getProperties() == null ? new HashMap<StringString>()
                             : new HashMap<StringString>(cd.getProperties());
 
                         props.put("provider"placement.getAccount().getCloudProvider().getCloudProviderType());
                         if (placement.getLocation() != null) {
                             props.put("location"placement.getLocation().getCountryName());
                         }
                         vc.setProperties(props);
 
                         Job j = this..createVolume(vc);
                         j.setParentJob(parentJob);
 
                         SystemVolume sc = (SystemVolumethis.createCollection(SystemVolume.classj.getTargetResource(),
                             ..);
                         this..persist(sc);
                     }
                 }
                 if (cd.getComponentType() == .) {
                     // creating new systems
                     for (int i = 0; i < cd.getComponentQuantity(); i++) {
                         SystemCreate sc = new SystemCreate();
                         if (cd.getComponentQuantity() > 1) {
                             String name = cd.getName() == null ? "" : cd.getName();
                             sc.setName(name + new Integer(i + 1).toString());
                         } else {
                             sc.setName(cd.getName());
                         }
                         SystemTemplate st = (SystemTemplatecd.getComponentTemplate();
                         sc.setSystemTemplate(st);
                         sc.setDescription(cd.getDescription());
                         Map<StringStringprops = cd.getProperties() == null ? new HashMap<StringString>()
                             : new HashMap<StringString>(cd.getProperties());
 
                         props.put("provider"placement.getAccount().getCloudProvider().getCloudProviderType());
                         if (placement.getLocation() != null) {
                             props.put("location"placement.getLocation().getCountryName());
                         }
                         sc.setProperties(props);
 
                         Job j = this.createSystem(sc);
                         j.setParentJob(parentJob);
 
                         SystemSystem ss = (SystemSystemthis.createCollection(SystemSystem.classj.getTargetResource(),
                             ..);
                         this..persist(ss);
 
                         // special case: created system is in mixed state,
                         // because it has no childs
                         // =>parent system must be in mixed state as well
                         if (((Systemss.getResource()).getState().equals(..)) {
                             system.setState(..);
                         }
                     }
                 }
                 if (cd.getComponentType() == .) {
                     // creating new networks
                     for (int i = 0; i < cd.getComponentQuantity(); i++) {
                         NetworkCreate nc = new NetworkCreate();
                         if (cd.getComponentQuantity() > 1) {
                             String name = cd.getName() == null ? "" : cd.getName();
                             nc.setName(name + new Integer(i + 1).toString());
                         } else {
                             nc.setName(cd.getName());
                         }
                         NetworkTemplate nt = (NetworkTemplatecd.getComponentTemplate();
                         nc.setNetworkTemplate(nt);
                         nc.setDescription(cd.getDescription());
                         Map<StringStringprops = cd.getProperties() == null ? new HashMap<StringString>()
                             : new HashMap<StringString>(cd.getProperties());
 
                         props.put("provider"placement.getAccount().getCloudProvider().getCloudProviderType());
                         if (placement.getLocation() != null) {
                             props.put("location"placement.getLocation().getCountryName());
                         }
                         nc.setProperties(props);
 
                         Job j = this..createNetwork(nc);
                         j.setParentJob(parentJob);
 
                         SystemNetwork sc = (SystemNetworkthis.createCollection(SystemNetwork.classj.getTargetResource(),
                             ..);
                         this..persist(sc);
                     }
                 }
             }
             // has this system any running job?
             List<JobnestedJobs = parentJob.getNestedJobs();
             if (nestedJobs != null && nestedJobs.size() == 0) {
                 // no job handling, job finished instantly and system in mixed
                 // state
                 parentJob.setState(.);
                 system.setState(.);
             }
 
         }
 
         return parentJob;
 
     }
 
     private void setJobProperty(final Job jfinal String keyfinal String value) {
         Map<StringStringprop = j.getProperties();
         prop.put(keyvalue);
         j.setProperties(prop);
     }
 
     private String getJobProperty(final Job jfinal String key) {
         Map<StringStringprop = j.getProperties();
         return prop.get(key);
     }
 
     private boolean findComponentDescriptor(final SystemTemplate systemTemplatefinal String namefinal ComponentType type) {
         for (ComponentDescriptor cd : systemTemplate.getComponentDescriptors()) {
             if (cd.getComponentType() == type && cd.getName() != null && cd.getName().equals(name)) {
                 return true;
             }
         }
         return false;
     }
 
     private void validateSystemTemplate(final SystemTemplate systemTemplatethrows InvalidRequestException,
         CloudProviderException {
         for (ComponentDescriptor cd : systemTemplate.getComponentDescriptors()) {
             if (cd.getId() == null) {
                 CloudTemplate ct = cd.getComponentTemplate();
                 if (ct instanceof SystemTemplate) {
                     this.validateSystemTemplate((SystemTemplatect);
                 } else if (ct instanceof MachineTemplate) {
                     MachineTemplate machineTemplate = (MachineTemplatect;
                     if (machineTemplate.getNetworkInterfaces() != null) {
                         for (MachineTemplateNetworkInterface nic : machineTemplate.getNetworkInterfaces()) {
                             if (nic.getSystemNetworkName() != null) {
                                 if (!this.findComponentDescriptor(systemTemplatenic.getSystemNetworkName(),
                                     .)) {
                                     throw new InvalidRequestException("Invalid network interface: component #"
                                         + nic.getSystemNetworkName() + " not found");
                                 }
                             } else if (nic.getNetwork() == null) {
                                 throw new InvalidRequestException("Invalid network interface: missing network");
                             }
 
                         }
                     }
                     if (machineTemplate.getVolumes() != null) {
                         for (MachineVolume machineVolume : machineTemplate.getVolumes()) {
                             if (machineVolume.getSystemVolumeName() != null
                                 && !this.findComponentDescriptor(systemTemplatemachineVolume.getSystemVolumeName(),
                                     .)) {
                                 throw new InvalidRequestException("Invalid volume: component #"
                                     + machineVolume.getSystemVolumeName() + " not found");
                             }
                         }
                     }
                     if (machineTemplate.getSystemCredentialName() != null
                         && !this.findComponentDescriptor(systemTemplatemachineTemplate.getSystemCredentialName(),
                             .)) {
                         throw new InvalidRequestException("Invalid credential: component #"
                             + machineTemplate.getSystemCredentialName() + " not found");
                     }
                 }
             }
         }
     }
 
     @Override
         CloudProviderException {
         ..info("Creating SystemTemplate name=" + systemT.getName());
         this.validateSystemTemplate(systemT);
         systemT.setUser(this.getUser());
         systemT.setCreated(new Date());
 
         for (ComponentDescriptor cd : systemT.getComponentDescriptors()) {
             if (cd.getId() == null) {
                 // no id, will be persisted as jpa entity
                 cd.setUser(this.getUser());
                 cd.setCreated(new Date());
                 CloudTemplate ct = cd.getComponentTemplate();
 
                 if (ct.getId() == null) {
                     // no id, the template is new: calling manager
                     // createTemplate for each one
 
                     ct.setIsEmbeddedInSystemTemplate(true);
 
                     if (ct instanceof SystemTemplate) {
                         // recursive calls
                         this.createSystemTemplate((SystemTemplatect);
                     }
                     if (ct instanceof VolumeTemplate) {
                         this..createVolumeTemplate((VolumeTemplatect);
                     }
                     if (ct instanceof NetworkTemplate) {
                         this..createNetworkTemplate((NetworkTemplatect);
                     }
                     if (ct instanceof CredentialsTemplate) {
                         this..createCredentialsTemplate((CredentialsTemplatect);
                     }
                     if (ct instanceof MachineTemplate) {
                         this..createMachineTemplate((MachineTemplatect);
                     }
 
                 }
             }
         }
 
         this..persist(systemT);
         this..flush();
         return systemT;
     }
 
     @Override
     public void deleteSystemTemplate(final String systemTemplateIdthrows ResourceNotFoundExceptionCloudProviderException {
         // TODO Auto-generated method stub
     }
 
     @SuppressWarnings("unchecked")
     @Override
     public List<SystemgetSystems() throws CloudProviderException {
         return QueryHelper.getEntityList("System"this.this.getUser().getUsername());
     }
 
     @Override
     public QueryResult<SystemgetSystems(final int firstfinal int lastfinal List<Stringfilters,
         final List<Stringattributesthrows InvalidRequestExceptionCloudProviderException {
         QueryHelper.QueryParamsBuilder params = QueryHelper.QueryParamsBuilder.builder("System"System.class);
         return QueryHelper.getEntityList(this.,
             params.userName(this.getUser().getUsername()).first(first).last(last).filter(filters).attributes(attributes)
                 .verifyDeletedState());
     }
 
     @Override
     public System getSystemById(final String systemIdthrows CloudProviderException {
         if (systemId == null) {
             throw new InvalidRequestException(" null system id");
         }
         System result = this..find(System.classnew Integer(systemId));
 
         if (result == null || result.getState() == ..) {
             throw new ResourceNotFoundException(" Invalid system id " + systemId);
         }
         result.getMachines().size();
         result.getNetworks().size();
         result.getSystems().size();
         result.getVolumes().size();
         result.getCredentials().size();
         result.getProperties().size();
         return result;
     }
 
     @Override
     public System getSystemAttributes(final String systemIdfinal List<Stringattributesthrows CloudProviderException {
         System sys = this.getSystemById(systemId);
         return UtilsForManagers.fillResourceAttributes(sysattributes);
     }
 
     @Override
     public SystemTemplate getSystemTemplateById(final String systemTemplateIdthrows CloudProviderException {
         SystemTemplate result = this..find(SystemTemplate.classnew Integer(systemTemplateId));
         if (result == null) {
             throw new ResourceNotFoundException("Invalid SystemTemplate id: " + systemTemplateId);
         }
         return result;
     }
 
     @Override
     public SystemTemplate getSystemTemplateAttributes(final String systemTemplateIdfinal List<Stringattributes)
         throws CloudProviderException {
         SystemTemplate sysTemplate = this.getSystemTemplateById(systemTemplateId);
         return UtilsForManagers.fillResourceAttributes(sysTemplateattributes);
     }
 
     @SuppressWarnings("unchecked")
     @Override
         return QueryHelper.getEntityList("SystemTemplate"this.this.getUser().getUsername(), false);
     }
 
     @Override
     public QueryResult<SystemTemplategetSystemTemplates(final int firstfinal int lastfinal List<Stringfilters,
         final List<Stringattributesthrows InvalidRequestExceptionCloudProviderException {
         QueryHelper.QueryParamsBuilder params = QueryHelper.QueryParamsBuilder.builder("SystemTemplate"SystemTemplate.class);
         return QueryHelper.getEntityList(this.,
             params.userName(this.getUser().getUsername()).first(first).last(last).filter(filters).attributes(attributes)
                 .filterEmbbededTemplate());
     }
 
     private ComponentDescriptor getComponentDescriptorById(final String componentDescriptorIdthrows CloudProviderException {
         ComponentDescriptor result = this..find(ComponentDescriptor.classnew Integer(componentDescriptorId));
         return result;
     }
 
     @Override
     public Job addEntityToSystem(final String systemIdfinal CloudCollectionItem entitythrows CloudProviderException {
 
         System s = this.getSystemById(systemId);
 
         if (entity == null || s == null) {
             throw new CloudProviderException("bad id given in parameter");
         }
         if (entity.getResource() == null) {
             throw new CloudProviderException("no resource linked to this entity");
         }
         if (QueryHelper.getCloudResourceById(this.entity.getResource().getId().toString()) == null) {
             throw new CloudProviderException("nonexisting resource linked to this SystemEntity");
         }
 
         // persist only systemXXX
         if (entity.getId() == null) {
             if (entity.getResource().getId() == null) {
                 throw new CloudProviderException("CloudCollectionItem must own an existing resource");
             } else {
                 CloudResource resTmp = this..merge(entity.getResource());
                 entity.setResource(null);
                 this..persist(entity);
                 // this.em.flush();
                 entity.setResource(resTmp);
             }
         } else {
             throw new CloudProviderException("CloudCollectionItem must be new");
         }
 
         String jobAction = null;
         if (entity instanceof SystemMachine) {
             s.getMachines().add((SystemMachineentity);
             jobAction = .;
         } else if (entity instanceof SystemVolume) {
             s.getVolumes().add((SystemVolumeentity);
             jobAction = .;
         } else if (entity instanceof SystemSystem) {
             s.getSystems().add((SystemSystementity);
             jobAction = .;
         } else if (entity instanceof SystemNetwork) {
             s.getNetworks().add((SystemNetworkentity);
             jobAction = .;
         } else if (entity instanceof SystemCredentials) {
             s.getCredentials().add((SystemCredentialsentity);
             jobAction = .;
         } else {
             throw new CloudProviderException("object type can't be owned by a system");
         }
 
         // for system not supported by underlying connector
         Job job = this.createJob("add"s);
         this.setJobProperty(job.jobAction);
         job.setState(.);// no call to connector
         this..persist(job);
 
         return job;
     }
 
     @Override
     public Job removeEntityFromSystem(final String systemIdfinal String entityIdthrows CloudProviderException {
 
         System s = this.getSystemById(systemId);
         CloudCollectionItem ce = QueryHelper.getCloudCollectionById(this.entityId);
         if (ce == null || s == null) {
             throw new CloudProviderException("bad id given in parameter");
         }
 
         // parent job
         Job parentJob = this.createJob("delete"s);
         this.setJobProperty(parentJob.entityId);
         this..persist(parentJob);
 
         String jobAction = null;
         if (ce instanceof SystemMachine) {
             jobAction = .;
         } else if (ce instanceof SystemVolume) {
             jobAction = .;
         } else if (ce instanceof SystemSystem) {
             jobAction = .;
         } else if (ce instanceof SystemNetwork) {
             jobAction = .;
         } else if (ce instanceof SystemCredentials) {
             jobAction = .;
         } else {
             throw new CloudProviderException("object type can't be owned by a system");
         }
         this.setJobProperty(parentJob.jobAction);
 
         ICloudProviderConnector connector = this.getConnector(s);
 
         // connector or not connector? that is the question!
         if (this.isSystemSupportedInConnector(connector)) {
             Job j;
             try {
                 j = connector.getSystemService().removeEntityFromSystem(s.getProviderAssignedId(),
                     ce.getResource().getProviderAssignedId());
             } catch (ConnectorException e) {
                 throw new ServiceUnavailableException(e.getMessage() + " action " + jobAction + " system id "
                     + s.getProviderAssignedId() + " " + s.getId());
             }
 
             j.setParentJob(parentJob);
             j.setUser(this.getUser());
             this..persist(j);
 
         } else {
             // no child job=> doing all immediately
             this.removeEntityFromSystem_Final(parentJobs);
             parentJob.setState(..);
 
         }
 
         return parentJob;
     }
 
     private void removeEntityFromSystem_Fi