Exemplo n.º 1
0
    def connect(self, application_name, **kwargs):
        network_application = NetworkApplication.get_instance()
        network_application.name = application_name
        self.network_element = network_application.get_network_element(
            self.element_hostname)
        if self.network_element is None:
            self.logger.error("Failed to get network element")
            sys.exit(1)
        self.logger.info("We have a NetworkElement : " +
                         self.network_element.__str__())

        session_config = SessionConfig(
            SessionConfig.SessionTransportMode.TLS)  #default is TLS

        session_config.eventQueueSize = self.properties["eventQueueSize"]
        session_config.eventThreadPool = self.properties["eventThreadPool"]
        session_config.eventDropMode = self.properties["eventDropMode"]
        session_config.keepAliveIdleTime = self.properties["keepAliveIdleTime"]
        session_config._keepAliveInterval = self.properties[
            "keepAliveInterval"]
        session_config.keepAliveRetryCount = self.properties[
            "keepAliveRetryCount"]
        session_config.reconnectTimer = self.properties["reconnectTimer"]

        session_config.ca_certs = self.root_cert_path

        self.session_handle = self.network_element.connect(
            self.username, self.password, session_config)
        if self.session_handle is None:
            logging.error("Failed to connect to the network element")
            return False

        logging.info("successful in connecting to the network element")
        return True
Exemplo n.º 2
0
    def createSessionConfig(self, mode):
        """
        Creates an instance of SessionConfig with the given transport mode and
        sets the reconnect timer to one minute. All other attributes are set to
        their default values.

        When connecting to a network element, the caller may optionally provide a
        SessionConfig that contains the desired configuration for the resulting
        session. When creating the SessionConfig, the only required attribute is
        the transport mode. TLS is the transport mode used for the end node
        hosting model. TIPC (sometimes referred to as LOCAL) may be used in
        process and blade hosting models. All other attributes are optional,
        and will take on their default values if not explicitly set. To
        demonstrate reconnecting to the session, the reconnect timer will be set
        to one minute.

        @param mode The transport mode used by the connection.
        @return a SessionConfig instance.
        """

        #  START SNIPPET: create_session_config
        #  Construct a SessionConfig instance with the given transport mode.
        config = SessionConfig(mode)
        #  Set the reconnect timer to one minute.
        config.reconnectTimer = 60
        #  The session attributes below this point are set to their default
        #  values.
        #
        #  Set the port to connect to on the network element.
        #  TLS      15002
        #  TIPC     N/A
        #
        if mode.lower() == "tls":
            config.port = config.DEFAULT_PORT
            config.transportMode = SessionConfig.SessionTransportMode.TLS
            config.ca_certs = tutorial.root_cert_path
            config.keyfile = tutorial.client_key_path
            config.certfile = tutorial.client_cert_path
        else:
            #  Not required for TIPC.
            pass
        #  Set the event queue size of the session.
        config.eventQueueSize = config.DEFAULT_EVENT_QUEUE_SIZE
        #  Set the event thread pool size of the session.
        config.eventThreadPool = config.DEFAULT_THREADPOOL_SIZE
        #  Set the event drop mode of the session.
        config.eventDropMode = config.DEFAULT_EVENT_DROP_MODE
        #  Set the keepalive attributes of the session.
        #  Idle time in seconds
        config.keepAliveIdleTime = config.DEFAULT_KEEPALIVE_IDLE_TIME
        #  Interval between keepalives in seconds
        config.keepAliveInterval = config.DEFAULT_KEEPALIVE_INTERVAL
        #  Number of keepalives
        config.keepAliveRetryCount = config.DEFAULT_KEEPALIVE_RETRY_COUNT
        #  END SNIPPET: create_session_config
        config.set_tls_pinning(tutorial.tls_pinning_file,
                               PinningHandler(tutorial.tls_pinning_file))
        return config