Class AbstractServiceManager<A extends AbstractArtifactDescriptor<S>,S extends AbstractServiceDescriptor<A>>

java.lang.Object
de.iip_ecosphere.platform.services.AbstractServiceManager<A,S>
Type Parameters:
A - the actual type of the artifact descriptor
S - the actual type of the service descriptor
All Implemented Interfaces:
ServiceManager, ServiceOperations

public abstract class AbstractServiceManager<A extends AbstractArtifactDescriptor<S>,S extends AbstractServiceDescriptor<A>> extends Object implements ServiceManager
A basic re-usable implementation of the service manager. Implementations shall override at least switchToService(String, String), migrateService(String, String) and call the implementation of this class to perform the changes. Implementations shall call the notify methods in ServicesAas.
Author:
Holger Eichelberger, SSE
  • Field Details

  • Constructor Details

    • AbstractServiceManager

      public AbstractServiceManager()
  • Method Details

    • getAvailablePredicate

      protected abstract Predicate<TypedDataConnectorDescriptor> getAvailablePredicate()
      Returns the available connector testing predicate.
      Returns:
      the predicate
    • getArtifactIds

      public Set<String> getArtifactIds()
      Description copied from interface: ServiceManager
      Returns the ids of all available artifacts.
      Specified by:
      getArtifactIds in interface ServiceManager
      Returns:
      the ids
    • getArtifacts

      public Collection<A> getArtifacts()
      Description copied from interface: ServiceManager
      Returns the available (installed) artifacts.
      Specified by:
      getArtifacts in interface ServiceManager
      Returns:
      the services
    • getServiceIds

      public Set<String> getServiceIds()
      Description copied from interface: ServiceManager
      Returns the ids of all available services.
      Specified by:
      getServiceIds in interface ServiceManager
      Returns:
      the ids
    • getServices

      public Collection<S> getServices()
      Description copied from interface: ServiceManager
      Returns the available (installed) services in all artifacts.
      Specified by:
      getServices in interface ServiceManager
      Returns:
      the services
    • getArtifact

      public A getArtifact(String artifactId)
      Description copied from interface: ServiceManager
      Returns an artifact descriptor.
      Specified by:
      getArtifact in interface ServiceManager
      Parameters:
      artifactId - the id of the service (may be null or invalid)
      Returns:
      the related artifact descriptor or null if the artifact does not exist
    • getService

      public S getService(String serviceId)
      Description copied from interface: ServiceManager
      Returns a service descriptor.
      Specified by:
      getService in interface ServiceManager
      Parameters:
      serviceId - the id of the service (may be null or invalid)
      Returns:
      the related service descriptor or null if the service does not exist
    • getServiceState

      public de.iip_ecosphere.platform.services.environment.ServiceState getServiceState(String serviceId)
      Description copied from interface: ServiceOperations
      Returns the state of the service.
      Specified by:
      getServiceState in interface ServiceOperations
      Parameters:
      serviceId - the id of the running service
      Returns:
      the state of the service
    • addArtifact

      protected String addArtifact(String artifactId, A descriptor) throws ExecutionException
      Adds an artifact.
      Parameters:
      artifactId - the artifact id
      descriptor - the service descriptor
      Returns:
      artifactId
      Throws:
      ExecutionException - in case that the id is invalid or already known
    • removeArtifact

      public void removeArtifact(String artifactId) throws ExecutionException
      Description copied from interface: ServiceOperations
      Removes the artifact (and transitively its services) from the management domain of this instance. This operation shall only remove the implementation of non-operational services and, thus, perform a state transition to ServiceState.UNDEPLOYING and ultimately the service(s) shall be removed and their descriptors (including the artifact descriptor) shall not be available anymore.
      Specified by:
      removeArtifact in interface ServiceOperations
      Parameters:
      artifactId - the id of the artifact to remove, or as fallback the canonical URI of the artifact
      Throws:
      ExecutionException - if removing the service fails, e.g., because it is still running
    • isValidId

      protected static final boolean isValidId(String id)
      Returns whether the given id is structurally valid, i.e., not null and not empty.
      Parameters:
      id - the id to check
      Returns:
      true if id is valid, false else
    • isValidIdBut

      protected static final boolean isValidIdBut(String id, String butId)
      Returns whether the given id is structurally valid, i.e., not null and not empty, but not butId.
      Parameters:
      id - the id to check
      butId - the id to exclude
      Returns:
      true if id is valid, false else
    • checkId

      protected static void checkId(String id, String text) throws ExecutionException
      Checks the given id for basic validity.
      Parameters:
      id - the id to check
      text - the text to include into the exception
      Throws:
      ExecutionException - if id is not considered valid
      See Also:
    • switchToService

      public void switchToService(String serviceId, String targetId) throws ExecutionException
      Description copied from interface: ServiceOperations
      Switches to an interface-compatible service. This method cares for stopping the old service, performing a handover if adequate, starting the target service. [adaptation]
      Specified by:
      switchToService in interface ServiceOperations
      Parameters:
      serviceId - the id of the running service
      targetId - the id of the target service
      Throws:
      ExecutionException - if switching the service cannot be performed for some reason
    • migrateService

      public void migrateService(String serviceId, String resourceId) throws ExecutionException
      Description copied from interface: ServiceOperations
      Migrates a service. However, it may be required to migrate/move the containing artifact. [adaptation]
      Specified by:
      migrateService in interface ServiceOperations
      Parameters:
      serviceId - the id of the service
      resourceId - the target resource id, e.g., a device
      Throws:
      ExecutionException - if migration fails
    • passivateService

      public void passivateService(String serviceId) throws ExecutionException
      Description copied from interface: ServiceOperations
      Passivates the service. [adaptation]
      Specified by:
      passivateService in interface ServiceOperations
      Parameters:
      serviceId - the id of the running service
      Throws:
      ExecutionException - in case that passivating fails, e.g., because the service is already passive
    • activateService

      public void activateService(String serviceId) throws ExecutionException
      Description copied from interface: ServiceOperations
      Activates the service. [adaptation]
      Specified by:
      activateService in interface ServiceOperations
      Parameters:
      serviceId - the id of the running service
      Throws:
      ExecutionException - in case that activating fails, e.g., because the service is already active
    • reconfigureService

      public void reconfigureService(String serviceId, Map<String,String> values) throws ExecutionException
      Description copied from interface: ServiceOperations
      Reconfigures the underlying service. [adaptation]
      Specified by:
      reconfigureService in interface ServiceOperations
      Parameters:
      serviceId - the serviceId of the running service
      values - the (service-specific) name-value mapping that shall lead to a reconfiguration of the service; values come either as primitive values or as JSON structures complying with the parameter descriptor. The service is responsible for correct JSON de-serialization according to the respective descriptor
      Throws:
      ExecutionException - if reconfiguration fails
    • getStub

      protected de.iip_ecosphere.platform.services.environment.ServiceStub getStub(S service)
      Returns the service stub for implementing the service operations.
      Parameters:
      service - the service to return the stub for
      Returns:
      the stub, may be null if the service is not running
    • getArtifactDescriptor

      protected A getArtifactDescriptor(String artifactId, String idText, String activityText) throws ExecutionException
      Returns a service descriptor.
      Parameters:
      artifactId - the artifact id
      idText - the id text to be passed to checkId(String, String)
      activityText - a description of the activity the service is requested for to construct an exception if the service does not exist
      Returns:
      the service (not null)
      Throws:
      ExecutionException - if id is invalid or the service is unknown
    • getServiceDescriptor

      protected S getServiceDescriptor(String serviceId, String idText, String activityText) throws ExecutionException
      Returns a service descriptor.
      Parameters:
      serviceId - the service id
      idText - the id text to be passed to checkId(String, String)
      activityText - a description of the activity the service is requested for to construct an exception if the service does not exist
      Returns:
      the service (not null)
      Throws:
      ExecutionException - if id is invalid or the service is unknown
    • setServiceState

      public void setServiceState(String serviceId, de.iip_ecosphere.platform.services.environment.ServiceState state) throws ExecutionException
      Description copied from interface: ServiceOperations
      Sets the state of the service. [adaptation]
      Specified by:
      setServiceState in interface ServiceOperations
      Parameters:
      serviceId - the id of the running service
      state - the new state of the service
      Throws:
      ExecutionException - if changing to the target state is not possible
    • setState

      protected void setState(ServiceDescriptor service, de.iip_ecosphere.platform.services.environment.ServiceState state) throws ExecutionException
      Changes the service state and notifies ServicesAas.
      Parameters:
      service - the service
      state - the new state
      Throws:
      ExecutionException - if changing the state fails
    • getParameters

      public List<TypedDataDescriptor> getParameters(String serviceId)
      Description copied from interface: ServiceManager
      Returns all information about parameter for the given serviceId. [adaptation]
      Specified by:
      getParameters in interface ServiceManager
      Parameters:
      serviceId - the serviceId of the service
      Returns:
      the descriptors for all supported parameters
    • getInputDataConnectors

      public List<TypedDataConnectorDescriptor> getInputDataConnectors(String serviceId)
      Description copied from interface: ServiceManager
      Returns all input connector information for the given serviceId.
      Specified by:
      getInputDataConnectors in interface ServiceManager
      Parameters:
      serviceId - the serviceId of the service
      Returns:
      the descriptors for all input data connectors
    • getOutputDataConnectors

      public List<TypedDataConnectorDescriptor> getOutputDataConnectors(String serviceId)
      Description copied from interface: ServiceManager
      Returns all output connector information for the given serviceId.
      Specified by:
      getOutputDataConnectors in interface ServiceManager
      Parameters:
      serviceId - the serviceId of the service
      Returns:
      the descriptors for all output data connectors
    • sortByDependency

      protected String[] sortByDependency(String[] serviceIds, boolean start)
      Sorts the given list by the dependencies specified in the deployment descriptor.
      Parameters:
      serviceIds - the service ids to sort
      start - sequence for service start
      Returns:
      the sorted service ids
    • sortByDependency

      public static <S extends ServiceDescriptor> List<S> sortByDependency(List<S> services, Collection<? extends ServiceDescriptor> localServices, Predicate<TypedDataConnectorDescriptor> available, boolean reverse)
      Sorts a list of services by their dependencies, considering prerequisite input nodes outside the own ensemble. Ensemble nodes are simply listed after their ensemble leader. [public, static for testing]
      Type Parameters:
      S - the service type
      Parameters:
      services - the services to sort
      localServices - all known local services including services
      available - the available predicate
      reverse - reverse the order (for stopping)
      Returns:
      the list of sorted services
    • containsIdSafe

      private static final boolean containsIdSafe(Collection<String> coll, String id)
      Returns whether coll contains id.
      Parameters:
      coll - the collection to search within
      id - the id to search for, may be null or empty
      Returns:
      false if the id is not valid, whether it is contained in coll else
    • determineExternalConnections

      public static Set<AbstractServiceManager.TypedDataConnection> determineExternalConnections(ServiceManager mgr, String... serviceIds)
      Determines the external connections of the given services.
      Parameters:
      mgr - the service manager
      serviceIds - the services to determine the connections for
      Returns:
      the external connections
    • determineInternalConnections

      public static Set<AbstractServiceManager.TypedDataConnection> determineInternalConnections(ServiceManager mgr, String... serviceIds)
      Determines all connections within serviceIds.
      Parameters:
      mgr - the service manager
      serviceIds - the services to determine the connections for
      Returns:
      the internal connections
    • determineFunctionalConnections

      public static Set<AbstractServiceManager.TypedDataConnection> determineFunctionalConnections(ServiceManager mgr, String... serviceIds)
      Determines all connections characterizing the functional output/input services in serviceIds.
      Parameters:
      mgr - the service manager
      serviceIds - the services to determine the connections for
      Returns:
      the internal connections
    • topLevel

      public static String[] topLevel(ServiceManager mgr, String... serviceIds)
      Returns only top-level services from serviceIds.
      Parameters:
      mgr - the service manager to take the descriptors from
      serviceIds - the service ids to filter out
      Returns:
      serviceIds or a subset of serviceIds
    • pruneServers

      public static String[] pruneServers(ServiceManager mgr, String... serviceIds)
      Returns only non-server services from serviceIds, i.e., only instances that can be identified as server are pruned.
      Parameters:
      mgr - the service manager to take the descriptors from
      serviceIds - the service ids to filter out
      Returns:
      serviceIds or a subset of serviceIds
    • checkServiceInstances

      protected List<String> checkServiceInstances(String[] sId)
      Checks for service instances regarding application id and application instance id. If needed, creates instances using existing service descriptors as templates via instantiateFromTemplate(AbstractServiceDescriptor, String). Call handleInstantiatedServices(Iterable) on the result, immediately or after further initializations are done.
      Parameters:
      sId - service ids to check
      Returns:
      the instantiated services
    • handleInstantiatedServices

      protected void handleInstantiatedServices(Iterable<String> sId)
      Handles the services instantiated in checkServiceInstances(String[]) by adding them to the Services AAS.
      Parameters:
      sId - the instantiated service ids, may be empty
    • instantiateFromTemplate

      protected abstract S instantiateFromTemplate(S template, String serviceId)
      Creates a service instance descriptor from template. Application and application instance id shall match template.
      Parameters:
      template - the service descriptor template
      serviceId - the service id, usually including application id and application instance id
      Returns:
      the instantiated descriptor
    • clear

      public void clear()
      Clears the internal data. [testing]
    • getServiceInstanceCount

      public int getServiceInstanceCount(String serviceId)
      Description copied from interface: ServiceOperations
      Returns the number of service instances of the same service in the same application.
      Specified by:
      getServiceInstanceCount in interface ServiceOperations
      Parameters:
      serviceId - the service id, application instance id is ignored if not given
      Returns:
      0 if there is no other service instance, 1 if no applicationId is given in serviceId but there is a service with the same id, the number of instances otherwise