Beispiel #1
0
def mqtt_subscribe():
    # Encapsulates Identity
    identity = Identity(config['broker_root_ca_cert'],
                        config['broker_username'], config['broker_password'],
                        config['edge_system_cert_file'],
                        config['edge_system_key_file'])
    # Encapsulate TLS parameters
    tls_conf = TLSConf(cert_required=config['cert_required'],
                       tls_version=config['tls_version'],
                       cipher=config['cipher'])

    # Create MQTT connection object with required params
    mqtt_conn = MqttDeviceComms(url=config['BrokerIP'],
                                port=config['BrokerPort'],
                                identity=identity,
                                tls_conf=tls_conf,
                                qos_details=None,
                                clean_session=True,
                                keep_alive=config['keep_alive'],
                                enable_authentication=True)

    # Subscribe to channels : "temperature/kitchen" and "temperature/living-room" with preferred QoS level 0, 1 or 2
    # Provide callback function as a parameter for corresponding channel
    mqtt_conn.subscribe(config['MqttChannel1'], 1, callback_kitchen_temp)
    mqtt_conn.subscribe(config['MqttChannel2'], 1, callback_living_room_temp)
Beispiel #2
0
    def run(self, registry):
        import copy
        from liota.dccs.wavefront import Wavefront
        from liota.dcc_comms.mqtt_dcc_comms import MqttDccComms
        from liota.lib.transports.mqtt import MqttMessagingAttributes
        from liota.lib.transports.mqtt import QoSDetails
        from liota.lib.utilities.identity import Identity
        from liota.lib.utilities.tls_conf import TLSConf
        from liota.lib.utilities.offline_buffering import BufferingParams
        from liota.lib.utilities.utility import systemUUID

        # Acquire resources from registry
        # Creating a copy of system object to keep original object "clean"
        edge_system = copy.copy(registry.get("edge_system"))

        # Get values from configuration file
        config_path = registry.get("package_conf")
        config = read_user_config(config_path + '/sampleProp.conf')

        # Initialize DCC object with transport
        offline_buffering = BufferingParams(persistent_storage=True,
                                            queue_size=-1,
                                            data_drain_size=10,
                                            draining_frequency=1)
        identity = Identity(root_ca_cert=config['broker_root_ca_cert'],
                            username=config['broker_username'],
                            password=['broker_password'],
                            cert_file=None,
                            key_file=None)
        # Encapsulate TLS parameters
        tls_conf = TLSConf(config['cert_required'], config['tls_version'],
                           config['cipher'])
        # Encapsulate QoS related parameters
        qos_details = QoSDetails(config['in_flight'], config['queue_size'],
                                 config['retry'])

        #  Connecting to emqtt broker
        client_id = systemUUID().get_uuid(edge_system.name)
        self.wavefront = Wavefront(MqttDccComms(
            edge_system_name=edge_system.name,
            url=config['BrokerIP'],
            port=config['BrokerPort'],
            identity=identity,
            tls_conf=tls_conf,
            qos_details=qos_details,
            client_id=client_id,
            clean_session=True,
            protocol=config['protocol'],
            transport=['transport'],
            conn_disconn_timeout=config['ConnectDisconnectTimeout']),
                                   buffering_params=offline_buffering)

        # Register gateway system
        wavefront_edge_system = self.wavefront.register(edge_system)

        registry.register("wavefront", self.wavefront)
        registry.register("wavefront_edge_system", wavefront_edge_system)
