def step_impl(context):
    """
    :type context behave.runner.Context
    :type SERVICE_ADMIN str
    :type SERVICE_PWD str
    """

    # Recover a Token
    user = context.config["env_data"]["users"]["user_1"]["user_name"]
    password = context.config["env_data"]["users"]["user_1"]["user_password"]
    service = context.config["env_data"]["users"]["user_1"]["user_service"]
    subservice = None

    context.admin_token = ks_get_token(context,
                                       service=service,
                                       user=user,
                                       password=password,
                                       subservice=subservice
                                       )

    print ("\n#>> Admin Token to use: {} \n".format(context.admin_token))
def happy_path_retrieve_token(context, SERVICE_ADMIN, SERVICE_PWD):
    """
    :type context behave.runner.Context
    :type SERVICE_ADMIN str
    :type SERVICE_PWD str
    """

    # Recover a Token
    context.user = SERVICE_ADMIN
    context.password = SERVICE_PWD

    if "service" in context and "servicepath" in context:
        # context.service = context.service
        context.subservice = context.servicepath
    else:
        print ("\n #>> Token data needed FAIL: {} \n".format(context.service))
        return False

    context.token = ks_get_token(context,
                                 service=context.service,
                                 user=context.user,
                                 password=context.password)

    print ("\n #>> Token to use: {} \n".format(context.token))
