Olt.py 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820
  1. from Base import Base
  2. from Flowdat.Client import Client
  3. from Util import Strings
  4. class Olt(Base):
  5. SQL = {}
  6. SQL_DELETE = {}
  7. ONU_DEACTIVATE = []
  8. def __init__(self):
  9. Base.__init__(self)
  10. self.SQL_DELETE['olt'] = []
  11. self.SQL_DELETE['vlan'] = []
  12. self.SQL_DELETE['tcont_profile'] = []
  13. self.SQL_DELETE['traffic_profile'] = []
  14. self.SQL_DELETE['profile'] = []
  15. self.SQL_DELETE['nap'] = []
  16. self.SQL_DELETE['client'] = []
  17. self.SQL_DELETE['onu_model'] = []
  18. self.SQL_DELETE['onus'] = []
  19. self.SQL_DELETE['service_ports'] = []
  20. self.SQL['vlan'] = []
  21. self.SQL['tcont_profile'] = []
  22. self.SQL['traffic_profile'] = []
  23. self.SQL['profile'] = []
  24. self.SQL['nap'] = []
  25. self.SQL['client'] = []
  26. self.SQL['onu_model'] = []
  27. self.SQL['onus'] = []
  28. self.SQL['service_ports'] = []
  29. def delete_sql_olt(self, database, olt, tenancy_id):
  30. """
  31. Create sql delete for olt and add to property SQL_DELETE
  32. :param database: Database name
  33. :param olt: Object olt
  34. :param tenancy_id: Tenancy id
  35. """
  36. self.SQL_DELETE['olt'] = Strings.delete_table(database, "olt",
  37. Strings.get_values_delete(
  38. {"id": olt.idDataBase, "tenancy_id": tenancy_id})) + "\n"
  39. def create_sql_olt(self, database, olt, tenancy_id, olt_model_id):
  40. """
  41. Create sql insert for olt
  42. :param database: Database name
  43. :param olt: Object olt
  44. :param tenancy_id: Tenancy id
  45. :param olt_model_id: Model id for olt
  46. :return: Return string with sql insert
  47. """
  48. return Strings.insert_table(database, "olt",
  49. "id, tenancy_id, model_id, name",
  50. Strings.get_values([olt.idDataBase, tenancy_id, olt_model_id, olt.name])) + "\n"
  51. def create_sql_object(self, load_disabled, database, database_client, tenancy_id, init_vlan, init_tcont_profile,
  52. init_traffic_profile, init_profile, init_nap, init_client, init_onu_model, init_onus, init_service_ports):
  53. """
  54. Create sql insert for all object
  55. :param load_disabled: Load disabled onus
  56. :param database: Database name
  57. :param database_client: Database name for client table
  58. :param tenancy_id: Tenancy id
  59. :param init_vlan: Value to init id for vlan table
  60. :param init_tcont_profile: Value to init id for tcont_profile table
  61. :param init_traffic_profile: Value to init id for taffic_profile table
  62. :param init_profile: Value to init id for profile table
  63. :param init_nap: Value to init id for nap table
  64. :param init_client: Value to init id for client table
  65. :param init_onu_model: Value to init id for onu_model table
  66. :param init_onus: Value to init id for onus table
  67. :return: Return string with sql insert
  68. """
  69. init_vlan = int(init_vlan)
  70. init_tcont_profile = int(init_tcont_profile)
  71. init_traffic_profile = int(init_traffic_profile)
  72. init_profile = int(init_profile)
  73. init_nap = int(init_nap)
  74. init_client = int(init_client)
  75. init_onu_model = int(init_onu_model)
  76. init_onus = int(init_onus)
  77. init_service_ports = int(init_service_ports)
  78. for gpon in self.child:
  79. if gpon:
  80. for obj in gpon.child:
  81. if obj.state:
  82. init_vlan = self.vlan(obj, database, init_vlan, tenancy_id)
  83. init_tcont_profile = self.tcont_profile(obj, database, init_tcont_profile, tenancy_id)
  84. init_traffic_profile = self.traffic_profile_ingress(obj, database, init_traffic_profile,
  85. tenancy_id)
  86. init_traffic_profile = self.traffic_profile_egress(obj, database, init_traffic_profile,
  87. tenancy_id)
  88. init_profile = self.profile_ingress(obj, database, init_profile, tenancy_id)
  89. init_profile = self.profile_egress(obj, database, init_profile, tenancy_id)
  90. init_nap = self.nap(obj, database, init_nap, tenancy_id)
  91. if obj.client is None:
  92. obj.client = Client()
  93. obj.client.name = obj.serial_number
  94. init_client = self.client(obj, database_client, init_client, tenancy_id)
  95. init_onu_model = self.onu_model(obj, database, init_onu_model, tenancy_id)
  96. init_onus = self.onus(gpon, obj, database, init_onus, tenancy_id)
  97. init_service_ports = self.service_ports(gpon, obj, database, init_service_ports, tenancy_id)
  98. else:
  99. if load_disabled == 1:
  100. # init_vlan = self.vlan(obj, database, init_vlan, tenancy_id)
  101. # init_tcont_profile = self.tcont_profile(obj, database, init_tcont_profile, tenancy_id)
  102. # init_traffic_profile = self.traffic_profile_ingress(obj, database, init_traffic_profile,
  103. # tenancy_id)
  104. # init_traffic_profile = self.traffic_profile_egress(obj, database, init_traffic_profile,
  105. # tenancy_id)
  106. # init_profile = self.profile_ingress(obj, database, init_profile, tenancy_id)
  107. # init_profile = self.profile_egress(obj, database, init_profile, tenancy_id)
  108. init_nap = self.nap(obj, database, init_nap, tenancy_id)
  109. if obj.client is None:
  110. obj.client = Client()
  111. obj.client.name = obj.serial_number
  112. init_client = self.client(obj, database_client, init_client, tenancy_id)
  113. init_onu_model = self.onu_model(obj, database, init_onu_model, tenancy_id)
  114. init_onus = self.onus_disabled(obj, database, init_onus, tenancy_id)
  115. else:
  116. self.ONU_DEACTIVATE.append("-- ONU Desactivada. Nombre: " + obj.name + ", serial numbre: " + obj.serial_number)
  117. return self.print_sql()
  118. def print_sql_delete(self):
  119. """
  120. :return: Create string in order to execute en sql console
  121. """
  122. resp = ""
  123. for obj in self.SQL_DELETE['onus']:
  124. resp = resp + obj + "\n"
  125. resp = resp + "\n"
  126. resp = resp + self.SQL_DELETE['olt']
  127. resp = resp + "\n"
  128. for obj in self.SQL_DELETE['vlan']:
  129. resp = resp + obj + "\n"
  130. resp = resp + "\n"
  131. for obj in self.SQL_DELETE['tcont_profile']:
  132. resp = resp + obj + "\n"
  133. resp = resp + "\n"
  134. for obj in self.SQL_DELETE['traffic_profile']:
  135. resp = resp + obj + "\n"
  136. resp = resp + "\n"
  137. for obj in self.SQL_DELETE['profile']:
  138. resp = resp + obj + "\n"
  139. resp = resp + "\n"
  140. for obj in self.SQL_DELETE['nap']:
  141. resp = resp + obj + "\n"
  142. resp = resp + "\n"
  143. for obj in self.SQL_DELETE['client']:
  144. resp = resp + obj + "\n"
  145. resp = resp + "\n"
  146. for obj in self.SQL_DELETE['onu_model']:
  147. resp = resp + obj + "\n"
  148. resp = resp + "\n"
  149. for obj in self.SQL_DELETE['service_ports']:
  150. resp = resp + obj + "\n"
  151. resp = resp + "\n"
  152. return resp
  153. def print_sql(self):
  154. """
  155. :return: Create string in order to execute en sql console
  156. """
  157. resp = ""
  158. for obj in self.SQL['vlan']:
  159. resp = resp + obj['sql'] + "\n"
  160. resp = resp + "\n"
  161. for obj in self.SQL['tcont_profile']:
  162. resp = resp + obj['sql'] + "\n"
  163. resp = resp + "\n"
  164. for obj in self.SQL['traffic_profile']:
  165. resp = resp + obj['sql'] + "\n"
  166. resp = resp + "\n"
  167. for obj in self.SQL['profile']:
  168. resp = resp + obj['sql'] + "\n"
  169. resp = resp + "\n"
  170. for obj in self.SQL['nap']:
  171. resp = resp + obj['sql'] + "\n"
  172. resp = resp + "\n"
  173. for obj in self.SQL['client']:
  174. resp = resp + obj['sql'] + "\n"
  175. resp = resp + "\n"
  176. for obj in self.SQL['onu_model']:
  177. resp = resp + obj['sql'] + "\n"
  178. resp = resp + "\n"
  179. for obj in self.SQL['onus']:
  180. resp = resp + obj['sql'] + "\n"
  181. resp = resp + "\n"
  182. for obj in self.SQL['service_ports']:
  183. resp = resp + obj['sql'] + "\n"
  184. resp = resp + "\n"
  185. return resp
  186. def add_list_delete(self, position, delete):
  187. """
  188. Add delete value
  189. :param position: Name of position in SQL property
  190. :param delete: SQL delete
  191. :return: Return -1 if value not exists or value of id
  192. """
  193. self.SQL_DELETE[position].append(delete)
  194. def add_list(self, position, val, sql, init):
  195. """
  196. Add value to sql list
  197. :param position: Name of position in SQL property
  198. :param val: Unique value
  199. :param sql: Sql generated
  200. :param init: Value of id
  201. :return: Return -1 if value not exists or value of id
  202. """
  203. id_prov = None
  204. try:
  205. for obj in self.SQL[position]:
  206. if obj['val'] == val:
  207. id_prov = obj['id']
  208. break
  209. except Exception:
  210. id_prov = -1
  211. if id_prov == -1 or id_prov == None:
  212. self.SQL[position].append({'id': init, 'sql': sql, 'val': val})
  213. id_prov = -1
  214. return id_prov
  215. def vlan(self, obj, database, init, tenancy_id):
  216. """
  217. Check object for vlan
  218. :param obj: Object Onu
  219. :param database: Database name
  220. :param init: Value for id
  221. :param tenancy_id: Tenancy id
  222. :return: Return last id
  223. """
  224. val = 0
  225. if obj.switchport_vlan:
  226. val = int(obj.switchport_vlan.get_vlan_id())
  227. tmp = Strings.insert_table(database, "vlan_id",
  228. "id, tenancy_id, name, value",
  229. Strings.get_values([init,
  230. tenancy_id,
  231. val,
  232. val]))
  233. else:
  234. tmp = "Error al cargar el vlan id. No existe el objeto. Objeto padre " + obj.name
  235. obj.add_error(tmp)
  236. tmp = ""
  237. if len(tmp) > 0 and val > 0:
  238. id = self.add_list('vlan', val, tmp, init)
  239. if id == -1:
  240. id = init
  241. init += 1
  242. self.add_list_delete("vlan",
  243. Strings.delete_table(database, "vlan_id",
  244. Strings.get_values_delete(
  245. {"id": id, "tenancy_id": tenancy_id, "name": val})))
  246. obj.switchport_vlan.idDataBase = id
  247. return init
  248. def tcont_profile(self, obj, database, init, tenancy_id):
  249. """
  250. Check object for tcont_profile
  251. :param obj: Object Onu
  252. :param database: Database name
  253. :param init: Value for id
  254. :param tenancy_id: Tenancy id
  255. :return: Return last id
  256. """
  257. val = 0
  258. if obj.tcont:
  259. val = int(obj.tcont.get_tcont_id())
  260. tmp = Strings.insert_table(database, "tcont_profile",
  261. "id, tenancy_id, name, value, used_by_default",
  262. Strings.get_values([init,
  263. tenancy_id,
  264. obj.tcont.get_tcont_profile(),
  265. val,
  266. 0]))
  267. else:
  268. tmp = "Error al cargar el tcont. No existe el objeto. Objeto padre " + obj.name
  269. obj.add_error(tmp)
  270. tmp = ""
  271. if len(tmp) > 0 and val > 0:
  272. id = self.add_list('tcont_profile', val, tmp, init)
  273. if id == -1:
  274. id = init
  275. init += 1
  276. self.add_list_delete("tcont_profile",
  277. Strings.delete_table(database, "tcont_profile",
  278. Strings.get_values_delete(
  279. {"id": id, "tenancy_id": tenancy_id,
  280. "name": obj.tcont.get_tcont_profile()})))
  281. obj.tcont.idDataBase = id
  282. return init
  283. def traffic_profile_ingress(self, obj, database, init, tenancy_id):
  284. """
  285. Check object for traffic_profile
  286. :param obj: Object Onu
  287. :param database: Database name
  288. :param init: Value for id
  289. :param tenancy_id: Tenancy id
  290. :return: Return last id
  291. """
  292. val = 0
  293. if obj.traffic_profile_ingress:
  294. val = obj.traffic_profile_ingress.get_traffic_profile()
  295. tmp = Strings.insert_table(database, "traffic_profile",
  296. "id, tenancy_id, name, value, used_by_default",
  297. Strings.get_values([init,
  298. tenancy_id,
  299. val,
  300. val,
  301. 0]))
  302. else:
  303. tmp = "Error al cargar el perfil de trafico. No existe el objeto. Objeto padre " + obj.name
  304. obj.add_error(tmp)
  305. tmp = ""
  306. if len(tmp) > 0 and val > 0:
  307. id = self.add_list('traffic_profile', val, tmp, init)
  308. if id == -1:
  309. id = init
  310. init += 1
  311. self.add_list_delete("traffic_profile",
  312. Strings.delete_table(database, "traffic_profile",
  313. Strings.get_values_delete(
  314. {"id": id, "tenancy_id": tenancy_id,
  315. "name": val})))
  316. obj.traffic_profile_ingress.idDataBase = id
  317. return init
  318. def traffic_profile_egress(self, obj, database, init, tenancy_id):
  319. """
  320. Check object for traffic_profile
  321. :param obj: Object Onu
  322. :param database: Database name
  323. :param init: Value for id
  324. :param tenancy_id: Tenancy id
  325. :return: Return last id
  326. """
  327. val = 0
  328. if obj.traffic_profile_egress:
  329. val = obj.traffic_profile_egress.get_traffic_profile()
  330. tmp = Strings.insert_table(database, "traffic_profile",
  331. "id, tenancy_id, name, value, used_by_default",
  332. Strings.get_values([init,
  333. tenancy_id,
  334. val,
  335. val,
  336. 0]))
  337. else:
  338. tmp = "Error al cargar el perfil de trafico. No existe el objeto. Objeto padre " + obj.name
  339. obj.add_error(tmp)
  340. tmp = ""
  341. if len(tmp) > 0 and val > 0:
  342. id = self.add_list('traffic_profile', val, tmp, init)
  343. if id == -1:
  344. id = init
  345. init += 1
  346. self.add_list_delete("traffic_profile",
  347. Strings.delete_table(database, "traffic_profile",
  348. Strings.get_values_delete(
  349. {"id": id, "tenancy_id": tenancy_id,
  350. "name": val})))
  351. obj.traffic_profile_ingress.idDataBase = id
  352. return init
  353. def profile_ingress(self, obj, database, init, tenancy_id):
  354. """
  355. Check object for profile
  356. :param obj: Object Onu
  357. :param database: Database name
  358. :param init: Value for id
  359. :param tenancy_id: Tenancy id
  360. :return: Return last id
  361. """
  362. val = ""
  363. if obj.traffic_profile_ingress:
  364. val = obj.traffic_profile_ingress.get_traffic_profile()
  365. tmp = Strings.insert_table(database, "profile",
  366. "id, tenancy_id, name",
  367. Strings.get_values([init,
  368. tenancy_id,
  369. val]))
  370. else:
  371. tmp = "Error al cargar el profile. No existe el objeto. Objeto padre " + obj.name
  372. obj.add_error(tmp)
  373. tmp = ""
  374. if len(tmp) > 0 and len(val) > 0:
  375. id = self.add_list('profile', val, tmp, init)
  376. if id == -1:
  377. id = init
  378. init += 1
  379. self.add_list_delete("profile",
  380. Strings.delete_table(database, "profile",
  381. Strings.get_values_delete(
  382. {"id": id, "tenancy_id": tenancy_id, "name": val})))
  383. obj.traffic_profile_ingress.idDataBaseProfile = id
  384. return init
  385. def profile_egress(self, obj, database, init, tenancy_id):
  386. """
  387. Check object for profile
  388. :param obj: Object Onu
  389. :param database: Database name
  390. :param init: Value for id
  391. :param tenancy_id: Tenancy id
  392. :return: Return last id
  393. """
  394. val = ""
  395. if obj.traffic_profile_egress:
  396. val = obj.traffic_profile_egress.get_traffic_profile()
  397. tmp = Strings.insert_table(database, "profile",
  398. "id, tenancy_id, name",
  399. Strings.get_values([init,
  400. tenancy_id,
  401. val]))
  402. else:
  403. tmp = "Error al cargar el profile. No existe el objeto. Objeto padre " + obj.name
  404. obj.add_error(tmp)
  405. tmp = ""
  406. if len(tmp) > 0 and len(val) > 0:
  407. id = self.add_list('profile', val, tmp, init)
  408. if id == -1:
  409. id = init
  410. init += 1
  411. self.add_list_delete("profile",
  412. Strings.delete_table(database, "profile",
  413. Strings.get_values_delete(
  414. {"id": id, "tenancy_id": tenancy_id, "name": val})))
  415. obj.traffic_profile_ingress.idDataBaseProfile = id
  416. return init
  417. def nap(self, obj, database, init, tenancy_id):
  418. """
  419. Check object for nap
  420. :param obj: Object Onu
  421. :param database: Database name
  422. :param init: Value for id
  423. :param tenancy_id: Tenancy id
  424. :return: Return last id
  425. """
  426. val = ""
  427. if obj.nap:
  428. val = obj.nap.name.strip()
  429. tmp1, slot, link = val.split("/")
  430. tmp = Strings.insert_table(database, "nap",
  431. "id, tenancy_id, name, capacity, slot, link, olt_id",
  432. Strings.get_values([init,
  433. tenancy_id,
  434. val,
  435. 256,
  436. slot,
  437. link,
  438. self.idDataBase]))
  439. else:
  440. tmp = "Error al cargar el nap. No existe el objeto. Objeto padre " + obj.name
  441. obj.add_error(tmp)
  442. tmp = ""
  443. if len(tmp) > 0 and len(val) > 0:
  444. id = self.add_list('nap', val, tmp, init)
  445. if id == -1:
  446. id = init
  447. init += 1
  448. self.add_list_delete("nap",
  449. Strings.delete_table(database, "nap",
  450. Strings.get_values_delete(
  451. {"id": id, "tenancy_id": tenancy_id, "name": val})))
  452. obj.nap.idDataBase = id
  453. return init
  454. def client(self, obj, database, init, tenancy_id):
  455. """
  456. Check object for client
  457. :param obj: Object Onu
  458. :param database: Database name
  459. :param init: Value for id
  460. :param tenancy_id: Tenancy id
  461. :return: Return last id
  462. """
  463. val = ""
  464. if obj.client:
  465. val = obj.client.name.strip()
  466. tmp = Strings.insert_table(database, "client",
  467. "id, tenancy_id, name, created, updated",
  468. Strings.get_values([init,
  469. tenancy_id,
  470. val,
  471. "now()",
  472. "now()"]))
  473. else:
  474. tmp = "Error al cargar el cliente. No existe el objeto. Objeto padre " + obj.name
  475. obj.add_error(tmp)
  476. tmp = ""
  477. if len(tmp) > 0 and len(val) > 0:
  478. id = self.add_list('client', val, tmp, init)
  479. if id == -1:
  480. id = init
  481. init += 1
  482. self.add_list_delete("client",
  483. Strings.delete_table(database, "client",
  484. Strings.get_values_delete(
  485. {"id": id, "tenancy_id": tenancy_id, "name": val})))
  486. obj.client.idDataBase = id
  487. return init
  488. def onu_model(self, obj, database, init, tenancy_id):
  489. """
  490. Check object for onu_model
  491. :param obj: Object Onu
  492. :param database: Database name
  493. :param init: Value for id
  494. :param tenancy_id: Tenancy id
  495. :return: Return last id
  496. """
  497. val = ""
  498. if obj.onumodel:
  499. val = obj.onumodel.name.strip()
  500. tmp = Strings.insert_table(database, "onumodel",
  501. "id, tenancy_id, name",
  502. Strings.get_values([init,
  503. tenancy_id,
  504. val]))
  505. else:
  506. tmp = "Error al cargar el modelo de onu. No existe el objeto. Objeto padre " + obj.name
  507. obj.add_error(tmp)
  508. tmp = ""
  509. if len(tmp) > 0 and len(val) > 0:
  510. id = self.add_list('onu_model', val, tmp, init)
  511. if id == -1:
  512. id = init
  513. init += 1
  514. self.add_list_delete("onu_model",
  515. Strings.delete_table(database, "onumodel",
  516. Strings.get_values_delete(
  517. {"id": id, "tenancy_id": tenancy_id, "name": val})))
  518. obj.onumodel.idDataBase = id
  519. return init
  520. def onus(self, gpon, obj, database, init, tenancy_id):
  521. """
  522. Check object for onus
  523. :param obj: Object Onu
  524. :param database: Database name
  525. :param init: Value for id
  526. :param tenancy_id: Tenancy id
  527. :return: Return last id
  528. """
  529. val = obj.serial_number
  530. if obj.onumodel and \
  531. obj.nap and \
  532. obj.tcont and \
  533. obj.traffic_profile_ingress and \
  534. obj.switchport_vlan and \
  535. obj.serial_number and \
  536. obj.client and \
  537. obj.onumodel.idDataBase > 0 and \
  538. obj.nap.idDataBase > 0 and \
  539. obj.tcont.idDataBase > 0 and \
  540. obj.traffic_profile_ingress.idDataBase > 0 and \
  541. obj.switchport_vlan.idDataBase > 0 and \
  542. obj.client.idDataBase > 0:
  543. fields_default = "id, tenancy_id, olt_id, model_id, nap_id, tcontprofile_id, " \
  544. "traffic_profile_id, profile_id, vlan_id, client_id, serial_number, " \
  545. "pon_serial_number, pon_serial_number_aux, position, created, updated"
  546. values_default = [init, tenancy_id, self.idDataBase, obj.onumodel.idDataBase, obj.nap.idDataBase,
  547. obj.tcont.idDataBase, obj.traffic_profile_ingress.idDataBase,
  548. obj.traffic_profile_ingress.idDataBaseProfile, obj.switchport_vlan.idDataBase,
  549. obj.client.idDataBase, obj.serial_number, obj.serial_number,
  550. obj.serial_number,
  551. obj.id, "now()", "now()"]
  552. if obj.catv is not None:
  553. fields_default = fields_default + ", catv"
  554. values_default.append(obj.catv.is_catv())
  555. tmp = Strings.insert_table(database, "onu",
  556. fields_default,
  557. Strings.get_values(values_default))
  558. elif obj.onumodel and obj.nap and obj.serial_number and \
  559. obj.client and obj.onumodel.idDataBase > 0 and \
  560. obj.nap.idDataBase > 0 and obj.client.idDataBase > 0:
  561. fields_default = "id, tenancy_id, olt_id, model_id, nap_id, " \
  562. "client_id, serial_number, " \
  563. "pon_serial_number, pon_serial_number_aux, " \
  564. "position, created, updated"
  565. values_default = [init, tenancy_id, self.idDataBase, obj.onumodel.idDataBase,
  566. obj.nap.idDataBase, obj.client.idDataBase, obj.serial_number,
  567. obj.serial_number, obj.serial_number, obj.position, "now()", "now()"]
  568. if obj.catv is not None:
  569. fields_default = fields_default + ", catv"
  570. values_default.append(obj.catv.is_catv())
  571. tmp = Strings.insert_table(database, "onu",
  572. fields_default,
  573. Strings.get_values(values_default))
  574. else:
  575. tmp = "Error al cargar la onu con serial number: " + obj.serial_number + ", "
  576. if not obj.onumodel or obj.onumodel.idDataBase == 0:
  577. tmp = tmp + " no posee modelo de onu, "
  578. if not obj.nap or obj.nap.idDataBase == 0:
  579. tmp = tmp + " no posee nap, "
  580. if not obj.tcont or obj.tcont.idDataBase == 0:
  581. tmp = tmp + " no posee tcont, "
  582. if not obj.traffic_profile_ingress or obj.traffic_profile_ingress.idDataBase == 0:
  583. tmp = tmp + " no posee perfil de trafico de ingreso, "
  584. if not obj.switchport_vlan or obj.switchport_vlan.idDataBase == 0:
  585. tmp = tmp + " no posee vlan, "
  586. if not obj.client or obj.client.idDataBase == 0:
  587. tmp = tmp + " no posee cliente, "
  588. tmp = tmp[:-2]
  589. gpon.add_error(tmp)
  590. tmp = ""
  591. if len(tmp) > 0 and len(val) > 0:
  592. id = self.add_list('onus', val, tmp, init)
  593. if id == -1:
  594. id = init
  595. init += 1
  596. self.add_list_delete("onus",
  597. Strings.delete_table(database, "onu",
  598. Strings.get_values_delete(
  599. {"id": id, "tenancy_id": tenancy_id,
  600. "pon_serial_number": obj.serial_number})))
  601. obj.idDataBase = id
  602. return init
  603. def onus_disabled(self, obj, database, init, tenancy_id):
  604. """
  605. Check object for onus
  606. :param obj: Object Onu
  607. :param database: Database name
  608. :param init: Value for id
  609. :param tenancy_id: Tenancy id
  610. :return: Return last id
  611. """
  612. val = obj.serial_number
  613. fields_default = "id, tenancy_id, olt_id, model_id, nap_id, tcontprofile_id, " \
  614. "traffic_profile_id, profile_id, vlan_id, client_id, serial_number, " \
  615. "pon_serial_number, pon_serial_number_aux, position, created, updated"
  616. onumodel_id = "null"
  617. if obj.onumodel is not None and obj.onumodel.idDataBase > 0:
  618. onumodel_id = obj.onumodel.idDataBase
  619. nap_id = "null"
  620. if obj.nap is not None and obj.nap.idDataBase > 0:
  621. nap_id = obj.nap.idDataBase
  622. tcont_id = "null"
  623. if obj.tcont is not None and obj.tcont.idDataBase > 0:
  624. tcont_id = obj.tcont.idDataBase
  625. traffic_profile_ingress_id = "null"
  626. if obj.traffic_profile_ingress is not None and obj.traffic_profile_ingress.idDataBase > 0 :
  627. traffic_profile_ingress_id = obj.traffic_profile_ingress.idDataBase
  628. traffic_profile_ingress_id_profile = "null"
  629. if obj.traffic_profile_ingress is not None and obj.traffic_profile_ingress.idDataBaseProfile > 0 :
  630. traffic_profile_ingress_id_profile = obj.traffic_profile_ingress.idDataBaseProfile
  631. switchport_vlan_id = "null"
  632. if obj.switchport_vlan is not None and obj.switchport_vlan.idDataBase > 0 :
  633. switchport_vlan_id = obj.switchport_vlan.idDataBase
  634. client_id = "null"
  635. if obj.client is not None and obj.client.idDataBase > 0:
  636. client_id = obj.client.idDataBase
  637. values_default = [init, tenancy_id, self.idDataBase, onumodel_id, nap_id,
  638. tcont_id, traffic_profile_ingress_id,
  639. traffic_profile_ingress_id_profile, switchport_vlan_id,
  640. client_id, obj.serial_number, obj.serial_number,
  641. obj.serial_number,
  642. obj.id, "now()", "now()"]
  643. if obj.catv is not None:
  644. fields_default = fields_default + ", catv"
  645. values_default.append(obj.catv.is_catv())
  646. tmp = Strings.insert_table(database, "onu",
  647. fields_default,
  648. Strings.get_values(values_default))
  649. if len(tmp) > 0 and len(val) > 0:
  650. id = self.add_list('onus', val, tmp, init)
  651. if id == -1:
  652. id = init
  653. init += 1
  654. self.add_list_delete("onus",
  655. Strings.delete_table(database, "onu",
  656. Strings.get_values_delete(
  657. {"id": id, "tenancy_id": tenancy_id,
  658. "pon_serial_number": obj.serial_number})))
  659. obj.idDataBase = id
  660. return init
  661. def change_error_a_string(self, obj):
  662. """
  663. Transform error object into string
  664. :param obj: Object to check
  665. :return: Return string
  666. """
  667. if obj.error and len(obj.error) > 0:
  668. resp = "\n"
  669. for err in obj.error:
  670. resp = resp + "-- " + err + "\n"
  671. else:
  672. resp = ""
  673. return resp
  674. def add_value_error(self, errores, key, value):
  675. """
  676. Add error
  677. :param errores: Property error
  678. :param key: Contains key to add
  679. :param value: Contains value to add
  680. :return: Retorna param errores
  681. """
  682. if not errores.get(key):
  683. errores[key] = []
  684. if len(value) > 0:
  685. errores[key].append(value)
  686. return errores
  687. def obtains_errors(self, list_error_key=None):
  688. """
  689. Obtains errors for all object
  690. :param list_error_key: List error to show. Default None
  691. :return: Return with all errors
  692. """
  693. errores = {}
  694. resp = ""
  695. for gpon in self.child:
  696. if gpon:
  697. self.add_value_error(errores, 'gpon', self.change_error_a_string(gpon))
  698. # errores de los objetos hijos
  699. for obj in gpon.child:
  700. self.add_value_error(errores, 'onu', self.change_error_a_string(obj))
  701. if obj.sn_bind:
  702. self.add_value_error(errores, 'sn_bind', self.change_error_a_string(obj.sn_bind))
  703. if obj.tcont:
  704. self.add_value_error(errores, 'tcont', self.change_error_a_string(obj.tcont))
  705. if obj.gemport:
  706. self.add_value_error(errores, 'gemport', self.change_error_a_string(obj.gemport))
  707. if obj.switchport_mode:
  708. self.add_value_error(errores, 'switchport_mode',
  709. self.change_error_a_string(obj.switchport_mode))
  710. if obj.switchport_vlan:
  711. self.add_value_error(errores, 'switchport_vlan',
  712. self.change_error_a_string(obj.switchport_vlan))
  713. if obj.traffic_profile_ingress:
  714. self.add_value_error(errores, 'traffic_profile_ingress',
  715. self.change_error_a_string(obj.traffic_profile_ingress))
  716. if obj.traffic_profile_egress:
  717. self.add_value_error(errores, 'traffic_profile_egress',
  718. self.change_error_a_string(obj.traffic_profile_egress))
  719. if obj.nap:
  720. self.add_value_error(errores, 'nap', self.change_error_a_string(obj.nap))
  721. if obj.client:
  722. self.add_value_error(errores, 'client', self.change_error_a_string(obj.client))
  723. self.add_value_error(errores, 'olt', self.change_error_a_string(self))
  724. for deactivate in self.ONU_DEACTIVATE:
  725. resp = resp + deactivate + "\n"
  726. if resp:
  727. resp = resp + "\n"
  728. for key in errores:
  729. if list_error_key == None or len(list_error_key) == 0 or key in list_error_key:
  730. for err in errores[key]:
  731. resp = resp + err
  732. return resp
  733. def service_ports(self, gpon, obj, database, init, tenancy_id):
  734. """
  735. Check object for onus
  736. :param obj: Object Onu
  737. :param database: Database name
  738. :param init: Value for id
  739. :param tenancy_id: Tenancy id
  740. :return: Return last id
  741. """
  742. val = obj.serial_number
  743. if obj.service_port and \
  744. obj.nap and \
  745. obj.serial_number and \
  746. obj.client and \
  747. obj.onumodel.idDataBase > 0 and \
  748. obj.nap.idDataBase > 0 and \
  749. obj.client.idDataBase > 0:
  750. for sp in obj.service_port:
  751. fields_default = "id, olt_id, onu_id, number, gemport, vlan, type"
  752. values_default = [init, self.idDataBase, obj.idDataBase, sp.number, sp.gemport, sp.vlan, sp.type]
  753. tmp = Strings.insert_table(database, "service_port",
  754. fields_default,
  755. Strings.get_values(values_default))
  756. val = sp.number
  757. if len(tmp) > 0:
  758. id = self.add_list('service_ports', val, tmp, init)
  759. if id == -1:
  760. id = init
  761. init += 1
  762. self.add_list_delete("service_ports",
  763. Strings.delete_table(database, "service_port",
  764. Strings.get_values_delete(
  765. {"id": id, "tenancy_id": tenancy_id})))
  766. sp.idDataBase = id
  767. return init