def create_mirror_session(src, dst, direction):
    """create a new mirror session
    @src - source mirror interface
    @dst - destiantion mirrorr interface
    @direction - direction in which packets needs to be mirrored
    @return true if mirror session was created successfully otherwise false
    """
    data_dict = {"dst-intf": nu.if_nametoindex(str(dst)), "type": 1}
    obj = cps_object.CPSObject(module="base-mirror/entry", data=data_dict)
    src_intf_list = ["intf", "0", "src"]
    obj.add_embed_attr(src_intf_list, nu.if_nametoindex(str(src)))
    src_intf_list[2] = "direction"
    obj.add_embed_attr(src_intf_list, direction_map[str(direction)])
    return do_transaction(obj, "create")
def create_mirror_session(src, dst, direction):
    """create a new mirror session
    @src - source mirror interface
    @dst - destiantion mirrorr interface
    @direction - direction in which packets needs to be mirrored
    @return true if mirror session was created successfully otherwise false
    """
    data_dict = {"dst-intf": nu.if_nametoindex(str(dst)), "type": 1}
    obj = cps_object.CPSObject(module="base-mirror/entry",
                               data=data_dict)
    src_intf_list = ["intf", "0", "src"]
    obj.add_embed_attr(src_intf_list, nu.if_nametoindex(str(src)))
    src_intf_list[2] = "direction"
    obj.add_embed_attr(src_intf_list, direction_map[str(direction)])
    return do_transaction(obj, "create")
def handle_show(vlan, port, mac_valid, mac, all, static, static_type):
    get = []
    get_obj = CPSObject("base-mac/query")
    get_obj.add_attr_type("mac-address", "mac")
    if (vlan != 0):
        get_obj.add_attr("vlan", vlan)
        get_obj.add_attr("request-type", 1)
    if (port != 0):
        index = nas_os_utils.if_nametoindex(port)
        get_obj.add_attr("ifindex", index)
        get_obj.add_attr("request-type", 3)
    if (mac_valid == 1):
        get_obj.add_attr("mac-address", mac)
        get_obj.add_attr("request-type", 2)
    if (static_type == 1):
        get_obj.add_attr("static", static)
    print get_obj.get()
    print("\n calling get \n")
    if cps.get([get_obj.get()], get):
        print("\n get returned true\n")
        for i in get:
            print_obj(i)
            print "\n\n"
    else:
        print("\n no objects received")
Esempio n. 4
0
def get_port_queue_id(ifname, queue_number, queue_type):
    """
    Get Queue id
    Sample: get_port_queue_id('e101-032-0', 1, 'UCAST')
    """
    return_data_list = []
    port_id = os_utl.if_nametoindex(ifname)

    queue_obj = QueueCPSObj(
        queue_type=queue_type,
        queue_number=queue_number,
        port_id=port_id)
    ret = cps.get([queue_obj.data()], return_data_list)

    if ret:
        for cps_ret_data in return_data_list:
            m = QueueCPSObj(
                port_id=port_id,
                queue_type=queue_type,
                queue_number=queue_number,
                cps_data=cps_ret_data)
            if (m.extract_attr('type') == queue_type and
                    m.extract_attr('queue-number') == queue_number):
                return m.extract_id()
    else:
        print 'Error in get'

    return None
Esempio n. 5
0
def set_stp_state(stg_id, interface, state):
    obj = cps_object.CPSObject(module=nas_stg_utils.get_stg_keys()[0],
                               data={"switch-id": "0", "id": stg_id})
    el = ["intf", "0", "ifindex"]
    obj.add_embed_attr(el, nas_os_utils.if_nametoindex(interface))
    el[2] = "state"
    obj.add_embed_attr(el, port_state_map[state])
    nas_ut.get_cb_method("set")(obj)
Esempio n. 6
0
def set_stp_state(stg_id, interface, state):
    obj = cps_object.CPSObject(module=nas_stg_utils.get_stg_keys()[0],
                               data={
                                   "switch-id": "0",
                                   "id": stg_id
                               })
    el = ["intf", "0", "ifindex"]
    obj.add_embed_attr(el, nas_os_utils.if_nametoindex(interface))
    el[2] = "state"
    obj.add_embed_attr(el, port_state_map[state])
    nas_common.get_cb_method("set")(obj)
def create_sflow_session(interface, direction, rate):
    """create a new sflow session
    @interface - interface on which sflow is to be enabled
    @direction - direction in which packet sampling is to be done
    @rate - sampling packet rate
    @return true if sflow session is created othersie false
    """
    data_dict = {"ifindex": nu.if_nametoindex(str(interface)),
                 "direction": direction_map[str(direction)],
                 "sampling-rate": str(rate)
                 }
    obj = cps_object.CPSObject(module="base-sflow/entry", data=data_dict)
    return do_transaction(obj, "create")
