示例#1
0
文件: topology.py 项目: lamr14/KNet
    def delete(self):
        if not self.status:
            log.warn("No Topology Exists for delete")
            return "No Topology Exists "

        log.debug("Deleting nodes")
        for n in self.nodeobjs:
            n.delete()
        del self.nodeobjs[:]

        log.debug("Deleting switches")
        for s in self.switchobjs:
            s.delete()
        del self.switchobjs[:]

        log.debug("Deleting Links")
        del self.linkobjs[:]

        log.debug("Deleting Networks")
        del self.networkobjs[:]

        log.debug("Cleaning DB")
        utils.purge_db()
        # reinitializing the instance variable
        self.initialize()
        # resetting the id generator(start from 0)
        utils.reset_id()
        # removing the UI data file
        self.__delete_ui_data()
        res = "****--------- Topology Deleted---------****"
        log.debug(res)
        return res
示例#2
0
文件: topology.py 项目: lamr14/KNet
    def get(self):
        if not self.status:
            log.warn("No Topology Exists")
            return "No Topology Exists "

        topo = utils.format_topo({"Name": self.name,
                                  "Status": self.status,
                                  "Controller": self.controller})
        nodes = utils.format_nodes(self.__getNodeDetails())
        switches = utils.format_switches(self.__getSwitchDetails())
        links = utils.format_links(utils.link_t.all())
        result = "Topology \n" + str(topo) + "\n"
        result += "Nodes \n" + str(nodes) + "\n"
        result += "Switches \n" + str(switches) + "\n"
        result += "Links \n" + str(links) + "\n"
        log.debug(result)
        return result
示例#3
0
文件: topology.py 项目: lamr14/KNet
    def create(self, tdata):
        log.debug("Topology Create called with Data" + str(tdata))
        if self.status:
            res = "Already Topology is running, we cannot create another one"
            log.warn(res)
            return res

        if not self.__validate(tdata):
            log.error("Topology data schmea validation check failed")
            return {"Error": "Invalid Topology Schema Check Data"}

        # create network
        self.name = tdata["Topology"]["name"]
        self.controller = tdata["Topology"]["controller"]["url"]

        # check network object is present in the topology input
        if "networks" in tdata["Topology"]:
            log.debug("Topology Creating Networks")
            for net in tdata["Topology"]["networks"]:
                networkobj = Network(net)
                self.networkobjs.append(networkobj)

        # qos - doesnt require objects. we need to just pass this
        # complete dict to Linkobj.
        if "qos" in tdata["Topology"]:
            self.qos = tdata["Topology"]["qos"]

        # create nodes
        log.debug("Topology Creating Nodes")
        for n in tdata["Topology"]["nodes"]:
            # get the network object for the node
            net = None
            if "network" in n:
                net = self.__getnetwork(n["network"])
            nodeobj = Node(data=n, network=net)
            nodeobj.create()
            self.nodeobjs.append(nodeobj)

        # Create switches
        log.debug("Topology Creating Switches")
        for s in tdata["Topology"]["switches"]:
            # Take details from global openflow json
            if "openflow" not in s:
                s["openflow"] = tdata["Topology"]["openflow"]
            sobj = Switch(data=s, controller=self.controller)
            sobj.create()
            self.switchobjs.append(sobj)

        # create links
        log.debug("Topology Creating Links")
        for l in tdata["Topology"]["links"]:
            # creating nodeLinks
            if "nodes" in l:
                lobj = NodeLink(data=l, qos=self.qos)
            else:
                # creating Switch Links
                lobj = SwitchLink(data=l)
            lobj.create()
            self.linkobjs.append(lobj)

        self.status = "Created"

        log.debug("Topology Creation Completed")

        # Todo - Store Network, Qos in DB??

        # write the topology data for UI
        self.__write_ui_data()
        log.debug("Topology details updated in data.js for UI")

        # return the topology data(for CLI)
        res = utils.format_createtopo({"Name": self.name,
                                       "Status": self.status,
                                       "Controller": self.controller,
                                       "Nodes": self.__getNodeNames(),
                                       "Switches": self.__getSwitchNames(),
                                       "Links": self.__getLinks()})
        log.debug(res)
        return res
