Khen Nursimulu | 68b9be3 | 2016-10-25 11:57:04 -0400 | [diff] [blame] | 1 | # |
Zsolt Haraszti | 3eb27a5 | 2017-01-03 21:56:48 -0800 | [diff] [blame] | 2 | # Copyright 2017 the original author or authors. |
Khen Nursimulu | 68b9be3 | 2016-10-25 11:57:04 -0400 | [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 | # |
alshabib | 06b449c | 2017-01-15 17:33:16 -0600 | [diff] [blame] | 16 | import os |
Khen Nursimulu | 68b9be3 | 2016-10-25 11:57:04 -0400 | [diff] [blame] | 17 | |
| 18 | import sys |
| 19 | |
| 20 | from twisted.internet import reactor |
| 21 | from twisted.internet.defer import Deferred, inlineCallbacks, returnValue |
| 22 | |
| 23 | from common.utils.asleep import asleep |
| 24 | from common.utils.consulhelpers import get_endpoint_from_consul |
| 25 | from structlog import get_logger |
| 26 | import grpc |
Zsolt Haraszti | 1edb828 | 2016-11-08 10:57:19 -0800 | [diff] [blame] | 27 | from ofagent.protos import third_party |
Khen Nursimulu | 68b9be3 | 2016-10-25 11:57:04 -0400 | [diff] [blame] | 28 | from protos import voltha_pb2 |
| 29 | from grpc_client import GrpcClient |
| 30 | |
| 31 | from agent import Agent |
Zsolt Haraszti | 7eeb2b3 | 2016-11-06 14:04:55 -0800 | [diff] [blame] | 32 | from google.protobuf.empty_pb2 import Empty |
| 33 | |
Khen Nursimulu | 68b9be3 | 2016-10-25 11:57:04 -0400 | [diff] [blame] | 34 | |
Zsolt Haraszti | cd22adc | 2016-10-25 00:13:06 -0700 | [diff] [blame] | 35 | log = get_logger() |
Zsolt Haraszti | 7eeb2b3 | 2016-11-06 14:04:55 -0800 | [diff] [blame] | 36 | # _ = third_party |
Zsolt Haraszti | cd22adc | 2016-10-25 00:13:06 -0700 | [diff] [blame] | 37 | |
Khen Nursimulu | 68b9be3 | 2016-10-25 11:57:04 -0400 | [diff] [blame] | 38 | class ConnectionManager(object): |
| 39 | |
Khen Nursimulu | 68b9be3 | 2016-10-25 11:57:04 -0400 | [diff] [blame] | 40 | def __init__(self, consul_endpoint, voltha_endpoint, controller_endpoint, |
Zsolt Haraszti | cd22adc | 2016-10-25 00:13:06 -0700 | [diff] [blame] | 41 | voltha_retry_interval=0.5, devices_refresh_interval=5): |
Khen Nursimulu | 68b9be3 | 2016-10-25 11:57:04 -0400 | [diff] [blame] | 42 | |
Zsolt Haraszti | cd22adc | 2016-10-25 00:13:06 -0700 | [diff] [blame] | 43 | log.info('init-connection-manager') |
Khen Nursimulu | 68b9be3 | 2016-10-25 11:57:04 -0400 | [diff] [blame] | 44 | self.controller_endpoint = controller_endpoint |
| 45 | self.consul_endpoint = consul_endpoint |
| 46 | self.voltha_endpoint = voltha_endpoint |
| 47 | |
| 48 | self.channel = None |
Zsolt Haraszti | cd22adc | 2016-10-25 00:13:06 -0700 | [diff] [blame] | 49 | self.grpc_client = None # single, shared gRPC client to Voltha |
| 50 | |
| 51 | self.agent_map = {} # datapath_id -> Agent() |
| 52 | self.device_id_to_datapath_id_map = {} |
Khen Nursimulu | 68b9be3 | 2016-10-25 11:57:04 -0400 | [diff] [blame] | 53 | |
| 54 | self.voltha_retry_interval = voltha_retry_interval |
| 55 | self.devices_refresh_interval = devices_refresh_interval |
| 56 | |
| 57 | self.running = False |
| 58 | |
Zsolt Haraszti | 2bdb6b3 | 2016-11-03 16:56:17 -0700 | [diff] [blame] | 59 | def start(self): |
Zsolt Haraszti | cd22adc | 2016-10-25 00:13:06 -0700 | [diff] [blame] | 60 | |
Khen Nursimulu | 68b9be3 | 2016-10-25 11:57:04 -0400 | [diff] [blame] | 61 | if self.running: |
| 62 | return |
| 63 | |
Zsolt Haraszti | 2bdb6b3 | 2016-11-03 16:56:17 -0700 | [diff] [blame] | 64 | log.debug('starting') |
Khen Nursimulu | 68b9be3 | 2016-10-25 11:57:04 -0400 | [diff] [blame] | 65 | |
| 66 | self.running = True |
| 67 | |
| 68 | # Get voltha grpc endpoint |
| 69 | self.channel = self.get_grpc_channel_with_voltha() |
| 70 | |
Khen Nursimulu | 68b9be3 | 2016-10-25 11:57:04 -0400 | [diff] [blame] | 71 | # Create shared gRPC API object |
Zsolt Haraszti | 2bdb6b3 | 2016-11-03 16:56:17 -0700 | [diff] [blame] | 72 | self.grpc_client = GrpcClient(self, self.channel).start() |
Khen Nursimulu | 68b9be3 | 2016-10-25 11:57:04 -0400 | [diff] [blame] | 73 | |
Zsolt Haraszti | cd22adc | 2016-10-25 00:13:06 -0700 | [diff] [blame] | 74 | # Start monitoring logical devices and manage agents accordingly |
| 75 | reactor.callLater(0, self.monitor_logical_devices) |
Khen Nursimulu | 68b9be3 | 2016-10-25 11:57:04 -0400 | [diff] [blame] | 76 | |
Zsolt Haraszti | 2bdb6b3 | 2016-11-03 16:56:17 -0700 | [diff] [blame] | 77 | log.info('started') |
| 78 | |
Zsolt Haraszti | cd22adc | 2016-10-25 00:13:06 -0700 | [diff] [blame] | 79 | return self |
Khen Nursimulu | 68b9be3 | 2016-10-25 11:57:04 -0400 | [diff] [blame] | 80 | |
Zsolt Haraszti | 2bdb6b3 | 2016-11-03 16:56:17 -0700 | [diff] [blame] | 81 | def stop(self): |
| 82 | log.debug('stopping') |
Khen Nursimulu | 68b9be3 | 2016-10-25 11:57:04 -0400 | [diff] [blame] | 83 | # clean up all controller connections |
Zsolt Haraszti | 2bdb6b3 | 2016-11-03 16:56:17 -0700 | [diff] [blame] | 84 | for agent in self.agent_map.itervalues(): |
| 85 | agent.stop() |
Khen Nursimulu | 68b9be3 | 2016-10-25 11:57:04 -0400 | [diff] [blame] | 86 | self.running = False |
Zsolt Haraszti | 2bdb6b3 | 2016-11-03 16:56:17 -0700 | [diff] [blame] | 87 | self.grpc_client.stop() |
| 88 | del self.channel |
| 89 | log.info('stopped') |
Khen Nursimulu | 68b9be3 | 2016-10-25 11:57:04 -0400 | [diff] [blame] | 90 | |
| 91 | def resolve_endpoint(self, endpoint): |
| 92 | ip_port_endpoint = endpoint |
| 93 | if endpoint.startswith('@'): |
| 94 | try: |
| 95 | ip_port_endpoint = get_endpoint_from_consul( |
| 96 | self.consul_endpoint, endpoint[1:]) |
Zsolt Haraszti | cd22adc | 2016-10-25 00:13:06 -0700 | [diff] [blame] | 97 | log.info( |
Khen Nursimulu | 68b9be3 | 2016-10-25 11:57:04 -0400 | [diff] [blame] | 98 | 'Found endpoint {} service at {}'.format(endpoint, |
| 99 | ip_port_endpoint)) |
| 100 | except Exception as e: |
Zsolt Haraszti | cd22adc | 2016-10-25 00:13:06 -0700 | [diff] [blame] | 101 | log.error('Failure to locate {} service from ' |
Khen Nursimulu | 68b9be3 | 2016-10-25 11:57:04 -0400 | [diff] [blame] | 102 | 'consul {}:'.format(endpoint, repr(e))) |
alshabib | 06b449c | 2017-01-15 17:33:16 -0600 | [diff] [blame] | 103 | log.error('Committing suicide...') |
| 104 | # Committing suicide in order to let docker restart ofagent |
| 105 | os.system("kill -15 {}".format(os.getpid())) |
Khen Nursimulu | 68b9be3 | 2016-10-25 11:57:04 -0400 | [diff] [blame] | 106 | if ip_port_endpoint: |
| 107 | host, port = ip_port_endpoint.split(':', 2) |
| 108 | return host, int(port) |
| 109 | |
| 110 | def get_grpc_channel_with_voltha(self): |
Zsolt Haraszti | cd22adc | 2016-10-25 00:13:06 -0700 | [diff] [blame] | 111 | log.info('Resolving voltha endpoint {} from consul'.format( |
Khen Nursimulu | 68b9be3 | 2016-10-25 11:57:04 -0400 | [diff] [blame] | 112 | self.voltha_endpoint)) |
| 113 | host, port = self.resolve_endpoint(self.voltha_endpoint) |
| 114 | assert host is not None |
| 115 | assert port is not None |
| 116 | # Create grpc channel to Voltha |
| 117 | channel = grpc.insecure_channel('{}:{}'.format(host, port)) |
Zsolt Haraszti | cd22adc | 2016-10-25 00:13:06 -0700 | [diff] [blame] | 118 | log.info('Acquired a grpc channel to voltha') |
Khen Nursimulu | 68b9be3 | 2016-10-25 11:57:04 -0400 | [diff] [blame] | 119 | return channel |
| 120 | |
Khen Nursimulu | 68b9be3 | 2016-10-25 11:57:04 -0400 | [diff] [blame] | 121 | @inlineCallbacks |
| 122 | def get_list_of_logical_devices_from_voltha(self): |
Zsolt Haraszti | cd22adc | 2016-10-25 00:13:06 -0700 | [diff] [blame] | 123 | |
Khen Nursimulu | 68b9be3 | 2016-10-25 11:57:04 -0400 | [diff] [blame] | 124 | while True: |
Zsolt Haraszti | cd22adc | 2016-10-25 00:13:06 -0700 | [diff] [blame] | 125 | log.info('Retrieve devices from voltha') |
Khen Nursimulu | 68b9be3 | 2016-10-25 11:57:04 -0400 | [diff] [blame] | 126 | try: |
Zsolt Haraszti | 6686203 | 2016-11-28 14:28:39 -0800 | [diff] [blame] | 127 | stub = voltha_pb2.VolthaLocalServiceStub(self.channel) |
Zsolt Haraszti | 7eeb2b3 | 2016-11-06 14:04:55 -0800 | [diff] [blame] | 128 | devices = stub.ListLogicalDevices(Empty()).items |
Khen Nursimulu | 68b9be3 | 2016-10-25 11:57:04 -0400 | [diff] [blame] | 129 | for device in devices: |
Zsolt Haraszti | cd22adc | 2016-10-25 00:13:06 -0700 | [diff] [blame] | 130 | log.info("Devices {} -> {}".format(device.id, |
Zsolt Haraszti | 3300f74 | 2017-01-09 01:14:20 -0800 | [diff] [blame] | 131 | device.datapath_id)) |
Zsolt Haraszti | cd22adc | 2016-10-25 00:13:06 -0700 | [diff] [blame] | 132 | returnValue(devices) |
| 133 | |
Khen Nursimulu | 68b9be3 | 2016-10-25 11:57:04 -0400 | [diff] [blame] | 134 | except Exception as e: |
Zsolt Haraszti | cd22adc | 2016-10-25 00:13:06 -0700 | [diff] [blame] | 135 | log.error('Failure to retrieve devices from ' |
Khen Nursimulu | 68b9be3 | 2016-10-25 11:57:04 -0400 | [diff] [blame] | 136 | 'voltha: {}'.format(repr(e))) |
| 137 | |
Zsolt Haraszti | cd22adc | 2016-10-25 00:13:06 -0700 | [diff] [blame] | 138 | log.info('reconnect', after_delay=self.voltha_retry_interval) |
Khen Nursimulu | 68b9be3 | 2016-10-25 11:57:04 -0400 | [diff] [blame] | 139 | yield asleep(self.voltha_retry_interval) |
| 140 | |
Zsolt Haraszti | cd22adc | 2016-10-25 00:13:06 -0700 | [diff] [blame] | 141 | def refresh_agent_connections(self, devices): |
| 142 | """ |
| 143 | Based on the new device list, update the following state in the class: |
| 144 | * agent_map |
| 145 | * datapath_map |
| 146 | * device_id_map |
| 147 | :param devices: full device list freshly received from Voltha |
| 148 | :return: None |
| 149 | """ |
Khen Nursimulu | 68b9be3 | 2016-10-25 11:57:04 -0400 | [diff] [blame] | 150 | |
Zsolt Haraszti | cd22adc | 2016-10-25 00:13:06 -0700 | [diff] [blame] | 151 | # Use datapath ids for deciding what's new and what's obsolete |
| 152 | desired_datapath_ids = set(d.datapath_id for d in devices) |
| 153 | current_datapath_ids = set(self.agent_map.iterkeys()) |
Khen Nursimulu | 68b9be3 | 2016-10-25 11:57:04 -0400 | [diff] [blame] | 154 | |
Zsolt Haraszti | cd22adc | 2016-10-25 00:13:06 -0700 | [diff] [blame] | 155 | # if identical, nothing to do |
| 156 | if desired_datapath_ids == current_datapath_ids: |
| 157 | return |
Khen Nursimulu | 68b9be3 | 2016-10-25 11:57:04 -0400 | [diff] [blame] | 158 | |
Zsolt Haraszti | cd22adc | 2016-10-25 00:13:06 -0700 | [diff] [blame] | 159 | # ... otherwise calculate differences |
| 160 | to_add = desired_datapath_ids.difference(current_datapath_ids) |
| 161 | to_del = current_datapath_ids.difference(desired_datapath_ids) |
Khen Nursimulu | 68b9be3 | 2016-10-25 11:57:04 -0400 | [diff] [blame] | 162 | |
Zsolt Haraszti | cd22adc | 2016-10-25 00:13:06 -0700 | [diff] [blame] | 163 | # remove what we don't need |
| 164 | for datapath_id in to_del: |
| 165 | self.delete_agent(datapath_id) |
Khen Nursimulu | 68b9be3 | 2016-10-25 11:57:04 -0400 | [diff] [blame] | 166 | |
Zsolt Haraszti | cd22adc | 2016-10-25 00:13:06 -0700 | [diff] [blame] | 167 | # start new agents as needed |
| 168 | for device in devices: |
| 169 | if device.datapath_id in to_add: |
| 170 | self.create_agent(device) |
Khen Nursimulu | 68b9be3 | 2016-10-25 11:57:04 -0400 | [diff] [blame] | 171 | |
Zsolt Haraszti | cd22adc | 2016-10-25 00:13:06 -0700 | [diff] [blame] | 172 | log.debug('updated-agent-list', count=len(self.agent_map)) |
| 173 | log.debug('updated-device-id-to-datapath-id-map', |
| 174 | map=str(self.device_id_to_datapath_id_map)) |
| 175 | |
| 176 | def create_agent(self, device): |
| 177 | datapath_id = device.datapath_id |
| 178 | device_id = device.id |
| 179 | agent = Agent(self.controller_endpoint, datapath_id, |
| 180 | device_id, self.grpc_client) |
Zsolt Haraszti | 2bdb6b3 | 2016-11-03 16:56:17 -0700 | [diff] [blame] | 181 | agent.start() |
Zsolt Haraszti | cd22adc | 2016-10-25 00:13:06 -0700 | [diff] [blame] | 182 | self.agent_map[datapath_id] = agent |
| 183 | self.device_id_to_datapath_id_map[device_id] = datapath_id |
| 184 | |
| 185 | def delete_agent(self, datapath_id): |
| 186 | agent = self.agent_map[datapath_id] |
| 187 | device_id = agent.get_device_id() |
| 188 | agent.stop() |
| 189 | del self.agent_map[datapath_id] |
| 190 | del self.device_id_to_datapath_id_map[device_id] |
Khen Nursimulu | 68b9be3 | 2016-10-25 11:57:04 -0400 | [diff] [blame] | 191 | |
| 192 | @inlineCallbacks |
Zsolt Haraszti | cd22adc | 2016-10-25 00:13:06 -0700 | [diff] [blame] | 193 | def monitor_logical_devices(self): |
Khen Nursimulu | 68b9be3 | 2016-10-25 11:57:04 -0400 | [diff] [blame] | 194 | while True: |
Zsolt Haraszti | cd22adc | 2016-10-25 00:13:06 -0700 | [diff] [blame] | 195 | # TODO @khen We should switch to a polling mode based on a |
| 196 | # streaming gRPC method |
| 197 | |
| 198 | # get current list from Voltha |
| 199 | devices = yield self.get_list_of_logical_devices_from_voltha() |
| 200 | |
| 201 | # update agent list and mapping tables as needed |
| 202 | self.refresh_agent_connections(devices) |
| 203 | |
| 204 | # wait before next poll |
Khen Nursimulu | 68b9be3 | 2016-10-25 11:57:04 -0400 | [diff] [blame] | 205 | yield asleep(self.devices_refresh_interval) |
Zsolt Haraszti | cd22adc | 2016-10-25 00:13:06 -0700 | [diff] [blame] | 206 | log.info('Monitor connections') |
Khen Nursimulu | 68b9be3 | 2016-10-25 11:57:04 -0400 | [diff] [blame] | 207 | |
Zsolt Haraszti | cd22adc | 2016-10-25 00:13:06 -0700 | [diff] [blame] | 208 | def forward_packet_in(self, device_id, ofp_packet_in): |
| 209 | datapath_id = self.device_id_to_datapath_id_map.get(device_id, None) |
| 210 | if datapath_id: |
| 211 | agent = self.agent_map[datapath_id] |
| 212 | agent.forward_packet_in(ofp_packet_in) |
Zsolt Haraszti | 217a12e | 2016-12-19 16:37:55 -0800 | [diff] [blame] | 213 | |
| 214 | def forward_change_event(self, device_id, event): |
| 215 | datapath_id = self.device_id_to_datapath_id_map.get(device_id, None) |
| 216 | if datapath_id: |
| 217 | agent = self.agent_map[datapath_id] |
| 218 | agent.forward_change_event(event) |