Beispiel #3
0
    def __init__(self, mqtt_cfg, name=None, simulator=None):
        super(MqttSimulator, self).__init__(name=name)
        log.debug("MqttSimulator is initializing")

        broker_ip_port_topic = mqtt_cfg["broker_ip_port_topic"]
        str_list = broker_ip_port_topic.split(':')
        if str_list[0] == "" or str_list[0] == None:
            log.debug("No broker ip is specified!")
            self.broker_ip = "127.0.0.1"
        else:
            self.broker_ip = str(str_list[0])
        if str_list[1] == "" or str_list[1] == None:
            log.debug("No broker port is specified!")
        self.broker_port = int(str_list[1])
        if str_list[2] == "" or str_list[2] == None:
            log.debug("No broker topic is specified!")
        else:
            self.topic = str(str_list[2])
        self.simulator = simulator

        # Encapsulates Identity
        self.identity = Identity(mqtt_cfg['broker_root_ca_cert'],
                                 mqtt_cfg['broker_username'],
                                 mqtt_cfg['broker_password'],
                                 mqtt_cfg['edge_system_cert_file'],
                                 mqtt_cfg['edge_system_key_file'])
        if ((mqtt_cfg['cert_required'] == "None")
                or (mqtt_cfg['cert_required'] == "CERT_NONE")):
            self.tls_conf = None
        else:
            # Encapsulate TLS parameters
            self.tls_conf = TLSConf(mqtt_cfg['cert_required'],
                                    mqtt_cfg['tls_version'],
                                    mqtt_cfg['cipher'])
        # Encapsulate QoS related parameters
        self.qos_details = QoSDetails(mqtt_cfg['in_flight'],
                                      int(mqtt_cfg['queue_size']),
                                      mqtt_cfg['retry'])
        # Create MQTT connection object with required params
        self.mqtt_conn = MqttDeviceComms(url=self.broker_ip,
                                         port=int(self.broker_port),
                                         identity=self.identity,
                                         tls_conf=self.tls_conf,
                                         qos_details=self.qos_details,
                                         clean_session=True,
                                         keep_alive=int(
                                             mqtt_cfg['keep_alive']),
                                         enable_authentication=False)

        log.debug("MqttSimulator is initialized")
        print "MqttSimulator is initialized"
        self.cfg_sets = mqtt_cfg
        self.cnt = 0
        self.flag_alive = True
        self.start()
Beispiel #4
0
    def run(self, registry):
        import copy
        from liota.lib.utilities.identity import Identity
        from liota.dccs.iotcc import IotControlCenter
        from liota.dcc_comms.mqtt_dcc_comms import MqttDccComms
        from liota.dccs.dcc import RegistrationFailure
        from liota.lib.utilities.tls_conf import TLSConf

        # Get values from configuration file
        self.config_path = registry.get("package_conf")
        config = {}
        execfile(self.config_path + '/sampleProp.conf', config)

        # Acquire resources from registry
        # Creating a copy of edge_system object to keep original object "clean"
        edge_system = copy.copy(registry.get("edge_system"))

        #  Encapsulates Identity
        identity = Identity(root_ca_cert=config['broker_root_ca_cert'],
                            username=config['broker_username'],
                            password=config['broker_password'],
                            cert_file=config['edge_system_cert_file'],
                            key_file=config['edge_system_key_file'])

        # Encapsulate TLS parameters
        tls_conf = TLSConf(config['cert_required'], config['tls_version'],
                           config['cipher'])

        # Initialize DCC object with MQTT transport
        self.iotcc = IotControlCenter(
            config['broker_username'], config['broker_password'],
            MqttDccComms(edge_system_name=edge_system.name,
                         url=config['BrokerIP'],
                         port=config['BrokerPort'],
                         identity=identity,
                         tls_conf=tls_conf,
                         enable_authentication=True))

        try:
            # Register edge system (gateway)
            self.iotcc_edge_system = self.iotcc.register(edge_system)
            """
            Use iotcc & iotcc_edge_system as common identifiers
            in the registry to easily refer the objects in other packages
            """
            registry.register("iotcc_mqtt", self.iotcc)
            registry.register("iotcc_mqtt_edge_system", self.iotcc_edge_system)
        except RegistrationFailure:
            print "EdgeSystem registration to IOTCC failed"
        self.iotcc.set_properties(self.iotcc_edge_system,
                                  config['SystemPropList'])
