Illyoung Choi | a9d2c2c | 2019-07-12 13:29:42 -0700 | [diff] [blame^] | 1 | #!/usr/bin/env python3 |
| 2 | |
| 3 | # Copyright 2019-present Open Networking Foundation |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | |
| 17 | """ |
| 18 | Workflow Probe |
| 19 | |
| 20 | This module implements Workflow Probe interface |
| 21 | """ |
| 22 | |
| 23 | import json |
| 24 | import socketio |
| 25 | |
| 26 | from .utils import get_noop_logger, gen_id |
| 27 | from .errors import ClientInputError |
| 28 | |
| 29 | GREETING = 'cord.workflow.ctlsvc.greeting' |
| 30 | |
| 31 | |
| 32 | class Probe(object): |
| 33 | def __init__(self, logger=None, name=None): |
| 34 | self.sio = socketio.Client() |
| 35 | |
| 36 | if logger: |
| 37 | self.logger = logger |
| 38 | else: |
| 39 | self.logger = get_noop_logger() |
| 40 | |
| 41 | if name: |
| 42 | self.name = name |
| 43 | else: |
| 44 | self.name = 'probe_%s' % gen_id() |
| 45 | |
| 46 | # set sio handlers |
| 47 | self.logger.debug('Setting event handlers to Socket.IO') |
| 48 | self.sio.on('connect', self.__on_sio_connect) |
| 49 | self.sio.on('disconnect', self.__on_sio_disconnect) |
| 50 | self.sio.on(GREETING, self.__on_greeting_message) |
| 51 | |
| 52 | self.handlers = { |
| 53 | 'connect': self.__noop_connect_handler, |
| 54 | 'disconnect': self.__noop_disconnect_handler |
| 55 | } |
| 56 | |
| 57 | def set_logger(self, logger): |
| 58 | self.logger = logger |
| 59 | |
| 60 | def get_logger(self): |
| 61 | return self.logger |
| 62 | |
| 63 | def __on_sio_connect(self): |
| 64 | self.logger.debug('connected to the server') |
| 65 | handler = self.handlers['connect'] |
| 66 | if callable(handler): |
| 67 | handler() |
| 68 | |
| 69 | def __noop_connect_handler(self): |
| 70 | self.logger.debug('no-op connect handler') |
| 71 | |
| 72 | def __on_sio_disconnect(self): |
| 73 | self.logger.debug('disconnected from the server') |
| 74 | handler = self.handlers['disconnect'] |
| 75 | if callable(handler): |
| 76 | handler() |
| 77 | |
| 78 | def __noop_disconnect_handler(self): |
| 79 | self.logger.debug('no-op disconnect handler') |
| 80 | |
| 81 | def __on_greeting_message(self, data): |
| 82 | self.logger.debug('received a greeting message from the server') |
| 83 | |
| 84 | def connect(self, url): |
| 85 | """ |
| 86 | Connect to the given url |
| 87 | """ |
| 88 | query_string = 'id=%s&type=probe&name=%s' % (self.name, self.name) |
| 89 | connect_url = '%s?%s' % (url, query_string) |
| 90 | |
| 91 | if not (connect_url.startswith('http://') or connect_url.startswith('https://')): |
| 92 | connect_url = 'http://%s' % connect_url |
| 93 | |
| 94 | self.logger.debug('Connecting to a Socket.IO server (%s)' % connect_url) |
| 95 | self.sio.connect(url=connect_url, transports=['websocket']) |
| 96 | |
| 97 | def disconnect(self): |
| 98 | """ |
| 99 | Disconnect from the server |
| 100 | """ |
| 101 | self.sio.disconnect() |
| 102 | |
| 103 | def get_handlers(self): |
| 104 | return self.handlers |
| 105 | |
| 106 | def set_handlers(self, new_handlers): |
| 107 | for k in self.handlers: |
| 108 | if k in new_handlers: |
| 109 | self.handlers[k] = new_handlers[k] |
| 110 | |
| 111 | def emit_event(self, event, body): |
| 112 | """ |
| 113 | Emit event to Workflow Controller |
| 114 | """ |
| 115 | if event and body: |
| 116 | self.sio.emit(event, body) |
| 117 | else: |
| 118 | self.logger.error( |
| 119 | 'invalid arguments event(%s), body(%s)' % |
| 120 | (event, json.dumps(body)) |
| 121 | ) |
| 122 | raise ClientInputError( |
| 123 | 'invalid arguments event(%s), body(%s)' % |
| 124 | (event, json.dumps(body)) |
| 125 | ) |