Zsolt Haraszti | 023ea7c | 2016-10-16 19:30:34 -0700 | [diff] [blame] | 1 | # |
Zsolt Haraszti | 3eb27a5 | 2017-01-03 21:56:48 -0800 | [diff] [blame] | 2 | # Copyright 2017 the original author or authors. |
Zsolt Haraszti | 023ea7c | 2016-10-16 19:30:34 -0700 | [diff] [blame] | 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 | # |
| 16 | |
| 17 | import sys |
| 18 | |
| 19 | import structlog |
Girish | f6eeaea | 2017-11-13 10:53:57 +0530 | [diff] [blame] | 20 | import os.path |
Jonathan Hart | 8d21c32 | 2018-04-17 07:42:02 -0700 | [diff] [blame] | 21 | from twisted.internet import protocol, reactor, ssl |
Zsolt Haraszti | 023ea7c | 2016-10-16 19:30:34 -0700 | [diff] [blame] | 22 | from twisted.internet.defer import Deferred, inlineCallbacks |
| 23 | |
| 24 | import loxi.of13 as of13 |
| 25 | from common.utils.asleep import asleep |
| 26 | from of_connection import OpenFlowConnection |
| 27 | from of_protocol_handler import OpenFlowProtocolHandler |
Zsolt Haraszti | 023ea7c | 2016-10-16 19:30:34 -0700 | [diff] [blame] | 28 | |
| 29 | log = structlog.get_logger() |
| 30 | |
| 31 | |
| 32 | class Agent(protocol.ClientFactory): |
| 33 | |
sathishg | 00433e5 | 2017-05-23 16:07:41 +0530 | [diff] [blame] | 34 | generation_is_defined = False |
| 35 | cached_generation_id = None |
| 36 | |
Zsolt Haraszti | cd22adc | 2016-10-25 00:13:06 -0700 | [diff] [blame] | 37 | def __init__(self, |
| 38 | controller_endpoint, |
| 39 | datapath_id, |
| 40 | device_id, |
| 41 | rpc_stub, |
Girish | f6eeaea | 2017-11-13 10:53:57 +0530 | [diff] [blame] | 42 | enable_tls=False, |
| 43 | key_file=None, |
| 44 | cert_file=None, |
Zsolt Haraszti | 023ea7c | 2016-10-16 19:30:34 -0700 | [diff] [blame] | 45 | conn_retry_interval=1): |
Zsolt Haraszti | cd22adc | 2016-10-25 00:13:06 -0700 | [diff] [blame] | 46 | |
Zsolt Haraszti | 023ea7c | 2016-10-16 19:30:34 -0700 | [diff] [blame] | 47 | self.controller_endpoint = controller_endpoint |
| 48 | self.datapath_id = datapath_id |
Zsolt Haraszti | cd22adc | 2016-10-25 00:13:06 -0700 | [diff] [blame] | 49 | self.device_id = device_id |
Zsolt Haraszti | 023ea7c | 2016-10-16 19:30:34 -0700 | [diff] [blame] | 50 | self.rpc_stub = rpc_stub |
Girish | f6eeaea | 2017-11-13 10:53:57 +0530 | [diff] [blame] | 51 | self.enable_tls = enable_tls |
| 52 | self.key_file = key_file |
| 53 | self.cert_file = cert_file |
Zsolt Haraszti | 023ea7c | 2016-10-16 19:30:34 -0700 | [diff] [blame] | 54 | self.retry_interval = conn_retry_interval |
| 55 | |
| 56 | self.running = False |
| 57 | self.connector = None # will be a Connector instance once connected |
| 58 | self.d_disconnected = None # a deferred to signal reconnect loop when |
| 59 | # TCP connection is lost |
| 60 | self.connected = False |
| 61 | self.exiting = False |
Zsolt Haraszti | 4732463 | 2016-12-20 00:50:33 -0800 | [diff] [blame] | 62 | self.proto_handler = None |
Zsolt Haraszti | 023ea7c | 2016-10-16 19:30:34 -0700 | [diff] [blame] | 63 | |
Zsolt Haraszti | cd22adc | 2016-10-25 00:13:06 -0700 | [diff] [blame] | 64 | def get_device_id(self): |
| 65 | return self.device_id |
| 66 | |
Zsolt Haraszti | 2bdb6b3 | 2016-11-03 16:56:17 -0700 | [diff] [blame] | 67 | def start(self): |
| 68 | log.debug('starting') |
Zsolt Haraszti | 023ea7c | 2016-10-16 19:30:34 -0700 | [diff] [blame] | 69 | if self.running: |
| 70 | return |
| 71 | self.running = True |
| 72 | reactor.callLater(0, self.keep_connected) |
Zsolt Haraszti | 2bdb6b3 | 2016-11-03 16:56:17 -0700 | [diff] [blame] | 73 | log.info('started') |
Zsolt Haraszti | 023ea7c | 2016-10-16 19:30:34 -0700 | [diff] [blame] | 74 | return self |
| 75 | |
Zsolt Haraszti | 2bdb6b3 | 2016-11-03 16:56:17 -0700 | [diff] [blame] | 76 | def stop(self): |
| 77 | log.debug('stopping') |
| 78 | self.connected = False |
| 79 | self.exiting = True |
| 80 | self.connector.disconnect() |
| 81 | log.info('stopped') |
| 82 | |
Zsolt Haraszti | 023ea7c | 2016-10-16 19:30:34 -0700 | [diff] [blame] | 83 | def resolve_endpoint(self, endpoint): |
alshabib | c3fb494 | 2017-01-26 15:34:24 -0800 | [diff] [blame] | 84 | # enable optional resolution via consul; |
| 85 | # see https://jira.opencord.org/browse/CORD-820 |
Zsolt Haraszti | 023ea7c | 2016-10-16 19:30:34 -0700 | [diff] [blame] | 86 | host, port = endpoint.split(':', 2) |
| 87 | return host, int(port) |
| 88 | |
| 89 | @inlineCallbacks |
| 90 | def keep_connected(self): |
| 91 | """Keep reconnecting to the controller""" |
| 92 | while not self.exiting: |
| 93 | host, port = self.resolve_endpoint(self.controller_endpoint) |
| 94 | log.info('connecting', host=host, port=port) |
Girish | f6eeaea | 2017-11-13 10:53:57 +0530 | [diff] [blame] | 95 | if self.enable_tls: |
| 96 | try: |
| 97 | # Check that key_file and cert_file is provided and |
| 98 | # the files exist |
| 99 | if self.key_file is None or \ |
| 100 | self.cert_file is None or \ |
| 101 | not os.path.isfile(self.key_file) or \ |
| 102 | not os.path.isfile(self.cert_file): |
| 103 | raise Exception('key_file "{}" or cert_file "{}"' |
| 104 | ' is not found'. |
| 105 | format(self.key_file, self.cert_file)) |
| 106 | with open(self.key_file) as keyFile: |
| 107 | with open(self.cert_file) as certFile: |
| 108 | clientCert = ssl.PrivateCertificate.loadPEM( |
| 109 | keyFile.read() + certFile.read()) |
schowdhury | 3fbd470 | 2017-06-30 03:50:25 -0700 | [diff] [blame] | 110 | |
Girish | f6eeaea | 2017-11-13 10:53:57 +0530 | [diff] [blame] | 111 | ctx = clientCert.options() |
| 112 | self.connector = reactor.connectSSL(host, port, self, ctx) |
| 113 | log.info('tls-enabled') |
schowdhury | 3fbd470 | 2017-06-30 03:50:25 -0700 | [diff] [blame] | 114 | |
Girish | f6eeaea | 2017-11-13 10:53:57 +0530 | [diff] [blame] | 115 | except Exception as e: |
| 116 | log.exception('failed-to-connect', reason=e) |
| 117 | else: |
| 118 | self.connector = reactor.connectTCP(host, port, self) |
| 119 | log.info('tls-disabled') |
schowdhury | 3fbd470 | 2017-06-30 03:50:25 -0700 | [diff] [blame] | 120 | |
Zsolt Haraszti | 023ea7c | 2016-10-16 19:30:34 -0700 | [diff] [blame] | 121 | self.d_disconnected = Deferred() |
| 122 | yield self.d_disconnected |
| 123 | log.debug('reconnect', after_delay=self.retry_interval) |
| 124 | yield asleep(self.retry_interval) |
| 125 | |
Zsolt Haraszti | 023ea7c | 2016-10-16 19:30:34 -0700 | [diff] [blame] | 126 | def enter_disconnected(self, event, reason): |
| 127 | """Internally signal entering disconnected state""" |
Zsolt Haraszti | 023ea7c | 2016-10-16 19:30:34 -0700 | [diff] [blame] | 128 | self.connected = False |
Zsolt Haraszti | 2bdb6b3 | 2016-11-03 16:56:17 -0700 | [diff] [blame] | 129 | if not self.exiting: |
| 130 | log.error(event, reason=reason) |
| 131 | self.d_disconnected.callback(None) |
Zsolt Haraszti | 023ea7c | 2016-10-16 19:30:34 -0700 | [diff] [blame] | 132 | |
| 133 | def enter_connected(self): |
| 134 | """Handle transitioning from disconnected to connected state""" |
| 135 | log.info('connected') |
| 136 | self.connected = True |
| 137 | self.read_buffer = None |
Zsolt Haraszti | 2bdb6b3 | 2016-11-03 16:56:17 -0700 | [diff] [blame] | 138 | reactor.callLater(0, self.proto_handler.start) |
Zsolt Haraszti | 023ea7c | 2016-10-16 19:30:34 -0700 | [diff] [blame] | 139 | |
| 140 | # protocol.ClientFactory methods |
| 141 | |
| 142 | def protocol(self): |
| 143 | cxn = OpenFlowConnection(self) # Low level message handler |
| 144 | self.proto_handler = OpenFlowProtocolHandler( |
Zsolt Haraszti | cd22adc | 2016-10-25 00:13:06 -0700 | [diff] [blame] | 145 | self.datapath_id, self.device_id, self, cxn, self.rpc_stub) |
Zsolt Haraszti | 023ea7c | 2016-10-16 19:30:34 -0700 | [diff] [blame] | 146 | return cxn |
| 147 | |
| 148 | def clientConnectionFailed(self, connector, reason): |
| 149 | self.enter_disconnected('connection-failed', reason) |
| 150 | |
| 151 | def clientConnectionLost(self, connector, reason): |
Zsolt Haraszti | 2bdb6b3 | 2016-11-03 16:56:17 -0700 | [diff] [blame] | 152 | if not self.exiting: |
| 153 | log.error('client-connection-lost', |
| 154 | reason=reason, connector=connector) |
Zsolt Haraszti | 023ea7c | 2016-10-16 19:30:34 -0700 | [diff] [blame] | 155 | |
Zsolt Haraszti | cd22adc | 2016-10-25 00:13:06 -0700 | [diff] [blame] | 156 | def forward_packet_in(self, ofp_packet_in): |
Zsolt Haraszti | 4732463 | 2016-12-20 00:50:33 -0800 | [diff] [blame] | 157 | if self.proto_handler is not None: |
| 158 | self.proto_handler.forward_packet_in(ofp_packet_in) |
Zsolt Haraszti | cd22adc | 2016-10-25 00:13:06 -0700 | [diff] [blame] | 159 | |
Zsolt Haraszti | 217a12e | 2016-12-19 16:37:55 -0800 | [diff] [blame] | 160 | def forward_change_event(self, event): |
| 161 | # assert isinstance(event, ChangeEvent) |
| 162 | log.info('got-change-event', change_event=event) |
Gamze Abaka | 9d34329 | 2019-02-20 06:35:18 +0000 | [diff] [blame] | 163 | if self.proto_handler is not None: |
| 164 | if event.HasField("port_status"): |
Zsolt Haraszti | 4732463 | 2016-12-20 00:50:33 -0800 | [diff] [blame] | 165 | self.proto_handler.forward_port_status(event.port_status) |
Gamze Abaka | 9d34329 | 2019-02-20 06:35:18 +0000 | [diff] [blame] | 166 | elif event.HasField("flow_removed"): |
| 167 | self.proto_handler.forward_flow_removed(event.flow_removed) |
Zsolt Haraszti | 217a12e | 2016-12-19 16:37:55 -0800 | [diff] [blame] | 168 | else: |
| 169 | log.error('unknown-change-event', change_event=event) |
| 170 | |
Zsolt Haraszti | 023ea7c | 2016-10-16 19:30:34 -0700 | [diff] [blame] | 171 | |
| 172 | if __name__ == '__main__': |
| 173 | """Run this to test the agent for N concurrent sessions: |
| 174 | python agent [<number-of-desired-instances>] |
| 175 | """ |
| 176 | |
| 177 | n = 1 if len(sys.argv) < 2 else int(sys.argv[1]) |
| 178 | |
| 179 | from utils import mac_str_to_tuple |
| 180 | |
| 181 | class MockRpc(object): |
| 182 | @staticmethod |
| 183 | def get_port_list(_): |
| 184 | ports = [] |
| 185 | cap = of13.OFPPF_1GB_FD | of13.OFPPF_FIBER |
| 186 | for pno, mac, nam, cur, adv, sup, spe in ( |
| 187 | (1, '00:00:00:00:00:01', 'onu1', cap, cap, cap, |
| 188 | of13.OFPPF_1GB_FD), |
| 189 | (2, '00:00:00:00:00:02', 'onu2', cap, cap, cap, |
| 190 | of13.OFPPF_1GB_FD), |
| 191 | (129, '00:00:00:00:00:81', 'olt', cap, cap, cap, |
| 192 | of13.OFPPF_1GB_FD) |
| 193 | ): |
| 194 | port = of13.common.port_desc(pno, mac_str_to_tuple(mac), nam, |
| 195 | curr=cur, advertised=adv, |
| 196 | supported=sup, |
| 197 | curr_speed=spe, max_speed=spe) |
| 198 | ports.append(port) |
| 199 | return ports |
| 200 | |
| 201 | stub = MockRpc() |
Zsolt Haraszti | ee5c4c8 | 2017-01-09 14:37:57 -0800 | [diff] [blame] | 202 | agents = [Agent('localhost:6653', 256 + i, stub).start() for i in range(n)] |
Zsolt Haraszti | 023ea7c | 2016-10-16 19:30:34 -0700 | [diff] [blame] | 203 | |
| 204 | def shutdown(): |
| 205 | [a.stop() for a in agents] |
| 206 | |
| 207 | reactor.addSystemEventTrigger('before', 'shutdown', shutdown) |
| 208 | reactor.run() |