import re
import csv
import string
class ConfigBuilder(object):
def __init__(self, CSV,basetemplatefile,vrftemplatefile):
self.CSV = CSV
self.basetemplatefile = basetemplatefile
self.basetemplatecontents = ''
with open(self.basetemplatefile) as b:
self.basetemplatecontents = b.read()
self.vrftemplatefile = vrftemplatefile
self.vrftemplatecontents = ''
self.baseresultstring = self.basetemplatecontents
with open(self.vrftemplatefile) as v:
self.vrftemplatecontents = v.read()
self.vrfresultstring = ''
def generateBaseConfig(self):
# OPEN CSV CONFIG FILE AND READ INTO AN ARRAY
with open(self.CSV, mode='r') as csvfile:
Detail_file = csv.reader(csvfile, delimiter=',')
# Cycle through Each ITEM-VALUE pair in the DETAIL FILE
for row in Detail_file:
# DEREFERENCE row ARRAY FOR CLARITY IN READING CODE
ITEM = row[0]
VALUE = row[1]
# CHECK
@BUILD_SITE_ID,CINCINNATI
@BUILD_ADDRESS_NUMBER,700
@BUILD_ADDRESS_STREET,Pete Rose Way
@BUILD_ADDRESS_CITY,CINCINNATI
@BUILD_ADDRESS_STATE,OH
@BUILD_ZIP,45203
@BUILD_ADDRESS_COUNTRY,US
@BUILD_DATE,3/9/2018
@BUILD_SCRIPT_VERSION,2.0
@=============SITE_CONFIG=============,
@SITE_NUM_ROUTERS,2
@SITE_OTHER_RTR_NAME_OF_ROUTER,CINCINNATI.R2
@SITE_ATT_HANDOFF_DEVICE,FLEXWARE
@SITE_ATT_FLEXWARE_HOSTNAME,
@SITE_ATT_FLEXWARE_LAN_IP
@SITE_ATT_FLEXWARE_LAN_MASK
@SITE_ATT_ANIRA_HOSTNAME,
@SITE_ATT_ANIRA_LAN_IP
@SITE_ATT_ANIRA_LAN_MASK
@============ROUTER_CONFIG============,
@RTR_ROUTER_NUMBER,1
@RTR_NAME_OF_ROUTER,CINCINNATI.RTR1
@RTR_MGRE_IP,2.2.2.2
@RTR_MGMT_IP,1.1.1.1
@=============ROUTER_INFO=============,
@RTR_MODEL,ASR1001
@RTR_SERIALNUM,
@RTR_IOS_IMAGE,
@RTR_NUM_INTS,
@RTR_NUM_FIBER_SFPS,
@RTR_NUM_COPPER_SFPS,
@========GLOBAL_VRF_CONFIG============,
@GLOBAL_VRF_NUM_INTS,2
@GLOBAL_VRF_INT1_WAN_INTNAME,
@GLOBAL_VRF_INT1_WAN_DESC, TO
if VALUE.strip()!='':
# DO THE ACTUAL SUBSTITUTION
self.baseresultstring = self.baseresultstring.replace(ITEM,VALUE)
return self.baseresultstring
def generateAgencyVRFs(self):
NumVRFs = self.queryitem('@NUMBER_OF_AGENCY_VRFS')
for x in range(0, int(NumVRFs)):
print x
# OPEN CSV CONFIG FILE AND READ INTO AN ARRAY
self.vrfresultstring = self.vrfresultstring + self.vrftemplatecontents
with open(self.CSV, mode='r') as csvfile:
Detail_file = csv.reader(csvfile, delimiter=',')
# Cycle through Each ITEM-VALUE pair in the DETAIL FILE
for row in Detail_file:
# DEREFERENCE row ARRAY FOR CLARITY IN READING CODE
ITEM = row[0]
VALUE = row[1]
# remove VRF1, VRF2, etc in ITEM key and replace with just VRF so I can use one template for all vrfs
ITEM = ITEM.replace(re.match('VRF[0-9]',ITEM), 'VRF')
print ITEM + '=' + VALUE
# CHECK TO MAKE SURE THE CONFIG_VALUE ISN"T BLANK
if VALUE.strip()!='':
# DO THE ACTUAL SUBSTITUTION
self.vrfresultstring = self.vrfresultstring.replace(ITEM,VALUE)
return self.vrfresultstring
def queryitem(self,querystring):
# OPEN CSV CONFIG FILE AND READ INTO AN ARRAY
with open(self.CSV, mode='r') as csvfile:
Detail_file = csv.reader(csvfile, delimiter=',')
# Cycle through Each ITEM-VALUE pair in the DETAIL FILE
for row in Detail_file:
# DEREFERENCE row ARRAY FOR CLARITY IN READING CODE
ITEM = row[0]
VALUE = row[1]
if VALUE.strip()!='':
if ITEM == querystring:
queryresult = VALUE
return queryresult
def writeconfig(self,outputfilename,FinalOutput):
if os.path.isfile(outputfilename):
os.remove(outputfilename)
with open(outputfilename + '.txt', 'w') as outputFile:
print '\n WRITING FINAL CONFIG FILE: ' + outputfilename + '.txt'
outputFile.write(FinalOutput)
def fullbuild(self):
baseconfig = self.generateBaseConfig()
vrfconfig = self.generateAgencyVRFs()
outputfilename = self.queryitem('@RTR_NAME_OF_ROUTER')
FinalOutput = baseconfig + vrfconfig
self.writeconfig(outputfilename,FinalOutput)
def main():
MPLSGen = ConfigBuilder('RouterDetails.csv','BaseMPLSRouter.tmpl','AgencyVRF.tmpl')
MPLSGen.fullbuild()
if __name__ == '__main__':
main()
@GLOBAL_VRF_INT1_WAN_FUNCTION,
@GLOBAL_VRF_INT1_WAN_MEDIATYPE,
@GLOBAL_VRF_INT1_WAN_SFP,
@GLOBAL_VRF_INT1_WAN_IP1_MASK,
@GLOBAL_VRF_INT2_R2R_INTNAME,
@GLOBAL_VRF_INT2_R2R_DESC,
@GLOBAL_VRF_INT2_R2R_FUNCTION,
@GLOBAL_VRF_INT2_R2R_MEDIATYPE,
@GLOBAL_VRF_INT2_R2R_SFP,
@GLOBAL_VRF_INT2_R2R_IP1_MASK,
@GLOBAL_WAN_NEXT_HOP,
@========AGENCY VRF_CONFIG============,
@NUMBER_OF_AGENCY_VRFS,1
@AGENCY_VRF1_AGENCY_NUMBER,801
@AGENCY_VRF1_NAME,E801-01-VR-AT
@AGENCY_VRF1_RD,733:801
@AGENCY_VRF1_DESC,INTERBRAND
@AGENCY_VRF1_NUM_INTS,1
@AGENCY_VRF1_INT1_LAN_HANDOFF_INTNAME,GigabitEthernet0/0/2
@AGENCY_VRF1_INT1_LAN_HANDOFF_DESC,LAN To IBCIN-RRASA
@AGENCY_VRF1_INT1_LAN_HANDOFF_FUNCTION,LAN_HANDOFF
@AGENCY_VRF1_INT1_LAN_HANDOFF_MEDIATYPE,T
@AGENCY_VRF1_INT1_LAN_HANDOFF_SFP,GE T
@AGENCY_VRF1_INT1_LAN_HANDOFF_HSRP_IP,10.1.1.31
@AGENCY_VRF1_INT1_LAN_HANDOFF_HSRP_GROUP,3425
@AGENCY_VRF1_INT1_LAN_HANDOFF_IP1_MASK,ip address 10.1.1.30 255.255.255.0
@AGENCY_VRF1_INT1_LAN_HANDOFF_IP2_MASK,ip address 10.201.240.30 255.255.240.0 secondary
@AGENCY_VRF1_NUM_ADVERTS,1
@AGENCY_VRF1_ADVERT1,network 10.1.1.0 mask 255.255.255.0
@AGENCY_VRF1_ADVERT2,
@AGENCY_VRF1_ADVERT3,
@AGENCY_VRF1_ADVERT5,
@AGENCY_VRF1_ADVERT6,
@AGENCY_VRF1_ADVERT7,
@AGENCY_VRF2_NAME,
@AGENCY_VRF2_RD,
@AGENCY_VRF2_DESC,
@AGENCY_VRF2_NUM_INTS,
@AGENCY_VRF2_INT1_INTNAME,
@AGENCY_VRF2_INT1_DESC,
@AGENCY_VRF2_INT1_FUNCTION,
@AGENCY_VRF2_INT1_MEDIATYPE,
@AGENCY_VRF2_INT1_SFP,
@AGENCY_VRF2_INT1_HSRP_IP,
@AGENCY_VRF2_INT1_HSRP_GROUP,
@AGENCY_VRF2_INT1_IP1_MASK,
@AGENCY_VRF2_INT1_IP2_MASK,
@AGENCY_VRF2_NUM_ADVERTS,
@AGENCY_VRF2_ADVERT1,
@AGENCY_VRF2_ADVERT2,
@AGENCY_VRF2_ADVERT3,
@AGENCY_VRF2_ADVERT5,
@AGENCY_VRF2_ADVERT6,
@AGENCY_VRF2_ADVERT7,