nodes: 0, module; 1, function_definition; 2, function_name:configure_switch_entries; 3, parameters; 4, block; 5, identifier:self; 6, identifier:switch_ip; 7, identifier:port_bindings; 8, expression_statement; 9, expression_statement; 10, expression_statement; 11, expression_statement; 12, expression_statement; 13, expression_statement; 14, expression_statement; 15, expression_statement; 16, comment:# Let's make these lists a set to exclude duplicates; 17, expression_statement; 18, expression_statement; 19, expression_statement; 20, expression_statement; 21, expression_statement; 22, for_statement; 23, if_statement; 24, expression_statement; 25, expression_statement; 26, expression_statement; 27, if_statement; 28, expression_statement; 29, expression_statement; 30, expression_statement; 31, comment:"""Create a nexus switch entry in Nexus.
  
          The port_bindings is sorted by vlan_id, vni, port_id.
          When there is a change in vlan_id or vni, then vlan
          data is configured in Nexus device.
          Otherwise we check if there is a change in port_id
          where we configure the port with vlan trunk config.
  
          Called during switch replay event.
          """; 32, assignment; 33, assignment; 34, assignment; 35, assignment; 36, assignment; 37, call; 38, call; 39, assignment; 40, assignment; 41, assignment; 42, assignment; 43, assignment; 44, identifier:port; 45, identifier:port_bindings; 46, block; 47, identifier:pvlans; 48, block; 49, call; 50, call; 51, assignment; 52, identifier:vlans; 53, block; 54, call; 55, call; 56, call; 57, identifier:prev_vlan; 58, unary_operator; 59, identifier:prev_vni; 60, unary_operator; 61, identifier:prev_port; 62, None; 63, identifier:prev_native_vlan; 64, integer:0; 65, identifier:starttime; 66, call; 67, attribute; 68, argument_list; 69, attribute; 70, argument_list; 71, identifier:vlans; 72, call; 73, identifier:pvlans; 74, call; 75, identifier:interface_count; 76, integer:0; 77, identifier:duplicate_port; 78, integer:0; 79, identifier:vlan_count; 80, integer:0; 81, if_statement; 82, expression_statement; 83, if_statement; 84, expression_statement; 85, expression_statement; 86, attribute; 87, argument_list; 88, attribute; 89, argument_list; 90, identifier:vlans; 91, call; 92, expression_statement; 93, expression_statement; 94, if_statement; 95, attribute; 96, argument_list; 97, attribute; 98, argument_list; 99, attribute; 100, argument_list; 101, integer:1; 102, integer:1; 103, attribute; 104, argument_list; 105, identifier:port_bindings; 106, identifier:sort; 107, keyword_argument; 108, attribute; 109, identifier:capture_and_print_timeshot; 110, identifier:starttime; 111, string:"replay_t2_aft_sort"; 112, keyword_argument; 113, identifier:set; 114, argument_list; 115, identifier:set; 116, argument_list; 117, call; 118, block; 119, assignment; 120, comparison_operator:port.port_id == prev_port; 121, block; 122, else_clause; 123, call; 124, call; 125, identifier:LOG; 126, identifier:debug; 127, string:"Replayed total %d ports for Switch %s"; 128, binary_operator:interface_count + 1; 129, identifier:switch_ip; 130, attribute; 131, identifier:capture_and_print_timeshot; 132, identifier:starttime; 133, string:"replay_part_1"; 134, keyword_argument; 135, identifier:list; 136, argument_list; 137, call; 138, assignment; 139, comparison_operator:vni == 0; 140, block; 141, else_clause; 142, identifier:self; 143, identifier:set_switch_ip_and_active_state; 144, identifier:switch_ip; 145, attribute; 146, identifier:self; 147, identifier:configure_next_batch_of_vlans; 148, identifier:switch_ip; 149, attribute; 150, identifier:capture_and_print_timeshot; 151, identifier:starttime; 152, string:"replay_part_2"; 153, keyword_argument; 154, identifier:time; 155, identifier:time; 156, identifier:key; 157, lambda; 158, identifier:self; 159, identifier:driver; 160, identifier:switch; 161, identifier:switch_ip; 162, attribute; 163, argument_list; 164, continue_statement; 165, pattern_list; 166, call; 167, attribute; 168, identifier:prev_port; 169, if_statement; 170, comment:# Different port - write out interface trunk on previous port; 171, block; 172, attribute; 173, argument_list; 174, attribute; 175, argument_list; 176, identifier:interface_count; 177, integer:1; 178, identifier:self; 179, identifier:driver; 180, identifier:switch; 181, identifier:switch_ip; 182, identifier:vlans; 183, attribute; 184, argument_list; 185, pattern_list; 186, subscript; 187, identifier:vni; 188, integer:0; 189, expression_statement; 190, block; 191, identifier:const; 192, identifier:SWITCH_RESTORE_S2; 193, identifier:self; 194, identifier:driver; 195, identifier:switch; 196, identifier:switch_ip; 197, lambda_parameters; 198, tuple; 199, identifier:nxos_db; 200, identifier:is_reserved_binding; 201, identifier:port; 202, identifier:auto_create; 203, identifier:auto_trunk; 204, attribute; 205, argument_list; 206, identifier:port; 207, identifier:port_id; 208, boolean_operator; 209, comment:# Same port/Same Vlan - skip duplicate; 210, block; 211, else_clause; 212, if_statement; 213, expression_statement; 214, expression_statement; 215, if_statement; 216, comment:# Start tracking new port; 217, if_statement; 218, if_statement; 219, expression_statement; 220, if_statement; 221, identifier:LOG; 222, identifier:debug; 223, concatenated_string; 224, identifier:switch_ip; 225, attribute; 226, identifier:vlan_count; 227, identifier:duplicate_port; 228, identifier:self; 229, identifier:_restore_port_binding; 230, identifier:switch_ip; 231, identifier:pvlans; 232, identifier:prev_port; 233, identifier:prev_native_vlan; 234, identifier:vlans; 235, identifier:sort; 236, identifier:vlan; 237, identifier:vni; 238, identifier:vlans; 239, integer:0; 240, call; 241, expression_statement; 242, identifier:x; 243, attribute; 244, attribute; 245, attribute; 246, identifier:self; 247, identifier:_gather_config_parms; 248, call; 249, attribute; 250, comparison_operator:port.vlan_id == prev_vlan; 251, comparison_operator:port.vni == prev_vni; 252, expression_statement; 253, continue_statement; 254, comment:# Same port/different Vlan - track it; 255, block; 256, identifier:prev_port; 257, block; 258, assignment; 259, assignment; 260, identifier:pvlans; 261, block; 262, identifier:auto_create; 263, block; 264, identifier:auto_trunk; 265, block; 266, assignment; 267, attribute; 268, block; 269, string:"Switch %s port %s replay summary: unique vlan "; 270, string:"count %d, duplicate port entries %d"; 271, identifier:port; 272, identifier:port_id; 273, attribute; 274, argument_list; 275, call; 276, identifier:x; 277, identifier:port_id; 278, identifier:x; 279, identifier:vlan_id; 280, identifier:x; 281, identifier:vni; 282, attribute; 283, argument_list; 284, identifier:port; 285, identifier:vlan_id; 286, attribute; 287, identifier:prev_vlan; 288, attribute; 289, identifier:prev_vni; 290, augmented_assignment; 291, expression_statement; 292, if_statement; 293, if_statement; 294, if_statement; 295, expression_statement; 296, expression_statement; 297, identifier:duplicate_port; 298, integer:0; 299, identifier:vlan_count; 300, integer:0; 301, expression_statement; 302, expression_statement; 303, expression_statement; 304, expression_statement; 305, expression_statement; 306, identifier:prev_port; 307, attribute; 308, identifier:port; 309, identifier:is_native; 310, expression_statement; 311, identifier:self; 312, identifier:_save_switch_vlan_range; 313, identifier:switch_ip; 314, identifier:vlans; 315, attribute; 316, argument_list; 317, identifier:nxos_db; 318, identifier:is_provider_vlan; 319, attribute; 320, identifier:port; 321, identifier:vlan_id; 322, identifier:port; 323, identifier:vni; 324, identifier:duplicate_port; 325, integer:1; 326, augmented_assignment; 327, identifier:auto_create; 328, block; 329, identifier:auto_trunk; 330, block; 331, attribute; 332, block; 333, augmented_assignment; 334, call; 335, call; 336, call; 337, assignment; 338, call; 339, call; 340, identifier:port; 341, identifier:port_id; 342, assignment; 343, identifier:self; 344, identifier:_save_switch_vxlan_range; 345, identifier:switch_ip; 346, identifier:vlans; 347, identifier:port; 348, identifier:vlan_id; 349, identifier:vlan_count; 350, integer:1; 351, expression_statement; 352, expression_statement; 353, identifier:port; 354, identifier:is_native; 355, expression_statement; 356, identifier:interface_count; 357, integer:1; 358, attribute; 359, argument_list; 360, attribute; 361, argument_list; 362, attribute; 363, argument_list; 364, identifier:prev_native_vlan; 365, integer:0; 366, attribute; 367, argument_list; 368, attribute; 369, argument_list; 370, identifier:prev_native_vlan; 371, attribute; 372, call; 373, call; 374, assignment; 375, identifier:LOG; 376, identifier:debug; 377, concatenated_string; 378, identifier:switch_ip; 379, identifier:prev_port; 380, identifier:vlan_count; 381, identifier:duplicate_port; 382, identifier:self; 383, identifier:_restore_port_binding; 384, identifier:switch_ip; 385, identifier:pvlans; 386, identifier:prev_port; 387, identifier:prev_native_vlan; 388, identifier:pvlans; 389, identifier:clear; 390, identifier:vlans; 391, identifier:add; 392, tuple; 393, identifier:pvlans; 394, identifier:add; 395, attribute; 396, identifier:port; 397, identifier:vlan_id; 398, attribute; 399, argument_list; 400, attribute; 401, argument_list; 402, identifier:prev_native_vlan; 403, attribute; 404, string:"Switch %s port %s replay summary: unique vlan "; 405, string:"count %d, duplicate port entries %d"; 406, attribute; 407, attribute; 408, identifier:port; 409, identifier:vlan_id; 410, identifier:vlans; 411, identifier:add; 412, tuple; 413, identifier:pvlans; 414, identifier:add; 415, attribute; 416, identifier:port; 417, identifier:vlan_id; 418, identifier:port; 419, identifier:vlan_id; 420, identifier:port; 421, identifier:vni; 422, attribute; 423, attribute; 424, identifier:port; 425, identifier:vlan_id; 426, identifier:port; 427, identifier:vlan_id; 428, identifier:port; 429, identifier:vni
code: def configure_switch_entries(self, switch_ip, port_bindings):
          """Create a nexus switch entry in Nexus.
  
          The port_bindings is sorted by vlan_id, vni, port_id.
          When there is a change in vlan_id or vni, then vlan
          data is configured in Nexus device.
          Otherwise we check if there is a change in port_id
          where we configure the port with vlan trunk config.
  
          Called during switch replay event.
          """
          prev_vlan = -1
          prev_vni = -1
          prev_port = None
          prev_native_vlan = 0
          starttime = time.time()
  
          port_bindings.sort(key=lambda x: (x.port_id, x.vlan_id, x.vni))
          self.driver.capture_and_print_timeshot(
              starttime, "replay_t2_aft_sort",
              switch=switch_ip)
  
          # Let's make these lists a set to exclude duplicates
          vlans = set()
          pvlans = set()
          interface_count = 0
          duplicate_port = 0
          vlan_count = 0
          for port in port_bindings:
              if nxos_db.is_reserved_binding(port):
                  continue
  
              auto_create, auto_trunk = self._gather_config_parms(
                  nxos_db.is_provider_vlan(port.vlan_id), port.vlan_id)
              if port.port_id == prev_port:
                  if port.vlan_id == prev_vlan and port.vni == prev_vni:
                      # Same port/Same Vlan - skip duplicate
                      duplicate_port += 1
                      continue
                  else:
                      # Same port/different Vlan - track it
                      vlan_count += 1
                      if auto_create:
                          vlans.add((port.vlan_id, port.vni))
                      if auto_trunk:
                          pvlans.add(port.vlan_id)
                      if port.is_native:
                          prev_native_vlan = port.vlan_id
              else:
                  # Different port - write out interface trunk on previous port
                  if prev_port:
                      interface_count += 1
                      LOG.debug("Switch %s port %s replay summary: unique vlan "
                                "count %d, duplicate port entries %d",
                                switch_ip, prev_port, vlan_count, duplicate_port)
                  duplicate_port = 0
                  vlan_count = 0
                  if pvlans:
                      self._restore_port_binding(
                          switch_ip, pvlans, prev_port, prev_native_vlan)
                      pvlans.clear()
                      prev_native_vlan = 0
                  # Start tracking new port
                  if auto_create:
                      vlans.add((port.vlan_id, port.vni))
                  if auto_trunk:
                      pvlans.add(port.vlan_id)
                  prev_port = port.port_id
                  if port.is_native:
                      prev_native_vlan = port.vlan_id
  
          if pvlans:
              LOG.debug("Switch %s port %s replay summary: unique vlan "
                        "count %d, duplicate port entries %d",
                        switch_ip, port.port_id, vlan_count, duplicate_port)
              self._restore_port_binding(
                  switch_ip, pvlans, prev_port, prev_native_vlan)
  
          LOG.debug("Replayed total %d ports for Switch %s",
                    interface_count + 1, switch_ip)
  
          self.driver.capture_and_print_timeshot(
              starttime, "replay_part_1",
              switch=switch_ip)
          vlans = list(vlans)
          if vlans:
              vlans.sort()
              vlan, vni = vlans[0]
              if vni == 0:
                  self._save_switch_vlan_range(switch_ip, vlans)
              else:
                  self._save_switch_vxlan_range(switch_ip, vlans)
  
          self.set_switch_ip_and_active_state(
              switch_ip, const.SWITCH_RESTORE_S2)
          self.configure_next_batch_of_vlans(switch_ip)
          self.driver.capture_and_print_timeshot(
              starttime, "replay_part_2",
              switch=switch_ip)