Migrate 030_bgp_functional_evpn.robot
[integration/test.git] / csit / libraries / IoTDM / client_libs / iot_communication_concepts.py
1 """
2  Defines abstract classes which describe fundamental communication concepts as
3  abstract classes.
4 """
5
6 #
7 # Copyright (c) 2017 Cisco Systems, Inc. and others.  All rights reserved.
8 #
9 # This program and the accompanying materials are made available under the
10 # terms of the Eclipse Public License v1.0 which accompanies this distribution,
11 # and is available at http://www.eclipse.org/legal/epl-v10.html
12 #
13
14
15 class IoTCommunication(object):
16     """Aggregates Rx and Tx channel for sending and receiving IoTData."""
17
18     def __init__(self, tx, rx):
19         pass
20
21
22 class IotComm(object):
23     """
24     Implements communication concepts of starting and stopping communication.
25     Only methods _start() and _stop() should be implemented by child classes.
26     """
27
28     def __init__(self):
29         self._started = False
30
31     def _start(self):
32         """
33         Called by start() method, should contain implementation specific
34         procedures
35         """
36         raise NotImplementedError()
37
38     def start(self):
39         """Starts communication"""
40         if not self._started:
41             self._start()
42             self._started = True
43
44     def _stop(self):
45         """
46         Called by stop() method, should contain implementation specific
47         procedures to stop communication and release resources.
48         """
49         raise NotImplementedError()
50
51     def stop(self):
52         """Stops communication"""
53         if self._started:
54             self._stop()
55             self._started = False
56
57     def is_started(self):
58         """Returns True if the communication is started, False otherwise"""
59         return self._started
60
61
62 class IoTTx(IotComm):
63     """
64     Describes protocol specific blocking sync TX channel.
65     Uses protocol specific encoder to encode IoTData and protocol specific
66     decoder to decode result.
67     """
68
69     def __init__(self, encoder, decoder):
70         self.encoder = encoder
71         self.decoder = decoder
72         super(IoTTx, self).__init__()
73
74     def send(self, iotdata):
75         """
76         Uses encoder to encode iotdata to protocol specific message, sends the
77         message and decodes result to iotdata and returns the decoded result
78         """
79         raise NotImplementedError()
80
81
82 class IoTRx(IotComm):
83     """
84     Describes protocol specific non-blocking async RX channel.
85     Uses protocol specific decoder to decode received protocol message to
86     IoTData and protocol specific encoder is used to encode result of
87     handling.
88     """
89
90     def __init__(self, decoder, encoder):
91         self.encoder = encoder
92         self.decoder = decoder
93         self.receive_cb = None
94         super(IoTRx, self).__init__()
95
96     def start(self, receive_cb):
97         """
98         Starts communication and stores receive_cb() method which process iotdata
99         input and returns result as iotdata object.
100         The receive_cb() is called when protocol specific message is received and
101         decoded. Result of the receive_cb() is encoded to the protocol specific
102         message and sent as reply if needed.
103         """
104         self.receive_cb = receive_cb
105         super(IoTRx, self).start()
106
107     def stop(self):
108         """
109         Stops the communication, releases resources and clears stored
110         receive_cb() method
111         """
112         super(IoTRx, self).stop()
113         self.receive_cb = None