Exemple #1
0
def execute_check_cli_id_available(task):
    device_id = task['inputData']['device_id']
    uniconfig_tx_id = task['inputData'][
        'uniconfig_tx_id'] if 'uniconfig_tx_id' in task["inputData"] else ""

    id_url = Template(odl_url_cli_mount).substitute({"id": device_id})

    r = requests.get(id_url,
                     headers=add_uniconfig_tx_cookie(uniconfig_tx_id),
                     auth=odl_credentials)
    response_code, response_json = parse_response(r)

    if response_code != requests.codes.not_found:
        # Mountpoint with such ID already exists
        return {
            'status': 'FAILED',
            'output': {
                'url': id_url,
                'response_code': response_code,
                'response_body': response_json
            },
            'logs': ["Unable to mount device with ID %s" % device_id]
        }
    else:
        return {
            'status': 'COMPLETED',
            'output': {
                'url': id_url,
                'response_code': response_code,
                'response_body': response_json
            },
            'logs': ["Mountpoint with ID %s is available" % device_id]
        }
Exemple #2
0
def replace_config_with_snapshot(task):
    uniconfig_tx_id = task['inputData'][
        'uniconfig_tx_id'] if 'uniconfig_tx_id' in task['inputData'] else ""
    snapshot_body = create_snapshot_request(task)
    r = requests.post(odl_url_uniconfig_replace_config_with_snapshot,
                      data=json.dumps(snapshot_body),
                      headers=add_uniconfig_tx_cookie(uniconfig_tx_id),
                      auth=odl_credentials)
    response_code, response_json = parse_response(r)

    if response_code == requests.codes.ok and response_json["output"][
            "overall-status"] == "complete":
        return {
            'status': 'COMPLETED',
            'output': {
                'url': odl_url_uniconfig_replace_config_with_snapshot,
                'response_code': response_code,
                'response_body': response_json
            },
            'logs': ["Uniconfig replace config with snapshot was successful"]
        }
    else:
        return {
            'status': 'FAILED',
            'output': {
                'url': odl_url_uniconfig_replace_config_with_snapshot,
                'response_code': response_code,
                'response_body': response_json
            },
            'logs': ["Uniconfig replace config with snapshot failed"]
        }
def read_components(task):
    device_id = task['inputData']['device_id']
    uniconfig_tx_id = task['inputData'][
        'uniconfig_tx_id'] if 'uniconfig_tx_id' in task["inputData"] else ""

    id_url = Template(odl_url_components).substitute({"id": device_id})

    r = requests.get(id_url,
                     headers=add_uniconfig_tx_cookie(uniconfig_tx_id),
                     auth=odl_credentials)
    response_code, response_json = parse_response(r)

    if response_code == requests.codes.ok:
        return {
            'status': 'COMPLETED',
            'output': {
                'url': id_url,
                'response_code': response_code,
                'response_body': response_json
            },
            'logs': []
        }
    else:
        return {
            'status': 'FAILED',
            'output': {
                'url': id_url,
                'response_code': response_code,
                'response_body': response_json
            },
            'logs': []
        }
Exemple #4
0
def execute_read_cli_topology_operational(task):
    uniconfig_tx_id = task['inputData']['uniconfig_tx_id'] \
        if 'inputData' in task and 'uniconfig_tx_id' in task['inputData'] else ""

    r = requests.get(odl_url_cli_oper,
                     headers=add_uniconfig_tx_cookie(uniconfig_tx_id),
                     auth=odl_credentials)
    response_code, response_json = parse_response(r)

    if response_code == requests.codes.ok:
        return {
            'status': 'COMPLETED',
            'output': {
                'url': odl_url_cli_oper,
                'response_code': response_code,
                'response_body': response_json
            },
            'logs': []
        }
    else:
        return {
            'status': 'FAILED',
            'output': {
                'url': odl_url_cli_oper,
                'response_code': response_code,
                'response_body': response_json
            },
            'logs': []
        }
