Example #1
0
 def get(self, fid, cls, op, slot, session):
     """
     Get Handler for diff configuration to work with Rule Manager.
     """
     if fid == -1:
         fid = "None"
     ormop = clsmanager.getConfigOperation(op)
     if clsmanager.isConfigClass(cls, session):
         if fid in self.diffdict.keys():
             container = cls().getcontainer()
             if container in self.diffdict[fid].keys():
                 if ormop in self.diffdict[fid][container].keys():
                     if ormop == 'PATCH':
                         if op in \
                            self.diffdict[fid][container][ormop].keys():
                             if slot in\
                                self.diffdict[fid][container][ormop][op].\
                                keys():
                                 tmp = self.diffdict[fid][container]
                                 return tmp[ormop][op][slot]
                     else:
                         if slot in\
                            self.diffdict[fid][container][ormop].keys():
                             tmp = self.diffdict[fid][container]
                             return tmp[ormop][slot]
     # print("No config for :FID", fid, " XLSX:", cls,
     #      " OP: ", ormop, " Slot:", slot)
     return list([])
Example #2
0
    def handlediff(self, file, session):
        """
        Start the diff handling for all the switch configuration.
        """
        print("Handle Diff Start.")
        self.log(1, "Handle Diff Start.")
        ret = self.loadfromfile(file)
        if ret:
            self.log(3, "Load from file Failed")
            print("Load from file Failed")
            return ret
        self.loadcurrent(session)
        self.diffslots = list(set(self.currentslots + self.loadslots))
        self.difffids = list(set(self.loadfids + self.currentfids))
        print("Init Class Ordering.")
        self.log(1, "Init Class Ordering.")
        clsmanager.clsorderinginit(self.diffslots)
        print("Calculating Diff Start")
        self.log(1, "Calculating Diff Start")
        for j in range(len(self.difffids)):
            fid = self.difffids[j]
            sfid = fid
            dfid = fid
            fidkeys = self.currentdict.keys()
            if fid not in fidkeys:
                if fid == "None":
                    if '128' in fidkeys:
                        dfid = '128'

            if fid not in self.loadfids:
                if fid == "None":
                    if '128' in self.loadfids:
                        sfid = '128'
            self.diffdict.update(dict({fid: dict()}))
            self.diffdetails.update(dict({fid: dict()}))
            for i in range(len(self.clslist)):
                tgtcls = self.clslist[i]
                if tgtcls not in self.clsfilters:
                    continue
                clskey = tgtcls().getcontainer()
                if fid != '128':
                    if fid != 'None':
                        if not clsmanager.checkvfsupported(tgtcls):
                            continue
                if clsmanager.isConfigClass(tgtcls, session):
                    if sfid in self.loaddict.keys() and \
                       clskey in self.loaddict[sfid].keys():
                        old = self.loaddict[sfid][clskey]
                    else:
                        old = []
                    if dfid in self.currentdict.keys() and \
                       clskey in self.currentdict[dfid].keys():
                        new = self.currentdict[dfid][clskey]
                    else:
                        new = []
                    self.diff(self.clslist[i], fid, old, new)
        print("Calculating Diff Complete.")
        self.log(1, "Calculating Diff Complete.")
        self.showdiff()
        return 0
Example #3
0
 def loadcurrentfid(self, session, fid, container):
     """Load the current switch configuration per fid."""
     print("Loading Switch configuration Start[", fid, "].")
     self.log(1, "Loading Switch configuration Start[", fid, "].")
     tmpdict = dict()
     for i in range(len(self.clslist)):
         if not clsmanager.isConfigClass(self.clslist[i], session):
             continue
         if container is not None and \
            container != self.clslist[i]().getcontainer():
             continue
         if any(self.configlist) and\
            self.clslist[i]().getcontainer() not in self.configlist:
             continue
         ret = self.clslist[i].get(session)
         if utils.is_failed_resp(ret):
             # print(ret)
             tmpdict.update(dict({self.clslist[i]().getcontainer():
                                  list()}))
             continue
         if not isinstance(ret, list):
             ret = [ret]
         tmpdict.update(dict({self.clslist[i]().getcontainer(): ret}))
     self.currentdict.update(dict({fid: tmpdict}))
     print("Loading Switch configuration Complete.")
     self.log(1, "Loading Switch configuration Complete.")
