<commit_before>"""
CSIT test tools.
Authors: <NAME> <EMAIL>, <NAME>@<EMAIL>
Updated: 2013-11-01
"""
from restlib import *
from testmodule import TestModule

class SwitchManager(TestModule):
    """
    Test for the switch manager, including read switch nodes.
    Start 2-layer tree topology network. e.g., in Mininet, run  'sudo mn --controller=remote,ip=127.0.0.1 --mac --topo tree,2'
    """
    def __init__(self,restSubContext='/controller/nb/v2/switchmanager',user=DEFAULT_USER, password=DEFAULT_PWD,container=DEFAULT_CONTAINER,contentType='json',prefix=DEFAULT_PREFIX):
       super(self.__class__,self).__init__(restSubContext,user,password,container,contentType,prefix)

    def get_nodes(self):
        """
        The name is suggested to match the NB API.
        list all nodes and their properties
        """
        suffix='nodes'
        r=super(self.__class__,self).read(suffix)
        if r:
            return r

    def get_node(self,suffix):
        """
        The name is suggested to match the NB API.
        list nodeconnector and properties of a node.
        """
        r=super(self.__class__,self).read(suffix)
        if r:
            print r

    def add_property_to_node(self,suffix):
        """
        Add a property to given node.
        """
        r=super(self.__class__,self).update(suffix)

    def remove_property_from_node(self,suffix):
        """
        Remove a property from given node.
        """
        r=super(self.__class__,self).delete(suffix)

    def test_list_nodes(self):
        """
        The name is suggested to match the NB API.
        list all nodes and their properties
        >>> SwitchManager().test_list_nodes()
        True
        """
        r=self.get_nodes()
        if r:
            print ({u'type': u'OF', u'id': u'00:00:00:00:00:00:00:01'} in r and \
                   {u'type': u'OF', u'id': u'fd00:c2b6:b24b:be67:2827:688d:e6a1:6a3b'} in r and \
                   {u'type': u'OF', u'id': u'fd00:a516:7c1b:17cd:6d81:2137:bd2a:2c5b'} in r)

    def test_node_property_operations(self,node_type,node_id,property,value):
        """
        Test the add,remove,show actions on node properties.
        Remove a property from given node.

        >>> SwitchManager().test_node_property_operations('OF','00:00:00:00:00:00:00:01','description','Switch1')
        True
        True
        True
        >>> SwitchManager().test_node_property_operations('OF','fd00:c2b6:b24b:be67:2827:688d:e6a1:6a3b','description','Switch2')
        True
        True
        True
        """
        suffix='node/'+node_type+'/'+node_id+'/property'
        #current node properties should not include description
        r=self.get_nodes()
        index = r.index({u'type': node_type, u'id': node_id})
        print r[index+1][property] == {u'value': u'None'}
        #After adding, current node properties should include description
        self.add_property_to_node(suffix+'/'+property+'/'+value)
        r=self.get_nodes()
        index = r.index({u'type': node_type, u'id': node_id})
        print r[index+1][property] ==  {u'value': value}
        #After removing, current node properties should not include description
        self.remove_property_from_node(suffix+'/'+property)
        r=self.get_nodes()
        index = r.index({u'type': node_type, u'id': node_id})
        print r[index+1][property] ==  {u'value': u'None'}<commit_msg>Add nodeconnector property add/remove functions.<commit_after>"""
CSIT test tools.
Authors: <NAME> <EMAIL>, <NAME> <EMAIL>
Updated: 2013-11-01
"""
from restlib import *
from testmodule import TestModule

