def test_get_hp_switch_port_by_switchid_portname(self):
     """Test get_hp_switch_port_by_switchid_portname method."""
     self._add_switch_and_lag_port()
     result = db.get_all_hp_sw_port_by_swchid_portname(
         self.ctx,
         {'switch_id': "test_switch1", 'port_name': "Tengig0/1"})
     self.assertEqual('test_switch1', result[0].switch_id)
    def update_port(self, port_dict):
        """update_port. This call makes the REST request to the external

        SDN controller for provision VLAN on switch port where bare metal
        is connected.
        """

        LOG.debug("update_port with port dict %(port_dict)s",
                  {'port_dict': port_dict})
        port_id = port_dict['port']['id']
        host_id = port_dict['port']['host_id']
        rec_dict = {'neutron_port_id': port_id}
        ironic_port_list = db.get_hp_ironic_swport_map_by_id(self.context,
                                                             rec_dict)
        switchports = port_dict['port']['switchports']
        if not ironic_port_list:
            # We are creating the switch ports because initial ironic
            # port-create will not supply local link information for tenant .
            # networks . Later ironic port-update , the local link information
            # value will be supplied.
            self.create_switch_port(port_dict)
            return
        if not len(switchports) == len(ironic_port_list):
            err_msg = "given switchports dict does not match"
            self._raise_hp_net_provisioning_error(wexc.HTTPConflict,
                                                  'update_port',
                                                  err_msg)
        for ironic_port in ironic_port_list:
            hp_switch_port_id = ironic_port.switch_port_id
            hp_sw_port_dict = {'id': hp_switch_port_id}
            switch_port_map = db.get_hp_switch_port_by_id(self.context,
                                                          hp_sw_port_dict)
            switch_id_db = switch_port_map.switch_id
            port_name_db = switch_port_map.port_name
            if not any(d['port_id'] == port_name_db for d in switchports):
                err_msg = "given port does not exists"
                self._raise_hp_net_provisioning_error(wexc.HTTPNotFound,
                                                      'update_port',
                                                      err_msg)
            if not any(d['switch_id'] == switch_id_db for d in switchports):
                err_msg = "given switch does not exists"
                self._raise_hp_net_provisioning_error(wexc.HTTPNotFound,
                                                      'update_port',
                                                      err_msg)
            rec_dict = {'switch_id': switch_id_db,
                        'port_name': port_name_db}
            switch_ports = db.get_all_hp_sw_port_by_swchid_portname(
                self.context, rec_dict)
            if len(switch_ports) > 1 and host_id:
                for switch_port in switch_ports:
                    self._is_port_already_bound(switch_port, port_id)
        update_dict = {'neutron_port_id': port_id,
                       'host_id': host_id}
        db.update_hp_ironic_swport_map_with_host_id(self.context,
                                                    update_dict)
 def create_switch_port(self, port_dict):
     switchports = port_dict['port']['switchports']
     neutron_port_id = port_dict['port']['id']
     host_id = port_dict['port']['host_id']
     for switchport in switchports:
         switch_port_id = uuidutils.generate_uuid()
         switch_mac_id = switchport['switch_id']
         port_id = switchport['port_id']
         rec_dict = {'id': switch_port_id,
                     'switch_id': switch_mac_id,
                     'port_name': port_id,
                     'lag_id': None}
         sw_ports = db.get_all_hp_sw_port_by_swchid_portname(self.context,
                                                             rec_dict)
         if sw_ports and host_id:
             for sw_port in sw_ports:
                 self._is_port_already_bound(sw_port, neutron_port_id)
         switch_url = self._frame_switch_url(switch_mac_id)
         try:
             resp = self._do_request('GET', switch_url, None)
             LOG.info("response from SDN controller %(resp)s ",
                      {'resp': resp})
             if not resp:
                 self._raise_ml2_error(wexc.HTTPNotFound, 'create_port')
             port_list = resp.json()['ports']
             if port_id not in port_list:
                 self._roll_back_created_ports(neutron_port_id)
                 LOG.error("Given port is not found")
                 self._raise_ml2_error(wexc.HTTPNotFound, 'create_port')
             resp.raise_for_status()
             mapping_dict = {'neutron_port_id': neutron_port_id,
                             'switch_port_id': switch_port_id,
                             'lag_id': None,
                             'access_type': None,
                             'segmentation_id': None,
                             'host_id': None}
             session = self.context.session
             if resp.status_code == requests.codes.OK:
                 with session.begin(subtransactions=True):
                     db.add_hp_switch_port(self.context, rec_dict)
                     db.add_hp_ironic_switch_port_mapping(self.context,
                                                          mapping_dict)
             else:
                 LOG.error(" Given physical switch does not exists")
                 self._raise_ml2_error(wexc.HTTPNotFound, 'create_port')
         except requests.exceptions.Timeout as e:
             LOG.error(" Request timed out in SDN controller : %s", e)
             self._roll_back_created_ports(neutron_port_id)
             self._raise_ml2_error(wexc.HTTPRequestTimeout, 'create_port')
         except requests.exceptions.SSLError as e:
             LOG.error(" SSLError to SDN controller : %s", e)
             self._roll_back_created_ports(neutron_port_id)
             self._raise_ml2_error(wexc.HTTPBadRequest, 'create_port')
         except requests.exceptions.HTTPError as e:
             LOG.error(" HTTPError : %s", e)
             self._roll_back_created_ports(neutron_port_id)
             self._raise_ml2_error(wexc.HTTPNotFound, 'create_port')
         except requests.exceptions.URLRequired as e:
             LOG.error(" Invalid URL : %s", e)
             self._roll_back_created_ports(neutron_port_id)
             self._raise_ml2_error(wexc.HTTPNotFound, 'create_port')
         except Exception as e:
             LOG.error(" Bad request : %s", e)
             self._roll_back_created_ports(neutron_port_id)
             self._raise_ml2_error(wexc.HTTPBadRequest, 'create_port')