Exemple #5
0
def sync_from_network(task):
    uniconfig_tx_id = task['inputData'][
        'uniconfig_tx_id'] if 'uniconfig_tx_id' in task['inputData'] else ""
    r = requests.post(odl_url_uniconfig_sync_from_network,
                      data=json.dumps(create_commit_request(task)),
                      headers=add_uniconfig_tx_cookie(uniconfig_tx_id),
                      auth=odl_credentials)
    response_code, response_json = parse_response(r)

    if response_code == requests.codes.ok and response_json["output"][
            "overall-status"] == "complete":
        return {
            'status': 'COMPLETED',
            'output': {
                'url': odl_url_uniconfig_sync_from_network,
                'response_code': response_code,
                'response_body': response_json
            },
            'logs': ["Uniconfig sync successfull"]
        }
    else:
        return {
            'status': 'FAILED',
            'output': {
                'url': odl_url_uniconfig_sync_from_network,
                'response_code': response_code,
                'response_body': response_json
            },
            'logs': ["Uniconfig sync failed"]
        }
Exemple #6
0
def delete_snapshot(task):
    snapshot_body = copy.deepcopy(delete_snapshot_template)
    snapshot_body["input"]["name"] = task["inputData"]["name"]
    uniconfig_tx_id = task['inputData'][
        'uniconfig_tx_id'] if 'uniconfig_tx_id' in task['inputData'] else ""

    r = requests.post(odl_url_uniconfig_delete_snapshot,
                      data=json.dumps(snapshot_body),
                      headers=add_uniconfig_tx_cookie(uniconfig_tx_id),
                      auth=odl_credentials)
    response_code, response_json = parse_response(r)

    if response_code == requests.codes.ok and response_json["output"][
            "overall-status"] == "complete":
        return {
            'status': 'COMPLETED',
            'output': {
                'url': odl_url_uniconfig_delete_snapshot,
                'response_code': response_code,
                'response_body': response_json
            },
            'logs': ["Uniconfig delete snapshot successful"]
        }
    else:
        return {
            'status': 'FAILED',
            'output': {
                'url': odl_url_uniconfig_delete_snapshot,
                'response_code': response_code,
                'response_body': response_json
            },
            'logs': ["Uniconfig delete snapshot failed"]
        }
Exemple #7
0
def execute_get_cli_journal(task):
    device_id = task['inputData']['device_id']
    uniconfig_tx_id = task['inputData'][
        'uniconfig_tx_id'] if 'uniconfig_tx_id' in task['inputData'] else ""

    id_url = Template(odl_url_cli_read_journal).substitute({"id": device_id})

    r = requests.post(id_url,
                      headers=add_uniconfig_tx_cookie(uniconfig_tx_id),
                      auth=odl_credentials)
    response_code, response_json = parse_response(r)

    if response_code == requests.codes.ok:
        return {
            'status': 'COMPLETED',
            'output': {
                'url': id_url,
                'response_code': response_code,
                'response_body': response_json
            },
            'logs': []
        }
    else:
        return {
            'status': 'FAILED',
            'output': {
                'url': id_url,
                'response_code': response_code,
                'response_body': response_json
            },
            'logs': ["Mountpoint with ID %s, cannot read journal" % device_id]
        }
def export_lldp(task):
    uniconfig_tx_id = task['inputData']['uniconfig_tx_id'] \
        if 'inputData' in task and 'uniconfig_tx_id' in task['inputData'] else ""
    lldp_body = copy.deepcopy(lldp_export_template)

    r = requests.post(export_lldp_url,
                      data=json.dumps(lldp_body),
                      headers=add_uniconfig_tx_cookie(uniconfig_tx_id),
                      auth=odl_credentials)
    response_code, response_json = parse_response(r)

    if response_code == requests.codes.created or response_code == requests.codes.ok:
        return {
            'status': 'COMPLETED',
            'output': {
                'url': export_lldp_url,
                'request_body': lldp_body,
                'response_code': response_code,
                'response_body': response_json
            },
            'logs': ["LLDP topology exported"]
        }
    else:
        return {
            'status': 'FAILED',
            'output': {
                'url': export_lldp_url,
                'request_body': lldp_body,
                'response_code': response_code,
                'response_body': response_json
            },
            'logs': ["Failed to export LLDP topology"]
        }
def read_lldp(task):
    topo_id = task['inputData']['destination-topology']

    uniconfig_tx_id = task['inputData'][
        'uniconfig_tx_id'] if 'uniconfig_tx_id' in task["inputData"] else ""

    id_url = Template(read_lldp_url).substitute({"topo": topo_id})

    r = requests.get(id_url,
                     headers=add_uniconfig_tx_cookie(uniconfig_tx_id),
                     auth=odl_credentials)
    response_code, response_json = parse_response(r)

    if response_code == requests.codes.created or response_code == requests.codes.ok:
        return {
            'status': 'COMPLETED',
            'output': {
                'url': id_url,
                'response_code': response_code,
                'response_body': response_json
            },
            'logs': ["LLDP topology read: %s" % topo_id]
        }
    else:
        return {
            'status': 'FAILED',
            'output': {
                'url': id_url,
                'response_code': response_code,
                'response_body': response_json
            },
            'logs': ["Failed to read LLDP topology: %s" % topo_id]
        }
Exemple #10
0
def execute_check_connected_cli(task):
    device_id = task['inputData']['device_id']
    uniconfig_tx_id = task['inputData'][
        'uniconfig_tx_id'] if 'uniconfig_tx_id' in task["inputData"] else ""

    id_url = Template(odl_url_cli_mount_oper).substitute({"id": device_id})

    r = requests.get(id_url,
                     headers=add_uniconfig_tx_cookie(uniconfig_tx_id),
                     auth=odl_credentials)
    response_code, response_json = parse_response(r)

    if response_code == requests.codes.ok and response_json["node"][0][
            "cli-topology:connection-status"] == "connected":
        return {
            'status': 'COMPLETED',
            'output': {
                'url': id_url,
                'response_code': response_code,
                'response_body': response_json
            },
            'logs': ["Mountpoint with ID %s is connected" % device_id]
        }
    else:
        return {
            'status': 'FAILED',
            'output': {
                'url': id_url,
                'response_code': response_code,
                'response_body': response_json
            },
            'logs': ["Mountpoint with ID %s not yet connected" % device_id]
        }
Exemple #11
0
def execute_mount_cli(task):
    device_id = task['inputData']['device_id']
    uniconfig_tx_id = task['inputData'][
        'uniconfig_tx_id'] if 'uniconfig_tx_id' in task['inputData'] else ""

    mount_body = copy.deepcopy(mount_template)

    mount_body["network-topology:node"]["network-topology:node-id"] = task[
        'inputData']['device_id']
    mount_body["network-topology:node"]["cli-topology:host"] = task[
        'inputData']['host']
    mount_body["network-topology:node"]["cli-topology:port"] = task[
        'inputData']['port']
    mount_body["network-topology:node"]["cli-topology:transport-type"] = task[
        'inputData']['protocol']
    mount_body["network-topology:node"]["cli-topology:device-type"] = task[
        'inputData']['type']
    mount_body["network-topology:node"]["cli-topology:device-version"] = task[
        'inputData']['version']
    mount_body["network-topology:node"]["cli-topology:username"] = task[
        'inputData']['username']
    mount_body["network-topology:node"]["cli-topology:password"] = task[
        'inputData']['password']

    id_url = Template(odl_url_cli_mount).substitute({"id": device_id})

    r = requests.put(id_url,
                     data=json.dumps(mount_body),
                     headers=add_uniconfig_tx_cookie(uniconfig_tx_id),
                     auth=odl_credentials)
    response_code, response_json = parse_response(r)

    if response_code == requests.codes.created or response_code == requests.codes.no_content:
        return {
            'status': 'COMPLETED',
            'output': {
                'url': id_url,
                'request_body': mount_body,
                'response_code': response_code,
                'response_body': response_json
            },
            'logs': ["Mountpoint with ID %s registered" % device_id]
        }
    else:
        return {
            'status': 'FAILED',
            'output': {
                'url': id_url,
                'request_body': mount_body,
                'response_code': response_code,
                'response_body': response_json
            },
            'logs': ["Unable to register device with ID %s" % device_id]
        }
Exemple #12
0
def execute_unmount_netconf(task):
    device_id = task['inputData']['device_id']
    uniconfig_tx_id = task['inputData']['uniconfig_tx_id'] if 'uniconfig_tx_id' in task['inputData'] else ""

    id_url = Template(odl_url_netconf_mount).substitute({"id": device_id})

    r = requests.delete(id_url, headers=add_uniconfig_tx_cookie(uniconfig_tx_id), auth=odl_credentials)
    response_code, response_json = parse_response(r)

    return {'status': 'COMPLETED', 'output': {'url': id_url,
                                              'response_code': response_code,
                                              'response_body': response_json},
            'logs': ["Mountpoint with ID %s removed" % device_id]}