Esempio n. 8
0
def create_sflow_session(interface, direction, rate):
    """create a new sflow session
    @interface - interface on which sflow is to be enabled
    @direction - direction in which packet sampling is to be done
    @rate - sampling packet rate
    @return true if sflow session is created othersie false
    """
    data_dict = {"ifindex": nu.if_nametoindex(str(interface)),
                 "direction": direction_map[str(direction)],
                 "sampling-rate": str(rate)
                 }
    obj = cps_object.CPSObject(module="base-sflow/entry", data=data_dict)
    return do_transaction(obj, "create")
Esempio n. 9
0
def _add_filter(ports, udp_port, prio, entry_name, exclude=None):
    '''Add a filter for a port which drops the packet to CPU so that
       the DHCP agent can work on it'''

    ifaces = []
    for port in ports:
        if exclude is not None and ifaces != exclude:
            ifaces.append(nas_os_utils.if_nametoindex(port))
    entry = nas_acl.EntryCPSObj(table_id=TBL_ID,
                                entry_id=entry_name,
                                priority=prio)
    entry.add_match_filter(filter_type="IP_PROTOCOL", filter_val=UDP)
    entry.add_match_filter(filter_type="L4_DST_PORT", filter_val=udp_port)
    entry.add_match_filter(filter_type="IN_PORTS", filter_val=ifaces)
    entry.add_action(action_type="PACKET_ACTION", action_val="TRAP_TO_CPU")
    cps_upd = ({'operation': 'create', 'change': entry.data()})
    return cps.transaction([cps_upd])
Esempio n. 10
0
def create_mac_addr(macAddr, vlan, port, action,static,static_type,os_configure,op):

    cps_utils.add_attr_type("base-mac/table/mac-address", "mac")
    if not static_type:
        static = 1
    obj = CPSObject('base-mac/table',
                     data= {"switch-id" : 0, "mac-address" : macAddr,
                       "vlan" : vlan,  "static" : static,  })
    if port:
        index = nas_os_utils.if_nametoindex(port)
        obj.add_attr("ifindex",index)
    if action:
        obj.add_attr("actions",set_action[action])
    if os_configure:
        obj.add_attr("configure-os",os_configure)
    print obj.get()
    commit(obj, op)
    return
Esempio n. 11
0
def handle_delete(vlan, port, mac_valid, mac, all, static, static_type):
    obj = CPSObject("base-mac/table")
    obj.add_attr_type("mac-address", "mac")
    if (vlan != 0):
        obj.add_attr("vlan", vlan)
        obj.add_attr("request-type", 1)
    if (port != 0):
        index = nas_os_utils.if_nametoindex(port)
        obj.add_attr("ifindex", index)
        obj.add_attr("request-type", 3)
    if (mac_valid == 1):
        obj.add_attr("mac-address", mac)
        obj.add_attr("request-type", 2)
    if (all == 1):
        obj.add_attr("request-type", 5)
    if (static_type == 1):
        obj.add_attr("static", static)

    print obj.get()
    commit(obj, "delete")
    return
Esempio n. 12
0
    def __init__(
        self, port_name=None, level=None, switch_id=0, sg_id=None,
            sched_id=None, chld_id_list=[], cps_data=None):
        if cps_data is not None:
            self.cps_data = cps_data
            return

        self.cps_obj_wr = utl.CPSObjWrp(self.yang_name, self.get_type_map())
        self.cps_data = self.cps_obj_wr.get()
        self.set_attr('switch-id', switch_id)
        if port_name is not None:
            port_id = os_utl.if_nametoindex(port_name)
            self.set_attr('port-id', port_id)
        if sg_id is not None:
            self.set_attr('id', sg_id)
        if level is not None:
            self.set_attr('level', level)
        if sched_id is not None:
            self.set_attr('scheduler-profile-id', sched_id)
        if chld_id_list is not None and isinstance(chld_id_list, list) and len(chld_id_list) > 0:
            self.set_attr('child_count', len(chld_id_list))
            self.set_attr('child-list', chld_id_list)
