blob: 71ac056b060cf16893dac58e8ff02806eeb76815 [file] [log] [blame]
Matteo Scandolo48d3d2d2017-08-08 13:05:27 -07001
2# Copyright 2017-present Open Networking Foundation
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
A R Karthick36cfcef2016-08-18 15:20:07 -070017#
Chetan Gaonkercfcce782016-05-10 10:10:42 -070018# Copyright 2016-present Ciena Corporation
19#
20# Licensed under the Apache License, Version 2.0 (the "License");
21# you may not use this file except in compliance with the License.
22# You may obtain a copy of the License at
A R Karthick36cfcef2016-08-18 15:20:07 -070023#
Chetan Gaonkercfcce782016-05-10 10:10:42 -070024# http://www.apache.org/licenses/LICENSE-2.0
A R Karthick36cfcef2016-08-18 15:20:07 -070025#
Chetan Gaonkercfcce782016-05-10 10:10:42 -070026# Unless required by applicable law or agreed to in writing, software
27# distributed under the License is distributed on an "AS IS" BASIS,
28# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
29# See the License for the specific language governing permissions and
30# limitations under the License.
31#
Chetan Gaonker7997bb42016-03-28 09:46:15 -070032import os
33import json
34##load the olt config
35
36class OltConfig:
37 def __init__(self, olt_conf_file = ''):
38 if not olt_conf_file:
39 self.olt_conf_file = os.getenv('OLT_CONFIG')
40 else:
41 self.olt_conf_file = olt_conf_file
42 try:
43 self.olt_handle = open(self.olt_conf_file, 'r')
44 self.olt_conf = json.load(self.olt_handle)
Chetan Gaonker4ca5cca2016-04-11 13:59:35 -070045 self.olt_conf['olt'] = True
Chetan Gaonker7997bb42016-03-28 09:46:15 -070046 except:
47 self.olt_handle = None
48 self.olt_conf = {}
49 self.olt_conf['olt'] = False
A R Karthick7c864552016-07-27 10:47:04 -070050
Chetan Gaonker7997bb42016-03-28 09:46:15 -070051 def on_olt(self):
52 return self.olt_conf['olt'] is True
53
54 def olt_port_map(self):
Chetan Gaonker5209fe82016-04-19 10:09:53 -070055 if self.on_olt() and self.olt_conf.has_key('port_map'):
Chetan Gaonker7997bb42016-03-28 09:46:15 -070056 port_map = {}
A.R Karthick4f583842017-06-09 17:15:47 -070057 port_map['ponsim'] = self.olt_conf['port_map'].has_key('ponsim')
A.R Karthick88e80b92016-12-05 20:23:45 -080058 if self.olt_conf['port_map'].has_key('switches'):
59 port_map['switches'] = self.olt_conf['port_map']['switches']
60 else:
61 port_map['switches'] = []
62 nr_switches = 1
63 if self.olt_conf['port_map'].has_key('nr_switches'):
64 nr_switches = int(self.olt_conf['port_map']['nr_switches'])
65 for sw in xrange(nr_switches):
66 switch = 'br-int{}'.format(sw+1) if sw > 0 else 'br-int'
67 port_map['switches'].append(switch)
68 #if we have a host interface enabled, invalidate the switches config
69 if self.olt_conf['port_map'].has_key('host'):
70 #if host interface is specified, then use the host instead of ovs switch
71 port_map['host'] = self.olt_conf['port_map']['host']
A R Karthicke8bd80e2017-08-01 12:36:24 -070072 port_map['switches'] = [ port_map['host'] ] + port_map['switches']
A.R Karthick88e80b92016-12-05 20:23:45 -080073 else:
74 port_map['host'] = port_map['switches'][0]
75 nr_switches = len(port_map['switches'])
76 port_map['switch_port_list'] = []
A R Karthickb7e80902016-05-17 09:38:31 -070077 if self.olt_conf['port_map'].has_key('ports'):
78 port_map['ports'] = self.olt_conf['port_map']['ports']
A R Karthick36cfcef2016-08-18 15:20:07 -070079 num_ports = len(port_map['ports'])
A.R Karthick88e80b92016-12-05 20:23:45 -080080 port_map['switch_port_list'].append( (port_map['switches'][0], port_map['ports']) )
A R Karthicke8bd80e2017-08-01 12:36:24 -070081 index = 1
82 for switch in port_map['switches'][1:]:
83 port_start = index * num_ports * 2
84 port_end = port_start + num_ports * 2
85 index += 1
86 port_list = []
87 for port in xrange(port_start, port_end, 2):
88 port_name = 'veth{}'.format(port)
89 port_map['ports'].append(port_name)
90 port_list.append(port_name)
91 port_map['switch_port_list'].append( (switch, port_list) )
A R Karthickb7e80902016-05-17 09:38:31 -070092 else:
93 port_map['ports'] = []
94 num_ports = int(self.olt_conf['port_map']['num_ports'])
A.R Karthick88e80b92016-12-05 20:23:45 -080095 for sw in xrange(nr_switches):
96 port_list = []
97 switch = port_map['switches'][sw]
98 port_start = sw * num_ports * 2
99 port_end = port_start + num_ports * 2
100 for port in xrange(port_start, port_end, 2):
101 port_name = 'veth{}'.format(port)
102 port_map['ports'].append(port_name)
103 port_list.append(port_name)
104 port_map['switch_port_list'].append( (switch, port_list) )
A R Karthick36cfcef2016-08-18 15:20:07 -0700105 ##also add dhcprelay ports. We add as many relay ports as subscriber ports
A.R Karthick88e80b92016-12-05 20:23:45 -0800106 port_map['num_ports'] = num_ports
A R Karthick36cfcef2016-08-18 15:20:07 -0700107 relay_ports = num_ports
108 port_map['relay_ports'] = []
A.R Karthick88e80b92016-12-05 20:23:45 -0800109 port_map['switch_relay_port_list'] = []
A R Karthick1555c7c2017-09-07 14:59:41 -0700110 port_map['radius_ports'] = []
111 port_map['switch_radius_port_list'] = []
A.R Karthick88e80b92016-12-05 20:23:45 -0800112 for sw in xrange(nr_switches):
113 port_list = []
114 switch = port_map['switches'][sw]
115 port_start = (nr_switches + sw) * relay_ports * 2
116 port_end = port_start + relay_ports * 2
117 for port in xrange(port_start, port_end, 2):
118 port_name = 'veth{}'.format(port)
119 port_map['relay_ports'].append(port_name)
120 port_list.append(port_name)
121 port_map['switch_relay_port_list'].append( (switch, port_list) )
A R Karthick1555c7c2017-09-07 14:59:41 -0700122 for sw in xrange(nr_switches):
123 switch = port_map['switches'][sw]
124 if not switch.startswith('br-int'):
125 continue
126 port_name = 'veth{}'.format(port_end)
127 port_list = [ port_name ]
128 port_map['switch_radius_port_list'].append( (switch, port_list) )
129 port_map['radius_ports'].append(port_name)
130 port_end += 2
A R Karthickb7e80902016-05-17 09:38:31 -0700131 port_num = 1
132 port_map['uplink'] = int(self.olt_conf['uplink'])
A R Karthick07769362016-07-28 17:36:15 -0700133 port_map['wan'] = None
134 if self.olt_conf.has_key('wan'):
135 port_map['wan'] = self.olt_conf['wan']
A R Karthickb7e80902016-05-17 09:38:31 -0700136 port_list = []
137 ##build the port map and inverse port map
A.R Karthick88e80b92016-12-05 20:23:45 -0800138 for sw in xrange(nr_switches):
139 sw_portnum = 1
140 switch, ports = port_map['switch_port_list'][sw]
141 uplink = sw * num_ports + port_map['uplink']
142 port_map[switch] = {}
143 port_map[switch]['uplink'] = uplink
144 for p in ports:
145 port_map[port_num] = p
146 port_map[p] = port_num
147 if sw_portnum != port_map['uplink']:
148 #create tx, rx map
149 port_list.append( (uplink, port_num) )
150 port_num += 1
151 sw_portnum += 1
A R Karthick36cfcef2016-08-18 15:20:07 -0700152 ##build the port and inverse map for relay ports
153 for port in port_map['relay_ports']:
154 port_map[port_num] = port
155 port_map[port] = port_num
156 port_num += 1
A R Karthick1555c7c2017-09-07 14:59:41 -0700157 for port in port_map['radius_ports']:
158 port_map[port_num] = port
159 port_map[port] = port_num
160 port_num += 1
A R Karthickb7e80902016-05-17 09:38:31 -0700161 port_map['start_vlan'] = 0
A R Karthickb7e80902016-05-17 09:38:31 -0700162 if self.olt_conf['port_map'].has_key('start_vlan'):
163 port_map['start_vlan'] = int(self.olt_conf['port_map']['start_vlan'])
164
165 return port_map, port_list
166 else:
167 return None, None
168
Chetan Gaonkera2b87df2016-03-31 15:41:31 -0700169 def olt_device_data(self):
170 if self.on_olt():
171 accessDeviceDict = {}
172 accessDeviceDict['uplink'] = str(self.olt_conf['uplink'])
173 accessDeviceDict['vlan'] = str(self.olt_conf['vlan'])
174 return accessDeviceDict
175 return None
A.R Karthick369f89e2017-03-02 15:22:45 -0800176
177 def get_vcpes(self):
178 if self.on_olt():
179 if self.olt_conf.has_key('vcpe'):
180 return self.olt_conf['vcpe']
181 return []
A R Karthick03f40aa2017-03-20 19:33:55 -0700182
183 def get_vcpes_by_type(self, service):
184 return filter(lambda vcpe: vcpe['type'].lower() == service.lower(), self.get_vcpes())