blob: 71318af2a0754b7c40538532be6df35e430638e2 [file] [log] [blame]
Khen Nursimulu68b9be32016-10-25 11:57:04 -04001#
Zsolt Haraszti3eb27a52017-01-03 21:56:48 -08002# Copyright 2017 the original author or authors.
Khen Nursimulu68b9be32016-10-25 11:57:04 -04003#
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
17import sys
18
19from twisted.internet import reactor
20from twisted.internet.defer import Deferred, inlineCallbacks, returnValue
21
22from common.utils.asleep import asleep
23from common.utils.consulhelpers import get_endpoint_from_consul
24from structlog import get_logger
25import grpc
Zsolt Haraszti1edb8282016-11-08 10:57:19 -080026from ofagent.protos import third_party
Khen Nursimulu68b9be32016-10-25 11:57:04 -040027from protos import voltha_pb2
28from grpc_client import GrpcClient
29
30from agent import Agent
Zsolt Haraszti7eeb2b32016-11-06 14:04:55 -080031from google.protobuf.empty_pb2 import Empty
32
Khen Nursimulu68b9be32016-10-25 11:57:04 -040033
Zsolt Haraszticd22adc2016-10-25 00:13:06 -070034log = get_logger()
Zsolt Haraszti7eeb2b32016-11-06 14:04:55 -080035# _ = third_party
Zsolt Haraszticd22adc2016-10-25 00:13:06 -070036
Khen Nursimulu68b9be32016-10-25 11:57:04 -040037class ConnectionManager(object):
38
Khen Nursimulu68b9be32016-10-25 11:57:04 -040039 def __init__(self, consul_endpoint, voltha_endpoint, controller_endpoint,
Zsolt Haraszticd22adc2016-10-25 00:13:06 -070040 voltha_retry_interval=0.5, devices_refresh_interval=5):
Khen Nursimulu68b9be32016-10-25 11:57:04 -040041
Zsolt Haraszticd22adc2016-10-25 00:13:06 -070042 log.info('init-connection-manager')
Khen Nursimulu68b9be32016-10-25 11:57:04 -040043 self.controller_endpoint = controller_endpoint
44 self.consul_endpoint = consul_endpoint
45 self.voltha_endpoint = voltha_endpoint
46
47 self.channel = None
Zsolt Haraszticd22adc2016-10-25 00:13:06 -070048 self.grpc_client = None # single, shared gRPC client to Voltha
49
50 self.agent_map = {} # datapath_id -> Agent()
51 self.device_id_to_datapath_id_map = {}
Khen Nursimulu68b9be32016-10-25 11:57:04 -040052
53 self.voltha_retry_interval = voltha_retry_interval
54 self.devices_refresh_interval = devices_refresh_interval
55
56 self.running = False
57
Zsolt Haraszti2bdb6b32016-11-03 16:56:17 -070058 def start(self):
Zsolt Haraszticd22adc2016-10-25 00:13:06 -070059
Khen Nursimulu68b9be32016-10-25 11:57:04 -040060 if self.running:
61 return
62
Zsolt Haraszti2bdb6b32016-11-03 16:56:17 -070063 log.debug('starting')
Khen Nursimulu68b9be32016-10-25 11:57:04 -040064
65 self.running = True
66
67 # Get voltha grpc endpoint
68 self.channel = self.get_grpc_channel_with_voltha()
69
Khen Nursimulu68b9be32016-10-25 11:57:04 -040070 # Create shared gRPC API object
Zsolt Haraszti2bdb6b32016-11-03 16:56:17 -070071 self.grpc_client = GrpcClient(self, self.channel).start()
Khen Nursimulu68b9be32016-10-25 11:57:04 -040072
Zsolt Haraszticd22adc2016-10-25 00:13:06 -070073 # Start monitoring logical devices and manage agents accordingly
74 reactor.callLater(0, self.monitor_logical_devices)
Khen Nursimulu68b9be32016-10-25 11:57:04 -040075
Zsolt Haraszti2bdb6b32016-11-03 16:56:17 -070076 log.info('started')
77
Zsolt Haraszticd22adc2016-10-25 00:13:06 -070078 return self
Khen Nursimulu68b9be32016-10-25 11:57:04 -040079
Zsolt Haraszti2bdb6b32016-11-03 16:56:17 -070080 def stop(self):
81 log.debug('stopping')
Khen Nursimulu68b9be32016-10-25 11:57:04 -040082 # clean up all controller connections
Zsolt Haraszti2bdb6b32016-11-03 16:56:17 -070083 for agent in self.agent_map.itervalues():
84 agent.stop()
Khen Nursimulu68b9be32016-10-25 11:57:04 -040085 self.running = False
Zsolt Haraszti2bdb6b32016-11-03 16:56:17 -070086 self.grpc_client.stop()
87 del self.channel
88 log.info('stopped')
Khen Nursimulu68b9be32016-10-25 11:57:04 -040089
90 def resolve_endpoint(self, endpoint):
91 ip_port_endpoint = endpoint
92 if endpoint.startswith('@'):
93 try:
94 ip_port_endpoint = get_endpoint_from_consul(
95 self.consul_endpoint, endpoint[1:])
Zsolt Haraszticd22adc2016-10-25 00:13:06 -070096 log.info(
Khen Nursimulu68b9be32016-10-25 11:57:04 -040097 'Found endpoint {} service at {}'.format(endpoint,
98 ip_port_endpoint))
99 except Exception as e:
Zsolt Haraszticd22adc2016-10-25 00:13:06 -0700100 log.error('Failure to locate {} service from '
Khen Nursimulu68b9be32016-10-25 11:57:04 -0400101 'consul {}:'.format(endpoint, repr(e)))
102 return
103 if ip_port_endpoint:
104 host, port = ip_port_endpoint.split(':', 2)
105 return host, int(port)
106
107 def get_grpc_channel_with_voltha(self):
Zsolt Haraszticd22adc2016-10-25 00:13:06 -0700108 log.info('Resolving voltha endpoint {} from consul'.format(
Khen Nursimulu68b9be32016-10-25 11:57:04 -0400109 self.voltha_endpoint))
110 host, port = self.resolve_endpoint(self.voltha_endpoint)
111 assert host is not None
112 assert port is not None
113 # Create grpc channel to Voltha
114 channel = grpc.insecure_channel('{}:{}'.format(host, port))
Zsolt Haraszticd22adc2016-10-25 00:13:06 -0700115 log.info('Acquired a grpc channel to voltha')
Khen Nursimulu68b9be32016-10-25 11:57:04 -0400116 return channel
117
Khen Nursimulu68b9be32016-10-25 11:57:04 -0400118 @inlineCallbacks
119 def get_list_of_logical_devices_from_voltha(self):
Zsolt Haraszticd22adc2016-10-25 00:13:06 -0700120
Khen Nursimulu68b9be32016-10-25 11:57:04 -0400121 while True:
Zsolt Haraszticd22adc2016-10-25 00:13:06 -0700122 log.info('Retrieve devices from voltha')
Khen Nursimulu68b9be32016-10-25 11:57:04 -0400123 try:
Zsolt Haraszti66862032016-11-28 14:28:39 -0800124 stub = voltha_pb2.VolthaLocalServiceStub(self.channel)
Zsolt Haraszti7eeb2b32016-11-06 14:04:55 -0800125 devices = stub.ListLogicalDevices(Empty()).items
Khen Nursimulu68b9be32016-10-25 11:57:04 -0400126 for device in devices:
Zsolt Haraszticd22adc2016-10-25 00:13:06 -0700127 log.info("Devices {} -> {}".format(device.id,
Zsolt Haraszti3300f742017-01-09 01:14:20 -0800128 device.datapath_id))
Zsolt Haraszticd22adc2016-10-25 00:13:06 -0700129 returnValue(devices)
130
Khen Nursimulu68b9be32016-10-25 11:57:04 -0400131 except Exception as e:
Zsolt Haraszticd22adc2016-10-25 00:13:06 -0700132 log.error('Failure to retrieve devices from '
Khen Nursimulu68b9be32016-10-25 11:57:04 -0400133 'voltha: {}'.format(repr(e)))
134
Zsolt Haraszticd22adc2016-10-25 00:13:06 -0700135 log.info('reconnect', after_delay=self.voltha_retry_interval)
Khen Nursimulu68b9be32016-10-25 11:57:04 -0400136 yield asleep(self.voltha_retry_interval)
137
Zsolt Haraszticd22adc2016-10-25 00:13:06 -0700138 def refresh_agent_connections(self, devices):
139 """
140 Based on the new device list, update the following state in the class:
141 * agent_map
142 * datapath_map
143 * device_id_map
144 :param devices: full device list freshly received from Voltha
145 :return: None
146 """
Khen Nursimulu68b9be32016-10-25 11:57:04 -0400147
Zsolt Haraszticd22adc2016-10-25 00:13:06 -0700148 # Use datapath ids for deciding what's new and what's obsolete
149 desired_datapath_ids = set(d.datapath_id for d in devices)
150 current_datapath_ids = set(self.agent_map.iterkeys())
Khen Nursimulu68b9be32016-10-25 11:57:04 -0400151
Zsolt Haraszticd22adc2016-10-25 00:13:06 -0700152 # if identical, nothing to do
153 if desired_datapath_ids == current_datapath_ids:
154 return
Khen Nursimulu68b9be32016-10-25 11:57:04 -0400155
Zsolt Haraszticd22adc2016-10-25 00:13:06 -0700156 # ... otherwise calculate differences
157 to_add = desired_datapath_ids.difference(current_datapath_ids)
158 to_del = current_datapath_ids.difference(desired_datapath_ids)
Khen Nursimulu68b9be32016-10-25 11:57:04 -0400159
Zsolt Haraszticd22adc2016-10-25 00:13:06 -0700160 # remove what we don't need
161 for datapath_id in to_del:
162 self.delete_agent(datapath_id)
Khen Nursimulu68b9be32016-10-25 11:57:04 -0400163
Zsolt Haraszticd22adc2016-10-25 00:13:06 -0700164 # start new agents as needed
165 for device in devices:
166 if device.datapath_id in to_add:
167 self.create_agent(device)
Khen Nursimulu68b9be32016-10-25 11:57:04 -0400168
Zsolt Haraszticd22adc2016-10-25 00:13:06 -0700169 log.debug('updated-agent-list', count=len(self.agent_map))
170 log.debug('updated-device-id-to-datapath-id-map',
171 map=str(self.device_id_to_datapath_id_map))
172
173 def create_agent(self, device):
174 datapath_id = device.datapath_id
175 device_id = device.id
176 agent = Agent(self.controller_endpoint, datapath_id,
177 device_id, self.grpc_client)
Zsolt Haraszti2bdb6b32016-11-03 16:56:17 -0700178 agent.start()
Zsolt Haraszticd22adc2016-10-25 00:13:06 -0700179 self.agent_map[datapath_id] = agent
180 self.device_id_to_datapath_id_map[device_id] = datapath_id
181
182 def delete_agent(self, datapath_id):
183 agent = self.agent_map[datapath_id]
184 device_id = agent.get_device_id()
185 agent.stop()
186 del self.agent_map[datapath_id]
187 del self.device_id_to_datapath_id_map[device_id]
Khen Nursimulu68b9be32016-10-25 11:57:04 -0400188
189 @inlineCallbacks
Zsolt Haraszticd22adc2016-10-25 00:13:06 -0700190 def monitor_logical_devices(self):
Khen Nursimulu68b9be32016-10-25 11:57:04 -0400191 while True:
Zsolt Haraszticd22adc2016-10-25 00:13:06 -0700192 # TODO @khen We should switch to a polling mode based on a
193 # streaming gRPC method
194
195 # get current list from Voltha
196 devices = yield self.get_list_of_logical_devices_from_voltha()
197
198 # update agent list and mapping tables as needed
199 self.refresh_agent_connections(devices)
200
201 # wait before next poll
Khen Nursimulu68b9be32016-10-25 11:57:04 -0400202 yield asleep(self.devices_refresh_interval)
Zsolt Haraszticd22adc2016-10-25 00:13:06 -0700203 log.info('Monitor connections')
Khen Nursimulu68b9be32016-10-25 11:57:04 -0400204
Zsolt Haraszticd22adc2016-10-25 00:13:06 -0700205 def forward_packet_in(self, device_id, ofp_packet_in):
206 datapath_id = self.device_id_to_datapath_id_map.get(device_id, None)
207 if datapath_id:
208 agent = self.agent_map[datapath_id]
209 agent.forward_packet_in(ofp_packet_in)
Zsolt Haraszti217a12e2016-12-19 16:37:55 -0800210
211 def forward_change_event(self, device_id, event):
212 datapath_id = self.device_id_to_datapath_id_map.get(device_id, None)
213 if datapath_id:
214 agent = self.agent_map[datapath_id]
215 agent.forward_change_event(event)