Esempio n. 13
0
    def __init__(self, ifindex=None, ifname=None, cps_data=None, switch_id=0,
                 list_of_attr_value_pairs=[]):

        if cps_data is not None:
            self.cps_data = cps_data
            return

        self.cps_data = None
        self.cps_obj_wr = utl.CPSObjWrp(self.yang_name, self.get_type_map())

        if ifindex is not None:
            self.cps_obj_wr.add_leaf_attr('port-id', ifindex)
        elif ifname is not None:
            ifindex = os_utl.if_nametoindex(ifname)
            if ifindex is None:
                raise RuntimeError("Port " + ifname + " not available")
            self.cps_obj_wr.add_leaf_attr('port-id', ifindex)

        self.cps_obj_wr.add_leaf_attr('switch-id', switch_id)

        for pair in list_of_attr_value_pairs:
            self.set_attr(pair[0], pair[1])
#  Create   ACL  entry
#  Drop   all   packets   received   on  specific   port   from   specific   range   of   MACs
#
#  Create   CPS   Object   and fill leaf   attributes
cps_obj   =  cps_utils.CPSObject(module='base-acl/entry')
cps_obj.add_attr   ('table-id',   tbl_id)
cps_obj.add_attr   ('priority',   512)


#  Filters     #  Match   Filter   1  - Src   MAC
cps_obj.add_embed_attr   (['match','0','type'],   e_ftype['SRC_MAC'])
#  The 2  at   the   end indicates   that   the   type   should   be deducted   from   the   last   2  attrs   (SRC_MAC_VALUE,addr)
cps_obj.add_embed_attr   (['match','0','SRC_MAC_VALUE','addr'], '50:10:6e:00:00:00',   2)
#  Match   Filter   2  - Rx  Port
cps_obj.add_embed_attr   (['match','1','type'],   e_ftype['IN_PORT'])
cps_obj.add_embed_attr   (['match','1','IN_PORT_VALUE'],   nas_os_utils.if_nametoindex('e101-001-0')) 


#  Action   - Drop
cps_obj.add_embed_attr   (['action','0','type'],   e_atype['PACKET_ACTION'])
cps_obj.add_embed_attr   (['action','0','PACKET_ACTION_VALUE'],   e_ptype['DROP'])


#  Associate   the   CPS   Object   with   a CPS   operation cps_update   =  ('create',   cps_obj.get())
#  Add   the   CPS   object   to   a new   CPS   Transaction cps_trans   =  cps_utils.CPSTransaction([cps_update])

#  Commit   the   CPS   transaction r =  cps_trans.commit()
if not   r:
    raise   RuntimeError   ("Error   creating   MAC   ACL  Entry")

ret   =  cps_utils.CPSObject   (module='base-acl/entry',   obj=r[0]['change'])
Esempio n. 15
0
def nas_mirror_add_source(obj,intf,direction):
    l = ["intf","0","src"]
    obj.add_embed_attr(l,nas_os_utils.if_nametoindex(intf))
    l[2]="direction"
    obj.add_embed_attr(l,direction_type[direction])
Esempio n. 16
0
    print "cps_config_mirror.py set_dest [mirror_id] [dst_intf] - change destination interface"
    print "cps_config_mirror.py set_vlan [mirror_id] [vlan_id] - change vlan for rspan mirror session"
    print "cps_config_mirror.py get [mirror_id] - Get Mirror session info"

    exit()


if __name__ == '__main__':

    if len(sys.argv) == 1:
        usage()
    elif sys.argv[1] == "create" and len(sys.argv) >= 6:
        obj = nas_mirror_op(
            "create", {
                "type": mirror_type[sys.argv[2]],
                "dst-intf": nas_os_utils.if_nametoindex(sys.argv[3])
            }, False)
        nas_mirror_add_source(obj, sys.argv[4], sys.argv[5])
        if sys.argv[2] == "rspan" and len(sys.argv) == 7:
            obj.add_attr("vlan", sys.argv[6])
        elif sys.argv[2] != "rspan" and len(sys.argv) == 6:
            usage()
        print obj.get()
        nas_ut.get_cb_method("create")(obj)

    elif sys.argv[1] == "delete" and len(sys.argv) == 3:
        nas_mirror_op("delete", {"id": sys.argv[2]})

    elif sys.argv[1] == "get" and len(sys.argv) == 3:
        nas_mirror_op("get", {"id": sys.argv[2]})
Esempio n. 17
0
#  Create   ACL  entry
#  Drop   all   packets   received   on  specific   port   from   specific   range   of   MACs
#
#  Create   CPS   Object   and fill leaf   attributes
cps_obj   =  cps_utils.CPSObject(module='base-acl/entry')
cps_obj.add_attr   ('table-id',   tbl_id)
cps_obj.add_attr   ('priority',   512)