Exemple #13
0
def read_selected_devices(url, devices, uniconfig_tx_id):
    response_code = requests.codes.ok
    response_json = {'topology': [{'node': []}]}
    devices_array = [device.strip() for device in devices.split(',')]
    for d in devices_array:
        r = requests.get(Template(url).substitute({"id": d}),
                         headers=add_uniconfig_tx_cookie(uniconfig_tx_id),
                         auth=odl_credentials)
        response_code, response_json_tmp = parse_response(r)
        response_json['topology'][0]['node'].append(
            response_json_tmp['node'][0])
        if response_code != requests.codes.ok:
            raise "Cannot read device '" + d + "' from uniconfig topology"
    return response_code, response_json
Exemple #14
0
def build_lldp(task):
    topo_id = task['inputData']['destination-topology']

    lldp_body = copy.deepcopy(lldp_build_template)

    lldp_body["input"]["node-aggregation"] = task['inputData'][
        'node-aggregation']
    lldp_body["input"]["link-aggregation"] = task['inputData'][
        'link-aggregation']
    lldp_body["input"]["per-node-read-timeout"] = task['inputData'][
        'per-node-read-timeout']
    lldp_body["input"]["concurrent-read-nodes"] = task['inputData'][
        'concurrent-read-nodes']
    lldp_body["input"]["destination-topology"] = task['inputData'][
        'destination-topology']

    uniconfig_tx_id = task['inputData'][
        'uniconfig_tx_id'] if 'uniconfig_tx_id' in task["inputData"] else ""

    r = requests.post(build_lldp_url,
                      data=json.dumps(lldp_body),
                      headers=add_uniconfig_tx_cookie(uniconfig_tx_id),
                      auth=odl_credentials)
    response_code, response_json = parse_response(r)

    if response_code == requests.codes.created or response_code == requests.codes.ok:
        return {
            'status': 'COMPLETED',
            'output': {
                'url': build_lldp_url,
                'request_body': lldp_body,
                'response_code': response_code,
                'response_body': response_json
            },
            'logs': ["LLDP topology built and stored in %s" % topo_id]
        }
    else:
        return {
            'status': 'FAILED',
            'output': {
                'url': build_lldp_url,
                'request_body': lldp_body,
                'response_code': response_code,
                'response_body': response_json
            },
            'logs': ["Failed to build LLDP topology %s" % topo_id]
        }
Exemple #15
0
def read_all_devices(url, uniconfig_tx_id):
    r = requests.get(url,
                     headers=add_uniconfig_tx_cookie(uniconfig_tx_id),
                     auth=odl_credentials)
    response_code, response_json = parse_response(r)

    actual_nodes = response_json['topology'][0]['node']
    filtered_nodes = []

    for node in actual_nodes:
        if node['cli-topology:connection-status'] == "connected":
            filtered_nodes.append(node)

    response_json['topology'][0].pop('node')
    response_json['topology'][0].update({'node': filtered_nodes})

    return response_code, response_json
Exemple #16
0
def write_structured_data(task):
    device_id = task['inputData']['device_id']
    uri = task['inputData']['uri']
    uri = apply_functions(uri)
    template = task['inputData']['template']
    params = task['inputData']['params']
    params = json.loads(params) if isinstance(
        params, str) else (params if params else {})
    uniconfig_tx_id = task['inputData'][
        'uniconfig_tx_id'] if 'uniconfig_tx_id' in task['inputData'] else ""

    data_json = template if isinstance(
        template, str) else json.dumps(template if template else {})
    data_json = Template(data_json).substitute(params)

    id_url = Template(odl_url_uniconfig_mount).substitute({
        "id": device_id
    }) + "/frinx-uniconfig-topology:configuration" + (uri if uri else "")
    id_url = Template(id_url).substitute(params)

    r = requests.put(id_url,
                     data=data_json,
                     headers=add_uniconfig_tx_cookie(uniconfig_tx_id),
                     auth=odl_credentials)
    response_code, response_json = parse_response(r)

    if response_code == requests.codes.no_content or response_code == requests.codes.created:
        return {
            'status': 'COMPLETED',
            'output': {
                'url': id_url,
                'response_code': response_code,
                'response_body': response_json
            },
            'logs': ["Node with ID %s updated successfully" % device_id]
        }
    else:
        return {
            'status': 'FAILED',
            'output': {
                'url': id_url,
                'response_code': response_code,
                'response_body': response_json
            },
            'logs': ["Unable to update device with ID %s" % device_id]
        }
