Skip to content

Commit

Permalink
Prepare device monitring workflow (#157)
Browse files Browse the repository at this point in the history
Co-authored-by: Jozef Volak <[email protected]>
  • Loading branch information
Jozefiel and Jozef Volak authored Jul 31, 2023
1 parent 6722c57 commit 007eca6
Show file tree
Hide file tree
Showing 5 changed files with 243 additions and 0 deletions.
63 changes: 63 additions & 0 deletions demo-workflows/workers/inventory_worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -656,6 +656,50 @@ def add_netconf_device(task):
'logs': []}


def add_any_device(task):

body = task['inputData']['mount_body']
label_names = "" if task['inputData']['labels'] is None else task['inputData']['labels'].replace(" ", "")

variables = {'input': {
"name": task['inputData']['device_id'],
"zoneId": get_zone_id(task['inputData']['uniconfig_zone']),
"serviceState": task['inputData']["service_state"],
"mountParameters": str(body).replace("'", '"'),
}}

if len(label_names)>0:
labels_id_name = get_label_id()
label_ids=[]

# check if all input labels exist
for label_name in label_names.split(","):
label_id=labels_id_name.get(label_name)
if label_id == None:
label_id, error = create_label(label_name)
if error is not NoneType:
return {'status': 'FAILED', 'output': {'url': inventory_url_base, 'response_code': 404, 'response_body': error},
'logs': []}
label_ids.append(label_id)

variables['input']['labelIds'] = label_ids

response = execute_inventory(add_device_template, variables)

if response.get('errors'):
return {'status': 'FAILED', 'output': {'url': inventory_url_base, 'response_code': 404, 'response_body': response['errors'][0]['message']},
'logs': []}

body = {
"id": response['data']['addDevice']['device']['id'],
"name": response['data']['addDevice']['device']['name'],
"isInstalled": response['data']['addDevice']['device']['isInstalled']
}

return {'status': 'COMPLETED', 'output': {'url': inventory_url_base, 'response_code': 200, 'response_body': body},
'logs': []}


def start(cc):
print('Starting Inventory workers')

Expand Down Expand Up @@ -866,3 +910,22 @@ def start(cc):
"response_body"
]
}, add_netconf_device)


cc.register('INVENTORY_add_device', {
"description": '{"description": "add device to inventory database", "labels": ["BASICS","MAIN","INVENTORY","NETCONF"]}',
"responseTimeoutSeconds": 3600,
"timeoutSeconds": 3600,
"inputKeys": [
"device_id",
"mount_body",
"labels",
"uniconfig_zone",
"service_state",
],
"outputKeys": [
"url",
"response_code",
"response_body"
]
}, add_any_device)
2 changes: 2 additions & 0 deletions demo-workflows/workers/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ def _register_workers(conductor) -> None:
import lldp_identification_worker
import influx_db
import topology_discovery_worker
import schellar_worker
# import vll_worker
# import vll_service_worker
# import vpls_worker
Expand All @@ -97,6 +98,7 @@ def _register_workers(conductor) -> None:
lldp_identification_worker.start(conductor)
influx_db.start(conductor)
topology_discovery_worker.start(conductor)
schellar_worker.start(conductor)
# vll_worker.start(cc)
# vll_service_worker.start(cc)
# vpls_worker.start(cc)
Expand Down
63 changes: 63 additions & 0 deletions demo-workflows/workers/schellar_worker.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import copy
from frinx_conductor_workers.frinx_rest import conductor_headers
import json

import requests

# Templates
create_schedule_tmpl = {
"name": None,
"workflowName": None,
"workflowVersion": None,
"cronString": None,
"enabled": None,
"parallelRuns": False,
"workflowContext": None,
"correlationId": 'network-admin',
"taskToDomain": None
}

SCHELLAR_URL = 'http://workflow-proxy:8088/schedule/'


def create_schedule(task):
body = copy.deepcopy(create_schedule_tmpl)

body['name'] = task['inputData']['workflow_name'] + ":" + task['inputData']['workflow_version']
body['workflowName'] = task['inputData']['workflow_name']
body['workflowVersion'] = str(task['inputData']['workflow_version'])
body['cronString'] = str(task['inputData']['cron'])
body['enabled'] = bool(task['inputData']['enabled'])

if task['inputData']['workflow_context'] is not None:
body['workflowContext'] = task['inputData']['workflow_context']

response = requests.put(SCHELLAR_URL + body['name'], data=json.dumps(body), headers=conductor_headers)
status = 'FAILED'
if response.ok:
status = 'COMPLETED'

return {'status': status, 'output': {'url': SCHELLAR_URL, 'response_code': response.status_code, 'response_body': {}}, 'logs': []}



def start(cc):
print('Starting Inventory workers')