Beispiel #5
0
    def run(self, registry):
        import copy
        from liota.dccs.aws_iot import AWSIoT
        from liota.dcc_comms.mqtt_dcc_comms import MqttDccComms
        from liota.lib.transports.mqtt import QoSDetails
        from liota.lib.utilities.identity import Identity
        from liota.lib.utilities.tls_conf import TLSConf

        # Acquire resources from registry
        # Creating a copy of edge_system object to keep original object "clean"
        edge_system = copy.copy(registry.get("edge_system"))

        # Get values from configuration file
        config_path = registry.get("package_conf")
        config = {}
        execfile(config_path + '/sampleProp.conf', config)
        # Encapsulates Identity
        identity = Identity(root_ca_cert=config['broker_root_ca_cert'],
                            username=None,
                            password=None,
                            cert_file=config['edge_system_cert_file'],
                            key_file=config['edge_system_key_file'])
        # Encapsulate TLS parameters
        tls_conf = TLSConf(config['cert_required'], config['tls_version'],
                           config['cipher'])
        # Encapsulate QoS related parameters
        qos_details = QoSDetails(config['in_flight'], config['queue_size'],
                                 config['retry'])

        #  Connecting to AWSIoT
        #  Publish topic for all Metrics will be 'liota/generated_local_uuid_of_edge_system/request'
        #  Create and pass custom MqttMessagingAttributes object to MqttDccComms to have custom topic
        self.aws_iot = AWSIoT(MqttDccComms(
            edge_system_name=edge_system.name,
            url=config['BrokerIP'],
            port=config['BrokerPort'],
            identity=identity,
            tls_conf=tls_conf,
            qos_details=qos_details,
            clean_session=True,
            userdata=config['userdata'],
            protocol=config['protocol'],
            transport=['transport'],
            conn_disconn_timeout=config['ConnectDisconnectTimeout']),
                              enclose_metadata=True)

        # Register edge system (gateway)
        aws_iot_edge_system = self.aws_iot.register(edge_system)

        registry.register("aws_iot", self.aws_iot)
        registry.register("aws_iot_edge_system", aws_iot_edge_system)
    def setUp(self):
        """
        Setup all required parameters for AmqpDccComms tests
        :return: None
        """
        # ConfigParser to parse init file
        self.config = ConfigParser()
        self.uuid_file = read_liota_config('UUID_PATH', 'uuid_path')

        # Broker details
        self.url = "Broker-IP"
        self.port = "Broker-Port"
        self.amqp_username = "******"
        self.amqp_password = "******"
        self.enable_authentication = True
        self.transport = "tcp"
        self.connection_disconnect_timeout_sec = 2

        # EdgeSystem name
        self.edge_system = Dell5KEdgeSystem("TestGateway")

        # TLS configurations
        self.root_ca_cert = "/etc/liota/amqp/conf/ca.crt"
        self.client_cert_file = "/etc/liota/amqp/conf/client.crt"
        self.client_key_file = "/etc/liota/amqp/conf/client.key"
        self.cert_required = "CERT_REQUIRED"
        self.tls_version = "PROTOCOL_TLSv1"
        self.cipher = None

        # Encapsulate the authentication details
        self.identity = Identity(self.root_ca_cert, self.amqp_username,
                                 self.amqp_password, self.client_cert_file,
                                 self.client_key_file)

        # Encapsulate TLS parameters
        self.tls_conf = TLSConf(self.cert_required, self.tls_version,
                                self.cipher)

        self.send_message = "test-message"

        # Creating messaging attributes
        self.amqp_msg_attr = AmqpPublishMessagingAttributes(
            exchange_name="test_exchange", routing_key=["test"])

        with mock.patch.object(Amqp, '_init_or_re_init') as mocked_init_or_re_init, \
                mock.patch.object(Amqp, "declare_publish_exchange") as mocked_declare_publish_exchange:
            # Instantiate client for DCC communication
            self.client = AmqpDccComms(edge_system_name=self.edge_system.name,
                                       url=self.url,
                                       port=self.port)
