from oslo_serialization import jsonutils as json
from nca47.api.controllers.v1 import base
from nca47.common.i18n import _
from nca47.common.i18n import _LI, _LE
from nca47.common.exception import Nca47Exception
from oslo_log import log
from nca47.api.controllers.v1 import tools
from nca47.manager.central import CentralManager
from nca47.common.exception import ParamFormatError
from amqp.five import string
from nca47.common.exception import BadRequest
from oslo_messaging import RemoteError
from nca47.common import exception

LOG = log.getLogger(__name__)


class SecurityZoneController(base.BaseRestController):
    def __init__(self):
        self.manager = CentralManager.get_instance()
        super(SecurityZoneController, self).__init__()

    def create(self, req, *args, **kwargs):
        try:
            url = req.url
            if len(args) > 1:
                raise BadRequest(resource="SecurityZone create", msg=url)
            context = req.context
            body_values = json.loads(req.body)
            valid_attributes = ['tenant_id', 'dc_name', 'network_zone',
                                'name', 'ifnames', 'priority', 'vfwname']
            values = tools.validat_values(body_values, valid_attributes)
            LOG.info(_LI("input the SecurityZone values with dic format \
                    is %(json)s"),
                     {"json": body_values})
            values["name"] = (values["tenant_id"] + "_" +
                              values["network_zone"] +
                              "_" + values["name"])
            response = self.manager.create_securityZone(context, values)
            return response
        except Nca47Exception as e:
            self.response.status = e.code
            LOG.error(_LE('Error exception! error info: %' + e.message))
            LOG.exception(e)
            self.response.status = e.code
            return tools.ret_info(e.code, e.message)
        except RemoteError as exception:
            self.response.status = 500
            message = exception.value
            return tools.ret_info(self.response.status, message)
        except Exception as e:
            LOG.exception(e)
            self.response.status = 500
            return tools.ret_info(self.response.status, e.message)

    def remove(self, req, *args, **kwargs):
        try:
            url = req.url
            if len(args) > 1:
                raise BadRequest(resource="SecurityZone del", msg=url)
            context = req.context
            body_values = json.loads(req.body)
            valid_attributes = ['tenant_id', 'dc_name', 'network_zone', 'id']
            values = tools.validat_values(body_values, valid_attributes)
            # input the SecurityZone values with dic format
            LOG.info(_LI("delete the SecurityZone values with dic forma \
                        is %(json)s"),
                     {"json": body_values})
            response = self.manager.del_securityZone(context, values)
            return response
        except Nca47Exception as e:
            self.response.status = e.code
            LOG.error(_LE('Error exception! error info: %' + e.message))
            LOG.exception(e)
            self.response.status = e.code
            return tools.ret_info(e.code, e.message)
        except RemoteError as exception:
            self.response.status = 500
            message = exception.value
            return tools.ret_info(self.response.status, message)
        except Exception as e:
            LOG.exception(e)
            self.response.status = 500
            return tools.ret_info(self.response.status, e.message)

    def list(self, req, *args, **kwargs):
        try:
            url = req.url
            if len(args) > 1:
                raise BadRequest(resource="SecurityZone getAll", msg=url)
            context = req.context
            body_values = json.loads(req.body)
            valid_attributes = ['tenant_id', 'dc_name',
                                'network_zone', 'vfwname']
            values = tools.validat_values(body_values, valid_attributes)
            # get_all the SecurityZone values with dic format
            LOG.info(_LI("get_all the SecurityZone values with dic format \
                        is %(json)s"),
                     {"json": body_values})
            response = self.manager.get_securityZones(context, values)
            return response
        except Nca47Exception as e:
            self.response.status = e.code
            LOG.error(_LE('Error exception! error info: %' + e.message))
            LOG.exception(e)
            self.response.status = e.code
            return tools.ret_info(e.code, e.message)
        except RemoteError as exception:
            self.response.status = 500
            message = exception.value
            return tools.ret_info(self.response.status, message)
        except Exception as e:
            LOG.exception(e)
            self.response.status = 500
            return tools.ret_info(self.response.status, e.message)

    def show(self, req, *args, **kwargs):
        try:
            url = req.url
            if len(args) > 1:
                raise BadRequest(resource="SecurityZone get", msg=url)
            context = req.context
            body_values = json.loads(req.body)
            valid_attributes = ['id']
            values = tools.validat_values(body_values, valid_attributes)
            # get the staticnat values with dic format
            LOG.info(_LI("get the SecurityZone values with dic format\
                         is %(json)s"),
                     {"json": body_values})
            response = self.manager.get_securityZone(context, values)
            return response
        except Nca47Exception as e:
            self.response.status = e.code
            LOG.error(_LE('Error exception! error info: %' + e.message))
            LOG.exception(e)
            self.response.status = e.code
            return tools.ret_info(e.code, e.message)
        except RemoteError as exception:
            self.response.status = 500
            message = exception.value
            return tools.ret_info(self.response.status, message)
        except Exception as e:
            LOG.exception(e)
            self.response.status = 500
            return tools.ret_info(self.response.status, e.message)

    def addif(self, req, *args, **kwargs):
        try:
            url = req.url
            if len(args) > 1:
                raise BadRequest(resource="SecurityZone add vlan", msg=url)
            context = req.context
            body_values = json.loads(req.body)
            valid_attributes = ['tenant_id', 'dc_name', 'network_zone', 'id',
                                'ifname']
            values = tools.validat_values(body_values, valid_attributes)
            # input the SecurityZone values with dic format
            LOG.info(_LI("input the SecurityZone values with dic formatO is\
                         %(json)s"),
                     {"json": body_values})
            response = self.manager.get_securityZone(context, values)
            if not isinstance(values["ifname"], string):
                raise ParamFormatError(param_name="ifname")
            if values["ifname"] in response.ifnames:
                message = ("securityZone with ifname=" +
                           values["ifname"] + " already exists")
                return tools.ret_info("400", message)
            response.ifnames.append(values["ifname"])
            values["ifnames"] = response.ifnames
            response = self.manager.update_securityZone(context, values)
            return response
        except Nca47Exception as e:
            self.response.status = e.code
            LOG.error(_LE('Error exception! error info: %' + e.message))
            LOG.exception(e)
            self.response.status = e.code
            return tools.ret_info(e.code, e.message)
        except RemoteError as exception:
            self.response.status = 500
            message = exception.value
            return tools.ret_info(self.response.status, message)
        except Exception as e:
            LOG.exception(e)
            self.response.status = 500
            return tools.ret_info(self.response.status, e.message)

    def delif(self, req, *args, **kwargs):
        try:
            url = req.url
            if len(args) > 1:
                raise BadRequest(resource="SecurityZone del vlan", msg=url)
            context = req.context
            body_values = json.loads(req.body)
            valid_attributes = ['tenant_id', 'dc_name', 'network_zone', 'id',
                                'ifname']
            values = tools.validat_values(body_values, valid_attributes)
            # input the SecurityZone values with dic format
            LOG.info(_LI("input the SecurityZone values with dic format\
                         is %(json)s"),
                     {"json": body_values})
            response = self.manager.get_securityZone(context, values)
            if not isinstance(values["ifname"], string):
                raise ParamFormatError(param_name="ifname")
            if values["ifname"] not in response.ifnames:
                message = ("securityZone with ifname=" +
                           values["ifname"]+" don't exist!")
                return tools.ret_info("400", message)
            response.ifnames.remove(values["ifname"])
            values["ifnames"] = response.ifnames
            response = self.manager.update_securityZone(context, values)
            return response
        except Nca47Exception as e:
            self.response.status = e.code
            LOG.error(_LE('Error exception! error info: %' + e.message))
            LOG.exception(e)
            self.response.status = e.code
            return tools.ret_info(e.code, e.message)
        except RemoteError as exception:
            self.response.status = 500
            message = exception.value
            return tools.ret_info(self.response.status, message)
        except Exception as e:
            LOG.exception(e)
            self.response.status = 500
            return tools.ret_info(self.response.status, e.message)