class SwitchManager(TestModule):
    """
    Test for the switch manager, including read switch nodes.
    Start 2-layer tree topology network. e.g., in Mininet, run  'sudo mn --controller=remote,ip=127.0.0.1 --mac --topo tree,2'
    """
    def __init__(self,restSubContext='/controller/nb/v2/switchmanager',user=DEFAULT_USER, password=<PASSWORD>,container=DEFAULT_CONTAINER,contentType='json',prefix=DEFAULT_PREFIX):
       super(self.__class__,self).__init__(restSubContext,user,password,container,contentType,prefix)

    def get_nodes(self):
        """
        The name is suggested to match the NB API.
        list all nodes and their properties
        """
        suffix='nodes'
        r=super(self.__class__,self).read(suffix)
        if r:
            return r

    def get_node(self,suffix):
        """
        The name is suggested to match the NB API.
        list nodeconnector and properties of a node.
        """
        r=super(self.__class__,self).read(suffix)
        if r:
            return r

    def add_property_to_node(self,node_type,node_id,property,value):
        """
        Add a property to given node.
        """
        suffix='node/'+node_type+'/'+node_id+'/property'
        r=super(self.__class__,self).update(suffix+'/'+property+'/'+str(value))

    def remove_property_from_node(self,node_type,node_id,property):
        """
        Remove a property from given node.
        """
        suffix='node/'+node_type+'/'+node_id+'/property'
        r=super(self.__class__,self).delete(suffix+'/'+property)

    def add_property_to_nodeconnector(self,node_type,node_id,nc_type,nc_id,property,value):
        """
        Add a property to given node.
        """
        suffix='nodeconnector/'+node_type+'/'+node_id+'/'+nc_type+'/'+nc_id+'/property'
        r=super(self.__class__,self).update(suffix+'/'+property+'/'+str(value))

    def remove_property_from_nodeconnector(self,node_type,node_id,nc_type,nc_id,property):
        """
        Add a property to given node.
        """
        suffix='nodeconnector/'+node_type+'/'+node_id+'/'+nc_type+'/'+nc_id+'/property'
        r=super(self.__class__,self).delete(suffix+'/'+property)

    def test_list_nodes(self):
        """
        The name is suggested to match the NB API.
        list all nodes and their properties
        >>> SwitchManager().test_list_nodes()
        True
        """
        r=self.get_nodes()
        if r:
            print ({u'type': u'OF', u'id': u'00:00:00:00:00:00:00:01'} in r and \
                   {u'type': u'OF', u'id': u'fd00:c2b6:b24b:be67:2827:688d:e6a1:6a3b'} in r and \
                   {u'type': u'OF', u'id': u'fd00:a516:7c1b:17cd:6d81:2137:bd2a:2c5b'} in r)

    def test_node_property_operations(self,node_type,node_id,property,value):
        """
        Test the add,remove,show actions on node properties.

        >>> SwitchManager().test_node_property_operations('OF','00:00:00:00:00:00:00:01','description','Switch1')
        True
        True
        True
        >>> SwitchManager().test_node_property_operations('OF','fd00:c2b6:b24b:be67:2827:688d:e6a1:6a3b','description','Switch2')
        True
        True
        True
        >>> SwitchManager().test_node_property_operations('OF','fd00:a516:7c1b:17cd:6d81:2137:bd2a:2c5b','description','Switch3')
        True
        True
        True
        """
        #current node properties should not include description
        r=self.get_nodes()
        index = r.index({u'type': node_type, u'id': node_id})
        print r[index+1][property] == {u'value': u'None'}
        #After adding, current node properties should include description
        self.add_property_to_node(node_type,node_id,property,value)
        r=self.get_nodes()
        index = r.index({u'type': node_type, u'id': node_id})
        print r[index+1][property] ==  {u'value': value}
        #After removing, current node properties should not include description
        self.remove_property_from_node(node_type,node_id,property)
        r=self.get_nodes()
        index = r.index({u'type': node_type, u'id': node_id})
        print r[index+1][property] ==  {u'value': u'None'}

    def test_nodeconnector_property_operations(self,node_type,node_id,nc_type, nc_id,property,value):
        """
        Test the add,remove,show actions on nodeconnector properties.

        >>> SwitchManager().test_nodeconnector_property_operations('OF','00:00:00:00:00:00:00:01','OF','1','bandwidth',1000)
        True
        True
        True
        """
        node_suffix='node/'+node_type+'/'+node_id
        #default bw should be 10000000000L
        r=self.get_node(node_suffix)
        index = r.index({u'node': {u'type': node_type, u'id': node_id}, u'type': nc_type, u'id': nc_id})
        default_value = r[index+1][property]['value']
        #After setting, the value should be the value
        self.add_property_to_nodeconnector(node_type,node_id,nc_type,nc_id,property,value)
        r=self.get_node(node_suffix)
        index = r.index({u'node': {u'type': node_type, u'id': node_id}, u'type': nc_type, u'id': nc_id})
        current_value = r[index+1][property]['value']
        print current_value == value
        #After removing, the bandwidth property should be empty
        self.remove_property_from_nodeconnector(node_type,node_id,nc_type,nc_id,property)
        r=self.get_node(node_suffix)
        index = r.index({u'node': {u'type': node_type, u'id': node_id}, u'type': nc_type, u'id': nc_id})
        print property not in r[index+1]
        self.add_property_to_nodeconnector(node_type,node_id,nc_type,nc_id,property,default_value)
        r=self.get_node(node_suffix)
        index = r.index({u'node': {u'type': node_type, u'id': node_id}, u'type': nc_type, u'id': nc_id})
        current_value = r[index+1][property]['value']
        print current_value == default_value
{'fraction_non_alphanumeric': 0.1403823809020809, 'fraction_numerical': 0.029365163198478927, 'mean_word_length': 3.0478837109876014, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 36, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 1, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1852698', 'n_tokens_mistral': 3211, 'n_tokens_neox': 3034, 'n_words': 591}