blob: 0d8c8d46e6490935d26080d84b67c72984541db7 [file] [log] [blame]
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -07001#!/usr/bin/env python
2#
3# Copyright 2016 the original author or authors.
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16#
17
18"""TODO This is a POC placeholder """
Zsolt Haraszti8a774382016-10-24 18:25:54 -070019import os
20
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -070021import grpc
Zsolt Haraszti8a774382016-10-24 18:25:54 -070022import yaml
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -070023from twisted.internet import reactor
24
25from agent import Agent
Zsolt Haraszti8a774382016-10-24 18:25:54 -070026from common.utils.structlog_setup import setup_logging
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -070027from protos import voltha_pb2
28
29from grpc_client import GrpcClient
30
31
Zsolt Haraszti8a774382016-10-24 18:25:54 -070032def load_config(path):
33 if path.startswith('.'):
34 dir = os.path.dirname(os.path.abspath(__file__))
35 path = os.path.join(dir, path)
36 path = os.path.abspath(path)
37 with open(path) as fd:
38 config = yaml.load(fd)
39 return config
40
41
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -070042if __name__ == '__main__':
43
Zsolt Haraszti8a774382016-10-24 18:25:54 -070044 # Load config and setup logging
45 config = load_config('./ofagent.yml')
46 setup_logging(config.get('logging', {}), '1')
47
48
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -070049 # Create grpc channel to Voltha and grab client stub
50 channel = grpc.insecure_channel('localhost:50055')
51
52 # Connect to voltha using grpc and fetch the list of logical devices
53 stub = voltha_pb2.VolthaLogicalLayerStub(channel)
54 devices = stub.ListLogicalDevices(voltha_pb2.NullMessage()).items
55 print 'device id and datapaht_id list:'
56 for device in devices:
57 print '\t{} -> {}'.format(device.id, device.datapath_id)
58
59 # make a device.datapath_id -> device.id map (this will need to be actively
60 # managed in the real agent based on devices coming and going
61 device_id_map = dict((device.datapath_id, device.id) for device in devices)
62
63 # Create shared gRPC API object
64 grpc_client = GrpcClient(channel, device_id_map)
65
66 # Instantiate an OpenFlow agent for each logical device
67 agents = [
68 Agent('localhost:6633', device.datapath_id, grpc_client).run()
69 for device in devices
70 ]
71
72 def shutdown():
73 [a.stop() for a in agents]
74
75 reactor.addSystemEventTrigger('before', 'shutdown', shutdown)
76 reactor.run()