Zsolt Haraszti | 023ea7c | 2016-10-16 19:30:34 -0700 | [diff] [blame] | 1 | #!/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 Haraszti | 8a77438 | 2016-10-24 18:25:54 -0700 | [diff] [blame] | 19 | import os |
| 20 | |
Zsolt Haraszti | 023ea7c | 2016-10-16 19:30:34 -0700 | [diff] [blame] | 21 | import grpc |
Zsolt Haraszti | 8a77438 | 2016-10-24 18:25:54 -0700 | [diff] [blame] | 22 | import yaml |
Zsolt Haraszti | 023ea7c | 2016-10-16 19:30:34 -0700 | [diff] [blame] | 23 | from twisted.internet import reactor |
| 24 | |
| 25 | from agent import Agent |
Zsolt Haraszti | 8a77438 | 2016-10-24 18:25:54 -0700 | [diff] [blame] | 26 | from common.utils.structlog_setup import setup_logging |
Zsolt Haraszti | 023ea7c | 2016-10-16 19:30:34 -0700 | [diff] [blame] | 27 | from protos import voltha_pb2 |
| 28 | |
| 29 | from grpc_client import GrpcClient |
| 30 | |
| 31 | |
Zsolt Haraszti | 8a77438 | 2016-10-24 18:25:54 -0700 | [diff] [blame] | 32 | def 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 Haraszti | 023ea7c | 2016-10-16 19:30:34 -0700 | [diff] [blame] | 42 | if __name__ == '__main__': |
| 43 | |
Zsolt Haraszti | 8a77438 | 2016-10-24 18:25:54 -0700 | [diff] [blame] | 44 | # Load config and setup logging |
| 45 | config = load_config('./ofagent.yml') |
| 46 | setup_logging(config.get('logging', {}), '1') |
| 47 | |
| 48 | |
Zsolt Haraszti | 023ea7c | 2016-10-16 19:30:34 -0700 | [diff] [blame] | 49 | # 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() |