#!/usr/bin/ruby -w require 'imperituroard/projects/static/phpipamforcps' require 'imperituroard/platforms/public/telegram' require 'imperituroard/add_functions/add/additional' class StaticImperituroard attr_accessor :telegram, :cps_ipam_connector def initialize(telegram_api_url, telegram_chat_id, telegram_log) @telegram = Telegram_2.new(telegram_api_url, telegram_chat_id, telegram_log) @cps_ipam_connector = PHPipamcps.new end #mts_1_add_new_address def mts_add_new_addr begin prefix = 'not implemented' #request id generation for logging o = [('a'..'z'), ('A'..'Z')].map(&:to_a).flatten request_id = (0...50).map {o[rand(o.length)]}.join #logging before processing mess1 = "input Request ID: '#{request_id}' HAproxy #{request.remote_ip}, request device real ip: #{request.headers['X-Forwarded-For']} requested mts_1_add_new_address procedure with such parameter as: type of service: #{params[:type]}, MSISDN: #{params[:msisdn]}, region: #{params[:region]}, description: #{params[:description]}, note: #{params[:note]}, ipcount: #{params[:ipcount].to_s}, ipversion: #{params[:ipversion].to_s} " logger.info mess1 telegram.telegram_message2(mess1) #checking if msisdn exist in database begin dfd = DatabaseMethods2.new che = dfd.phpipam_get_address(params[:msisdn]) mess = "Request ID: '#{request_id}' HAproxy #{request.remote_ip}, request device real ip: #{request.headers['X-Forwarded-For']} requested mts_1_add_new_address procedure and processed from database: #{che}" logger.info mess telegram.telegram_message2(mess) rescue mess = "Request ID: error. cant get address from database, Request ID: '#{request_id}' HAproxy #{request.remote_ip}, request device real ip: #{request.headers['X-Forwarded-For']} requested mts_1_add_new_address procedure and processed from database: #{che}" logger.info mess telegram.telegram_message2(mess) end # if che=1 - address not exist in database if che=='1' #type of subscriber #type - tip abonenta (0 - obychnyj abonent fizicheskoe lico, 1 - government, 2 - ord, 3 - government ord filter) type = params[:type] #subscribers msisdn msisdn = params[:msisdn] #region Minsk, Brest, Gomel etc. region = params[:region] #old parameter. not need now iptype = "ip" #description - lyuboj tekst. predlagaetsya zdes ukazyvat adres abonenta description = params[:description] #note - vtoroj deskripshn. tozhe lyuboj tekst pri neobxodimosti note = params[:note] prefixmask = AdditionalImperituroard::stat_parse_ip_num(params[:ipcount]) #adding ip address to phpipam #first iteration. If one ip address #ip address inserting to phpipam procedure begin php = PHPipam.new respo = php.insertip("FTTx", iptype, region, prefixmask, description, note, msisdn, params[:type], params[:ipcount].to_s, params[:ipversion].to_s) #parse = respo.split("---") p respo addr = respo[:address] mask = respo[:netmask] gateway = respo[:gateway] if addr!="unknown" && mask!="unknown" && gateway!="unknown" output = {:answercode => "200", :message => "Request completed successfully", :stataddress => addr, :network => mask, :gateway => gateway, :prefix => prefix} else mess = "Output error. Request ID: '#{request_id}' HAproxy #{request.remote_ip}, request device real ip: #{request.headers['X-Forwarded-For']} requested mts_1_add_new_address procedure and output errorMethod, No available addresses." logger.info mess telegram.telegram_message2(mess) raise SOAPError, "ERROR. No available addresses." end mess = "Request ID: '#{request_id}' HAproxy #{request.remote_ip}, request device real ip: #{request.headers['X-Forwarded-For']} requested mts_1_add_new_address procedure and recieved #{output.to_s} response" logger.info mess telegram.telegram_message2(mess) rescue mess = "Request ID: '#{request_id}' HAproxy #{request.remote_ip}, request device real ip: #{request.headers['X-Forwarded-For']} requested mts_1_add_new_address procedure and output errorMethod, Cant get ip address from phpipam. " logger.info mess telegram.telegram_message2(mess) raise SOAPError, "ERROR. Request ID: '#{request_id}' Cant get ip address from phpipam." end p prefixmask begin if prefixmask<32 p "stata" php1 = PHPipam.new rere = php1.ins_prefix_ip("prefix", region, msisdn, prefixmask, note, description, php1.gettoken) prefix = rere[:prefix] output[:prefix] = prefix cpsxxx = PHPipamcps.new resppp = cpsxxx.add_attribute_prefix(msisdn, rere[:prefix]) if resppp=="0" mess = "Request ID: '#{request_id}' HAproxy #{request.remote_ip}, request device real ip: #{request.headers['X-Forwarded-For']} requested mts_1_add_new_address procedure and output: #{output.to_s}" logger.info mess telegram.telegram_message2(mess) else mess = "Request ID: '#{request_id}' HAproxy #{request.remote_ip}, request device real ip: #{request.headers['X-Forwarded-For']} requested mts_1_add_new_address procedure and output cant insert address to CPS." logger.info mess telegram.telegram_message2(mess) raise SOAPError, "ERROR. cant insert address to CPS." end elsif prefixmask==32 else raise SOAPError, "ERROR. Unknown parameter." end rescue mess = "Request ID: '#{request_id}' HAproxy #{request.remote_ip}, request device real ip: #{request.headers['X-Forwarded-For']} requested mts_1_add_new_address procedure and output errorMethod, Cant insert prefix framed route into CPS. " logger.info mess telegram.telegram_message2(mess) raise SOAPError, "ERROR. Request ID: '#{request_id}' Cant insert prefix into CPS." end #inserting address to cps begin if params[:type]=="0" cpsxxx = PHPipamcps.new p resppp = cpsxxx.add_attribute(msisdn, addr, mask, gateway) if resppp=="0" mess = "Request ID: '#{request_id}' HAproxy #{request.remote_ip}, request device real ip: #{request.headers['X-Forwarded-For']} requested mts_1_add_new_address procedure and output: #{output.to_s}" logger.info mess telegram.telegram_message2(mess) begin cps_ipam_connector.stop_session(params[:msisdn]) rescue p "f" end render :soap => output else mess = "Request ID: '#{request_id}' HAproxy #{request.remote_ip}, request device real ip: #{request.headers['X-Forwarded-For']} requested mts_1_add_new_address procedure and output cant insert address to CPS." logger.info mess telegram.telegram_message2(mess) raise SOAPError, "ERROR. cant insert address to CPS." end elsif params[:type]=="1" cpsxxx = PHPipamcps.new resppp = cpsxxx.add_attribute1(msisdn, addr, mask, gateway) p resppp if resppp.to_s=="0" mess = "Request ID: '#{request_id}' HAproxy #{request.remote_ip}, request device real ip: #{request.headers['X-Forwarded-For']} requested mts_1_add_new_address procedure and output: #{output.to_s}" logger.info mess p "p1" telegram.telegram_message2(mess) p "p2" begin cps_ipam_connector.stop_session(params[:msisdn]) rescue p "f" end render :soap => output else mess = "Request ID: '#{request_id}' HAproxy #{request.remote_ip}, request device real ip: #{request.headers['X-Forwarded-For']} requested mts_1_add_new_address procedure and output cant insert address to CPS." logger.info mess telegram.telegram_message2(mess) p "Errrrr" raise SOAPError, "ERROR. cant insert address to CPS." end elsif params[:type]=="2" cpsxxx = PHPipamcps.new p resppp = cpsxxx.add_attribute2(msisdn, addr, mask, gateway) if resppp=="0" mess = "Request ID: '#{request_id}' HAproxy #{request.remote_ip}, request device real ip: #{request.headers['X-Forwarded-For']} requested mts_1_add_new_address procedure and output: #{output.to_s}" logger.info mess telegram.telegram_message2(mess) begin cps_ipam_connector.stop_session(params[:msisdn]) rescue p "f" end render :soap => output else mess = "Request ID: '#{request_id}' HAproxy #{request.remote_ip}, request device real ip: #{request.headers['X-Forwarded-For']} requested mts_1_add_new_address procedure and output cant insert address to CPS." logger.info mess telegram.telegram_message2(mess) p "Errrrr1" raise SOAPError, "ERROR. cant insert address to CPS." end elsif params[:type]=="3" cpsxxx = PHPipamcps.new p resppp = cpsxxx.add_attribute3(msisdn, addr, mask, gateway) if resppp=="0" mess = "Request ID: '#{request_id}' HAproxy #{request.remote_ip}, request device real ip: #{request.headers['X-Forwarded-For']} requested mts_1_add_new_address procedure and output: #{output.to_s}" logger.info mess telegram.telegram_message2(mess) begin cps_ipam_connector.stop_session(params[:msisdn]) rescue p "f" end render :soap => output else mess = "Request ID: '#{request_id}' HAproxy #{request.remote_ip}, request device real ip: #{request.headers['X-Forwarded-For']} requested mts_1_add_new_address procedure and output cant insert address to CPS." logger.info mess telegram.telegram_message2(mess) p "Errrrr2" raise SOAPError, "ERROR. cant insert address to CPS." end else mess = "Request ID: '#{request_id}' HAproxy #{request.remote_ip}, request device real ip: #{request.headers['X-Forwarded-For']} requested mts_1_add_new_address procedure and output ERROR. Unknown parameter." logger.info mess telegram.telegram_message2(mess) raise SOAPError, "ERROR. Unknown parameter." end rescue mess = "Request ID: '#{request_id}' HAproxy #{request.remote_ip}, request device real ip: #{request.headers['X-Forwarded-For']} requested mts_1_add_new_address procedure and output cant insert address to CPS. Unknown error" logger.info mess telegram.telegram_message2(mess) p "Errrrr5" raise SOAPError, "ERROR. cant insert address to CPS. Unknown error" end else #objavlenie peremennix sended_ipaddress = che prefix = '' gateway = '' subnet = '' editdate = '' hostname = '' region = '' broadcast = '' netmask = '' wildcardmask = '' minhostip = '' maxhostip = '' numberofhosts = '' sectionid = '' description = '' note = '' code = '' resp1 = '' #get ip address from phpipam database begin php = PHPipam.new p respo = php.getaddressinfo(sended_ipaddress) if respo == '404' resp1 = 'Address not found' code = '404' elsif respo == '500' code = '500' resp1 = 'Error' else para = respo.split("---") prefix = para[0] gateway = para[1] subnet = para[2] editdate = para[3] hostname = para[4] region = para[5] broadcast = para[6] netmask = para[7] wildcardmask = para[8] minhostip = para[9] maxhostip = para[10] numberofhosts = para[11] sectionid = para[12] description = para[13] note = para[14] resp1 = 'Request completed successfully' code = '200' end output = {:answercode => '200', :message => 'Address already exists', :stataddress => sended_ipaddress, :network => netmask, :gateway => gateway} begin dfd = DatabaseMethods2.new che = dfd.phpipam_get_address_pre(params[:msisdn]) if che=='1' output[:prefix]= 'not implemented' else output[:prefix]=che end rescue telegram.telegram_message2('ERROR. With getting prefix from database') raise SOAPError, 'ERROR. With getting prefix from database' end mess = "Request ID: '#{request_id}' HAproxy #{request.remote_ip}, request device real ip: #{request.headers['X-Forwarded-For']} requested mts_1_add_new_address procedure and output: #{output.to_s}" logger.info mess telegram.telegram_message2(mess) begin cps_ipam_connector.stop_session(params[:msisdn]) rescue p 'f' end render :soap => output rescue mess = "Request ID: '#{request_id}' HAproxy #{request.remote_ip}, request device real ip: #{request.headers['X-Forwarded-For']} requested mts_1_add_new_address procedure and output ERROR Unknown error. can't get ip address from phpipam API" logger.info mess telegram.telegram_message2(mess) raise SOAPError, "ERROR. Unknown error. can't get ip address from phpipam API" end end rescue mess = "Request ID: '#{request_id}' HAproxy #{request.remote_ip}, request device real ip: #{request.headers['X-Forwarded-For']} requested mts_1_add_new_address procedure and output FATALITY ERROR" logger.info mess telegram.telegram_message2(mess) raise SOAPError, 'ERROR. FATAL FATAL FATAL' end end #mts_2_delete_address def mts_2_delete_address #formirovanie request ID o = [('a'..'z'), ('A'..'Z')].map(&:to_a).flatten request_id = (0...50).map {o[rand(o.length)]}.join mess = "HAproxy input Request ID: #{request_id}, RemoteIP: #{request.remote_ip}, request device real ip: #{request.headers['X-Forwarded-For']}, requested mts_2_delete_address procedure with such parameter as msisdn: #{params[:msisdn]}" logger.info mess telegram.telegram_message2(mess) begin p '1' #opredelenie adresa po msisdn dfd = DatabaseMethods2.new ipaddr = dfd.phpipam_get_address(params[:msisdn]) #udalenie addresa iz bazi phpipam php = PHPipam.new respo = php.deleteip(ipaddr) p '2' #udalenie podseti iz bazi phpipam begin php.deletesubnet(params[:msisdn]) rescue p 'f' end code = '' resp1 = '' if respo == '200' code = '200' resp1 = 'Request completed successfully' else if respo == '404' resp1 = 'Address not found' code = '404' else code = '500' resp1 = 'Error' end end #udalenie atributa iz CPS cpsxxx = PHPipamcps.new cpsxxx.del_attribute(params[:msisdn]) #clear session begin cpsxxx11345 = PHPipamcps.new cpsxxx11345.stop_session(params[:msisdn]) rescue p 'f' end #formirovanie otveta output = {:answercode => code, :message => resp1} mess = "HAproxy output Request ID: #{request_id}, RemoteIP: #{request.remote_ip}, request device real ip: #{request.headers['X-Forwarded-For']}, requested mts_2_delete_address procedure with such parameter as msisdn: #{params[:msisdn]} and recieved #{output} response" logger.info mess telegram.telegram_message2(mess) render :soap => output rescue mess = "HAproxy output error Request ID: #{request_id}, RemoteIP: #{request.remote_ip}, request device real ip: #{request.headers['X-Forwarded-For']}, requested mts_2_delete_address procedure with such parameter as msisdn: #{params[:msisdn]} and recieved ERROR. Something wrong. response" logger.info mess telegram.telegram_message2(mess) raise SOAPError, 'ERROR. Something wrong.' end end #mts_3_technical_reconnect def mts_3_technical_reconnect o = [('a'..'z'), ('A'..'Z')].map(&:to_a).flatten request_id = (0...50).map {o[rand(o.length)]}.join output={} begin dfd = DatabaseMethods2.new ipaddr = dfd.phpipam_get_address(params[:msisdn]) if ipaddr == '1' raise SOAPError, 'ERROR. Address not found in phpipam.' end cpsxxx = PHPipamcps.new cpsxxx.del_attribute(params[:msisdn]) begin sended_ipaddress = ipaddr prefix="" gateway="" subnet="" editdate="" hostname="" region="" broadcast="" netmask="" wildcardmask="" minhostip="" maxhostip="" numberofhosts="" sectionid="" description="" note="" substype = "" ipcount = "" ipversion = "" code ="" resp1 ="" php = PHPipam.new respo = php.getaddressinfo2(sended_ipaddress) p respo if respo[:code] == '404' resp1 = 'Address not found' code = '404' elsif respo[:code] == '500' code = '500' resp1 = 'Error' else #attributes from procedure getaddressinfo2 to procedure for adding avp to cps gateway = respo[:gateway] subnet = respo[:submask] substype = respo[:substype], ipcount = respo[:ipcount], ipversion = respo[:ipversion] resp1 = 'Request completed successfully' code = '200' end cpsxxx1 = PHPipamcps.new resppp = "" p params[:msisdn] p ipaddr p subnet p gateway p substype begin if substype[0].to_i == 0 resppp = cpsxxx1.add_attribute(params[:msisdn], ipaddr, subnet, gateway) elsif substype[0].to_i == 1 resppp = cpsxxx1.add_attribute1(params[:msisdn], ipaddr, subnet, gateway) elsif substype[0].to_i == 2 resppp = cpsxxx1.add_attribute2(params[:msisdn], ipaddr, subnet, gateway) elsif substype[0].to_i == 3 resppp = cpsxxx1.add_attribute3(params[:msisdn], ipaddr, subnet, gateway) end rescue thr21 = Thread.new do begin telegram('ERROR. Something wrong with CPS.') rescue p 'f' end end raise SOAPError, 'ERROR. Something wrong with CPS.' end p ipcount p params[:msisdn] if ipcount.to_i>1 p 'qwerty' dfd2 = DatabaseMethods2.new prrr = dfd2.phpipam_get_address_pre(params[:msisdn]) resppp = cpsxxx1.add_attribute_prefix(params[:msisdn], prrr) end code = '' resp1 = '' if resppp == '0' code = '200' resp1 = 'Request completed successfully' else if resppp == '404' resp1 = 'Address not found' code = '404' else code = '500' resp1 = 'Error' end end output = {:answercode => code, :message => resp1} # if resppp=="0" # logger.info "Request ID: '#{request_id}' HAproxy #{request.remote_ip}, request device real ip: #{request.headers['X-Client']} requested mts_1_add_new_address procedure and output: #{output}" # # #render :soap => output # else # # logger.info "Request ID: '#{request_id}' HAproxy #{request.remote_ip}, request device real ip: #{request.headers['X-Client']} requested mts_1_add_new_address procedure and output cant insert address to CPS." # # raise SOAPError, "ERROR. cant insert address to CPS." # end rescue raise SOAPError, 'ERROR. Something wrong.' end #cpsxxx = PHPipamcps.new #resppp = cpsxxx.reconnect(params[:msisdn]) output = {:answercode => '200', :message => 'Request completed successfully'} #output = {:answercode => "200", :message => "Request completed successfully"} mess = "HAproxy #{request.remote_ip}, request device real ip: #{request.headers['X-Forwarded-For']} requested mts_3_technical_reconnect procedure with such parameter as: MSISDN: #{params[:msisdn]}, and recieved #{output} response" logger.info mess thr29 = Thread.new do begin telegram(mess) rescue p 'f' end end render :soap => output rescue puts 'SOAP mts_3_technical_reconnect errorMethod' mess = 'ERROR. Something wrong gen.' logger.info mess telegram.telegram_message2(mess) raise SOAPError, 'ERROR. Something wrong gen.' end end #mts_4_change_attr def mts_4_change_attr type = params[:type] msisdn = params[:msisdn] region = params[:region] iptype = 'ip' description = params[:description] prefixlength = '32' note = params[:note] begin php = PHPipam.new substype = 'ip' ipcount = '1' ipversion = 'IPv4' respo = php.insertip(type, iptype, region, prefixlength, description, note, msisdn, substype, ipcount, ipversion) parse = respo.split('---') addr = parse[0] mask = parse[1] gateway = parse[2] if addr!='unknown' && mask!='unknown' && gateway!='unknown' output = {:answercode => '200', :message => 'Request completed successfully', :stataddress => addr, :network => mask, :gateway => gateway} else output = {:answercode => '407', :message => 'No available address', :stataddress => '', :network => '', :gateway => ""} end logger.info "HAproxy #{request.remote_ip}, request device real ip: #{request.headers['X-Client']} requested mts_4_change_attr procedure with such parameter as: type of service: #{params[:type]}, MSISDN: #{params[:msisdn]}, region: #{params[:region]}, description: #{params[:description]}, note: #{params[:note]} and recieved #{output} response" render :soap => output rescue puts 'SOAP mts_4_change_attr errorMethod' raise SOAPError, 'ERROR. Something wrong.' end end #mts_5_save_avp def mts_5_save_avp o = [('a'..'z'), ('A'..'Z')].map(&:to_a).flatten request_id = (0...50).map {o[rand(o.length)]}.join begin cpsxx11 = PHPipamcps.new cpsxx11.getavp_backup_tomongo(params[:msisdn]) mess12 = "HAproxy #{request.remote_ip}, request device real ip: #{request.headers['X-Forwarded-For']} requested mts_5_save_avp procedure with such parameter as: MSISDN: #{params[:msisdn]}, and processed success" logger.info mess12 telegram.telegram_message2(mess12) output = {:answercode => "200", :message => "Request completed successfully"} render :soap => output rescue mess12 = "HAproxy #{request.remote_ip}, request device real ip: #{request.headers['X-Forwarded-For']} requested mts_5_save_avp procedure with such parameter as: MSISDN: #{params[:msisdn]}, and recieved ERROR. Failed to backup avp from CPS response" logger.info mess12 telegram.telegram_message2(mess12) raise SOAPError, 'ERROR. Failed to backup avp from CPS' end end #mts_7_technical_reconnect def mts_7_technical_reconnect o = [('a'..'z'), ('A'..'Z')].map(&:to_a).flatten request_id = (0...50).map {o[rand(o.length)]}.join begin p 'dfs' tee = PHPipamcps.new t = Phpipam.new p '0' li = t.form_attr_list(params[:msisdn]) p li p '1' ans = '' mess ='' if li != 'notfound' ans = tee.return_backup_tomongo(params[:msisdn], li) mess = 'Request completed successfully' else ans ='404' mess = 'Not found backup' end rescue mess12 = "HAproxy #{request.remote_ip}, request device real ip: #{request.headers['X-Forwarded-For']} requested mts_7_technical_reconnect procedure with such parameter as: MSISDN: #{params[:msisdn]}, and recieved ERROR. something wrong" logger.info mess12 telegram.telegram_message2(mess12) raise SOAPError, 'ERROR. mts_7_technical_reconnect something wrong' end output= {:answercode => ans, :message => mess} mess12 = "HAproxy #{request.remote_ip}, request device real ip: #{request.headers['X-Forwarded-For']} requested mts_7_technical_reconnect procedure with such parameter as: MSISDN: #{params[:msisdn]}, and recieved SUCCESS #{output.to_s}" logger.info mess12 telegram.telegram_message2(mess12) render :soap => output end end