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