Beispiel #7
0
    def run(self, registry):
        import copy
        from liota.dccs.influx import influx
        from liota.dcc_comms.mqtt_dcc_comms import MqttDccComms
        from liota.lib.transports.mqtt import MqttMessagingAttributes
        from liota.lib.transports.mqtt import QoSDetails
        from liota.lib.utilities.identity import Identity
        from liota.lib.utilities.tls_conf import TLSConf

        # Acquire resources from registry
        # Creating a copy of system object to keep original object "clean"
        edge_system = copy.copy(registry.get("edge_system"))

        # Get values from configuration file
        config_path = registry.get("package_conf")
        config = read_user_config(config_path + '/sampleProp.conf')

        # Initialize DCC object with transport
        identity = Identity(root_ca_cert=config['broker_root_ca_cert'],
                            username=config['broker_username'],
                            password=['broker_password'],
                            cert_file=None,
                            key_file=None)
        # Encapsulate TLS parameters
        tls_conf = TLSConf(config['cert_required'], config['tls_version'],
                           config['cipher'])
        # Encapsulate QoS related parameters
        qos_details = QoSDetails(config['in_flight'], config['queue_size'],
                                 config['retry'])

        # Connecting to emqtt broker
        self.influx = influx(
            MqttDccComms(
                edge_system_name=edge_system.name,
                url=config['BrokerIP'],
                port=config['BrokerPort'],
                identity=identity,
                tls_conf=tls_conf,
                qos_details=qos_details,
                clean_session=True,
                protocol=config['protocol'],
                transport=['transport'],
                conn_disconn_timeout=config['ConnectDisconnectTimeout']))

        # Register gateway system
        influx_edge_system = self.influx.register(edge_system)

        registry.register("influx", self.influx)
        registry.register("influx_edge_system", influx_edge_system)
Beispiel #8
0
# along with the sensor data payload of a Metric.
#
# This example showcases publishing Metrics using (a) and enclose_metadata
# -----------------------------------------------------------------------------------------------------------------

