blob: 3b4254d1b943d7569ac61e4149aafb03514cf2f1 [file] [log] [blame]
A R Karthick36cfcef2016-08-18 15:20:07 -07001#
Chetan Gaonkercfcce782016-05-10 10:10:42 -07002# Copyright 2016-present Ciena Corporation
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
A R Karthick36cfcef2016-08-18 15:20:07 -07007#
Chetan Gaonkercfcce782016-05-10 10:10:42 -07008# http://www.apache.org/licenses/LICENSE-2.0
A R Karthick36cfcef2016-08-18 15:20:07 -07009#
Chetan Gaonkercfcce782016-05-10 10:10:42 -070010# 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#
Chetan Gaonker7997bb42016-03-28 09:46:15 -070016import os
17import json
18##load the olt config
19
20class OltConfig:
21 def __init__(self, olt_conf_file = ''):
22 if not olt_conf_file:
23 self.olt_conf_file = os.getenv('OLT_CONFIG')
24 else:
25 self.olt_conf_file = olt_conf_file
26 try:
27 self.olt_handle = open(self.olt_conf_file, 'r')
28 self.olt_conf = json.load(self.olt_handle)
Chetan Gaonker4ca5cca2016-04-11 13:59:35 -070029 self.olt_conf['olt'] = True
Chetan Gaonker7997bb42016-03-28 09:46:15 -070030 except:
31 self.olt_handle = None
32 self.olt_conf = {}
33 self.olt_conf['olt'] = False
A R Karthick7c864552016-07-27 10:47:04 -070034
Chetan Gaonker7997bb42016-03-28 09:46:15 -070035 def on_olt(self):
36 return self.olt_conf['olt'] is True
37
38 def olt_port_map(self):
Chetan Gaonker5209fe82016-04-19 10:09:53 -070039 if self.on_olt() and self.olt_conf.has_key('port_map'):
Chetan Gaonker7997bb42016-03-28 09:46:15 -070040 port_map = {}
A.R Karthick4f583842017-06-09 17:15:47 -070041 port_map['ponsim'] = self.olt_conf['port_map'].has_key('ponsim')
A.R Karthick88e80b92016-12-05 20:23:45 -080042 if self.olt_conf['port_map'].has_key('switches'):
43 port_map['switches'] = self.olt_conf['port_map']['switches']
44 else:
45 port_map['switches'] = []
46 nr_switches = 1
47 if self.olt_conf['port_map'].has_key('nr_switches'):
48 nr_switches = int(self.olt_conf['port_map']['nr_switches'])
49 for sw in xrange(nr_switches):
50 switch = 'br-int{}'.format(sw+1) if sw > 0 else 'br-int'
51 port_map['switches'].append(switch)
52 #if we have a host interface enabled, invalidate the switches config
53 if self.olt_conf['port_map'].has_key('host'):
54 #if host interface is specified, then use the host instead of ovs switch
55 port_map['host'] = self.olt_conf['port_map']['host']
56 port_map['switches'] = [ port_map['host'] ]
57 else:
58 port_map['host'] = port_map['switches'][0]
59 nr_switches = len(port_map['switches'])
60 port_map['switch_port_list'] = []
A R Karthickb7e80902016-05-17 09:38:31 -070061 if self.olt_conf['port_map'].has_key('ports'):
62 port_map['ports'] = self.olt_conf['port_map']['ports']
A R Karthick36cfcef2016-08-18 15:20:07 -070063 num_ports = len(port_map['ports'])
A.R Karthick88e80b92016-12-05 20:23:45 -080064 port_map['switch_port_list'].append( (port_map['switches'][0], port_map['ports']) )
A R Karthickb7e80902016-05-17 09:38:31 -070065 else:
66 port_map['ports'] = []
67 num_ports = int(self.olt_conf['port_map']['num_ports'])
A.R Karthick88e80b92016-12-05 20:23:45 -080068 for sw in xrange(nr_switches):
69 port_list = []
70 switch = port_map['switches'][sw]
71 port_start = sw * num_ports * 2
72 port_end = port_start + num_ports * 2
73 for port in xrange(port_start, port_end, 2):
74 port_name = 'veth{}'.format(port)
75 port_map['ports'].append(port_name)
76 port_list.append(port_name)
77 port_map['switch_port_list'].append( (switch, port_list) )
A R Karthick36cfcef2016-08-18 15:20:07 -070078 ##also add dhcprelay ports. We add as many relay ports as subscriber ports
A.R Karthick88e80b92016-12-05 20:23:45 -080079 port_map['num_ports'] = num_ports
A R Karthick36cfcef2016-08-18 15:20:07 -070080 relay_ports = num_ports
81 port_map['relay_ports'] = []
A.R Karthick88e80b92016-12-05 20:23:45 -080082 port_map['switch_relay_port_list'] = []
83 for sw in xrange(nr_switches):
84 port_list = []
85 switch = port_map['switches'][sw]
86 port_start = (nr_switches + sw) * relay_ports * 2
87 port_end = port_start + relay_ports * 2
88 for port in xrange(port_start, port_end, 2):
89 port_name = 'veth{}'.format(port)
90 port_map['relay_ports'].append(port_name)
91 port_list.append(port_name)
92 port_map['switch_relay_port_list'].append( (switch, port_list) )
A R Karthickb7e80902016-05-17 09:38:31 -070093 port_num = 1
94 port_map['uplink'] = int(self.olt_conf['uplink'])
A R Karthick07769362016-07-28 17:36:15 -070095 port_map['wan'] = None
96 if self.olt_conf.has_key('wan'):
97 port_map['wan'] = self.olt_conf['wan']
A R Karthickb7e80902016-05-17 09:38:31 -070098 port_list = []
99 ##build the port map and inverse port map
A.R Karthick88e80b92016-12-05 20:23:45 -0800100 for sw in xrange(nr_switches):
101 sw_portnum = 1
102 switch, ports = port_map['switch_port_list'][sw]
103 uplink = sw * num_ports + port_map['uplink']
104 port_map[switch] = {}
105 port_map[switch]['uplink'] = uplink
106 for p in ports:
107 port_map[port_num] = p
108 port_map[p] = port_num
109 if sw_portnum != port_map['uplink']:
110 #create tx, rx map
111 port_list.append( (uplink, port_num) )
112 port_num += 1
113 sw_portnum += 1
A R Karthick36cfcef2016-08-18 15:20:07 -0700114 ##build the port and inverse map for relay ports
115 for port in port_map['relay_ports']:
116 port_map[port_num] = port
117 port_map[port] = port_num
118 port_num += 1
A R Karthickb7e80902016-05-17 09:38:31 -0700119 port_map['start_vlan'] = 0
A R Karthickb7e80902016-05-17 09:38:31 -0700120 if self.olt_conf['port_map'].has_key('start_vlan'):
121 port_map['start_vlan'] = int(self.olt_conf['port_map']['start_vlan'])
122
123 return port_map, port_list
124 else:
125 return None, None
126
Chetan Gaonkera2b87df2016-03-31 15:41:31 -0700127 def olt_device_data(self):
128 if self.on_olt():
129 accessDeviceDict = {}
130 accessDeviceDict['uplink'] = str(self.olt_conf['uplink'])
131 accessDeviceDict['vlan'] = str(self.olt_conf['vlan'])
132 return accessDeviceDict
133 return None
A.R Karthick369f89e2017-03-02 15:22:45 -0800134
135 def get_vcpes(self):
136 if self.on_olt():
137 if self.olt_conf.has_key('vcpe'):
138 return self.olt_conf['vcpe']
139 return []
A R Karthick03f40aa2017-03-20 19:33:55 -0700140
141 def get_vcpes_by_type(self, service):
142 return filter(lambda vcpe: vcpe['type'].lower() == service.lower(), self.get_vcpes())