Khen Nursimulu | 3869d8d | 2016-11-28 20:44:28 -0500 | [diff] [blame] | 1 | # |
Khen Nursimulu | c9ef7c1 | 2017-01-04 20:40:53 -0500 | [diff] [blame] | 2 | # Copyright 2017 the original author or authors. |
Khen Nursimulu | 3869d8d | 2016-11-28 20:44:28 -0500 | [diff] [blame] | 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 | from twisted.internet.defer import inlineCallbacks, returnValue |
| 18 | |
| 19 | from common.utils.consulhelpers import get_endpoint_from_consul |
| 20 | from structlog import get_logger |
| 21 | from netconf.nc_server import NCServer |
| 22 | |
| 23 | log = get_logger() |
| 24 | |
| 25 | |
| 26 | class ConnectionManager(object): |
| 27 | def __init__(self, |
| 28 | consul_endpoint, |
| 29 | voltha_endpoint, |
| 30 | netconf_port, |
| 31 | server_private_key_file, |
| 32 | server_public_key_file, |
| 33 | client_public_keys_file, |
| 34 | client_passwords_file, |
| 35 | voltha_retry_interval=0.5, |
| 36 | devices_refresh_interval=5): |
| 37 | |
| 38 | log.info('init-connection-manager') |
| 39 | self.netconf_port = netconf_port |
| 40 | self.server_private_key_file = server_private_key_file |
| 41 | self.server_public_key_file = server_public_key_file |
| 42 | self.client_public_keys_file = client_public_keys_file |
| 43 | self.client_passwords_file = client_passwords_file |
| 44 | self.consul_endpoint = consul_endpoint |
| 45 | self.voltha_endpoint = voltha_endpoint |
| 46 | |
| 47 | self.channel = None |
| 48 | self.grpc_client = None # single, shared gRPC client to Voltha |
| 49 | |
| 50 | self.nc_server = None |
| 51 | |
| 52 | self.voltha_retry_interval = voltha_retry_interval |
| 53 | self.devices_refresh_interval = devices_refresh_interval |
| 54 | |
| 55 | self.running = False |
| 56 | |
| 57 | @inlineCallbacks |
| 58 | def start(self): |
| 59 | |
| 60 | if self.running: |
| 61 | return |
| 62 | |
| 63 | log.debug('starting') |
| 64 | |
| 65 | self.running = True |
| 66 | |
| 67 | # # Get voltha grpc endpoint |
| 68 | # self.channel = self.get_grpc_channel_with_voltha() |
| 69 | # |
| 70 | # # Create shared gRPC API object |
| 71 | # self.grpc_client = GrpcClient(self, self.channel).start() |
| 72 | |
| 73 | # Start the netconf server |
| 74 | self.nc_server = yield self.start_netconf_server().start() |
| 75 | |
| 76 | log.info('started') |
| 77 | |
| 78 | returnValue(self) |
| 79 | |
| 80 | def stop(self): |
| 81 | log.debug('stopping') |
| 82 | self.running = False |
| 83 | # clean the netconf server |
| 84 | self.nc_server.stop() |
| 85 | log.info('stopped') |
| 86 | |
| 87 | def resolve_endpoint(self, endpoint): |
| 88 | ip_port_endpoint = endpoint |
| 89 | if endpoint.startswith('@'): |
| 90 | try: |
| 91 | ip_port_endpoint = get_endpoint_from_consul( |
| 92 | self.consul_endpoint, endpoint[1:]) |
| 93 | log.debug('found-service-from-consul', endpoint=endpoint, |
| 94 | ip_port=ip_port_endpoint) |
| 95 | |
| 96 | except Exception as e: |
| 97 | log.error('not-found-service-from-consul', |
| 98 | endpoint=endpoint, exception=repr(e)) |
| 99 | |
| 100 | return |
| 101 | if ip_port_endpoint: |
| 102 | host, port = ip_port_endpoint.split(':', 2) |
| 103 | return host, int(port) |
| 104 | |
| 105 | def start_netconf_server(self): |
| 106 | return NCServer(self.netconf_port, |
| 107 | self.server_private_key_file, |
| 108 | self.server_public_key_file, |
| 109 | self.client_public_keys_file, |
| 110 | self.client_passwords_file, |
| 111 | self.grpc_client) |