if __name__ == '__main__':
    #  Creating EdgeSystem
    edge_system = Dell5KEdgeSystem(config['EdgeSystemName'])
    #  Encapsulates Identity
    identity = Identity(root_ca_cert=config['broker_root_ca_cert'],
                        username=None,
                        password=None,
                        cert_file=config['edge_system_cert_file'],
                        key_file=config['edge_system_key_file'])
    # Encapsulate TLS parameters
    tls_conf = TLSConf(config['cert_required'], config['tls_version'],
                       config['cipher'])
    # Encapsulate QoS related parameters
    qos_details = QoSDetails(config['in_flight'], config['queue_size'],
                             config['retry'])

    #  Connecting to AWSIoT
    #  AWSIoT broker doesn't support session persistence.  So, always use "clean_session=True"
    #  Publish topic for all Metrics will be 'liota/generated_local_uuid_of_edge_system/request'
    aws = AWSIoT(MqttDccComms(
        edge_system_name=edge_system.name,
        url=config['BrokerIP'],
        port=config['BrokerPort'],
        identity=identity,
        tls_conf=tls_conf,
        qos_details=qos_details,
        clean_session=True,
Beispiel #9
0
    def run(self, registry):
        import copy
        from liota.lib.utilities.identity import Identity
        from liota.dccs.iotcc import IotControlCenter
        from liota.dcc_comms.mqtt_dcc_comms import MqttDccComms
        from liota.lib.transports.mqtt import MqttMessagingAttributes
        from liota.dccs.dcc import RegistrationFailure
        from liota.lib.utilities.tls_conf import TLSConf

        # Acquire resources from the registry
        # Creating a copy of edge_system object to keep original object "clean"
        edge_system = copy.copy(registry.get("edge_system"))

        #  Encapsulates Identity
        # Acquire credentials and required certificates from the registry
        identity = Identity(root_ca_cert=registry.get("broker_root_ca_cert"),
                            username=registry.get("broker_username"),
                            password=registry.get("broker_password"),
                            cert_file=registry.get("edge_system_cert_file"),
                            key_file=registry.get("edge_system_key_file"))

        # Encapsulate TLS parameters
        tls_conf = TLSConf(cert_required="CERT_REQUIRED",
                           tls_version="PROTOCOL_TLSv1_2",
                           cipher=None)

        # Initialize DCC object with MQTT transport
        mqtt_msg_attr = MqttMessagingAttributes(
            pub_topic="liota/" + registry.get("broker_username") + "/request",
            sub_topic="liota/" + registry.get("broker_username") + "/response")
        self.iotcc = IotControlCenter(
            MqttDccComms(edge_system_name=edge_system.name,
                         url=registry.get("broker_ip"),
                         port=registry.get("broker_port"),
                         identity=identity,
                         tls_conf=tls_conf,
                         client_id=registry.get("broker_username"),
                         enable_authentication=True,
                         mqtt_msg_attr=mqtt_msg_attr))

        try:
            # Register edge system (gateway)
            self.iotcc_edge_system = self.iotcc.register(edge_system)
            # System Properties has to be set only for the registered edge system before it is stored in the package
            # manager registry, all the devices will internally inherit the the system properties from the
            # registered edge system
            self.iotcc.set_system_properties(self.iotcc_edge_system,
                                             registry.get("system_properties"))
            # Set the properties for edge system as key:value pair, you can also set the location
            # by passing the latitude and longitude as a property in the user package
            self.iotcc.set_properties(self.iotcc_edge_system, {
                "key1": "value1",
                "key2": "value2"
            })
            registry.register("iotcc_mqtt", self.iotcc)
            # Store the registered edge system object in liota package manager registry after the
            # system properties are set for it
            registry.register("iotcc_mqtt_edge_system", self.iotcc_edge_system)

        except RegistrationFailure:
            print "EdgeSystem registration to IOTCC failed"
Beispiel #10
0
    def run(self, registry):
        """
        The execution function of a liota package.

        Establishes connection with IoTControlCenter DCC using MqttDccComms

        :param registry: the instance of ResourceRegistryPerPackage of the package
        :return:
        """
        import copy
        import time
        from liota.lib.utilities.identity import Identity
        from liota.dccs.iotcc import IotControlCenter
        from liota.dcc_comms.mqtt_dcc_comms import MqttDccComms
        from liota.lib.transports.mqtt import MqttMessagingAttributes
        from liota.dccs.dcc import RegistrationFailure
        from liota.lib.utilities.tls_conf import TLSConf

        # Acquire resources from the registry
        # Creating a copy of edge_system object to keep original object "clean"
        edge_system = copy.copy(registry.get("edge_system"))

        #  Encapsulates Identity
        # Acquire credentials and required certificates from the registry
        identity = Identity(root_ca_cert=registry.get("broker_root_ca_cert"), username=registry.get("broker_username"),
                            password=registry.get("broker_password"),
                            cert_file=registry.get("edge_system_cert_file"),
                            key_file=registry.get("edge_system_key_file"))

        # Encapsulate TLS parameters
        tls_conf = TLSConf(cert_required="CERT_REQUIRED", tls_version="PROTOCOL_TLSv1_2", cipher=None)

        # Initialize DCC object with MQTT transport
        mqtt_msg_attr = MqttMessagingAttributes(pub_topic="liota/" + registry.get("broker_username") + "/request",
                                                sub_topic="liota/" + registry.get("broker_username") + "/response")
        self.iotcc = IotControlCenter(MqttDccComms(edge_system_name=edge_system.name,
                                                   url=registry.get("broker_ip"), port=registry.get("broker_port"),
                                                   identity=identity,
                                                   tls_conf=tls_conf, client_id=registry.get("broker_username"),
                                                   enable_authentication=True, mqtt_msg_attr=mqtt_msg_attr))

        try:
            # Register edge system (gateway)
            self.iotcc_edge_system = self.iotcc.register(edge_system)
            # Set the properties for edge system as key:value pair, you can also set the location
            # by passing the latitude and longitude as a property in the user package
            # If the set_properties or register call fails due to DCC_Comms Publish exception
            # the optional retry mechanism can be implemented in the following way
            attempts = 0
            max_retry_attempts = 3
            while attempts < max_retry_attempts:
                try:
                    # Register edge system (gateway)
                    self.iotcc.set_properties(self.iotcc_edge_system, {"key1": "value1", "key2": "value2"})
                    break
                except Exception:
                    # In the third attempt if get exception raise it
                    if attempts == max_retry_attempts:
                        raise
                    attempts += 1
                    # The sleep time before re-trying depends on the infrastructure requirement of broker to restart
                    # It can be modified or removed as per the infrastructure requirement
                    time.sleep(5)
            registry.register("iotcc_mqtt", self.iotcc)
            # Store the registered edge system object in liota package manager registry after the
            registry.register("iotcc_mqtt_edge_system", self.iotcc_edge_system)

        except RegistrationFailure:
            print "EdgeSystem registration to IOTCC failed"
Beispiel #11
0
    def run(self, registry):
        """
        The execution function of a liota package.
        Establishes connection with IoTControlCenter DCC using MqttDccComms
        :param registry: the instance of ResourceRegistryPerPackage of the package
        :return:
        """
        import copy
        from liota.lib.utilities.identity import Identity
        from liota.dccs.iotcc import IotControlCenter
        from liota.dcc_comms.mqtt_dcc_comms import MqttDccComms
        from liota.lib.transports.mqtt import MqttMessagingAttributes
        from liota.lib.utilities.tls_conf import TLSConf

        # Acquire resources from the registry
        # Creating a copy of edge_system object to keep original object "clean"
        edge_system = copy.copy(registry.get("edge_system"))

        #  Encapsulates Identity
        # Acquire credentials and required certificates from the registry
        identity = Identity(root_ca_cert=registry.get("broker_root_ca_cert"), username=registry.get("broker_username"),
                            password=registry.get("broker_password"),
                            cert_file=registry.get("edge_system_cert_file"),
                            key_file=registry.get("edge_system_key_file"))

        # Encapsulate TLS parameters
        tls_conf = TLSConf(cert_required="CERT_REQUIRED", tls_version="PROTOCOL_TLSv1_2", cipher=None)

        # Initialize DCC object with MQTT transport
        mqtt_msg_attr = MqttMessagingAttributes(pub_topic="liota/" + registry.get("broker_username") + "/request",
                                                sub_topic="liota/" + registry.get("broker_username") + "/response")

        # Attempts for establishing MQTT Connection
        conn_attempts = 0

        try:
            # Trying to establish MQTT Connection with retry attempts in case of exception
            while conn_attempts <= retry_attempts:
                try:
                    self.iotcc = IotControlCenter(
                        MqttDccComms(edge_system_name=edge_system.name, url=registry.get("broker_ip"),
                                     port=registry.get("broker_port"), identity=identity, tls_conf=tls_conf,
                                     client_id=registry.get("broker_username"), enable_authentication=True,
                                     mqtt_msg_attr=mqtt_msg_attr))
                    break
                except Exception as e:
                    if conn_attempts == retry_attempts:
                        raise
                    conn_attempts += 1
                    log.error('MQTT Connection failed - {0}'.format(str(e)))
                    log.info('Trying MQTT Connection: Attempt - {0}'.format(str(conn_attempts)))
                    time.sleep(mqtt_connection_delay_retries)

            # Attempts for Edge System Registration
            reg_attempts = 0
            # Edge System Registration with retry attempts in case of exception
            while reg_attempts <= retry_attempts:
                try:
                    self.iotcc_edge_system = self.iotcc.register(edge_system)
                    break
                except Exception as e:
                    if reg_attempts == retry_attempts:
                        raise
                    reg_attempts += 1
                    log.error('Exception while registering Edge System- {0}'.format(str(e)))
                    log.info('Trying Edge System {0} Registration: Attempt - {1}'.format(edge_system.name,
                                                                                         str(reg_attempts)))
                    time.sleep(delay_retries)

            registry.register("iotcc_mqtt", self.iotcc)
            registry.register("iotcc_mqtt_edge_system", self.iotcc_edge_system)

            # Attempts for setting edge system properties
            prop_attempts = 0
            # Set multiple properties by passing Dictonary object for Edge System with the retry attempts
            # in case of exceptions
            while prop_attempts < retry_attempts:
                try:
                    self.iotcc.set_properties(self.iotcc_edge_system,
                                              {"Country": "USA-G", "State": "California", "City": "Palo Alto",
                                               "Location": "VMware HQ", "Building": "Promontory H Lab",
                                               "Floor": "First Floor"})
                    break
                except Exception as e:
                    prop_attempts += 1
                    log.error(
                        'Exception while setting Property for Edge System {0} - {1}'.format(edge_system.name, str(e)))
                    log.info('Trying setting properties for Edge System {0}: Attempt - {1}'.format(edge_system.name,
                                                                                                   str(prop_attempts)))
                    time.sleep(delay_retries)

        except Exception:
            log.error("EdgeSystem registration to IOTCC failed even after all the retries, starting connection cleanup")
            # Disconnecting MQTT
            self.iotcc.comms.client.disconnect()
            raise