Alex Shatov | a39f4e8 | 2018-12-05 15:23:50 -0500 | [diff] [blame^] | 1 | # ============LICENSE_START======================================================= |
| 2 | # Copyright (c) 2018 AT&T Intellectual Property. All rights reserved. |
| 3 | # ================================================================================ |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | # ============LICENSE_END========================================================= |
| 16 | # |
| 17 | # ECOMP is a trademark and service mark of AT&T Intellectual Property. |
| 18 | """mocking for the websocket - for testing of policy-engine""" |
| 19 | |
| 20 | import json |
| 21 | import time |
| 22 | |
| 23 | from policyhandler.policy_consts import POLICY_NAME |
| 24 | from policyhandler.policy_receiver import (LOADED_POLICIES, POLICY_VER, |
| 25 | REMOVED_POLICIES) |
| 26 | |
| 27 | from .mock_policy_engine import MockPolicyEngine |
| 28 | from .mock_settings import Settings |
| 29 | |
| 30 | |
| 31 | class MockWebSocket(object): |
| 32 | """Mock websocket""" |
| 33 | on_message = None |
| 34 | |
| 35 | @staticmethod |
| 36 | def send_notification(updated_indexes): |
| 37 | """fake notification through the web-socket""" |
| 38 | if not MockWebSocket.on_message: |
| 39 | return |
| 40 | message = { |
| 41 | LOADED_POLICIES: [ |
| 42 | {POLICY_NAME: "{0}.{1}.xml".format( |
| 43 | MockPolicyEngine.get_policy_id(policy_index), policy_index + 1), |
| 44 | POLICY_VER: str(policy_index + 1)} |
| 45 | for policy_index in updated_indexes or [] |
| 46 | ], |
| 47 | REMOVED_POLICIES : [] |
| 48 | } |
| 49 | message = json.dumps(message) |
| 50 | Settings.logger.info("send_notification: %s", message) |
| 51 | MockWebSocket.on_message(None, message) |
| 52 | |
| 53 | @staticmethod |
| 54 | def enableTrace(yes_no): |
| 55 | """ignore""" |
| 56 | pass |
| 57 | |
| 58 | class MockSocket(object): |
| 59 | """Mock websocket""" |
| 60 | def __init__(self): |
| 61 | self.connected = True |
| 62 | |
| 63 | class WebSocketApp(object): |
| 64 | """Mocked WebSocketApp""" |
| 65 | def __init__(self, web_socket_url, |
| 66 | on_open=None, on_message=None, on_close=None, on_error=None, on_pong=None): |
| 67 | self.web_socket_url = web_socket_url |
| 68 | self.on_open = on_open |
| 69 | self.on_message = MockWebSocket.on_message = on_message |
| 70 | self.on_close = on_close |
| 71 | self.on_error = on_error |
| 72 | self.on_pong = on_pong |
| 73 | self.sock = MockWebSocket.MockSocket() |
| 74 | Settings.logger.info("MockWebSocket for: %s", self.web_socket_url) |
| 75 | |
| 76 | def run_forever(self, sslopt=None): |
| 77 | """forever in the loop""" |
| 78 | Settings.logger.info("MockWebSocket run_forever with sslopt=%s...", |
| 79 | json.dumps(sslopt)) |
| 80 | counter = 0 |
| 81 | while self.sock.connected: |
| 82 | counter += 1 |
| 83 | Settings.logger.info("MockWebSocket sleep %s...", counter) |
| 84 | time.sleep(5) |
| 85 | Settings.logger.info("MockWebSocket exit %s", counter) |
| 86 | |
| 87 | def close(self): |
| 88 | """close socket""" |
| 89 | self.sock.connected = False |