blob: 1e00a90bbba4c3686a468d84f91b80c4ec81d364 [file] [log] [blame]
Rizwan Haider30b33792016-08-18 02:11:18 -04001from xos.logger import Logger, logging
Rizwan Haidereb2cc772016-09-08 12:14:55 -04002from services.metronetwork.models import *
Rizwan Haider30b33792016-08-18 02:11:18 -04003from synchronizers.metronetwork.providers.metronetworkprovider import MetroNetworkProvider
4
5import requests, json
6from requests.auth import HTTPBasicAuth
7
8logger = Logger(level=logging.INFO)
9
10
11class MetroNetworkRestProvider(MetroNetworkProvider):
12 def __init__(self, networkdevice, **args):
13 MetroNetworkProvider.__init__(self, networkdevice, **args)
14
15 def get_network_ports(self):
16
17 objs = []
18
19 restCtrlUrl = self.networkdevice.restCtrlUrl
20 username = self.networkdevice.username
21 password = self.networkdevice.password
22
23 resp = requests.get("{}/mef-sca-api/SCA_ETH_FPP_UNI_N".format(restCtrlUrl),
24 auth=HTTPBasicAuth(username, password))
25
26 if resp.status_code == 200:
27 for uni in resp.json():
28 hostname = uni['transportPort']['Hostname']
29 port = uni['transportPort']['Port']
30
31 # Default values
32 bwpCfgCbs = 0
33 bwpCfgEbs = 0
34 bwpCfgCir = 0
35 bwpCfgEir = 0
36
37 if 'interfaceCfgIngressBwp' in uni:
38 bwpCfgCbs = uni['interfaceCfgIngressBwp']['bwpCfgCbs']
39 bwpCfgEbs = uni['interfaceCfgIngressBwp']['bwpCfgEbs']
40 bwpCfgCir = uni['interfaceCfgIngressBwp']['bwpCfgCir']
41 bwpCfgEir = uni['interfaceCfgIngressBwp']['bwpCfgEir']
42
43 uniPort = NetworkEdgePort()
44 uniPort.element = self.networkdevice
45 uniPort.pid = "{}.{}/{}".format(self.networkdevice.id, hostname, port)
46 uniPort.bwpCfgCbs = bwpCfgCbs
47 uniPort.bwpCfgEbs = bwpCfgEbs
48 uniPort.bwpCfgCir = bwpCfgCir
49 uniPort.bwpCfgEir = bwpCfgEir
50
51 objs.append(uniPort)
52
53 return objs
54
55 else:
56 raise Exception("OnosApiError: get_network_ports()")
57
58 def get_network_links(self):
59
60 objs = []
61
62 restCtrlUrl = self.networkdevice.restCtrlUrl
63 username = self.networkdevice.username
64 password = self.networkdevice.password
65
66 resp = requests.get("{}/mef-sca-api/SCA_ETH_FDFr_EC/findByState?state=Active".format(restCtrlUrl),
67 auth=HTTPBasicAuth(username, password))
68
69 if resp.status_code == 200:
70 for evc in resp.json():
71 id = evc['id']
72 evcServiceType = evc['evcServiceType']
73
74 if (evcServiceType == "Point_To_Point"):
75 uni1 = evc['SCA_ETH_Flow_Points'][0]
76 hostname = uni1['scaEthFppUniN']['transportPort']['Hostname']
77 port = uni1['scaEthFppUniN']['transportPort']['Port']
78
79 if 'interfaceCfgIngressBwp' in uni1['scaEthFppUniN']:
80 bwpCfgCbs = uni1['scaEthFppUniN']['interfaceCfgIngressBwp']['bwpCfgCbs']
81 bwpCfgEbs = uni1['scaEthFppUniN']['interfaceCfgIngressBwp']['bwpCfgEbs']
82 bwpCfgCir = uni1['scaEthFppUniN']['interfaceCfgIngressBwp']['bwpCfgCir']
83 bwpCfgEir = uni1['scaEthFppUniN']['interfaceCfgIngressBwp']['bwpCfgEir']
84
85 edgePort1 = NetworkEdgePort()
86 edgePort1.element = self.networkdevice
87 edgePort1.pid = "{}.{}/{}".format(self.networkdevice.id, hostname, port)
88 edgePort1.bwpCfgCbs = bwpCfgCbs
89 edgePort1.bwpCfgEbs = bwpCfgEbs
90 edgePort1.bwpCfgCir = bwpCfgCir
91 edgePort1.bwpCfgEir = bwpCfgEir
92
93 uni2 = evc['SCA_ETH_Flow_Points'][1]
94 hostname = uni2['scaEthFppUniN']['transportPort']['Hostname']
95 port = uni2['scaEthFppUniN']['transportPort']['Port']
96
97 if 'interfaceCfgIngressBwp' in uni1['scaEthFppUniN']:
98 bwpCfgCbs = uni2['scaEthFppUniN']['interfaceCfgIngressBwp']['bwpCfgCbs']
99 bwpCfgEbs = uni2['scaEthFppUniN']['interfaceCfgIngressBwp']['bwpCfgEbs']
100 bwpCfgCir = uni2['scaEthFppUniN']['interfaceCfgIngressBwp']['bwpCfgCir']
101 bwpCfgEir = uni2['scaEthFppUniN']['interfaceCfgIngressBwp']['bwpCfgEir']
102
103 edgePort2 = NetworkEdgePort()
104 edgePort2.element = self.networkdevice
105 edgePort2.pid = "{}.{}/{}".format(self.networkdevice.id, hostname, port)
106 edgePort2.bwpCfgCbs = bwpCfgCbs
107 edgePort2.bwpCfgEbs = bwpCfgEbs
108 edgePort2.bwpCfgCir = bwpCfgCir
109 edgePort2.bwpCfgEir = bwpCfgEir
110
111 edgeToEdgeConnectivity = NetworkEdgeToEdgePointConnection()
112 edgeToEdgeConnectivity.sid = id
113 edgeToEdgeConnectivity.type = evcServiceType
114 edgeToEdgeConnectivity.uni1 = edgePort1
115 edgeToEdgeConnectivity.uni2 = edgePort2
116 edgeToEdgeConnectivity.operstate = "active"
117 edgeToEdgeConnectivity.adminstate = "enabled"
118
119 objs.append(edgeToEdgeConnectivity)
120
121 return objs
122
123 else:
124 raise Exception("OnosApiError: get_network_links()")
125
126 def create_point_to_point_connectivity(self, obj):
127
128 restCtrlUrl = self.networkdevice.restCtrlUrl
129 username = self.networkdevice.username
130 password = self.networkdevice.password
131
132 evcServiceType = obj.type
133 # evcServiceType = "Point_To_Point"
134
135 uni1 = obj.uni1
136 uni1Id = uni1.pid
137 uni1IdToken = (uni1Id.split('.', 1))[1].split('/', 1)
138 uni1Hostname = uni1IdToken[0]
139 uni1Port = uni1IdToken[1]
140 uni1BwpCfgCbs = uni1.bwpCfgCbs
141 uni1BwpCfgEbs = uni1.bwpCfgEbs
142 uni1BwpCfgCir = uni1.bwpCfgCir
143 uni1BwpCfgEir = uni1.bwpCfgEir
144
145 uni2 = obj.uni2
146 uni2Id = uni2.pid
147 uni2IdToken = (uni2Id.split('.', 1))[1].split('/', 1)
148 uni2Hostname = uni2IdToken[0]
149 uni2Port = uni2IdToken[1]
150 uni2BwpCfgCbs = uni2.bwpCfgCbs
151 uni2BwpCfgEbs = uni2.bwpCfgEbs
152 uni2BwpCfgCir = uni2.bwpCfgCir
153 uni2BwpCfgEir = uni2.bwpCfgEir
154
155 data = {
156 "evcServiceType": evcServiceType,
157 "SCA_ETH_Flow_Points": [
158 {
159 "scaEthFppUniN": {"transportPort": {"Hostname": uni1Hostname, "Port": uni1Port},
160 "interfaceCfgIngressBwp": {"bwpCfgCbs": uni1BwpCfgCbs,
161 "bwpCfgEbs": uni1BwpCfgEbs,
162 "bwpCfgCir": uni1BwpCfgCir,
163 "bwpCfgEir": uni1BwpCfgEir}}},
164 {
165 "scaEthFppUniN": {"transportPort": {"Hostname": uni2Hostname, "Port": uni2Port},
166 "interfaceCfgIngressBwp": {"bwpCfgCbs": uni2BwpCfgCbs,
167 "bwpCfgEbs": uni2BwpCfgEbs,
168 "bwpCfgCir": uni2BwpCfgCir,
169 "bwpCfgEir": uni2BwpCfgEir}}}]
170 }
171
172 headers = {'Content-Type': 'application/json'}
173
174 resp = requests.post('{}/mef-sca-api/SCA_ETH_FDFr_EC'.format(restCtrlUrl),
175 data=json.dumps(data), headers=headers, auth=HTTPBasicAuth(username, password))
176
177 if resp.status_code == 201:
178 result = resp.json()
179 message = result['message']
180
181 msg_token = message.split()
182 for i, token in enumerate(msg_token):
183 if token == 'id':
184 service_id = msg_token[i + 1]
185 obj.sid = service_id
186 obj.adminstate = "enabled"
187 obj.operstate = "active"
188
189 return True
190
191 elif resp.status_code == 204:
192 obj.adminstate = "invalid" # what's the meaning?
193 obj.operstate = "inactive"
194 obj.backend_status = '204 - No network resource'
195 return False
196
197 elif resp.status_code == 500:
198 obj.adminstate = "enabled"
199 obj.operstate = "inactive"
200 obj.backend_status = '500 - Internal Server Error'
201 return False
202
203 else:
204 raise Exception("OnosApiError: create_point_to_point_connectivity()")
205
206 def delete_point_to_point_connectivity(self, obj):
207
208 restCtrlUrl = self.networkdevice.restCtrlUrl
209 username = self.networkdevice.username
210 password = self.networkdevice.password
211 evcId = obj.sid
212
213 resp = requests.delete("{}/mef-sca-api/SCA_ETH_FDFr_EC/{}".format(restCtrlUrl, evcId),
214 auth=HTTPBasicAuth(username, password))
215
216 if resp.status_code == 200:
217 obj.adminstate = 'disabled'
218 obj.operstate = 'inactive'
219 return True
220
221 elif resp.status_code == 204:
222 obj.adminstate = "invalid" # what's the meaning?
223 obj.backend_status = '204 - No such network resource: {}'.format(evcId)
224 return False
225
226 elif resp.status_code == 500:
227 obj.adminstate = "disabled"
228 obj.backend_status = '500 - Internal Server Error'
229 return False
230
231 else:
232 raise Exception("OnosApiError: delete_point_to_point_connectivity()")