def step_impl(context, REQUEST, ACTION):
    """
    :type context behave.runner.Context
    :type REQUEST str
    :type ACTION str
    """

    if REQUEST == "ENTITY" and ACTION == "CREATE":
        # create an CB sample entity
        cb = CbNgsi10Utils(protocol=context.config["components"]["CB"]["protocol"],
                           instance=context.config["components"]["CB"]["instance"],
                           port=context.config["components"]["CB"]["port"],
                           service=context.service,
                           subservice=context.subservice)

        context.r = cb_sample_entity_create(cb)


    elif REQUEST == "ENTITY" and ACTION == "GET":
        # recover a sample entity
        cb = CbNgsi10Utils(protocol=context.config["components"]["CB"]["protocol"],
                           instance=context.config["components"]["CB"]["instance"],
                           port=context.config["components"]["CB"]["port"],
                           service=context.service,
                           subservice=context.subservice)

        context.r = cb_sample_entity_recover(cb)


    elif REQUEST == "TOKEN" and ACTION == "GET":

        # Recover a Token
        context.token_header = ks_get_token(context)
        print ("\n #>> Token to use: {}".format(context.token_header))

        if context.r_ks.status_code == 200:
            ks_content_json = json.loads(context.r_ks.content)
            context.token_id = ks_content_json["token"]["user"]["domain"]["id"]
            print ("Token returned: {}".format(context.token_id))

        # add response to next step validation
        context.r = context.r_ks

    elif REQUEST == "SERVICES" and ACTION == "GET":
        # Recover created services (if any)
        iota_url = context.config["components"]["IOTA"]["protocol"] + "://" + \
                   context.config["components"]["IOTA"]["instance"] + ":" + \
                   context.config["components"]["IOTA"]["port"] + "/iot"

        # if it is the node iota update the url
        if context.config['components']["IOTA"]['iota_type'] == "node":
            iota_url = iota_url + "/agents/default"

        # Recover a Token
        context.token_header = ks_get_token(context)
        print ("\n #>> Token to use: {}".format(context.token_header))

        headers = {
            'content-type': "application/json",
            'accept': "application/json",
            'fiware-service': context.service,
            'fiware-servicepath': context.subservice
        }

        # Generate a iota object
        context.iota = Rest_Utils_IoTA(server_root=iota_url, server_root_secure=iota_url)

        # Add the token to the headers
        headers.update({'X-Auth-Token': context.token_header})
        context.r = context.iota.get_listServices(headers=headers)
        print (context.r.content)

        eq_(200, context.r.status_code, "ERROR: Service request IOTA failed: {}".format(context.r.status_code))
        iota_content_json = json.loads(context.r.content)

        # show returned response
        __logger__.debug("IOTA returns {} ".format(iota_content_json))

        if context.config['components']["IOTA"]['iota_type'] == "node":
            service = iota_content_json["service"]
            print ("Service: {}".format(service))
        else:
            for service in iota_content_json["services"]:
                print ("Service: {}".format(service))

            assert_greater_equal("1", iota_content_json["count"],
                                 'INCORRECT amount of services'
                                 '\n Services number Expected IS >= {} \n Received: {}'.format("1",
                                                                                               iota_content_json[
                                                                                                   "count"]))

            if iota_content_json["count"] >= 0:
                for service in iota_content_json["services"]:
                    if service["service"] == context.service:
                        print ("#> Service_name Found: {}".format(service["service"]))
                        break
                    else:
                        __logger__.debug(
                                "#> Service returned but does not match with headers: {}".format(service["service"]))

    elif REQUEST == "PROTOCOLS" and ACTION == "GET":
        # Recover created services (if any)
        iotm_url = context.config["components"]["IOTM"]["protocol"] + "://" + \
                   context.config["components"]["IOTM"]["instance"] + ":" + \
                   context.config["components"]["IOTM"]["port"] + "/iot/protocols?detailed=on"

        # Recover a Token
        context.token_header = ks_get_token(context)
        print ("\n #>> Token to use: {}".format(context.token_header))

        # Generate a iotM object
        context.iota = Rest_Utils_IoTA(server_root=iotm_url, server_root_secure=iotm_url)

        # Add the token to the headers
        headers = {
            'content-type': "application/json",
            'accept': "application/json",
            'fiware-service': context.service,
            'fiware-servicepath': context.subservice}

        headers.update({'X-Auth-Token': context.token_header})

        context.r = requests.get(iotm_url, headers=headers)
        iotm_content_json = json.loads(context.r.content)
        print ("IOTM returns {} ".format(iotm_content_json))

        # show returned response
        __logger__.debug("IOTM returns {} ".format(iotm_content_json))
        for protocol in iotm_content_json["protocols"]:
            __logger__.debug("Protocol: {}".format(protocol))

        eq_(200, context.r.status_code)

        assert_greater_equal("0", iotm_content_json["count"],
                             'INCORRECT amount of protocols'
                             '\n Protocols number Expected IS >= {} \n Received: {}'.format("0",
                                                                                            iotm_content_json["count"]))
        if iotm_content_json["count"] >= 0:
            for protocol in iotm_content_json["protocols"]:
                print ("#> Protocol_name Found: {}".format(protocol["protocol"]))

    elif REQUEST == "RULES" and ACTION == "GET":
        # Recover created services (if any)
        cep_url = context.config["components"]["CEP"]["protocol"] + "://" + \
                  context.config["components"]["CEP"]["instance"] + ":" + \
                  context.config["components"]["CEP"]["port"] + \
                  context.config["components"]["CEP"]["path_rules"]

        # Default headers
        headers = {
            'content-type': "application/json",
            'accept': "application/json",
            'fiware-service': context.service
        }

        # Recover the rules
        context.r = requests.get(cep_url, headers=headers)

        cep_rules_content_json = json.loads(context.r.content)
        # print ("CEP returns {} ".format(cep_rules_content_json))

        assert_greater_equal("0", cep_rules_content_json["count"],
                             'INCORRECT amount of rules'
                             '\n Protocols number Expected IS >= {} \n Received: {}'.format("0",
                                                                                            cep_rules_content_json[
                                                                                                "count"]))

        # show returned response
        __logger__.debug("CEP returns {} ".format(cep_rules_content_json))
        print ("\n")
        for rule in cep_rules_content_json["data"]:
            __logger__.debug("Rule: {}".format(rule["name"]))
            print ("#> Rule: {} | Active: {} | Card1: {} | Card2: {} ".format(rule["name"], rule["active"],
                                                                              rule["cards"][0]["type"],
                                                                              rule["cards"][1]["type"]))

    else:
        __logger__.error("No request configured")