Ejemplo n.º 1
0
    def create_switch(self):
        switch_id = self.payload['switch_id']

        logger.info('Switch %s creation request: timestamp=%s',
                    switch_id, self.timestamp)

        switch = graph.find_one('switch',
                                property_key='name',
                                property_value='{}'.format(switch_id))
        if not switch:
            new_switch = Node("switch",
                              name="{}".format(switch_id),
                              state="active",
                              address=self.payload['address'],
                              hostname=self.payload['hostname'],
                              description=self.payload['description'],
                              controller=self.payload['controller'])
            graph.create(new_switch)
            logger.info('Adding switch: %s', switch_id)
            return True
        else:
            graph.merge(switch)
            switch['state'] = "active"
            switch['address'] = self.payload['address']
            switch['hostname'] = self.payload['hostname']
            switch['description'] = self.payload['description']
            switch['controller'] = self.payload['controller']
            switch.push()
            logger.info('Activating switch: %s', switch_id)
            return True
Ejemplo n.º 2
0
    def activate_switch(self):
        switch_id = self.payload['switch_id']

        logger.info('Switch %s activation request: timestamp=%s',
                    switch_id, self.timestamp)

        switch = graph.find_one('switch',
                                property_key='name',
                                property_value='{}'.format(switch_id))
        if switch:
            graph.merge(switch)
            switch['state'] = "active"
            switch.push()
            logger.info('Activating switch: %s', switch_id)
        return True
Ejemplo n.º 3
0
    def remove_switch(self):
        switch_id = self.payload['switch_id']

        logger.info('Switch %s removing request: timestamp=%s',
                    switch_id, self.timestamp)

        switch = graph.find_one('switch',
                                property_key='name',
                                property_value='{}'.format(switch_id))
        if switch:
            graph.merge(switch)
            switch['state'] = "removed"
            switch.push()
            logger.info('Removing switch: %s', switch_id)

            if self.isl_exists(switch_id, None):
                self.delete_isl(switch_id, None)

        return True
Ejemplo n.º 4
0
    def deactivate_switch(self):
        switch_id = self.payload['switch_id']

        logger.info('Switch %s deactivation request: timestamp=%s, ',
                    switch_id, self.timestamp)

        switch = graph.find_one('switch',
                                property_key='name',
                                property_value='{}'.format(switch_id))
        if switch:
            graph.merge(switch)
            switch['state'] = "inactive"
            switch.push()
            logger.info('Deactivating switch: %s', switch_id)
            return True

        else:
            logger.warn('Switch %s does not exist in topology', switch_id)
        return True
Ejemplo n.º 5
0
    def create_isl(self):
        path = self.payload['path']
        latency = int(self.payload['latency_ns'])
        a_switch = path[0]['switch_id']
        a_port = int(path[0]['port_no'])
        b_switch = path[1]['switch_id']
        b_port = int(path[1]['port_no'])
        speed = int(self.payload['speed'])
        available_bandwidth = int(self.payload['available_bandwidth'])

        a_switch_node = graph.find_one('switch',
                                       property_key='name',
                                       property_value='{}'.format(a_switch))
        if not a_switch_node:
            logger.error('Isl source was not found: %s', a_switch_node)
            return False

        b_switch_node = graph.find_one('switch',
                                       property_key='name',
                                       property_value='{}'.format(b_switch))
        if not b_switch_node:
            logger.error('Isl destination was not found: %s', b_switch_node)
            return False

        isl_lock.acquire()
        try:
            isl_exists_query = ("MATCH (a:switch)-[r:isl {{"
                                "src_switch: '{}', "
                                "src_port: {}, "
                                "dst_switch: '{}', "
                                "dst_port: {}}}]->(b:switch) return r")
            isl_exists = graph.run(isl_exists_query.format(a_switch,
                                                           a_port,
                                                           b_switch,
                                                           b_port)).data()

            if not isl_exists:
                logger.info('Isl %s_%d creation request: timestamp=%s',
                            a_switch, a_port, self.timestamp)

                isl_query = ("MATCH (u:switch {{name:'{}'}}), "
                             "(r:switch {{name:'{}'}}) "
                             "MERGE (u)-[:isl {{"
                             "src_port: {}, "
                             "dst_port: {}, "
                             "src_switch: '{}', "
                             "dst_switch: '{}', "
                             "latency: {}, "
                             "speed: {}, "
                             "available_bandwidth: {}}}]->(r)")
                graph.run(isl_query.format(a_switch_node['name'],
                                           b_switch_node['name'],
                                           a_port,
                                           b_port,
                                           a_switch,
                                           b_switch,
                                           latency,
                                           speed,
                                           available_bandwidth))

                logger.info('ISL between %s and %s created',
                            a_switch_node['name'], b_switch_node['name'])
            else:
                logger.debug('Isl %s_%d update request: timestamp=%s',
                             a_switch, a_port, self.timestamp)

                isl_update_query = ("MATCH (a:switch)-[r:isl {{"
                                    "src_switch: '{}', "
                                    "src_port: {}, "
                                    "dst_switch: '{}', "
                                    "dst_port: {}}}]->(b:switch) "
                                    "set r.latency = {} return r")
                graph.run(isl_update_query.format(a_switch,
                                                  a_port,
                                                  b_switch,
                                                  b_port,
                                                  latency)).data()

                logger.debug('ISL between %s and %s updated',
                             a_switch_node['name'], b_switch_node['name'])

        except Exception as e:
            logger.exception('ISL between %s and %s creation error: %s',
                             a_switch_node['name'], b_switch_node['name'],
                             e.message)

        finally:
            isl_lock.release()

        return True