Exemple #17
0
def execute_and_read_rpc_cli(task):
    device_id = task['inputData']['device_id']
    template = task['inputData']['template']
    params = task['inputData']['params'] if task['inputData']['params'] else {}
    params = params if isinstance(params, dict) else eval(params)
    uniconfig_tx_id = task['inputData'][
        'uniconfig_tx_id'] if 'uniconfig_tx_id' in task["inputData"] else ""

    commands = Template(template).substitute(params)
    exec_body = copy.deepcopy(execute_and_read_template)

    exec_body["input"]["ios-cli:command"] = commands

    id_url = Template(odl_url_cli_mount_rpc).substitute({
        "id": device_id
    }) + "/yang-ext:mount/cli-unit-generic:execute-and-read"

    r = requests.post(id_url,
                      data=json.dumps(exec_body),
                      headers=add_uniconfig_tx_cookie(uniconfig_tx_id),
                      auth=odl_credentials)
    response_code, response_json = parse_response(r)

    if response_code == requests.codes.ok:
        return {
            'status': 'COMPLETED',
            'output': {
                'url': id_url,
                'request_body': exec_body,
                'response_code': response_code,
                'response_body': response_json
            },
            'logs': ["Mountpoint with ID %s configured" % device_id]
        }
    else:
        return {
            'status': 'FAILED',
            'output': {
                'url': id_url,
                'request_body': exec_body,
                'response_code': response_code,
                'response_body': response_json
            },
            'logs': ["Unable to configure device with ID %s" % device_id]
        }
Exemple #18
0
def execute_mount_netconf(task):
    device_id = task['inputData']['device_id']
    uniconfig_tx_id = task['inputData']['uniconfig_tx_id'] if 'uniconfig_tx_id' in task['inputData'] else ""

    mount_body = copy.deepcopy(mount_template)

    mount_body["node"]["node-id"] = task['inputData']['device_id']
    mount_body["node"]["netconf-node-topology:host"] = task['inputData']['host']
    mount_body["node"]["netconf-node-topology:port"] = task['inputData']['port']
    mount_body["node"]["netconf-node-topology:keepalive-delay"] = task['inputData']['keepalive-delay']
    mount_body["node"]["netconf-node-topology:tcp-only"] = task['inputData']['tcp-only']
    mount_body["node"]["netconf-node-topology:username"] = task['inputData']['username']
    mount_body["node"]["netconf-node-topology:password"] = task['inputData']['password']

    if 'uniconfig-native' in task['inputData'] and task['inputData']['uniconfig-native'] is not None:
        mount_body["node"]["uniconfig-config:uniconfig-native-enabled"] = task['inputData']['uniconfig-native']

    if 'blacklist' in task['inputData'] and task['inputData']['blacklist'] is not None:
        mount_body["node"]["uniconfig-config:blacklist"] = {'uniconfig-config:path':[]}
        model_array = [model.strip() for model in task['inputData']['blacklist'].split(',')]
        for model in model_array:
            mount_body["node"]["uniconfig-config:blacklist"]["uniconfig-config:path"].append(model)

    id_url = Template(odl_url_netconf_mount).substitute({"id": device_id})

    r = requests.put(id_url, data=json.dumps(mount_body), headers=add_uniconfig_tx_cookie(uniconfig_tx_id), auth=odl_credentials)
    response_code, response_json = parse_response(r)

    if response_code == requests.codes.created or response_code == requests.codes.no_content:
        return {'status': 'COMPLETED', 'output': {'url': id_url,
                                                  'request_body': mount_body,
                                                  'response_code': response_code,
                                                  'response_body': response_json},
                'logs': ["Mountpoint with ID %s registered" % device_id]}
    else:
        return {'status': 'FAILED', 'output': {'url': id_url,
                                               'request_body': mount_body,
                                               'response_code': response_code,
                                               'response_body': response_json},
                'logs': ["Unable to register device with ID %s" % device_id]}
