Class ServiceStub

  • All Implemented Interfaces:
    ParameterConfigurerProvider, Service, ServiceBase

    public class ServiceStub
    extends java.lang.Object
    implements Service
    Implements the service interface through AAS protocol operations. The operations stored in here can be directly used as suppliers, consumers and functions for an AAS. In addition, this class can be used standalone with an appropriate invocables creator as client stub.
    Author:
    Holger Eichelberger, SSE
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private java.util.Map<java.lang.String,​java.util.function.Supplier<java.lang.Object>> getters  
      private java.util.Map<java.lang.String,​java.util.function.Function<java.lang.Object[],​java.lang.Object>> operations  
      private java.util.Map<java.lang.String,​java.util.function.Consumer<java.lang.Object>> setters  
    • Constructor Summary

      Constructors 
      Constructor Description
      ServiceStub​(de.iip_ecosphere.platform.support.aas.InvocablesCreator iCreator, java.lang.String serviceId)
      Creates the setup and registers the operations.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void activate()
      Activates the service.
      private <T> T convertGetterResult​(java.lang.String getId, T dflt, java.util.function.Function<java.lang.Object,​T> conversion)
      Converts a getter result.
      private <T extends java.lang.Enum<T>>
      T
      convertGetterResultToEnum​(java.lang.String getId, T dflt, java.lang.Class<T> cls)
      Returns the result of the specified getter converted to the given enum type.
      private java.lang.String convertGetterResultToString​(java.lang.String getId)
      Returns the result of the specified getter converted to string with default value "".
      static <T extends java.lang.Enum<T>>
      T
      convertToEnumSafe​(java.lang.Object val, T dflt, java.lang.Class<T> cls)
      Converts an object val to an enum value without throwing exceptions.
      java.lang.String getDescription()
      The description of the service.
      java.util.function.Supplier<java.lang.Object> getGetter​(java.lang.String name)
      Returns the getter functor for a given property.
      java.lang.String getId()
      Returns the unique id of the service.
      ServiceKind getKind()
      Returns the service kind.
      java.lang.String getName()
      The name of the service.
      java.util.function.Function<java.lang.Object[],​java.lang.Object> getOperation​(java.lang.String name)
      Returns the functor for a given operation.
      java.util.function.Consumer<java.lang.Object> getSetter​(java.lang.String name)
      Returns the setter functor for a given property.
      ServiceState getState()
      Returns the state the service is currently in.
      de.iip_ecosphere.platform.support.iip_aas.Version getVersion()
      The version of the service.
      boolean isDeployable()
      Returns whether the service is deployable in distributable manner or fixed in deployment location.
      boolean isTopLevel()
      Returns whether the service is a top-level service.
      void migrate​(java.lang.String resourceId)
      Migrates a service.
      void passivate()
      Passivates the service.
      void reconfigure​(java.util.Map<java.lang.String,​java.lang.String> values)
      Reconfigures the service.
      private void registerOperation​(java.lang.String name, java.util.function.Function<java.lang.Object[],​java.lang.Object> operation)
      Registers the functor for an operation.
      private void registerProperty​(java.lang.String name, java.util.function.Supplier<java.lang.Object> getter, java.util.function.Consumer<java.lang.Object> setter)
      Registers the functors for a property.
      void setState​(ServiceState state)
      Changes the state.
      void switchTo​(java.lang.String targetId)
      Switches to an interface-compatible service.
      void update​(java.net.URI location)
      Updates the service by the service in the given location.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • getters

        private java.util.Map<java.lang.String,​java.util.function.Supplier<java.lang.Object>> getters
      • setters

        private java.util.Map<java.lang.String,​java.util.function.Consumer<java.lang.Object>> setters
      • operations

        private java.util.Map<java.lang.String,​java.util.function.Function<java.lang.Object[],​java.lang.Object>> operations
    • Constructor Detail

      • ServiceStub

        public ServiceStub​(de.iip_ecosphere.platform.support.aas.InvocablesCreator iCreator,
                           java.lang.String serviceId)
        Creates the setup and registers the operations.
        Parameters:
        iCreator - the AAS invocables creator
        serviceId - the service id to create the qualified names via ServiceMapper.getQName(Service, String)
    • Method Detail

      • registerProperty

        private void registerProperty​(java.lang.String name,
                                      java.util.function.Supplier<java.lang.Object> getter,
                                      java.util.function.Consumer<java.lang.Object> setter)
        Registers the functors for a property.
        Parameters:
        name - the (unqualified) name of the property
        getter - the getter functor
        setter - the setter functor
      • registerOperation

        private void registerOperation​(java.lang.String name,
                                       java.util.function.Function<java.lang.Object[],​java.lang.Object> operation)
        Registers the functor for an operation.
        Parameters:
        name - the (unqualified) name of the property
        operation - the operation functor
      • getGetter

        public java.util.function.Supplier<java.lang.Object> getGetter​(java.lang.String name)
        Returns the getter functor for a given property.
        Parameters:
        name - the (unqualified) property name
        Returns:
        the functor, may be null for none
      • getSetter

        public java.util.function.Consumer<java.lang.Object> getSetter​(java.lang.String name)
        Returns the setter functor for a given property.
        Parameters:
        name - the (unqualified) property name
        Returns:
        the functor, may be null for none
      • getOperation

        public java.util.function.Function<java.lang.Object[],​java.lang.Object> getOperation​(java.lang.String name)
        Returns the functor for a given operation.
        Parameters:
        name - the (unqualified) operation name
        Returns:
        the functor, may be null for none
      • convertGetterResult

        private <T> T convertGetterResult​(java.lang.String getId,
                                          T dflt,
                                          java.util.function.Function<java.lang.Object,​T> conversion)
        Converts a getter result.
        Type Parameters:
        T - the target type of the value
        Parameters:
        getId - the getter ID
        dflt - the default value to return if a conversion is not possible, e.g., response is null
        conversion - the conversion operation
        Returns:
        the converted value or dflt
      • convertGetterResultToString

        private java.lang.String convertGetterResultToString​(java.lang.String getId)
        Returns the result of the specified getter converted to string with default value "".
        Parameters:
        getId - the getter ID
        Returns:
        the converted value
        See Also:
        convertGetterResult(String, Object, Function)
      • convertToEnumSafe

        public static <T extends java.lang.Enum<T>> T convertToEnumSafe​(java.lang.Object val,
                                                                        T dflt,
                                                                        java.lang.Class<T> cls)
        Converts an object val to an enum value without throwing exceptions.
        Type Parameters:
        T - the enum value type
        Parameters:
        val - the value to be converted, may be null, a non-matching string, etc.
        dflt - the default value if the conversion cannot be applied
        cls - the enum type
        Returns:
        the converted value or dflt
      • convertGetterResultToEnum

        private <T extends java.lang.Enum<T>> T convertGetterResultToEnum​(java.lang.String getId,
                                                                          T dflt,
                                                                          java.lang.Class<T> cls)
        Returns the result of the specified getter converted to the given enum type.
        Type Parameters:
        T - the enum value type
        Parameters:
        getId - the getter ID
        dflt - the default value if the conversion fails
        cls - the enum type
        Returns:
        the converted value or dflt
        See Also:
        convertGetterResult(String, Object, Function), convertToEnumSafe(Object, Enum, Class)
      • getName

        public java.lang.String getName()
        Description copied from interface: Service
        The name of the service.
        Specified by:
        getName in interface Service
        Returns:
        the name
      • getVersion

        public de.iip_ecosphere.platform.support.iip_aas.Version getVersion()
        Description copied from interface: Service
        The version of the service.
        Specified by:
        getVersion in interface Service
        Returns:
        the version
      • getDescription

        public java.lang.String getDescription()
        Description copied from interface: Service
        The description of the service.
        Specified by:
        getDescription in interface Service
        Returns:
        the description, may be empty
      • getState

        public ServiceState getState()
        Description copied from interface: ServiceBase
        Returns the state the service is currently in. [R4c]
        Specified by:
        getState in interface ServiceBase
        Returns:
        the state
      • isDeployable

        public boolean isDeployable()
        Description copied from interface: Service
        Returns whether the service is deployable in distributable manner or fixed in deployment location.
        Specified by:
        isDeployable in interface Service
        Returns:
        true for deployable, false for fixed
      • isTopLevel

        public boolean isTopLevel()
        Description copied from interface: Service
        Returns whether the service is a top-level service.
        Specified by:
        isTopLevel in interface Service
        Returns:
        true for top-level, false for nested
      • getKind

        public ServiceKind getKind()
        Description copied from interface: Service
        Returns the service kind.
        Specified by:
        getKind in interface Service
        Returns:
        the service kind
      • setState

        public void setState​(ServiceState state)
                      throws java.util.concurrent.ExecutionException
        Description copied from interface: ServiceBase
        Changes the state. [R133c]
        Specified by:
        setState in interface ServiceBase
        Parameters:
        state - the new state
        Throws:
        java.util.concurrent.ExecutionException - if changing the state fails for some reason
      • migrate

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

        public void update​(java.net.URI location)
                    throws java.util.concurrent.ExecutionException
        Description copied from interface: Service
        Updates the service by the service in the given location. This operation is responsible for stopping the running service (if needed), replacing it, starting the new service.
        Specified by:
        update in interface Service
        Parameters:
        location - the location of the new service, e.g., an URL
        Throws:
        java.util.concurrent.ExecutionException - if the given service cannot be updated for some reason, e.g., because the replacement service is not an evolved version of the running service
      • switchTo

        public void switchTo​(java.lang.String targetId)
                      throws java.util.concurrent.ExecutionException
        Description copied from interface: Service
        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:
        switchTo in interface Service
        Parameters:
        targetId - the id of the target service
        Throws:
        java.util.concurrent.ExecutionException - if switching the service cannot be performed for some reason
      • activate

        public void activate()
                      throws java.util.concurrent.ExecutionException
        Description copied from interface: Service
        Activates the service. [adaptation]
        Specified by:
        activate in interface Service
        Throws:
        java.util.concurrent.ExecutionException - in case that activating fails, e.g., because the service is already active
      • passivate

        public void passivate()
                       throws java.util.concurrent.ExecutionException
        Description copied from interface: Service
        Passivates the service. [adaptation]
        Specified by:
        passivate in interface Service
        Throws:
        java.util.concurrent.ExecutionException - in case that passivating fails, e.g., because the service is already passive
      • reconfigure

        public void reconfigure​(java.util.Map<java.lang.String,​java.lang.String> values)
                         throws java.util.concurrent.ExecutionException
        Description copied from interface: Service
        Reconfigures the service. [adaptation]
        Specified by:
        reconfigure in interface Service
        Parameters:
        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:
        java.util.concurrent.ExecutionException - if reconfiguration fails