def delete(self, request, id, **kwargs):
     context = request.context
     self._check_admin(context)
     filters = {"credentials": id}
     switch_exists = db.get_if_bnp_phy_switch_exists(context, **filters)
     if switch_exists:
         raise webob.exc.HTTPConflict(
             _("credential with id=%s is associated with a switch." "Hence can't be deleted.") % id
         )
     snmp_cred = db.get_snmp_cred_by_id(context, id)
     netconf_cred = db.get_netconf_cred_by_id(context, id)
     if snmp_cred:
         db.delete_snmp_cred_by_id(context, id)
     elif netconf_cred:
         db.delete_netconf_cred_by_id(context, id)
     else:
         raise webob.exc.HTTPNotFound(_("Credential with id=%s does not exist") % id)
 def delete(self, request, id, **kwargs):
     context = request.context
     self._check_admin(context)
     filters = {'credentials': id}
     switch_exists = db.get_if_bnp_phy_switch_exists(context, **filters)
     if switch_exists:
         raise webob.exc.HTTPConflict(
             _("credential with id=%s is associated with a switch."
               "Hence can't be deleted.") % id)
     snmp_cred = db.get_snmp_cred_by_id(context, id)
     netconf_cred = db.get_netconf_cred_by_id(context, id)
     if snmp_cred:
         db.delete_snmp_cred_by_id(context, id)
     elif netconf_cred:
         db.delete_netconf_cred_by_id(context, id)
     else:
         raise webob.exc.HTTPNotFound(
             _("Credential with id=%s does not exist") % id)
 def create(self, request, **kwargs):
     context = request.context
     self._check_admin(context)
     body = validators.validate_request(request)
     key_list = ['name', 'ip_address', 'vendor',
                 'management_protocol', 'credentials',
                 'mac_address']
     keys = body.keys()
     for key in key_list:
         if key not in keys:
             raise webob.exc.HTTPBadRequest(
                 _("Key %s not found in request body") % key)
     key_list.append('family')
     validators.validate_attributes(keys, key_list)
     ip_address = body['ip_address']
     if const.FAMILY not in body:
         body['family'] = ''
     bnp_switch = db.get_bnp_phys_switch_by_ip(context,
                                               ip_address)
     if bnp_switch:
         raise webob.exc.HTTPConflict(
             _("Switch with ip_address %s is already present") %
             ip_address)
     filters = {'mac_address': body['mac_address']}
     switch_exists = db.get_if_bnp_phy_switch_exists(context, **filters)
     if switch_exists:
         raise webob.exc.HTTPConflict(
             _("Switch with mac_address %s is already present") %
             body['mac_address'])
     access_parameters = self._get_access_param(context,
                                                body['management_protocol'],
                                                body['credentials'])
     credentials = body['credentials']
     body['port_provisioning'] = const.PORT_PROVISIONING_STATUS['enable']
     result = self.validate_protocol(access_parameters, credentials, body)
     body['validation_result'] = result
     db_switch = db.add_bnp_phys_switch(context, body)
     return {const.BNP_SWITCH_RESOURCE_NAME: dict(db_switch)}
 def create(self, request, **kwargs):
     context = request.context
     self._check_admin(context)
     body = validators.validate_request(request)
     key_list = ['name', 'ip_address', 'vendor',
                 'management_protocol', 'credentials',
                 'mac_address']
     keys = body.keys()
     for key in key_list:
         if key not in keys:
             raise webob.exc.HTTPBadRequest(
                 _("Key %s not found in request body") % key)
     key_list.append('family')
     validators.validate_attributes(keys, key_list)
     ip_address = body['ip_address']
     if const.FAMILY not in body:
         body['family'] = ''
     bnp_switch = db.get_bnp_phys_switch_by_ip(context,
                                               ip_address)
     if bnp_switch:
         raise webob.exc.HTTPConflict(
             _("Switch with ip_address %s is already present") %
             ip_address)
     filters = {'mac_address': body['mac_address']}
     switch_exists = db.get_if_bnp_phy_switch_exists(context, **filters)
     if switch_exists:
         raise webob.exc.HTTPConflict(
             _("Switch with mac_address %s is already present") %
             body['mac_address'])
     access_parameters = self._get_access_param(context,
                                                body['management_protocol'],
                                                body['credentials'])
     credentials = body['credentials']
     body['port_provisioning'] = const.SWITCH_STATUS['enable']
     result = self.validate_protocol(access_parameters, credentials, body)
     body['validation_result'] = result
     db_switch = db.add_bnp_phys_switch(context, body)
     return {const.BNP_SWITCH_RESOURCE_NAME: dict(db_switch)}
 def update(self, request, id, **kwargs):
     context = request.context
     self._check_admin(context)
     body = validators.validate_request(request)
     key_list = [
         'name', 'ip_address', 'vendor', 'management_protocol',
         'credentials', 'mac_address', 'port_provisioning', 'validate'
     ]
     validators.validate_attributes(body.keys(), key_list)
     switch = db.get_bnp_phys_switch(context, id)
     if not switch:
         raise webob.exc.HTTPNotFound(_("Switch %s does not exist") % id)
     if body.get('ip_address'):
         if (body['ip_address'] != switch['ip_address']):
             ip = body['ip_address']
             bnp_switch = db.get_bnp_phys_switch_by_ip(context, ip)
             if bnp_switch:
                 raise webob.exc.HTTPConflict(
                     _("Switch with ip_address %s is already present") % ip)
             else:
                 switch['ip_address'] = ip
     if body.get('port_provisioning'):
         port_prov = body['port_provisioning']
         if (port_prov.upper()
                 not in const.PORT_PROVISIONING_STATUS.values()):
             raise webob.exc.HTTPBadRequest(
                 _("Invalid port-provisioning option %s ") % port_prov)
         switch['port_provisioning'] = port_prov.upper()
     if body.get('name'):
         switch['name'] = body['name']
     if body.get('vendor'):
         switch['vendor'] = body['vendor']
     if body.get('management_protocol') and body.get('credentials'):
         proto = body['management_protocol']
         cred = body['credentials']
         self._get_access_param(context, proto, cred)
         switch['management_protocol'] = proto
         switch['credentials'] = cred
     elif (body.get('management_protocol') and not body.get('credentials')):
         proto = body['management_protocol']
         if (body['management_protocol'] != switch['management_protocol']):
             raise webob.exc.HTTPBadRequest(
                 _("Invalid management_protocol : %s ") % proto)
         switch['management_protocol'] = proto
     elif (body.get('credentials') and not body.get('management_protocol')):
         cred = body['credentials']
         self._get_access_param(context, switch['management_protocol'],
                                cred)
         switch['credentials'] = cred
     if body.get('mac_address') or body.get('validate'):
         body['vendor'] = switch['vendor']
         body['management_protocol'] = switch['management_protocol']
         body['family'] = switch['family']
         sw_proto = switch['management_protocol']
         sw_cred = switch['credentials']
         body['ip_address'] = switch['ip_address']
         access_parameters = self._get_access_param(context, sw_proto,
                                                    sw_cred)
         if body.get('mac_address'):
             filters = {'mac_address': body['mac_address']}
             switch_exists = db.get_if_bnp_phy_switch_exists(
                 context, **filters)
             if switch_exists:
                 raise webob.exc.HTTPConflict(
                     _("Switch with mac_address %s is already present") %
                     body['mac_address'])
             result = self.validate_protocol(access_parameters,
                                             switch['credentials'], body)
             switch['validation_result'] = result
             switch['mac_address'] = body['mac_address']
         elif body.get('validate') and not body.get('mac_address'):
             body['mac_address'] = switch['mac_address']
             result = self.validate_protocol(access_parameters,
                                             switch['credentials'], body)
             switch['validation_result'] = result
     db.update_bnp_phy_switch(context, id, switch)
     return switch
 def update(self, request, id, **kwargs):
     context = request.context
     self._check_admin(context)
     body = validators.validate_request(request)
     key_list = ['name', 'ip_address', 'vendor',
                 'management_protocol', 'credentials',
                 'mac_address', 'port_provisioning', 'validate']
     validators.validate_attributes(body.keys(), key_list)
     switch = db.get_bnp_phys_switch(context, id)
     if not switch:
         raise webob.exc.HTTPNotFound(
             _("Switch %s does not exist") % id)
     if body.get('ip_address'):
         if (body['ip_address'] != switch['ip_address']):
             ip = body['ip_address']
             bnp_switch = db.get_bnp_phys_switch_by_ip(context, ip)
             if bnp_switch:
                 raise webob.exc.HTTPConflict(
                     _("Switch with ip_address %s is already present") %
                     ip)
             else:
                 switch['ip_address'] = ip
     if body.get('port_provisioning'):
         port_prov = body['port_provisioning']
         if (port_prov.upper() not in
                 const.PORT_PROVISIONING_STATUS.values()):
             raise webob.exc.HTTPBadRequest(
                 _("Invalid port-provisioning option %s ") % port_prov)
         switch['port_provisioning'] = port_prov.upper()
     if body.get('name'):
         switch['name'] = body['name']
     if body.get('vendor'):
         switch['vendor'] = body['vendor']
     if body.get('management_protocol') and body.get('credentials'):
         proto = body['management_protocol']
         cred = body['credentials']
         self._get_access_param(context,
                                proto,
                                cred)
         switch['management_protocol'] = proto
         switch['credentials'] = cred
     elif (body.get('management_protocol')
           and not body.get('credentials')):
         proto = body['management_protocol']
         if (body['management_protocol'] !=
                 switch['management_protocol']):
             raise webob.exc.HTTPBadRequest(
                 _("Invalid management_protocol : %s ") % proto)
         switch['management_protocol'] = proto
     elif (body.get('credentials') and not
           body.get('management_protocol')):
         cred = body['credentials']
         self._get_access_param(context,
                                switch['management_protocol'],
                                cred)
         switch['credentials'] = cred
     if body.get('mac_address') or body.get('validate'):
         body['vendor'] = switch['vendor']
         body['management_protocol'] = switch['management_protocol']
         body['family'] = switch['family']
         sw_proto = switch['management_protocol']
         sw_cred = switch['credentials']
         body['ip_address'] = switch['ip_address']
         access_parameters = self._get_access_param(context,
                                                    sw_proto,
                                                    sw_cred)
         if body.get('mac_address'):
             filters = {'mac_address': body['mac_address']}
             switch_exists = db.get_if_bnp_phy_switch_exists(
                 context, **filters)
             if switch_exists:
                 raise webob.exc.HTTPConflict(
                     _("Switch with mac_address %s is already present") %
                     body['mac_address'])
             result = self.validate_protocol(access_parameters,
                                             switch['credentials'], body)
             switch['validation_result'] = result
             switch['mac_address'] = body['mac_address']
         elif body.get('validate') and not body.get('mac_address'):
             body['mac_address'] = switch['mac_address']
             result = self.validate_protocol(access_parameters,
                                             switch['credentials'], body)
             switch['validation_result'] = result
     db.update_bnp_phy_switch(context, id, switch)
     return switch