blob: f2496eac574b06bd2c6bf1fc84913a66cd0dab6e [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'] = []
110 for sw in xrange(nr_switches):
111 port_list = []
112 switch = port_map['switches'][sw]
113 port_start = (nr_switches + sw) * relay_ports * 2
114 port_end = port_start + relay_ports * 2
115 for port in xrange(port_start, port_end, 2):
116 port_name = 'veth{}'.format(port)
117 port_map['relay_ports'].append(port_name)
118 port_list.append(port_name)
119 port_map['switch_relay_port_list'].append( (switch, port_list) )
A R Karthickb7e80902016-05-17 09:38:31 -0700120 port_num = 1
121 port_map['uplink'] = int(self.olt_conf['uplink'])
A R Karthick07769362016-07-28 17:36:15 -0700122 port_map['wan'] = None
123 if self.olt_conf.has_key('wan'):
124 port_map['wan'] = self.olt_conf['wan']
A R Karthickb7e80902016-05-17 09:38:31 -0700125 port_list = []
126 ##build the port map and inverse port map
A.R Karthick88e80b92016-12-05 20:23:45 -0800127 for sw in xrange(nr_switches):
128 sw_portnum = 1
129 switch, ports = port_map['switch_port_list'][sw]
130 uplink = sw * num_ports + port_map['uplink']
131 port_map[switch] = {}
132 port_map[switch]['uplink'] = uplink
133 for p in ports:
134 port_map[port_num] = p
135 port_map[p] = port_num
136 if sw_portnum != port_map['uplink']:
137 #create tx, rx map
138 port_list.append( (uplink, port_num) )
139 port_num += 1
140 sw_portnum += 1
A R Karthick36cfcef2016-08-18 15:20:07 -0700141 ##build the port and inverse map for relay ports
142 for port in port_map['relay_ports']:
143 port_map[port_num] = port
144 port_map[port] = port_num
145 port_num += 1
A R Karthickb7e80902016-05-17 09:38:31 -0700146 port_map['start_vlan'] = 0
A R Karthickb7e80902016-05-17 09:38:31 -0700147 if self.olt_conf['port_map'].has_key('start_vlan'):
148 port_map['start_vlan'] = int(self.olt_conf['port_map']['start_vlan'])
149
150 return port_map, port_list
151 else:
152 return None, None
153
Chetan Gaonkera2b87df2016-03-31 15:41:31 -0700154 def olt_device_data(self):
155 if self.on_olt():
156 accessDeviceDict = {}
157 accessDeviceDict['uplink'] = str(self.olt_conf['uplink'])
158 accessDeviceDict['vlan'] = str(self.olt_conf['vlan'])
159 return accessDeviceDict
160 return None
A.R Karthick369f89e2017-03-02 15:22:45 -0800161
162 def get_vcpes(self):
163 if self.on_olt():
164 if self.olt_conf.has_key('vcpe'):
165 return self.olt_conf['vcpe']
166 return []
A R Karthick03f40aa2017-03-20 19:33:55 -0700167
168 def get_vcpes_by_type(self, service):
169 return filter(lambda vcpe: vcpe['type'].lower() == service.lower(), self.get_vcpes())