Exemple #19
0
def execute_check_uniconfig_node_exists(task):
    device_id = task['inputData']['device_id']
    uniconfig_tx_id = task['inputData'][
        'uniconfig_tx_id'] if 'uniconfig_tx_id' in task['inputData'] else ""

    id_url = Template(odl_url_uniconfig_mount).substitute({
        "id": device_id
    }) + "/frinx-uniconfig-topology:connection-status?content=nonconfig"

    r = requests.get(id_url,
                     headers=add_uniconfig_tx_cookie(uniconfig_tx_id),
                     auth=odl_credentials)
    response_code, response_json = parse_response(r)

    if response_code != requests.codes.not_found and response_json[
            "frinx-uniconfig-topology:connection-status"] == "installed":
        # Mountpoint with such ID already exists
        return {
            'status': 'COMPLETED',
            'output': {
                'url': id_url,
                'response_code': response_code,
                'response_body': response_json
            },
            'logs': ["Uniconfig mountpoint with ID %s exists" % device_id]
        }
    else:
        return {
            'status': 'FAILED',
            'output': {
                'url': id_url,
                'response_code': response_code,
                'response_body': response_json
            },
            'logs':
            ["Uniconfig mountpoint with ID %s doesn't exist" % device_id]
        }
def delete_structured_data(task):
    device_id = task['inputData']['device_id']
    uri = task['inputData']['uri']
    uri = uniconfig_worker.apply_functions(uri)
    uniconfig_tx_id = task['inputData'][
        'uniconfig_tx_id'] if 'uniconfig_tx_id' in task["inputData"] else ""

    id_url = Template(odl_url_unified_mount).substitute(
        {"id": device_id}) + "/yang-ext:mount" + (uri if uri else "")

    r = requests.delete(id_url,
                        headers=add_uniconfig_tx_cookie(uniconfig_tx_id),
                        auth=odl_credentials)
    response_code, response_json = parse_response(r)

    if response_code == requests.codes.no_content:
        return {
            'status': 'COMPLETED',
            'output': {
                'url': id_url,
                'request_url': id_url,
                'response_code': response_code,
                'response_body': response_json
            },
            'logs': ["Mountpoint with ID %s updated successfully" % device_id]
        }
    else:
        return {
            'status': 'FAILED',
            'output': {
                'url': id_url,
                'request_url': id_url,
                'response_code': response_code,
                'response_body': response_json
            },
            'logs': ["Unable to update device with ID %s" % device_id]
        }
Exemple #21
0
def read_structured_data(task):
    device_id = task['inputData']['device_id']
    uri = task['inputData']['uri']
    uri = apply_functions(uri)
    uniconfig_tx_id = task['inputData'][
        'uniconfig_tx_id'] if 'uniconfig_tx_id' in task['inputData'] else ""

    id_url = Template(odl_url_uniconfig_mount).substitute({
        "id": device_id
    }) + "/frinx-uniconfig-topology:configuration" + (uri if uri else "")

    r = requests.get(id_url,
                     headers=add_uniconfig_tx_cookie(uniconfig_tx_id),
                     auth=odl_credentials)
    response_code, response_json = parse_response(r)

    if response_code == requests.codes.ok:
        return {
            'status': 'COMPLETED',
            'output': {
                'url': id_url,
                'response_code': response_code,
                'response_body': response_json
            },
            'logs': ["Node with ID %s read successfully" % device_id]
        }
    else:
        return {
            'status': 'FAILED',
            'output': {
                'url': id_url,
                'response_code': response_code,
                'response_body': response_json
            },
            'logs': ["Unable to read device with ID %s" % device_id]
        }
Exemple #22
0
def read_all_devices(url, uniconfig_tx_id):
    r = requests.get(url,
                     headers=add_uniconfig_tx_cookie(uniconfig_tx_id),
                     auth=odl_credentials)
    response_code, response_json = parse_response(r)
    return response_code, response_json