#  Filters     #  Match   Filter   1  - Src   MAC
cps_obj.add_embed_attr   (['match','0','type'],   e_ftype['SRC_MAC'])
#  The 2  at   the   end indicates   that   the   type   should   be deducted   from   the   last   2  attrs   (SRC_MAC_VALUE,addr)
cps_obj.add_embed_attr   (['match','0','SRC_MAC_VALUE','addr'], '50:10:6e:00:00:00',   2)
#  Match   Filter   2  - Rx  Port
cps_obj.add_embed_attr   (['match','1','type'],   e_ftype['IN_PORT'])
cps_obj.add_embed_attr   (['match','1','IN_PORT_VALUE'],   nas_os_utils.if_nametoindex('e101-001-0')) 


#  Action   - Drop
cps_obj.add_embed_attr   (['action','0','type'],   e_atype['PACKET_ACTION'])
cps_obj.add_embed_attr   (['action','0','PACKET_ACTION_VALUE'],   e_ptype['DROP'])


#  Associate   the   CPS   Object   with   a CPS   operation cps_update   =  ('create',   cps_obj.get())
#  Add   the   CPS   object   to   a new   CPS   Transaction cps_trans   =  cps_utils.CPSTransaction([cps_update])

#  Commit   the   CPS   transaction r =  cps_trans.commit()
if not   r:
    raise   RuntimeError   ("Error   creating   MAC   ACL  Entry")

ret   =  cps_utils.CPSObject   (module='base-acl/entry',   obj=r[0]['change'])
Esempio n. 18
0
def nas_mirror_add_source(obj, intf, direction):
    l = ["intf", "0", "src"]
    obj.add_embed_attr(l, nas_os_utils.if_nametoindex(intf))
    l[2] = "direction"
    obj.add_embed_attr(l, direction_type[direction])
Esempio n. 19
0
    print "nas_sflow_cli.py set_direction [id] [direction] - change the packet sampling direction \
          \n                                              of the given session id"

    print "nas_sflow_cli.py show [id] - dump packet sampling information of given session id or all session"
    print "\n[direction] - ingress, egress, ingress_egress\n"
    exit()


if __name__ == '__main__':

    if len(sys.argv) == 1:
        usage()
    elif sys.argv[1] == "enable" and len(sys.argv) == 5:
        nas_sflow_op(
            "create", {
                "ifindex": nas_os_utils.if_nametoindex(sys.argv[2]),
                "direction": dir_map[sys.argv[3]],
                "sampling-rate": sys.argv[4]
            })
    elif sys.argv[1] == "disable" and len(sys.argv) == 3:
        nas_sflow_op("delete", {"id": sys.argv[2]})
    elif sys.argv[1] == "show" and len(sys.argv) == 3:
        nas_sflow_op("get", {"id": sys.argv[2]})
    elif sys.argv[1] == "set_rate" and len(sys.argv) == 4:
        nas_sflow_op("set", {"id": sys.argv[2], "sampling-rate": sys.argv[3]})
    elif sys.argv[1] == "set_direction" and len(sys.argv) == 4:
        nas_sflow_op("set", {
            "id": sys.argv[2],
            "direction": dir_map[sys.argv[3]]
        })
    else:
Esempio n. 20
0
    print "cps_config_mirror.py delete [mirror_id] - delete mirror session"
    print "cps_config_mirror.py set_source [mirror_id] [source_intf] [dir] - change source interface and its direction"
    print "cps_config_mirror.py set_dest [mirror_id] [dst_intf] - change destination interface"
    print "cps_config_mirror.py set_vlan [mirror_id] [vlan_id] - change vlan for rspan mirror session"
    print "cps_config_mirror.py get [mirror_id] - Get Mirror session info"

    exit()

if __name__ == '__main__':

    if len(sys.argv) == 1:
        usage()
    elif sys.argv[1] == "create" and len(sys.argv) >=6:
        obj = nas_mirror_op("create",
                            {"type":mirror_type[sys.argv[2]],
                             "dst-intf":nas_os_utils.if_nametoindex(sys.argv[3])},False)
        nas_mirror_add_source(obj,sys.argv[4],sys.argv[5])
        if sys.argv[2]=="rspan" and len(sys.argv) == 7:
            obj.add_attr("vlan",sys.argv[6])
        elif sys.argv[2] !="rspan" and len(sys.argv) == 6:
            usage()
        print obj.get()
        nas_ut.get_cb_method("create")(obj)

    elif sys.argv[1] == "delete" and len(sys.argv) == 3:
        nas_mirror_op("delete", {"id": sys.argv[2]})

    elif sys.argv[1] == "get" and len(sys.argv) == 3:
        nas_mirror_op("get", {"id": sys.argv[2]})

    elif sys.argv[1] == "set_source" and len(sys.argv) == 5: