def _create_port(self, port):
     switchports = port['port']['switchports']
     LOG.debug(_LE("_create_port switch: %s"), port)
     network_id = port['port']['network_id']
     db_context = neutron_context.get_admin_context()
     subnets = db.get_subnets_by_network(db_context, network_id)
     if not subnets:
         LOG.error("Subnet not found for the network")
         self._raise_ml2_error(wexc.HTTPNotFound, 'create_port')
     for switchport in switchports:
         switch_mac_id = switchport['switch_id']
         port_id = switchport['port_id']
         bnp_switch = db.get_bnp_phys_switch_by_mac(db_context,
                                                    switch_mac_id)
         # check for port and switch level existence
         if not bnp_switch:
             LOG.error(_LE("No physical switch found '%s' "), switch_mac_id)
             self._raise_ml2_error(wexc.HTTPNotFound, 'create_port')
         phys_port = db.get_bnp_phys_port(db_context,
                                          bnp_switch.id,
                                          port_id)
         if not phys_port:
             LOG.error(_LE("No physical port found for '%s' "), phys_port)
             self._raise_ml2_error(wexc.HTTPNotFound, 'create_port')
         if bnp_switch.port_prov != hp_const.SWITCH_STATUS['enable']:
             LOG.error(_LE("Physical switch is not Enabled '%s' "),
                       bnp_switch.port_prov)
             self._raise_ml2_error(wexc.HTTPBadRequest, 'create_port')
 def test_delete_bnp_phys_switch(self):
     """Test delete_bnp_phys_switch method."""
     sw_dict = self._get_bnp_phys_switch_dict()
     db.add_bnp_phys_switch(self.ctx, sw_dict)
     switch = db.get_bnp_phys_switch_by_mac(self.ctx,
                                            sw_dict['mac_address'])
     db.delete_bnp_phys_switch(self.ctx, switch['id'])
     count = self.ctx.session.query(models.BNPPhysicalSwitch).count()
     self.assertEqual(0, count)
 def test_get_bnp_phys_switch(self):
     """Test get_bnp_phys_switch method."""
     sw_dict = self._get_bnp_phys_switch_dict()
     db.add_bnp_phys_switch(self.ctx, sw_dict)
     sw_mac = db.get_bnp_phys_switch_by_mac(self.ctx,
                                            sw_dict['mac_address'])
     sw_ip = db.get_bnp_phys_switch_by_ip(self.ctx, sw_dict['ip_address'])
     sw = db.get_bnp_phys_switch(self.ctx, sw_mac['id'])
     self.assertEqual(sw['id'], sw_mac['id'])
     self.assertEqual(sw['id'], sw_ip['id'])
 def bind_port_to_segment(self, port):
     """bind_port_to_segment ."""
     db_context = neutron_context.get_admin_context()
     LOG.info(_LI('bind_port_to_segment called from back-end mech driver'))
     switchports = port['port']['switchports']
     for switchport in switchports:
         switch_id = switchport['switch_id']
         bnp_switch = db.get_bnp_phys_switch_by_mac(db_context,
                                                    switch_id)
         port_name = switchport['port_id']
         if not bnp_switch:
             self._raise_ml2_error(wexc.HTTPNotFound, 'create_port')
         credentials_dict = port.get('port')
         cred_dict = self._get_credentials_dict(bnp_switch, 'create_port')
         credentials_dict['credentials'] = cred_dict
         try:
             prov_protocol = bnp_switch.management_protocol
             vendor = bnp_switch.vendor
             family = bnp_switch.family
             prov_driver = self._provisioning_driver(prov_protocol, vendor,
                                                     family)
             if not prov_driver:
                 LOG.error(_LE("No suitable provisioning driver found"
                               ))
                 self._raise_ml2_error(wexc.HTTPNotFound, 'create_port')
             port_list = prov_driver.obj.get_device_info(port)
             ifindex = self._get_if_index(port_list, port_name)
             switchport['ifindex'] = ifindex
             if not port_list:
                 LOG.error(_LE("No physical port found for '%s' "),
                           switch_id)
                 self._raise_ml2_error(wexc.HTTPNotFound, 'create_port')
             prov_driver.obj.set_isolation(port)
             port_id = port['port']['id']
             segmentation_id = port['port']['segmentation_id']
             mapping_dict = {'neutron_port_id': port_id,
                             'switch_port_name': port_name,
                             'switch_id': bnp_switch.id,
                             'lag_id': None,
                             'access_type': hp_const.ACCESS,
                             'segmentation_id': int(segmentation_id),
                             'bind_status': 0,
                             'ifindex': ifindex
                             }
             db.add_bnp_switch_port_map(db_context, mapping_dict)
             db.add_bnp_neutron_port(db_context, mapping_dict)
             if bnp_switch.validation_result != hp_const.SUCCESS:
                 db.update_bnp_phys_switch_result_status(db_context,
                                                         bnp_switch.id,
                                                         hp_const.SUCCESS)
             return hp_const.BIND_SUCCESS
         except Exception as e:
             LOG.error(_LE("Exception in configuring VLAN '%s' "), e)
             return hp_const.BIND_FAILURE
 def bind_port_to_segment(self, port):
     """bind_port_to_segment ."""
     db_context = neutron_context.get_admin_context()
     LOG.info(_LI('bind_port_to_segment called from back-end mech driver'))
     switchports = port['port']['switchports']
     for switchport in switchports:
         switch_id = switchport['switch_id']
         bnp_switch = db.get_bnp_phys_switch_by_mac(db_context,
                                                    switch_id)
         port_name = switchport['port_id']
         if not bnp_switch:
             self._raise_ml2_error(wexc.HTTPNotFound, 'create_port')
         credentials_dict = port.get('port')
         cred_dict = self._get_credentials_dict(bnp_switch, 'create_port')
         credentials_dict['credentials'] = cred_dict
         try:
             prov_protocol = bnp_switch.management_protocol
             vendor = bnp_switch.vendor
             family = bnp_switch.family
             prov_driver = self._provisioning_driver(prov_protocol, vendor,
                                                     family)
             if not prov_driver:
                 LOG.error(_LE("No suitable provisioning driver found"
                               ))
                 self._raise_ml2_error(wexc.HTTPNotFound, 'create_port')
             port_list = prov_driver.obj.get_device_info(port)
             ifindex = self._get_if_index(port_list, port_name)
             switchport['ifindex'] = ifindex
             if not port_list:
                 LOG.error(_LE("No physical port found for '%s' "),
                           switch_id)
                 self._raise_ml2_error(wexc.HTTPNotFound, 'create_port')
             prov_driver.obj.set_isolation(port)
             port_id = port['port']['id']
             segmentation_id = port['port']['segmentation_id']
             mapping_dict = {'neutron_port_id': port_id,
                             'switch_port_name': port_name,
                             'switch_id': bnp_switch.id,
                             'lag_id': None,
                             'access_type': hp_const.ACCESS,
                             'segmentation_id': int(segmentation_id),
                             'bind_status': 0,
                             'ifindex': ifindex
                             }
             db.add_bnp_switch_port_map(db_context, mapping_dict)
             db.add_bnp_neutron_port(db_context, mapping_dict)
             if bnp_switch.validation_result != hp_const.SUCCESS:
                 db.update_bnp_phys_switch_result_status(db_context,
                                                         bnp_switch.id,
                                                         hp_const.SUCCESS)
             return hp_const.BIND_SUCCESS
         except Exception as e:
             LOG.error(_LE("Exception in configuring VLAN '%s' "), e)
             return hp_const.BIND_FAILURE
 def _create_port(self, port):
     switchports = port['port']['switchports']
     LOG.debug(_LE("_create_port switch: %s"), port)
     network_id = port['port']['network_id']
     db_context = neutron_context.get_admin_context()
     subnets = db.get_subnets_by_network(db_context, network_id)
     if not subnets:
         LOG.error(_LE("Subnet not found for the network"))
         self._raise_ml2_error(wexc.HTTPNotFound, 'create_port')
     for switchport in switchports:
         switch_mac_id = switchport['switch_id']
         bnp_switch = db.get_bnp_phys_switch_by_mac(db_context,
                                                    switch_mac_id)
         # check for port and switch level existence
         if not bnp_switch:
             LOG.error(_LE("No physical switch found '%s' "), switch_mac_id)
             self._raise_ml2_error(wexc.HTTPNotFound, 'create_port')
         self.sw_obj = bnp_sw.BNPSwitchController()
         mgmt_proto = bnp_switch['management_protocol']
         creds = bnp_switch['credentials']
         access_parameters = self.sw_obj._get_access_param(db_context,
                                                           mgmt_proto,
                                                           creds)
         for key, value in access_parameters.iteritems():
             if key == hp_const.NAME:
                 continue
             bnp_switch[key] = value
         driver_key = self.sw_obj._protocol_driver(bnp_switch)
         try:
             if driver_key:
                 dr_obj = driver_key.obj
                 mac_val = dr_obj.get_protocol_validation_result(bnp_switch)
                 if mac_val != bnp_switch['mac_address']:
                     self._raise_ml2_error(wexc.HTTPBadRequest,
                                           'Invalid mac address')
         except Exception as e:
             LOG.error(e)
             self._raise_ml2_error(wexc.HTTPBadRequest, 'create_port')
         port_provisioning_db = bnp_switch.port_provisioning
         if (port_provisioning_db !=
                 hp_const.PORT_PROVISIONING_STATUS['enable']):
             LOG.error(_LE("Physical switch is not Enabled '%s' "),
                       bnp_switch.port_provisioning)
             self._raise_ml2_error(wexc.HTTPBadRequest, 'create_port')
 def _create_port(self, port):
     switchports = port['port']['switchports']
     LOG.debug(_LE("_create_port switch: %s"), port)
     network_id = port['port']['network_id']
     db_context = neutron_context.get_admin_context()
     subnets = db.get_subnets_by_network(db_context, network_id)
     if not subnets:
         LOG.error(_LE("Subnet not found for the network"))
         self._raise_ml2_error(wexc.HTTPNotFound, 'create_port')
     for switchport in switchports:
         switch_mac_id = switchport['switch_id']
         bnp_switch = db.get_bnp_phys_switch_by_mac(db_context,
                                                    switch_mac_id)
         # check for port and switch level existence
         if not bnp_switch:
             LOG.error(_LE("No physical switch found '%s' "), switch_mac_id)
             self._raise_ml2_error(wexc.HTTPNotFound, 'create_port')
         self.sw_obj = bnp_sw.BNPSwitchController()
         mgmt_proto = bnp_switch['management_protocol']
         creds = bnp_switch['credentials']
         access_parameters = self.sw_obj._get_access_param(db_context,
                                                           mgmt_proto,
                                                           creds)
         for key, value in access_parameters.iteritems():
             if key == hp_const.NAME:
                 continue
             bnp_switch[key] = value
         driver_key = self.sw_obj._protocol_driver(bnp_switch)
         try:
             if driver_key:
                 dr_obj = driver_key.obj
                 mac_val = dr_obj.get_protocol_validation_result(bnp_switch)
                 if mac_val != bnp_switch['mac_address']:
                     self._raise_ml2_error(wexc.HTTPBadRequest,
                                           'Invalid mac address')
         except Exception as e:
             LOG.error(e)
             self._raise_ml2_error(wexc.HTTPBadRequest, 'create_port')
         port_provisioning_db = bnp_switch.port_provisioning
         if port_provisioning_db != hp_const.SWITCH_STATUS['enable']:
             LOG.error(_LE("Physical switch is not Enabled '%s' "),
                       bnp_switch.port_provisioning)
             self._raise_ml2_error(wexc.HTTPBadRequest, 'create_port')
 def bind_port_to_segment(self, port):
     """bind_port_to_segment ."""
     LOG.info(_LI('bind_port_to_segment called from back-end mech driver'))
     switchports = port['port']['switchports']
     for switchport in switchports:
         switch_id = switchport['switch_id']
         bnp_switch = db.get_bnp_phys_switch_by_mac(self.context,
                                                    switch_id)
         port_name = switchport['port_id']
         if not bnp_switch:
             self._raise_ml2_error(wexc.HTTPNotFound, 'create_port')
         phys_port = db.get_bnp_phys_port(self.context,
                                          bnp_switch.id,
                                          port_name)
         if not phys_port:
             self._raise_ml2_error(wexc.HTTPNotFound, 'create_port')
         # port_maps = db.get_all_bnp_swport_mappings(self.context)
         # for port_map in port_maps:
         #    if phys_port.id == port_map.switch_port_id:
         #        self._raise_ml2_error(wexc.HTTPConflict, 'create_port')
         switchport['ifindex'] = phys_port.ifindex
     credentials_dict = port.get('port')
     cred_dict = self._get_credentials_dict(bnp_switch, 'create_port')
     credentials_dict['credentials'] = cred_dict
     try:
         self.protocol_driver.set_isolation(port)
         port_id = port['port']['id']
         segmentation_id = port['port']['segmentation_id']
         mapping_dict = {'neutron_port_id': port_id,
                         'switch_port_id': phys_port.id,
                         'switch_id': bnp_switch.id,
                         'lag_id': None,
                         'access_type': constants.ACCESS,
                         'segmentation_id': int(segmentation_id),
                         'bind_status': 0
                         }
         db.add_bnp_switch_port_map(self.context, mapping_dict)
         db.add_bnp_neutron_port(self.context, mapping_dict)
         return constants.BIND_SUCCESS
     except Exception as e:
         LOG.error(_LE("Exception in configuring VLAN '%s' "), e)
         return constants.BIND_FAILURE
 def _create_port(self, port):
     switchports = port["port"]["switchports"]
     LOG.debug(_LE("_create_port switch: %s"), port)
     network_id = port["port"]["network_id"]
     subnets = db.get_subnets_by_network(self.context, network_id)
     if not subnets:
         LOG.error("Subnet not found for the network")
         self._raise_ml2_error(wexc.HTTPNotFound, "create_port")
     for switchport in switchports:
         switch_mac_id = switchport["switch_id"]
         port_id = switchport["port_id"]
         bnp_switch = db.get_bnp_phys_switch_by_mac(self.context, switch_mac_id)
         # check for port and switch level existence
         if not bnp_switch:
             LOG.error(_LE("No physical switch found '%s' "), switch_mac_id)
             self._raise_ml2_error(wexc.HTTPNotFound, "create_port")
         phys_port = db.get_bnp_phys_port(self.context, bnp_switch.id, port_id)
         if not phys_port:
             LOG.error(_LE("No physical port found for '%s' "), phys_port)
             self._raise_ml2_error(wexc.HTTPNotFound, "create_port")
         if bnp_switch.status != constants.SWITCH_STATUS["enable"]:
             LOG.error(_LE("Physical switch is not Enabled '%s' "), bnp_switch.status)
             self._raise_ml2_error(wexc.HTTPBadRequest, "create_port")
 def bind_port_to_segment(self, port):
     """bind_port_to_segment ."""
     LOG.info(_LI("bind_port_to_segment called from back-end mech driver"))
     switchports = port["port"]["switchports"]
     for switchport in switchports:
         switch_id = switchport["switch_id"]
         bnp_switch = db.get_bnp_phys_switch_by_mac(self.context, switch_id)
         port_name = switchport["port_id"]
         if not bnp_switch:
             self._raise_ml2_error(wexc.HTTPNotFound, "create_port")
         phys_port = db.get_bnp_phys_port(self.context, bnp_switch.id, port_name)
         if not phys_port:
             self._raise_ml2_error(wexc.HTTPNotFound, "create_port")
         switchport["ifindex"] = phys_port.ifindex
     credentials_dict = port.get("port")
     cred_dict = self._get_credentials_dict(bnp_switch, "create_port")
     credentials_dict["credentials"] = cred_dict
     try:
         self.protocol_driver.set_isolation(port)
         port_id = port["port"]["id"]
         segmentation_id = port["port"]["segmentation_id"]
         mapping_dict = {
             "neutron_port_id": port_id,
             "switch_port_id": phys_port.id,
             "switch_id": bnp_switch.id,
             "lag_id": None,
             "access_type": constants.ACCESS,
             "segmentation_id": int(segmentation_id),
             "bind_status": 0,
         }
         db.add_bnp_switch_port_map(self.context, mapping_dict)
         db.add_bnp_neutron_port(self.context, mapping_dict)
         return constants.BIND_SUCCESS
     except Exception as e:
         LOG.error(_LE("Exception in configuring VLAN '%s' "), e)
         return constants.BIND_FAILURE