def update(self, request, id):
     context = request.context
     self._check_admin(context)
     body = validators.validate_request(request)
     phys_switch = db.get_bnp_phys_switch(context, id)
     if not phys_switch:
         raise webob.exc.HTTPNotFound(
             _("Switch %s does not exist") % id)
     if body.get('access_parameters', None):
         if body.get('access_protocol', None):
             protocol = body['access_protocol']
             if protocol.lower() not in const.SUPPORTED_PROTOCOLS:
                 raise webob.exc.HTTPBadRequest(
                     _("access protocol %s is not supported") % body[
                         'access_protocol'])
         else:
             protocol = phys_switch.__dict__.get('access_protocol')
         if protocol.lower() == const.SNMP_V3:
             validators.validate_snmpv3_parameters(
                 body['access_parameters'])
         else:
             validators.validate_snmp_parameters(
                 body['access_parameters'])
         access_parameters = body.pop("access_parameters")
         for key, value in access_parameters.iteritems():
             body[key] = value
     switch_dict = self._update_dict(body, dict(phys_switch))
     switch_to_show = self._switch_to_show(switch_dict)
     switch = switch_to_show[0]
     if 'enable' in body.keys():
         if body['enable'] is False:
             if body.get('rediscover', None):
                 raise webob.exc.HTTPBadRequest(
                     _("Rediscovery of Switch %s is not supported"
                       "when Enable=False") % id)
             switch_status = const.SWITCH_STATUS['disable']
             switch['status'] = switch_status
             db.update_bnp_phys_switch_status(context,
                                              id, switch_status)
             db.update_bnp_phys_switch_access_params(context, id,
                                                     switch_dict)
             return switch
         elif phys_switch.__dict__['status'] == const.SWITCH_STATUS[
                 'enable'] and body['enable'] is True:
             raise webob.exc.HTTPBadRequest(
                 _("Disable the switch %s to update") % id)
     if phys_switch.__dict__['status'] == const.SWITCH_STATUS[
        'enable'] and body.get('rediscover', None):
         raise webob.exc.HTTPBadRequest(
             _("Disable the switch %d to update") % id)
     self._discover_switch(switch_dict)
     switch_status = const.SWITCH_STATUS['enable']
     switch['status'] = switch_status
     db.update_bnp_phys_switch_status(context, id, switch_status)
     db.update_bnp_phys_switch_access_params(context, id, switch_dict)
     return switch
 def create(self, request, **kwargs):
     """Create a new Credential"""
     context = request.context
     self._check_admin(context)
     body = validators.validate_request(request)
     key_list = ['name', 'snmpv1', 'snmpv2c',
                 'snmpv3', 'netconf-ssh', 'netconf-soap']
     keys = body.keys()
     validators.validate_attributes(keys, key_list)
     protocol = validators.validate_access_parameters(body)
     if protocol in ['snmpv1', 'snmpv2c', 'snmpv3']:
         db_snmp_cred = self._create_snmp_creds(context, body, protocol)
         return {const.BNP_CREDENTIAL_RESOURCE_NAME: dict(db_snmp_cred)}
     else:
         db_netconf_cred = self._create_netconf_creds(context, body, protocol)
         return {const.BNP_CREDENTIAL_RESOURCE_NAME: dict(db_netconf_cred)}
 def create(self, request, **kwargs):
     """Create a new Credential."""
     context = request.context
     self._check_admin(context)
     body = validators.validate_request(request)
     key_list = ["name", "snmpv1", "snmpv2c", "snmpv3", "netconf_ssh", "netconf_soap"]
     keys = body.keys()
     validators.validate_attributes(keys, key_list)
     protocol = validators.validate_access_parameters(body)
     if protocol in ["snmpv1", "snmpv2c", "snmpv3"]:
         db_snmp_cred = self._create_snmp_creds(context, body, protocol)
         db_snmp_cred = self._creds_to_show(db_snmp_cred)
         return {const.BNP_CREDENTIAL_RESOURCE_NAME: dict(db_snmp_cred)}
     else:
         db_netconf_cred = self._create_netconf_creds(context, body, protocol)
         db_netconf_cred = self._creds_to_show(db_netconf_cred)
         return {const.BNP_CREDENTIAL_RESOURCE_NAME: dict(db_netconf_cred)}
 def create(self, request, **kwargs):
     context = request.context
     self._check_admin(context)
     body = validators.validate_request(request)
     key_list = ['name', 'ip_address', 'vendor',
                 'disc_proto', 'disc_creds',
                 'prov_proto', 'prov_creds']
     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)
     validators.validate_switch_attributes(keys, key_list)
     if body['vendor'] not in const.SUPPORTED_VENDORS:
         raise webob.exc.HTTPBadRequest(
             _("Switch with vendor %s is not supported") %
             body['vendor'])
     ip_address = body['ip_address']
     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)
     access_parameters = self._get_access_param(context, body['disc_proto'],
                                                body['disc_creds'])
     for key, value in access_parameters.iteritems():
         body[key] = value
     bnp_switch = self._discover_switch(body)
     if bnp_switch.get('mac_address'):
         body['mac_address'] = bnp_switch.get('mac_address')
         body['port_prov'] = const.SWITCH_STATUS['enable']
     else:
         body['port_prov'] = const.SWITCH_STATUS['create']
     if 'family' not in body:
         body['family'] = None
     db_switch = db.add_bnp_phys_switch(context, body)
     if bnp_switch.get('ports'):
         self._add_physical_port(context, db_switch.get('id'),
                                 bnp_switch.get('ports'))
     return {const.BNP_SWITCH_RESOURCE_NAME: dict(db_switch)}
 def create(self, request, **kwargs):
     """Create a new Credential."""
     context = request.context
     self._check_admin(context)
     body = validators.validate_request(request)
     key_list = [
         'name', 'snmpv1', 'snmpv2c', 'snmpv3', 'netconf_ssh',
         'netconf_soap'
     ]
     keys = body.keys()
     validators.validate_attributes(keys, key_list)
     protocol = validators.validate_access_parameters(body)
     if protocol in ['snmpv1', 'snmpv2c', 'snmpv3']:
         db_snmp_cred = self._create_snmp_creds(context, body, protocol)
         db_snmp_cred = self._creds_to_show(db_snmp_cred)
         return {const.BNP_CREDENTIAL_RESOURCE_NAME: dict(db_snmp_cred)}
     else:
         db_netconf_cred = self._create_netconf_creds(
             context, body, protocol)
         db_netconf_cred = self._creds_to_show(db_netconf_cred)
         return {const.BNP_CREDENTIAL_RESOURCE_NAME: dict(db_netconf_cred)}
 def create(self, request, **kwargs):
     """Create a new Credential."""
     context = request.context
     self._check_admin(context)
     body = validators.validate_request(request)
     key_list = ['name', 'snmpv1', 'snmpv2c',
                 'snmpv3', 'netconf_ssh', 'netconf_soap']
     keys = body.keys()
     validators.validate_attributes(keys, key_list)
     protocol = validators.validate_access_parameters(body)
     if protocol in ['snmpv1', 'snmpv2c', 'snmpv3']:
         db_snmp_cred = self._create_snmp_creds(context, body, protocol)
         db_snmp_cred = self._creds_to_show(db_snmp_cred)
         return {const.BNP_CREDENTIAL_RESOURCE_NAME: dict(db_snmp_cred)}
     else:
         if body[protocol].get('password'):
             body[protocol]['password'] = credential_manager.create_secret(
                 body[protocol]['password'])
         db_netconf_cred = self._create_netconf_creds(
             context, body, protocol)
         db_netconf_cred = self._creds_to_show(db_netconf_cred)
         return {const.BNP_CREDENTIAL_RESOURCE_NAME: dict(db_netconf_cred)}
 def create(self, request):
     context = request.context
     self._check_admin(context)
     body = validators.validate_request(request)
     key_list = ['ip_address', 'vendor',
                 'access_protocol', 'access_parameters']
     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)
     if body['vendor'] not in const.SUPPORTED_VENDORS:
         raise webob.exc.HTTPBadRequest(
             _("Switch with vendor %s is not supported") %
             body['vendor'])
     ip_address = body['ip_address']
     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)
     validators.validate_access_parameters(body)
     access_parameters = body.pop("access_parameters")
     switch_dict = self._create_switch_dict()
     for key, value in access_parameters.iteritems():
         body[key] = value
     switch = self._update_dict(body, switch_dict)
     bnp_switch = self._discover_switch(switch)
     if bnp_switch.get('mac_address'):
         switch['mac_address'] = bnp_switch.get('mac_address')
         switch['status'] = const.SWITCH_STATUS['enable']
     else:
         switch['status'] = const.SWITCH_STATUS['create']
     db_switch = db.add_bnp_phys_switch(context, switch)
     if bnp_switch.get('ports'):
         self._add_physical_port(context, db_switch.get('id'),
                                 bnp_switch.get('ports'))
     return 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.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)
        protocol = validators.validate_access_parameters_for_update(body)
        key_list = [
            'name', 'snmpv1', 'snmpv2c', 'snmpv3', 'netconf_ssh',
            'netconf_soap'
        ]
        keys = body.keys()
        validators.validate_attributes(keys, key_list)
        if not uuidutils.is_uuid_like(id):
            raise webob.exc.HTTPBadRequest(_("Invalid Id"))
        if not protocol:
            switch_creds = db.get_snmp_cred_by_id(context, id)
            if switch_creds:
                switch_creds_dict = self._update_dict(body, dict(switch_creds))
                db.update_bnp_snmp_cred_by_id(context, id, switch_creds_dict)
                return switch_creds_dict
            switch_creds = db.get_netconf_cred_by_id(context, id)
            if switch_creds:
                switch_creds_dict = self._update_dict(body, dict(switch_creds))
                db.update_bnp_netconf_cred_by_id(context, id,
                                                 switch_creds_dict)
                return switch_creds_dict
            raise webob.exc.HTTPNotFound(
                _("Credential with id=%s does not exist") % id)

        elif protocol in [const.SNMP_V1, const.SNMP_V2C]:
            switch_creds = db.get_snmp_cred_by_id(context, id)
            if not switch_creds:
                raise webob.exc.HTTPNotFound(
                    _("Credential with id=%s does not exist") % id)
            self.check_creds_proto_type(switch_creds, id, protocol)
            params = body.pop(protocol)
            for key, value in params.iteritems():
                body[key] = value
            creds_dict = self._update_dict(body, dict(switch_creds))
            db.update_bnp_snmp_cred_by_id(context, id, creds_dict)
            return creds_dict

        elif protocol == const.SNMP_V3:
            switch_creds = db.get_snmp_cred_by_id(context, id)
            if not switch_creds:
                raise webob.exc.HTTPNotFound(
                    _("Credential with id=%s does not exist") % id)
            self.check_creds_proto_type(switch_creds, id, protocol)
            params = body.pop(protocol)
            if ('auth_protocol' in params.keys()) ^ ('auth_key'
                                                     in params.keys()):
                if (not switch_creds['auth_protocol']) and (
                        not switch_creds['auth_key']):
                    raise webob.exc.HTTPBadRequest(
                        _("auth_protocol and auth_key values does not exist,"
                          " so both has to be provided"))
            if ('priv_protocol' in params.keys()) ^ ('priv_key'
                                                     in params.keys()):
                if (not switch_creds['priv_protocol']) and (
                        not switch_creds['priv_key']):
                    raise webob.exc.HTTPBadRequest(
                        _("priv_protocol and priv_key values does not exist,"
                          " so both has to be provided"))
            for key, value in params.iteritems():
                body[key] = value
            creds_dict = self._update_dict(body, dict(switch_creds))
            db.update_bnp_snmp_cred_by_id(context, id, creds_dict)
            return creds_dict

        elif protocol == const.NETCONF_SOAP:
            switch_creds = db.get_netconf_cred_by_id(context, id)
            if not switch_creds:
                raise webob.exc.HTTPNotFound(
                    _("Credential with id=%s does not exist") % id)
            self.check_creds_proto_type(switch_creds, id, protocol)
            params = body.pop(protocol)
            for key, value in params.iteritems():
                body[key] = value
            creds_dict = self._update_dict(body, dict(switch_creds))
            db.update_bnp_netconf_cred_by_id(context, id, creds_dict)
            return creds_dict

        elif protocol == const.NETCONF_SSH:
            switch_creds = db.get_netconf_cred_by_id(context, id)
            if not switch_creds:
                raise webob.exc.HTTPNotFound(
                    _("Credential with id=%s does not exist") % id)
            self.check_creds_proto_type(switch_creds, id, protocol)
            params = body.pop(protocol)
            if ('user_name' in params.keys()) ^ ('password' in params.keys()):
                if (not switch_creds['user_name']) and (
                        not switch_creds['password']):
                    raise webob.exc.HTTPBadRequest(
                        _("user_name and password values does not exist, so"
                          " both has to be provided"))
            for key, value in params.iteritems():
                body[key] = value
            creds_dict = self._update_dict(body, dict(switch_creds))
            db.update_bnp_netconf_cred_by_id(context, id, creds_dict)
            return creds_dict
    def update(self, request, id, **kwargs):
        context = request.context
        self._check_admin(context)
        body = validators.validate_request(request)
        protocol = validators.validate_access_parameters_for_update(body)
        key_list = ['name', 'snmpv1', 'snmpv2c',
                    'snmpv3', 'netconf_ssh', 'netconf_soap']
        keys = body.keys()
        validators.validate_attributes(keys, key_list)
        if not uuidutils.is_uuid_like(id):
            raise webob.exc.HTTPBadRequest(
                _("Invalid Id"))
        if not protocol:
            switch_creds = db.get_snmp_cred_by_id(context, id)
            if switch_creds:
                switch_creds_dict = self._update_dict(body, dict(switch_creds))
                db.update_bnp_snmp_cred_by_id(context, id, switch_creds_dict)
                return switch_creds_dict
            switch_creds = db.get_netconf_cred_by_id(context, id)
            if switch_creds:
                if switch_creds.get('password'):
                    password = credential_manager.retrieve_secret(
                        switch_creds['password'])
                    credential_manager.delete_secret(switch_creds['password'])
                    switch_creds['password'] = password
                switch_creds_dict = self._update_dict(body, dict(switch_creds))
                if switch_creds_dict.get('password'):
                    password = credential_manager.create_secret(
                        switch_creds_dict['password'])
                    switch_creds_dict['password'] = password
                db.update_bnp_netconf_cred_by_id(
                    context, id, switch_creds_dict)
                return switch_creds_dict
            raise webob.exc.HTTPNotFound(
                _("Credential with id=%s does not exist") % id)

        elif protocol in [const.SNMP_V1, const.SNMP_V2C]:
            switch_creds = db.get_snmp_cred_by_id(context, id)
            if not switch_creds:
                raise webob.exc.HTTPNotFound(
                    _("Credential with id=%s does not exist") % id)
            self.check_creds_proto_type(switch_creds, id, protocol)
            params = body.pop(protocol)
            for key, value in params.iteritems():
                body[key] = value
            creds_dict = self._update_dict(body, dict(switch_creds))
            db.update_bnp_snmp_cred_by_id(context, id, creds_dict)
            return creds_dict

        elif protocol == const.SNMP_V3:
            switch_creds = db.get_snmp_cred_by_id(context, id)
            if not switch_creds:
                raise webob.exc.HTTPNotFound(
                    _("Credential with id=%s does not exist") % id)
            self.check_creds_proto_type(switch_creds, id, protocol)
            params = body.pop(protocol)
            if ('auth_protocol' in params.keys()) ^ (
                    'auth_key' in params.keys()):
                if (not switch_creds['auth_protocol']) and (
                        not switch_creds['auth_key']):
                    raise webob.exc.HTTPBadRequest(
                        _("auth_protocol and auth_key values does not exist,"
                          " so both has to be provided"))
            if ('priv_protocol' in params.keys()) ^ ('priv_key'
                                                     in params.keys()):
                if (not switch_creds['priv_protocol']) and (
                        not switch_creds['priv_key']):
                    raise webob.exc.HTTPBadRequest(
                        _("priv_protocol and priv_key values does not exist,"
                          " so both has to be provided"))
            for key, value in params.iteritems():
                body[key] = value
            creds_dict = self._update_dict(body, dict(switch_creds))
            db.update_bnp_snmp_cred_by_id(context, id, creds_dict)
            return creds_dict

        elif protocol == const.NETCONF_SOAP:
            switch_creds = db.get_netconf_cred_by_id(context, id)
            if not switch_creds:
                raise webob.exc.HTTPNotFound(
                    _("Credential with id=%s does not exist") % id)
            self.check_creds_proto_type(switch_creds, id, protocol)
            params = body.pop(protocol)
            for key, value in params.iteritems():
                body[key] = value
            if switch_creds.get('password'):
                password = credential_manager.retrieve_secret(
                    switch_creds['password'])
                credential_manager.delete_secret(switch_creds['password'])
                switch_creds['password'] = password
            creds_dict = self._update_dict(body, dict(switch_creds))
            if creds_dict.get('password'):
                creds_dict['password'] = credential_manager.create_secret(
                    creds_dict['password'])
            db.update_bnp_netconf_cred_by_id(context, id, creds_dict)
            return creds_dict

        elif protocol == const.NETCONF_SSH:
            switch_creds = db.get_netconf_cred_by_id(context, id)
            if not switch_creds:
                raise webob.exc.HTTPNotFound(
                    _("Credential with id=%s does not exist") % id)
            self.check_creds_proto_type(switch_creds, id, protocol)
            params = body.pop(protocol)
            if ('user_name' in params.keys()) ^ ('password' in params.keys()):
                if (not switch_creds['user_name']) and (
                        not switch_creds['password']):
                    raise webob.exc.HTTPBadRequest(
                        _("user_name and password values does not exist, so"
                          " both has to be provided"))
            for key, value in params.iteritems():
                body[key] = value
            if switch_creds.get('password'):
                password = credential_manager.retrieve_secret(
                    switch_creds['password'])
                credential_manager.delete_secret(switch_creds['password'])
                switch_creds['password'] = password
            creds_dict = self._update_dict(body, dict(switch_creds))
            if creds_dict.get('password'):
                creds_dict['password'] = credential_manager.create_secret(
                    creds_dict['password'])
            db.update_bnp_netconf_cred_by_id(context, id, creds_dict)
            return creds_dict
 def update(self, request, id, **kwargs):
     context = request.context
     self._check_admin(context)
     body = validators.validate_request(request)
     key_list = ['access_protocol', 'access_parameters',
                 'enable', 'rediscover']
     validators.validate_attributes(body.keys(), key_list)
     validate_snmp_creds = False
     phys_switch = db.get_bnp_phys_switch(context, id)
     if not phys_switch:
         raise webob.exc.HTTPNotFound(
             _("Switch %s does not exist") % id)
     if body.get('access_parameters'):
         validate_snmp_creds = True
         access_parameters = body.pop("access_parameters")
         for key, value in access_parameters.iteritems():
             body[key] = value
     else:
         access_parameters = {
             'write_community': phys_switch['write_community'],
             'security_name': phys_switch['security_name'],
             'auth_protocol': phys_switch['auth_protocol'],
             'priv_protocol': phys_switch['priv_protocol'],
             'auth_key': phys_switch['auth_key'],
             'priv_key': phys_switch['priv_key'],
             'security_level': phys_switch['security_level']}
     if body.get('access_protocol'):
         validate_snmp_creds = True
         protocol = body['access_protocol']
         if protocol.lower() not in const.SUPPORTED_PROTOCOLS:
             raise webob.exc.HTTPBadRequest(
                 _("access protocol %s is not supported") % body[
                     'access_protocol'])
     else:
         protocol = phys_switch['access_protocol']
     switch_dict = self._update_dict(body, dict(phys_switch))
     switch_to_show = self._switch_to_show(switch_dict)
     switch = switch_to_show[0]
     if validate_snmp_creds:
         if protocol.lower() == const.SNMP_V3:
             validators.validate_snmpv3_parameters(access_parameters)
         else:
             validators.validate_snmp_parameters(access_parameters)
         try:
             snmp_driver = discovery_driver.SNMPDiscoveryDriver(switch_dict)
             snmp_driver.get_sys_name()
             db.update_bnp_phys_switch_access_params(context,
                                                     id, switch_dict)
         except Exception as e:
             LOG.error(_LE("Exception in validating credentials '%s' "), e)
             raise webob.exc.HTTPBadRequest(
                 _("Validation of credentials failed"))
     if body.get('enable'):
         enable = attributes.convert_to_boolean(body['enable'])
         if not enable:
             switch_status = const.SWITCH_STATUS['disable']
             db.update_bnp_phys_switch_status(context, id, switch_status)
         else:
             switch_status = const.SWITCH_STATUS['enable']
             db.update_bnp_phys_switch_status(context, id, switch_status)
         switch['status'] = switch_status
     if body.get('rediscover'):
         bnp_switch = self._discover_switch(switch_dict)
         db_switch_ports = db.get_bnp_phys_switch_ports_by_switch_id(
             context, id)
         self._update_switch_ports(context, id,
                                   bnp_switch.get('ports'),
                                   db_switch_ports)
     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
    def update(self, request, id, **kwargs):
        context = request.context
        self._check_admin(context)
        body = validators.validate_request(request)
        protocol = validators.validate_access_parameters_for_update(body)
        key_list = ["name", "snmpv1", "snmpv2c", "snmpv3", "netconf_ssh", "netconf_soap"]
        keys = body.keys()
        validators.validate_attributes(keys, key_list)
        if not uuidutils.is_uuid_like(id):
            raise webob.exc.HTTPBadRequest(_("Invalid Id"))
        if not protocol:
            switch_creds = db.get_snmp_cred_by_id(context, id)
            if switch_creds:
                switch_creds_dict = self._update_dict(body, dict(switch_creds))
                db.update_bnp_snmp_cred_by_id(context, id, switch_creds_dict)
                return switch_creds_dict
            switch_creds = db.get_netconf_cred_by_id(context, id)
            if switch_creds:
                switch_creds_dict = self._update_dict(body, dict(switch_creds))
                db.update_bnp_netconf_cred_by_id(context, id, switch_creds_dict)
                return switch_creds_dict
            raise webob.exc.HTTPNotFound(_("Credential with id=%s does not exist") % id)

        elif protocol in [const.SNMP_V1, const.SNMP_V2C]:
            switch_creds = db.get_snmp_cred_by_id(context, id)
            if not switch_creds:
                raise webob.exc.HTTPNotFound(_("Credential with id=%s does not exist") % id)
            self.check_creds_proto_type(switch_creds, id, protocol)
            params = body.pop(protocol)
            for key, value in params.iteritems():
                body[key] = value
            creds_dict = self._update_dict(body, dict(switch_creds))
            db.update_bnp_snmp_cred_by_id(context, id, creds_dict)
            return creds_dict

        elif protocol == const.SNMP_V3:
            switch_creds = db.get_snmp_cred_by_id(context, id)
            if not switch_creds:
                raise webob.exc.HTTPNotFound(_("Credential with id=%s does not exist") % id)
            self.check_creds_proto_type(switch_creds, id, protocol)
            params = body.pop(protocol)
            if ("auth_protocol" in params.keys()) ^ ("auth_key" in params.keys()):
                if (not switch_creds["auth_protocol"]) and (not switch_creds["auth_key"]):
                    raise webob.exc.HTTPBadRequest(
                        _("auth_protocol and auth_key values does not exist," " so both has to be provided")
                    )
            if ("priv_protocol" in params.keys()) ^ ("priv_key" in params.keys()):
                if (not switch_creds["priv_protocol"]) and (not switch_creds["priv_key"]):
                    raise webob.exc.HTTPBadRequest(
                        _("priv_protocol and priv_key values does not exist," " so both has to be provided")
                    )
            for key, value in params.iteritems():
                body[key] = value
            creds_dict = self._update_dict(body, dict(switch_creds))
            db.update_bnp_snmp_cred_by_id(context, id, creds_dict)
            return creds_dict

        elif protocol == const.NETCONF_SOAP:
            switch_creds = db.get_netconf_cred_by_id(context, id)
            if not switch_creds:
                raise webob.exc.HTTPNotFound(_("Credential with id=%s does not exist") % id)
            self.check_creds_proto_type(switch_creds, id, protocol)
            params = body.pop(protocol)
            for key, value in params.iteritems():
                body[key] = value
            creds_dict = self._update_dict(body, dict(switch_creds))
            db.update_bnp_netconf_cred_by_id(context, id, creds_dict)
            return creds_dict

        elif protocol == const.NETCONF_SSH:
            switch_creds = db.get_netconf_cred_by_id(context, id)
            if not switch_creds:
                raise webob.exc.HTTPNotFound(_("Credential with id=%s does not exist") % id)
            self.check_creds_proto_type(switch_creds, id, protocol)
            params = body.pop(protocol)
            if ("user_name" in params.keys()) ^ ("password" in params.keys()):
                if (not switch_creds["user_name"]) and (not switch_creds["password"]):
                    raise webob.exc.HTTPBadRequest(
                        _("user_name and password values does not exist, so" " both has to be provided")
                    )
            for key, value in params.iteritems():
                body[key] = value
            creds_dict = self._update_dict(body, dict(switch_creds))
            db.update_bnp_netconf_cred_by_id(context, id, creds_dict)
            return creds_dict