blob: 11bc0c0acededa34dabef39c807183f3220c12e7 [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#
alshabib06b449c2017-01-15 17:33:16 -060016import os
Khen Nursimulu68b9be32016-10-25 11:57:04 -040017
18import sys
19
20from twisted.internet import reactor
21from twisted.internet.defer import Deferred, inlineCallbacks, returnValue
22
23from common.utils.asleep import asleep
24from common.utils.consulhelpers import get_endpoint_from_consul
25from structlog import get_logger
26import grpc
Rouzbahan Rashidi-Tabrizi34b7dd72017-03-13 17:45:20 -040027from grpc import StatusCode
28from grpc._channel import _Rendezvous
Zsolt Haraszti1edb8282016-11-08 10:57:19 -080029from ofagent.protos import third_party
Khen Nursimulu68b9be32016-10-25 11:57:04 -040030from protos import voltha_pb2
31from grpc_client import GrpcClient
32
33from agent import Agent
Zsolt Haraszti7eeb2b32016-11-06 14:04:55 -080034from google.protobuf.empty_pb2 import Empty
35
Khen Nursimulu68b9be32016-10-25 11:57:04 -040036
Zsolt Haraszticd22adc2016-10-25 00:13:06 -070037log = get_logger()
Zsolt Haraszti7eeb2b32016-11-06 14:04:55 -080038# _ = third_party
Zsolt Haraszticd22adc2016-10-25 00:13:06 -070039
Khen Nursimulu68b9be32016-10-25 11:57:04 -040040class ConnectionManager(object):
41
Khen Nursimulu68b9be32016-10-25 11:57:04 -040042 def __init__(self, consul_endpoint, voltha_endpoint, controller_endpoint,
Zsolt Haraszticd22adc2016-10-25 00:13:06 -070043 voltha_retry_interval=0.5, devices_refresh_interval=5):
Khen Nursimulu68b9be32016-10-25 11:57:04 -040044
Zsolt Haraszticd22adc2016-10-25 00:13:06 -070045 log.info('init-connection-manager')
Khen Nursimulu68b9be32016-10-25 11:57:04 -040046 self.controller_endpoint = controller_endpoint
47 self.consul_endpoint = consul_endpoint
48 self.voltha_endpoint = voltha_endpoint
49
50 self.channel = None
Zsolt Haraszticd22adc2016-10-25 00:13:06 -070051 self.grpc_client = None # single, shared gRPC client to Voltha
52
53 self.agent_map = {} # datapath_id -> Agent()
54 self.device_id_to_datapath_id_map = {}
Khen Nursimulu68b9be32016-10-25 11:57:04 -040055
56 self.voltha_retry_interval = voltha_retry_interval
57 self.devices_refresh_interval = devices_refresh_interval
58
59 self.running = False
60
Zsolt Haraszti2bdb6b32016-11-03 16:56:17 -070061 def start(self):
Zsolt Haraszticd22adc2016-10-25 00:13:06 -070062
Khen Nursimulu68b9be32016-10-25 11:57:04 -040063 if self.running:
64 return
65
Zsolt Haraszti2bdb6b32016-11-03 16:56:17 -070066 log.debug('starting')
Khen Nursimulu68b9be32016-10-25 11:57:04 -040067
68 self.running = True
69
70 # Get voltha grpc endpoint
71 self.channel = self.get_grpc_channel_with_voltha()
72
Khen Nursimulu68b9be32016-10-25 11:57:04 -040073 # Create shared gRPC API object
Zsolt Haraszti2bdb6b32016-11-03 16:56:17 -070074 self.grpc_client = GrpcClient(self, self.channel).start()
Khen Nursimulu68b9be32016-10-25 11:57:04 -040075
Zsolt Haraszticd22adc2016-10-25 00:13:06 -070076 # Start monitoring logical devices and manage agents accordingly
77 reactor.callLater(0, self.monitor_logical_devices)
Khen Nursimulu68b9be32016-10-25 11:57:04 -040078
Zsolt Haraszti2bdb6b32016-11-03 16:56:17 -070079 log.info('started')
80
Zsolt Haraszticd22adc2016-10-25 00:13:06 -070081 return self
Khen Nursimulu68b9be32016-10-25 11:57:04 -040082
Zsolt Haraszti2bdb6b32016-11-03 16:56:17 -070083 def stop(self):
84 log.debug('stopping')
Khen Nursimulu68b9be32016-10-25 11:57:04 -040085 # clean up all controller connections
Zsolt Haraszti2bdb6b32016-11-03 16:56:17 -070086 for agent in self.agent_map.itervalues():
87 agent.stop()
Khen Nursimulu68b9be32016-10-25 11:57:04 -040088 self.running = False
Zsolt Haraszti2bdb6b32016-11-03 16:56:17 -070089 self.grpc_client.stop()
90 del self.channel
91 log.info('stopped')
Khen Nursimulu68b9be32016-10-25 11:57:04 -040092
93 def resolve_endpoint(self, endpoint):
94 ip_port_endpoint = endpoint
95 if endpoint.startswith('@'):
96 try:
97 ip_port_endpoint = get_endpoint_from_consul(
98 self.consul_endpoint, endpoint[1:])
Zsolt Haraszticd22adc2016-10-25 00:13:06 -070099 log.info(
Khen Nursimulu68b9be32016-10-25 11:57:04 -0400100 'Found endpoint {} service at {}'.format(endpoint,
101 ip_port_endpoint))
102 except Exception as e:
Zsolt Haraszticd22adc2016-10-25 00:13:06 -0700103 log.error('Failure to locate {} service from '
Khen Nursimulu68b9be32016-10-25 11:57:04 -0400104 'consul {}:'.format(endpoint, repr(e)))
alshabib06b449c2017-01-15 17:33:16 -0600105 log.error('Committing suicide...')
106 # Committing suicide in order to let docker restart ofagent
107 os.system("kill -15 {}".format(os.getpid()))
Khen Nursimulu68b9be32016-10-25 11:57:04 -0400108 if ip_port_endpoint:
109 host, port = ip_port_endpoint.split(':', 2)
110 return host, int(port)
111
112 def get_grpc_channel_with_voltha(self):
Zsolt Haraszticd22adc2016-10-25 00:13:06 -0700113 log.info('Resolving voltha endpoint {} from consul'.format(
Khen Nursimulu68b9be32016-10-25 11:57:04 -0400114 self.voltha_endpoint))
115 host, port = self.resolve_endpoint(self.voltha_endpoint)
116 assert host is not None
117 assert port is not None
118 # Create grpc channel to Voltha
119 channel = grpc.insecure_channel('{}:{}'.format(host, port))
Zsolt Haraszticd22adc2016-10-25 00:13:06 -0700120 log.info('Acquired a grpc channel to voltha')
Khen Nursimulu68b9be32016-10-25 11:57:04 -0400121 return channel
122
Khen Nursimulu68b9be32016-10-25 11:57:04 -0400123 @inlineCallbacks
124 def get_list_of_logical_devices_from_voltha(self):
Zsolt Haraszticd22adc2016-10-25 00:13:06 -0700125
Khen Nursimulu68b9be32016-10-25 11:57:04 -0400126 while True:
Zsolt Haraszticd22adc2016-10-25 00:13:06 -0700127 log.info('Retrieve devices from voltha')
Khen Nursimulu68b9be32016-10-25 11:57:04 -0400128 try:
Zsolt Haraszti66862032016-11-28 14:28:39 -0800129 stub = voltha_pb2.VolthaLocalServiceStub(self.channel)
Zsolt Haraszti7eeb2b32016-11-06 14:04:55 -0800130 devices = stub.ListLogicalDevices(Empty()).items
Khen Nursimulu68b9be32016-10-25 11:57:04 -0400131 for device in devices:
Zsolt Haraszticd22adc2016-10-25 00:13:06 -0700132 log.info("Devices {} -> {}".format(device.id,
Zsolt Haraszti3300f742017-01-09 01:14:20 -0800133 device.datapath_id))
Zsolt Haraszticd22adc2016-10-25 00:13:06 -0700134 returnValue(devices)
135
Rouzbahan Rashidi-Tabrizi34b7dd72017-03-13 17:45:20 -0400136 except _Rendezvous, e:
137 if e.code() == StatusCode.UNAVAILABLE:
138 os.system("kill -15 {}".format(os.getpid()))
139
Khen Nursimulu68b9be32016-10-25 11:57:04 -0400140 except Exception as e:
Zsolt Haraszticd22adc2016-10-25 00:13:06 -0700141 log.error('Failure to retrieve devices from '
Khen Nursimulu68b9be32016-10-25 11:57:04 -0400142 'voltha: {}'.format(repr(e)))
143
Zsolt Haraszticd22adc2016-10-25 00:13:06 -0700144 log.info('reconnect', after_delay=self.voltha_retry_interval)
Khen Nursimulu68b9be32016-10-25 11:57:04 -0400145 yield asleep(self.voltha_retry_interval)
146
Zsolt Haraszticd22adc2016-10-25 00:13:06 -0700147 def refresh_agent_connections(self, devices):
148 """
149 Based on the new device list, update the following state in the class:
150 * agent_map
151 * datapath_map
152 * device_id_map
153 :param devices: full device list freshly received from Voltha
154 :return: None
155 """
Khen Nursimulu68b9be32016-10-25 11:57:04 -0400156
Zsolt Haraszticd22adc2016-10-25 00:13:06 -0700157 # Use datapath ids for deciding what's new and what's obsolete
158 desired_datapath_ids = set(d.datapath_id for d in devices)
159 current_datapath_ids = set(self.agent_map.iterkeys())
Khen Nursimulu68b9be32016-10-25 11:57:04 -0400160
Zsolt Haraszticd22adc2016-10-25 00:13:06 -0700161 # if identical, nothing to do
162 if desired_datapath_ids == current_datapath_ids:
163 return
Khen Nursimulu68b9be32016-10-25 11:57:04 -0400164
Zsolt Haraszticd22adc2016-10-25 00:13:06 -0700165 # ... otherwise calculate differences
166 to_add = desired_datapath_ids.difference(current_datapath_ids)
167 to_del = current_datapath_ids.difference(desired_datapath_ids)
Khen Nursimulu68b9be32016-10-25 11:57:04 -0400168
Zsolt Haraszticd22adc2016-10-25 00:13:06 -0700169 # remove what we don't need
170 for datapath_id in to_del:
171 self.delete_agent(datapath_id)
Khen Nursimulu68b9be32016-10-25 11:57:04 -0400172
Zsolt Haraszticd22adc2016-10-25 00:13:06 -0700173 # start new agents as needed
174 for device in devices:
175 if device.datapath_id in to_add:
176 self.create_agent(device)
Khen Nursimulu68b9be32016-10-25 11:57:04 -0400177
Zsolt Haraszticd22adc2016-10-25 00:13:06 -0700178 log.debug('updated-agent-list', count=len(self.agent_map))
179 log.debug('updated-device-id-to-datapath-id-map',
180 map=str(self.device_id_to_datapath_id_map))
181
182 def create_agent(self, device):
183 datapath_id = device.datapath_id
184 device_id = device.id
185 agent = Agent(self.controller_endpoint, datapath_id,
186 device_id, self.grpc_client)
Zsolt Haraszti2bdb6b32016-11-03 16:56:17 -0700187 agent.start()
Zsolt Haraszticd22adc2016-10-25 00:13:06 -0700188 self.agent_map[datapath_id] = agent
189 self.device_id_to_datapath_id_map[device_id] = datapath_id
190
191 def delete_agent(self, datapath_id):
192 agent = self.agent_map[datapath_id]
193 device_id = agent.get_device_id()
194 agent.stop()
195 del self.agent_map[datapath_id]
196 del self.device_id_to_datapath_id_map[device_id]
Khen Nursimulu68b9be32016-10-25 11:57:04 -0400197
198 @inlineCallbacks
Zsolt Haraszticd22adc2016-10-25 00:13:06 -0700199 def monitor_logical_devices(self):
Khen Nursimulu68b9be32016-10-25 11:57:04 -0400200 while True:
alshabibc3fb4942017-01-26 15:34:24 -0800201 # should change to a gRPC streaming call
202 # see https://jira.opencord.org/browse/CORD-821
Zsolt Haraszticd22adc2016-10-25 00:13:06 -0700203
204 # get current list from Voltha
205 devices = yield self.get_list_of_logical_devices_from_voltha()
206
207 # update agent list and mapping tables as needed
208 self.refresh_agent_connections(devices)
209
210 # wait before next poll
Khen Nursimulu68b9be32016-10-25 11:57:04 -0400211 yield asleep(self.devices_refresh_interval)
Zsolt Haraszticd22adc2016-10-25 00:13:06 -0700212 log.info('Monitor connections')
Khen Nursimulu68b9be32016-10-25 11:57:04 -0400213
Zsolt Haraszticd22adc2016-10-25 00:13:06 -0700214 def forward_packet_in(self, device_id, ofp_packet_in):
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_packet_in(ofp_packet_in)
Zsolt Haraszti217a12e2016-12-19 16:37:55 -0800219
220 def forward_change_event(self, device_id, event):
221 datapath_id = self.device_id_to_datapath_id_map.get(device_id, None)
222 if datapath_id:
223 agent = self.agent_map[datapath_id]
224 agent.forward_change_event(event)