[((537, 560), 'oslo_log.log.getLogger', 'log.getLogger', (['__name__'], {}), '(__name__)\n', (550, 560), False, 'from oslo_log import log\n'), ((665, 694), 'nca47.manager.central.CentralManager.get_instance', 'CentralManager.get_instance', ([], {}), '()\n', (692, 694), False, 'from nca47.manager.central import CentralManager\n'), ((998, 1018), 'oslo_serialization.jsonutils.loads', 'json.loads', (['req.body'], {}), '(req.body)\n', (1008, 1018), True, 'from oslo_serialization import jsonutils as json\n'), ((1186, 1237), 'nca47.api.controllers.v1.tools.validat_values', 'tools.validat_values', (['body_values', 'valid_attributes'], {}), '(body_values, valid_attributes)\n', (1206, 1237), False, 'from nca47.api.controllers.v1 import tools\n'), ((2526, 2546), 'oslo_serialization.jsonutils.loads', 'json.loads', (['req.body'], {}), '(req.body)\n', (2536, 2546), True, 'from oslo_serialization import jsonutils as json\n'), ((2646, 2697), 'nca47.api.controllers.v1.tools.validat_values', 'tools.validat_values', (['body_values', 'valid_attributes'], {}), '(body_values, valid_attributes)\n', (2666, 2697), False, 'from nca47.api.controllers.v1 import tools\n'), ((3883, 3903), 'oslo_serialization.jsonutils.loads', 'json.loads', (['req.body'], {}), '(req.body)\n', (3893, 3903), True, 'from oslo_serialization import jsonutils as json\n'), ((4040, 4091), 'nca47.api.controllers.v1.tools.validat_values', 'tools.validat_values', (['body_values', 'valid_attributes'], {}), '(body_values, valid_attributes)\n', (4060, 4091), False, 'from nca47.api.controllers.v1 import tools\n'), ((5279, 5299), 'oslo_serialization.jsonutils.loads', 'json.loads', (['req.body'], {}), '(req.body)\n', (5289, 5299), True, 'from oslo_serialization import jsonutils as json\n'), ((5359, 5410), 'nca47.api.controllers.v1.tools.validat_values', 'tools.validat_values', (['body_values', 'valid_attributes'], {}), '(body_values, valid_attributes)\n', (5379, 5410), False, 'from nca47.api.controllers.v1 import tools\n'), ((6592, 6612), 'oslo_serialization.jsonutils.loads', 'json.loads', (['req.body'], {}), '(req.body)\n', (6602, 6612), True, 'from oslo_serialization import jsonutils as json\n'), ((6754, 6805), 'nca47.api.controllers.v1.tools.validat_values', 'tools.validat_values', (['body_values', 'valid_attributes'], {}), '(body_values, valid_attributes)\n', (6774, 6805), False, 'from nca47.api.controllers.v1 import tools\n'), ((8517, 8537), 'oslo_serialization.jsonutils.loads', 'json.loads', (['req.body'], {}), '(req.body)\n', (8527, 8537), True, 'from oslo_serialization import jsonutils as json\n'), ((8679, 8730), 'nca47.api.controllers.v1.tools.validat_values', 'tools.validat_values', (['body_values', 'valid_attributes'], {}), '(body_values, valid_attributes)\n', (8699, 8730), False, 'from nca47.api.controllers.v1 import tools\n'), ((886, 937), 'nca47.common.exception.BadRequest', 'BadRequest', ([], {'resource': '"""SecurityZone create"""', 'msg': 'url'}), "(resource='SecurityZone create', msg=url)\n", (896, 937), False, 'from nca47.common.exception import BadRequest\n'), ((1259, 1348), 'nca47.common.i18n._LI', '_LI', (['"""input the SecurityZone values with dic format                     is %(json)s"""'], {}), "('input the SecurityZone values with dic format                     is %(json)s'\n    )\n", (1262, 1348), False, 'from nca47.common.i18n import _LI, _LE\n'), ((1898, 1931), 'nca47.api.controllers.v1.tools.ret_info', 'tools.ret_info', (['e.code', 'e.message'], {}), '(e.code, e.message)\n', (1912, 1931), False, 'from nca47.api.controllers.v1 import tools\n'), ((2069, 2114), 'nca47.api.controllers.v1.tools.ret_info', 'tools.ret_info', (['self.response.status', 'message'], {}), '(self.response.status, message)\n', (2083, 2114), False, 'from nca47.api.controllers.v1 import tools\n'), ((2233, 2280), 'nca47.api.controllers.v1.tools.ret_info', 'tools.ret_info', (['self.response.status', 'e.message'], {}), '(self.response.status, e.message)\n', (2247, 2280), False, 'from nca47.api.controllers.v1 import tools\n'), ((2417, 2465), 'nca47.common.exception.BadRequest', 'BadRequest', ([], {'resource': '"""SecurityZone del"""', 'msg': 'url'}), "(resource='SecurityZone del', msg=url)\n", (2427, 2465), False, 'from nca47.common.exception import BadRequest\n'), ((2779, 2872), 'nca47.common.i18n._LI', '_LI', (['"""delete the SecurityZone values with dic forma                         is %(json)s"""'], {}), "('delete the SecurityZone values with dic forma                         is %(json)s'\n    )\n", (2782, 2872), False, 'from nca47.common.i18n import _LI, _LE\n'), ((3254, 3287), 'nca47.api.controllers.v1.tools.ret_info', 'tools.ret_info', (['e.code', 'e.message'], {}), '(e.code, e.message)\n', (3268, 3287), False, 'from nca47.api.controllers.v1 import tools\n'), ((3425, 3470), 'nca47.api.controllers.v1.tools.ret_info', 'tools.ret_info', (['self.response.status', 'message'], {}), '(self.response.status, message)\n', (3439, 3470), False, 'from nca47.api.controllers.v1 import tools\n'), ((3589, 3636), 'nca47.api.controllers.v1.tools.ret_info', 'tools.ret_info', (['self.response.status', 'e.message'], {}), '(self.response.status, e.message)\n', (3603, 3636), False, 'from nca47.api.controllers.v1 import tools\n'), ((3771, 3822), 'nca47.common.exception.BadRequest', 'BadRequest', ([], {'resource': '"""SecurityZone getAll"""', 'msg': 'url'}), "(resource='SecurityZone getAll', msg=url)\n", (3781, 3822), False, 'from nca47.common.exception import BadRequest\n'), ((4175, 4270), 'nca47.common.i18n._LI', '_LI', (['"""get_all the SecurityZone values with dic format                         is %(json)s"""'], {}), "('get_all the SecurityZone values with dic format                         is %(json)s'\n    )\n", (4178, 4270), False, 'from nca47.common.i18n import _LI, _LE\n'), ((4653, 4686), 'nca47.api.controllers.v1.tools.ret_info', 'tools.ret_info', (['e.code', 'e.message'], {}), '(e.code, e.message)\n', (4667, 4686), False, 'from nca47.api.controllers.v1 import tools\n'), ((4824, 4869), 'nca47.api.controllers.v1.tools.ret_info', 'tools.ret_info', (['self.response.status', 'message'], {}), '(self.response.status, message)\n', (4838, 4869), False, 'from nca47.api.controllers.v1 import tools\n'), ((4988, 5035), 'nca47.api.controllers.v1.tools.ret_info', 'tools.ret_info', (['self.response.status', 'e.message'], {}), '(self.response.status, e.message)\n', (5002, 5035), False, 'from nca47.api.controllers.v1 import tools\n'), ((5170, 5218), 'nca47.common.exception.BadRequest', 'BadRequest', ([], {'resource': '"""SecurityZone get"""', 'msg': 'url'}), "(resource='SecurityZone get', msg=url)\n", (5180, 5218), False, 'from nca47.common.exception import BadRequest\n'), ((5487, 5578), 'nca47.common.i18n._LI', '_LI', (['"""get the SecurityZone values with dic format                         is %(json)s"""'], {}), "('get the SecurityZone values with dic format                         is %(json)s'\n    )\n", (5490, 5578), False, 'from nca47.common.i18n import _LI, _LE\n'), ((5960, 5993), 'nca47.api.controllers.v1.tools.ret_info', 'tools.ret_info', (['e.code', 'e.message'], {}), '(e.code, e.message)\n', (5974, 5993), False, 'from nca47.api.controllers.v1 import tools\n'), ((6131, 6176), 'nca47.api.controllers.v1.tools.ret_info', 'tools.ret_info', (['self.response.status', 'message'], {}), '(self.response.status, message)\n', (6145, 6176), False, 'from nca47.api.controllers.v1 import tools\n'), ((6295, 6342), 'nca47.api.controllers.v1.tools.ret_info', 'tools.ret_info', (['self.response.status', 'e.message'], {}), '(self.response.status, e.message)\n', (6309, 6342), False, 'from nca47.api.controllers.v1 import tools\n'), ((6478, 6531), 'nca47.common.exception.BadRequest', 'BadRequest', ([], {'resource': '"""SecurityZone add vlan"""', 'msg': 'url'}), "(resource='SecurityZone add vlan', msg=url)\n", (6488, 6531), False, 'from nca47.common.exception import BadRequest\n'), ((6887, 6981), 'nca47.common.i18n._LI', '_LI', (['"""input the SecurityZone values with dic formatO is                         %(json)s"""'], {}), "('input the SecurityZone values with dic formatO is                         %(json)s'\n    )\n", (6890, 6981), False, 'from nca47.common.i18n import _LI, _LE\n'), ((7173, 7210), 'nca47.common.exception.ParamFormatError', 'ParamFormatError', ([], {'param_name': '"""ifname"""'}), "(param_name='ifname')\n", (7189, 7210), False, 'from nca47.common.exception import ParamFormatError\n'), ((7409, 7439), 'nca47.api.controllers.v1.tools.ret_info', 'tools.ret_info', (['"""400"""', 'message'], {}), "('400', message)\n", (7423, 7439), False, 'from nca47.api.controllers.v1 import tools\n'), ((7885, 7918), 'nca47.api.controllers.v1.tools.ret_info', 'tools.ret_info', (['e.code', 'e.message'], {}), '(e.code, e.message)\n', (7899, 7918), False, 'from nca47.api.controllers.v1 import tools\n'), ((8056, 8101), 'nca47.api.controllers.v1.tools.ret_info', 'tools.ret_info', (['self.response.status', 'message'], {}), '(self.response.status, message)\n', (8070, 8101), False, 'from nca47.api.controllers.v1 import tools\n'), ((8220, 8267), 'nca47.api.controllers.v1.tools.ret_info', 'tools.ret_info', (['self.response.status', 'e.message'], {}), '(self.response.status, e.message)\n', (8234, 8267), False, 'from nca47.api.controllers.v1 import tools\n'), ((8403, 8456), 'nca47.common.exception.BadRequest', 'BadRequest', ([], {'resource': '"""SecurityZone del vlan"""', 'msg': 'url'}), "(resource='SecurityZone del vlan', msg=url)\n", (8413, 8456), False, 'from nca47.common.exception import BadRequest\n'), ((8812, 8905), 'nca47.common.i18n._LI', '_LI', (['"""input the SecurityZone values with dic format                         is %(json)s"""'], {}), "('input the SecurityZone values with dic format                         is %(json)s'\n    )\n", (8815, 8905), False, 'from nca47.common.i18n import _LI, _LE\n'), ((9097, 9134), 'nca47.common.exception.ParamFormatError', 'ParamFormatError', ([], {'param_name': '"""ifname"""'}), "(param_name='ifname')\n", (9113, 9134), False, 'from nca47.common.exception import ParamFormatError\n'), ((9333, 9363), 'nca47.api.controllers.v1.tools.ret_info', 'tools.ret_info', (['"""400"""', 'message'], {}), "('400', message)\n", (9347, 9363), False, 'from nca47.api.controllers.v1 import tools\n'), ((9809, 9842), 'nca47.api.controllers.v1.tools.ret_info', 'tools.ret_info', (['e.code', 'e.message'], {}), '(e.code, e.message)\n', (9823, 9842), False, 'from nca47.api.controllers.v1 import tools\n'), ((9980, 10025), 'nca47.api.controllers.v1.tools.ret_info', 'tools.ret_info', (['self.response.status', 'message'], {}), '(self.response.status, message)\n', (9994, 10025), False, 'from nca47.api.controllers.v1 import tools\n'), ((10144, 10191), 'nca47.api.controllers.v1.tools.ret_info', 'tools.ret_info', (['self.response.status', 'e.message'], {}), '(self.response.status, e.message)\n', (10158, 10191), False, 'from nca47.api.controllers.v1 import tools\n'), ((1757, 1806), 'nca47.common.i18n._LE', '_LE', (["('Error exception! error info: %' + e.message)"], {}), "('Error exception! error info: %' + e.message)\n", (1760, 1806), False, 'from nca47.common.i18n import _LI, _LE\n'), ((3113, 3162), 'nca47.common.i18n._LE', '_LE', (["('Error exception! error info: %' + e.message)"], {}), "('Error exception! error info: %' + e.message)\n", (3116, 3162), False, 'from nca47.common.i18n import _LI, _LE\n'), ((4512, 4561), 'nca47.common.i18n._LE', '_LE', (["('Error exception! error info: %' + e.message)"], {}), "('Error exception! error info: %' + e.message)\n", (4515, 4561), False, 'from nca47.common.i18n import _LI, _LE\n'), ((5819, 5868), 'nca47.common.i18n._LE', '_LE', (["('Error exception! error info: %' + e.message)"], {}), "('Error exception! error info: %' + e.message)\n", (5822, 5868), False, 'from nca47.common.i18n import _LI, _LE\n'), ((7744, 7793), 'nca47.common.i18n._LE', '_LE', (["('Error exception! error info: %' + e.message)"], {}), "('Error exception! error info: %' + e.message)\n", (7747, 7793), False, 'from nca47.common.i18n import _LI, _LE\n'), ((9668, 9717), 'nca47.common.i18n._LE', '_LE', (["('Error exception! error info: %' + e.message)"], {}), "('Error exception! error info: %' + e.message)\n", (9671, 9717), False, 'from nca47.common.i18n import _LI, _LE\n')]