示例#4
0
    def create(self, tdata):
        log.debug("Topology Create called with Data" + str(tdata))
        if self.status:
            res = "Already Topology is running, we cannot create another one"
            log.warn(res)
            return res

        if not self.__validate(tdata):
            log.error("Topology data schmea validation check failed")
            return {"Error": "Invalid Topology Schema Check Data"}

        # setting the name
        self.name = tdata["Topology"]["name"]

        # setting the controller
        if "controller" in tdata["Topology"]:
            self.controller = tdata["Topology"]["controller"]["url"]
        '''
        # check network object is present in the topology input
        if "networks" in tdata["Topology"]:
            log.debug("Topology Creating Networks")
            for net in tdata["Topology"]["networks"]:
                networkobj = Network(net)
                self.networkobjs.append(networkobj)
        '''
        # qos - doesnt require objects. we need to just pass this
        # complete dict to Linkobj.
        log.debug("Topology Creating Qos")
        if "qos" in tdata["Topology"]:
            self.qos = tdata["Topology"]["qos"]

        # create hosts
        log.debug("Topology Creating Hosts")
        for h in tdata["Topology"]["hosts"]:
            hostobj = Host(data=h)
            hostobj.create()
            self.hostobjs.append(hostobj)

        # create servers
        if "servers" in tdata["Topology"]:
            log.debug("Topology Creating Servers")
            for s in tdata["Topology"]["servers"]:
                serverobj = Server(data=s)
                serverobj.create()
                self.serverobjs.append(serverobj)

        # Create switches
        log.debug("Topology Creating Switches")
        for s in tdata["Topology"]["switches"]:
            # Take details from global openflow json
            if "openflow" not in s:
                if "openflow" in tdata["Topology"]:
                    s["openflow"] = tdata["Topology"]["openflow"]

            my_controller = None
            if "controller" in s:
                my_controller = s["controller"]["url"]
            else:
                my_controller = self.controller

            sobj = Switch(data=s, controller=my_controller)
            sobj.create()
            self.switchobjs.append(sobj)

        # create routers
        if "routers" in tdata["Topology"]:
            log.debug("Topology Creating Routers")
            for n in tdata["Topology"]["routers"]:
                routerobj = Router(data=n)
                routerobj.create()
                self.routerobjs.append(routerobj)

        # create links
        log.debug("Topology Creating Links")
        for l in tdata["Topology"]["links"]:
            # creating nodeLinks
            if "hosts" in l:
                lobj = HostLink(data=l, qos=self.qos)
            elif "servers" in l:
                lobj = ServerLink(data=l, qos=self.qos)
            elif "routers" in l:
                lobj = RouterLink(data=l, qos=self.qos)
            else:
                # creating Switch Links
                lobj = SwitchLink(data=l)
            lobj.create()
            self.linkobjs.append(lobj)

        log.debug("Wait for 2 seconds for bring Links")
        time.sleep(2)

        # Adding Static Routes in the hosts
        for n in tdata["Topology"]["hosts"]:
            if "static_routes" in n:
                for route in n["static_routes"]:
                    docker.add_static_route(n["name"], route["subnet"],
                                            route["via"])

        # Adding Static Routes in the servers
        if "servers" in tdata["Topology"]:
            for n in tdata["Topology"]["servers"]:
                if "static_routes" in n:
                    for route in n["static_routes"]:
                        docker.add_static_route(n["name"], route["subnet"],
                                                route["via"])

        # Adding Static Routes in the routers
        if "routers" in tdata["Topology"]:
            for r in tdata["Topology"]["routers"]:
                if "static_routes" in r:
                    for route in r["static_routes"]:
                        docker.add_static_route(r["name"], route["subnet"],
                                                route["via"])

        self.status = "Created"
        log.debug("Topology Creation Completed")

        # Todo - Store Network, Qos in DB??

        # write the topology data for UI
        self.__write_ui_data()
        log.debug("Topology details updated in data.js for UI")

        # return the topology data(for CLI)
        res = utils.format_createtopo({
            "Name": self.name,
            "Status": self.status,
            "Controller": self.controller,
            "Hosts": self.__getHostNames(),
            "Switches": self.__getSwitchNames(),
            "Links": self.__getLinks()
        })
        log.debug(res)
        return res