cc.register('SCHELLAR_create_schedule', {
"description": '{"description": "create simple schedule", "labels": ["BASICS","MAIN","SCHELLAR"]}',
"responseTimeoutSeconds": 60,
"timeoutSeconds": 60,
"inputKeys": [
"workflow_name",
"workflow_version",
"enabled",
"cron",
"workflow_context"
],
"outputKeys": [
"url",
"response_code",
"response_body"
]
}, create_schedule)
42 changes: 42 additions & 0 deletions demo-workflows/workflows/Add_device.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"name": "Add_device",
"description": "{\"description\": \"Add device to device inventory and install\", \"labels\": [\"BASICS\",\"CLI\",\"INVENTORY\",\"MAIN\"]}",
"version": 1,
"tasks": [
{
"name": "INVENTORY_add_device",
"taskReferenceName": "INVENTORY_add_device_1",
"inputParameters": {
"device_id": "${workflow.input.device_id}",
"mount_body": "${workflow.input.mount_body}",
"labels": "${workflow.input.labels}",
"uniconfig_zone": "${workflow.input.uniconfig_zone}",
"service_state": "${workflow.input.service_state}"
},
"type": "SIMPLE",
"optional": true
},
{
"name": "INVENTORY_install_device_by_name",
"taskReferenceName": "INVENTORY_install_device_by_name_1",
"inputParameters": {
"device_name": "${workflow.input.device_id}"
},
"type": "SIMPLE"
}
],
"inputParameters": [
"{\"device_id\": {\"value\": \"\", \"description\": \"Unique device name\", \"type\": \"string\", \"options\": null}}",
"{\"mount_body\": {\"value\": \"\", \"description\": \"Device mount body\", \"type\": \"textarea\", \"options\": null}}",
"{\"labels\": {\"value\": \"\", \"description\": \"Device labels\", \"type\": \"string\", \"options\": null}}",
"{\"uniconfig_zone\": {\"value\": \"uniconfig\", \"description\": \"Uniconfig zone\", \"type\": \"string\", \"options\": null}}",
"{\"service_state\": {\"value\": \"IN_SERVICE\", \"description\": \"Current service state\", \"type\": \"select\", \"options\": [\"IN_SERVICE\", \"PLANNING\", \"OUT_OF_SERVICE\"]}}"
],
"outputParameters": {},
"schemaVersion": 2,
"restartable": true,
"variables": {},
"inputTemplate": {},
"hasSchedule": false,
"expectedScheduleName": "Add_device:1"
}
73 changes: 73 additions & 0 deletions demo-workflows/workflows/Schedule_device_monitoring.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
{
"name": "Schedule_device_monitoring",
"description": "{\"description\":\"\",\"labels\":[\"INFLUXDB\"]}",
"version": 1,
"inputParameters": [
"{\"device_name\": {\"value\": \"asr9k\", \"description\": \"Unique Device name\", \"type\": \"string\", \"options\": null}}",
"{\"mount_body\": {\"value\": \"\", \"description\": \"Installation body compatible with inventory format\", \"type\": \"textarea\", \"options\": null}}",
"{\"periode\": {\"value\": 5, \"description\": \"Time, how often to collect data from the device in minutes\", \"type\": \"int\", \"options\": null}}",
"{\"interface\": {\"value\": \"MgmtEth0/RSP0/CPU0/0\", \"description\": \"Monitored interface\", \"type\": \"string\", \"options\": null}}"
],
"outputParameters": {},
"schemaVersion": 2,
"tasks": [
{
"name": "Add_device",
"taskReferenceName": "Add_deviceRef_yWKj",
"inputParameters": {
"device_id": "${workflow.input.device_name}",
"mount_body": "${workflow.input.mount_body}",
"uniconfig_zone": "uniconfig",
"service_state": "IN_SERVICE",
"optional": true
},
"type": "SUB_WORKFLOW",
"subWorkflowParam": {
"name": "Add_device",
"version": 1
}
},
{
"name": "Create_influxdb_bucket",
"taskReferenceName": "Create_influxdb_bucketRef_IbZN",
"inputParameters": {
"bucket": "devices"
},
"type": "SUB_WORKFLOW",
"subWorkflowParam": {
"name": "Create_influxdb_bucket",
"version": 1
}
},
{
"name": "SCHELLAR_create_schedule",
"taskReferenceName": "SCHELLAR_create_scheduleRefName_QQtR",
"inputParameters": {
"workflow_name": "Device_interface_monitoring",
"workflow_version": "1",
"enabled": true,
"cron": "*/${workflow.input.periode} * * * *",
"workflow_context": {
"interface-name": "${workflow.input.interface}",
"node-id": "${workflow.input.device_name}"
}
},
"type": "SIMPLE"
},
{
"name": "SCHELLAR_create_schedule",
"taskReferenceName": "SCHELLAR_create_scheduleRefName_e0CN",
"inputParameters": {
"workflow_name": "Device_memory_monitoring",
"workflow_version": "1",
"enabled": true,
"cron": "*/${workflow.input.periode} * * * *",
"workflow_context": {
"interface-name": "${workflow.input.interface}",
"node-id": "${workflow.input.device_name}"
}
},
"type": "SIMPLE"
}
]
}

0 comments on commit 007eca6

Please sign in to comment.