Example #4
0
 def dumptofidclasses(self, wb, session, fmtobj):
     """
     Dump the switch configuration object in .xlsx file based on format.
     """
     containerlist = []
     for i in range(len(self.clslist)):
         cls = self.clslist[i]
         if not clsmanager.isConfigClass(cls, session):
             # print("skipping non config class", self.clslist[i])
             continue
         ret = self.clslist[i].get(session)
         if utils.is_failed_resp(ret):
             # print(ret)
             continue
         if not isinstance(ret, list):
             ret = [ret]
         container = clsmanager.getContainerfromCls(cls)
         containerlist += [container]
         sheetdict = clsmanager.getmultiplesheetdict(container)
         if sheetdict is None or fmtobj == 'json':
             ws = wb.create_sheet(container)
         self.dumplist = ret
         listlen = len(self.dumplist)
         if fmtobj == 'json':
             chunk = self.chunkcount
             cols = self.columncount
             loopcount = int(listlen / chunk) + 1
             if chunk > 1 and int(listlen % chunk) > 0:
                 loopcount += 1
             endindex = 0
             for j in range(loopcount):
                 startindex = endindex
                 endindex = startindex + chunk
                 r = int(2 + (j / cols))
                 c = int(2 + (j % cols))
                 cell = ws.cell(row=r, column=c)
                 if loopcount >= j + 1:
                     cell.value = str(self.dumplist[startindex:endindex])
                 else:
                     cell.value = str(self.dumplist[startindex:])
         else:
             baserow = 5
             addcount = 0
             for j in range(len(self.dumplist)):
                 obj = self.dumplist[j]
                 if sheetdict is None:
                     if j == 0:
                         obj.dumpheaders(ws, baserow - 1)
                     count = obj.dumpdata(ws, baserow + addcount)
                     addcount += count
                 else:
                     for k, v in sheetdict.items():
                         # print(k, v)
                         ws = wb.create_sheet(container + "." + k)
                         if j == 0:
                             obj.dumpheaders(ws, baserow - 1, v)
                         count = obj.dumpdata(ws, baserow + addcount, v)
         # cell = ws.cell(row=1, column=1)
         # cell.value = listlen
     self.dumpcontainers = containerlist
Example #5
0
 def dumptofiddefault(self, wb, session, fmtobj):
     """
     Dump the switch configuration object in .xlsx file in JSON format.
     """
     self.dumplist = []
     if fmtobj != 'json':
         self.log(1, "Defaulting to json for the supported fmtfile")
         print("Defaulting to json for the supported fmtfile")
     for i in range(len(self.clslist)):
         cls = self.clslist[i]
         if any(self.configlist) and\
            cls().getcontainer() not in self.configlist:
             continue
         if not clsmanager.isConfigClass(cls, session):
             # print("skipping non config class", self.clslist[i])
             continue
         ret = self.clslist[i].get(session)
         if utils.is_failed_resp(ret):
             # print(ret)
             continue
         if not isinstance(ret, list):
             ret = [ret]
         self.dumplist += ret
     wb.write(str(self.dumplist))
Example #6
0
    def applygoldenobject(self, session, dumpfile, container, osr):
        """
        Apply a specific golden configuration object from a persistent fil
        to all the objects of the same class on the switch.
        """
        my_file = Path(dumpfile)
        if not my_file.exists():
            # print("No file found with the name", dumpfile)
            self.log(1, "No file found with the name", dumpfile)
            return False
        # pylint: disable=W1401
        ext = re.sub(".*\.", ".", str(dumpfile))
        if ext == '.json':
            ret, gob = self.loadmygobjson(dumpfile, container)
            print(gob, container)
        else:
            wb = openpyxl.load_workbook(dumpfile)
            ret, gob = self.getmyobjectfromrow(wb, container, osr)

        if ret is False:
            print("Unable to load the object")
            self.log(3, "Unable to load the object", dumpfile, container, osr)
            return False
        # print(gob)
        self.log(1, dumpfile, container, osr, gob)
        clsmanager.sleep(10, session)
        self.loadcurrent(session, container)
        clsmanager.clsorderinginit(self.currentslots)
        self.diffslots = self.currentslots
        self.difffids = self.currentfids
        self.rmmanager = rmmanager(rmmanager.defaultconfigrules(), self)
        for j in range(len(self.currentfids)):
            fid = self.currentfids[j]
            self.diffdict.update(dict({fid: dict()}))
            self.loaddict.update(dict({fid: dict()}))
            self.diffdetails.update(dict({fid: dict()}))
            for i in range(len(self.clslist)):
                clskey = self.clslist[i]().getcontainer()
                if clskey != container:
                    continue
                if clsmanager.isConfigClass(self.clslist[i], session):
                    new = self.currentdict[fid][clskey]
                    # Form the old list
                    oldlist = []
                    for m in range(len(new)):
                        loadconfig = json.loads(gob.__repr__())
                        newobj = self.clslist[i](loadconfig)
                        for k in range(len(newobj.keyslist)):
                            for l in range(len(new[m].keyslist)):
                                if newobj.keyslist[k].uname ==\
                                   new[m].keyslist[l].uname:
                                    newobj.keyslist[k].setvalue(
                                        new[m].keyslist[l].value)
                        if container in 'fibrechannel':
                            newobj.set_user_friendly_name(
                                new[m].peek_user_friendly_name())
                        oldlist.append(newobj)
                    self.loaddict[fid].update(dict({container: oldlist}))
                    # End of old list creation
                    self.diff(self.clslist[i], fid, oldlist, new)
        self.showdiff(session)
        print("Apply Rule Manager.")
        self.log(1, "Apply Rule Manager.")
        self.rmmanager.allconfigrules(session, self.currentfids,
                                      self.currentslots)
        return True