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)

[((15, 6, 15, 29), 'oslo_log.log.getLogger', 'log.getLogger', ({(15, 20, 15, 28): '__name__'}, {}), '(__name__)', False, 'from oslo_log import log\n'), ((20, 23, 20, 52), 'nca47.manager.central.CentralManager.get_instance', 'CentralManager.get_instance', ({}, {}), '()', False, 'from nca47.manager.central import CentralManager\n'), ((29, 26, 29, 46), 'oslo_serialization.jsonutils.loads', 'json.loads', ({(29, 37, 29, 45): 'req.body'}, {}), '(req.body)', True, 'from oslo_serialization import jsonutils as json\n'), ((32, 21, 32, 72), 'nca47.api.controllers.v1.tools.validat_values', 'tools.validat_values', ({(32, 42, 32, 53): 'body_values', (32, 55, 32, 71): 'valid_attributes'}, {}), '(body_values, valid_attributes)', False, 'from nca47.api.controllers.v1 import tools\n'), ((62, 26, 62, 46), 'oslo_serialization.jsonutils.loads', 'json.loads', ({(62, 37, 62, 45): 'req.body'}, {}), '(req.body)', True, 'from oslo_serialization import jsonutils as json\n'), ((64, 21, 64, 72), 'nca47.api.controllers.v1.tools.validat_values', 'tools.validat_values', ({(64, 42, 64, 53): 'body_values', (64, 55, 64, 71): 'valid_attributes'}, {}), '(body_values, valid_attributes)', False, 'from nca47.api.controllers.v1 import tools\n'), ((92, 26, 92, 46), 'oslo_serialization.jsonutils.loads', 'json.loads', ({(92, 37, 92, 45): 'req.body'}, {}), '(req.body)', True, 'from oslo_serialization import jsonutils as json\n'), ((95, 21, 95, 72), 'nca47.api.controllers.v1.tools.validat_values', 'tools.validat_values', ({(95, 42, 95, 53): 'body_values', (95, 55, 95, 71): 'valid_attributes'}, {}), '(body_values, valid_attributes)', False, 'from nca47.api.controllers.v1 import tools\n'), ((123, 26, 123, 46), 'oslo_serialization.jsonutils.loads', 'json.loads', ({(123, 37, 123, 45): 'req.body'}, {}), '(req.body)', True, 'from oslo_serialization import jsonutils as json\n'), ((125, 21, 125, 72), 'nca47.api.controllers.v1.tools.validat_values', 'tools.validat_values', ({(125, 42, 125, 53): 'body_values', (125, 55, 125, 71): 'valid_attributes'}, {}), '(body_values, valid_attributes)', False, 'from nca47.api.controllers.v1 import tools\n'), ((153, 26, 153, 46), 'oslo_serialization.jsonutils.loads', 'json.loads', ({(153, 37, 153, 45): 'req.body'}, {}), '(req.body)', True, 'from oslo_serialization import jsonutils as json\n'), ((156, 21, 156, 72), 'nca47.api.controllers.v1.tools.validat_values', 'tools.validat_values', ({(156, 42, 156, 53): 'body_values', (156, 55, 156, 71): 'valid_attributes'}, {}), '(body_values, valid_attributes)', False, 'from nca47.api.controllers.v1 import tools\n'), ((193, 26, 193, 46), 'oslo_serialization.jsonutils.loads', 'json.loads', ({(193, 37, 193, 45): 'req.body'}, {}), '(req.body)', True, 'from oslo_serialization import jsonutils as json\n'), ((196, 21, 196, 72), 'nca47.api.controllers.v1.tools.validat_values', 'tools.validat_values', ({(196, 42, 196, 53): 'body_values', (196, 55, 196, 71): 'valid_attributes'}, {}), '(body_values, valid_attributes)', False, 'from nca47.api.controllers.v1 import tools\n'), ((27, 22, 27, 73), 'nca47.common.exception.BadRequest', 'BadRequest', (), '', False, 'from nca47.common.exception import BadRequest\n'), ((33, 21, 34, 33), 'nca47.common.i18n._LI', '_LI', ({(33, 25, 34, 32): '"""input the SecurityZone values with dic format                     is %(json)s"""'}, {}), "('input the SecurityZone values with dic format                     is %(json)s'\n    )", False, 'from nca47.common.i18n import _LI, _LE\n'), ((46, 19, 46, 52), 'nca47.api.controllers.v1.tools.ret_info', 'tools.ret_info', ({(46, 34, 46, 40): 'e.code', (46, 42, 46, 51): 'e.message'}, {}), '(e.code, e.message)', False, 'from nca47.api.controllers.v1 import tools\n'), ((50, 19, 50, 64), 'nca47.api.controllers.v1.tools.ret_info', 'tools.ret_info', ({(50, 34, 50, 54): 'self.response.status', (50, 56, 50, 63): 'message'}, {}), '(self.response.status, message)', False, 'from nca47.api.controllers.v1 import tools\n'), ((54, 19, 54, 66), 'nca47.api.controllers.v1.tools.ret_info', 'tools.ret_info', ({(54, 34, 54, 54): 'self.response.status', (54, 56, 54, 65): 'e.message'}, {}), '(self.response.status, e.message)', False, 'from nca47.api.controllers.v1 import tools\n'), ((60, 22, 60, 70), 'nca47.common.exception.BadRequest', 'BadRequest', (), '', False, 'from nca47.common.exception import BadRequest\n'), ((66, 21, 67, 37), 'nca47.common.i18n._LI', '_LI', ({(66, 25, 67, 36): '"""delete the SecurityZone values with dic forma                         is %(json)s"""'}, {}), "('delete the SecurityZone values with dic forma                         is %(json)s'\n    )", False, 'from nca47.common.i18n import _LI, _LE\n'), ((76, 19, 76, 52), 'nca47.api.controllers.v1.tools.ret_info', 'tools.ret_info', ({(76, 34, 76, 40): 'e.code', (76, 42, 76, 51): 'e.message'}, {}), '(e.code, e.message)', False, 'from nca47.api.controllers.v1 import tools\n'), ((80, 19, 80, 64), 'nca47.api.controllers.v1.tools.ret_info', 'tools.ret_info', ({(80, 34, 80, 54): 'self.response.status', (80, 56, 80, 63): 'message'}, {}), '(self.response.status, message)', False, 'from nca47.api.controllers.v1 import tools\n'), ((84, 19, 84, 66), 'nca47.api.controllers.v1.tools.ret_info', 'tools.ret_info', ({(84, 34, 84, 54): 'self.response.status', (84, 56, 84, 65): 'e.message'}, {}), '(self.response.status, e.message)', False, 'from nca47.api.controllers.v1 import tools\n'), ((90, 22, 90, 73), 'nca47.common.exception.BadRequest', 'BadRequest', (), '', False, 'from nca47.common.exception import BadRequest\n'), ((97, 21, 98, 37), 'nca47.common.i18n._LI', '_LI', ({(97, 25, 98, 36): '"""get_all the SecurityZone values with dic format                         is %(json)s"""'}, {}), "('get_all the SecurityZone values with dic format                         is %(json)s'\n    )", False, 'from nca47.common.i18n import _LI, _LE\n'), ((107, 19, 107, 52), 'nca47.api.controllers.v1.tools.ret_info', 'tools.ret_info', ({(107, 34, 107, 40): 'e.code', (107, 42, 107, 51): 'e.message'}, {}), '(e.code, e.message)', False, 'from nca47.api.controllers.v1 import tools\n'), ((111, 19, 111, 64), 'nca47.api.controllers.v1.tools.ret_info', 'tools.ret_info', ({(111, 34, 111, 54): 'self.response.status', (111, 56, 111, 63): 'message'}, {}), '(self.response.status, message)', False, 'from nca47.api.controllers.v1 import tools\n'), ((115, 19, 115, 66), 'nca47.api.controllers.v1.tools.ret_info', 'tools.ret_info', ({(115, 34, 115, 54): 'self.response.status', (115, 56, 115, 65): 'e.message'}, {}), '(self.response.status, e.message)', False, 'from nca47.api.controllers.v1 import tools\n'), ((121, 22, 121, 70), 'nca47.common.exception.BadRequest', 'BadRequest', (), '', False, 'from nca47.common.exception import BadRequest\n'), ((127, 21, 128, 38), 'nca47.common.i18n._LI', '_LI', ({(127, 25, 128, 37): '"""get the SecurityZone values with dic format                         is %(json)s"""'}, {}), "('get the SecurityZone values with dic format                         is %(json)s'\n    )", False, 'from nca47.common.i18n import _LI, _LE\n'), ((137, 19, 137, 52), 'nca47.api.controllers.v1.tools.ret_info', 'tools.ret_info', ({(137, 34, 137, 40): 'e.code', (137, 42, 137, 51): 'e.message'}, {}), '(e.code, e.message)', False, 'from nca47.api.controllers.v1 import tools\n'), ((141, 19, 141, 64), 'nca47.api.controllers.v1.tools.ret_info', 'tools.ret_info', ({(141, 34, 141, 54): 'self.response.status', (141, 56, 141, 63): 'message'}, {}), '(self.response.status, message)', False, 'from nca47.api.controllers.v1 import tools\n'), ((145, 19, 145, 66), 'nca47.api.controllers.v1.tools.ret_info', 'tools.ret_info', ({(145, 34, 145, 54): 'self.response.status', (145, 56, 145, 65): 'e.message'}, {}), '(self.response.status, e.message)', False, 'from nca47.api.controllers.v1 import tools\n'), ((151, 22, 151, 75), 'nca47.common.exception.BadRequest', 'BadRequest', (), '', False, 'from nca47.common.exception import BadRequest\n'), ((158, 21, 159, 35), 'nca47.common.i18n._LI', '_LI', ({(158, 25, 159, 34): '"""input the SecurityZone values with dic formatO is                         %(json)s"""'}, {}), "('input the SecurityZone values with dic formatO is                         %(json)s'\n    )", False, 'from nca47.common.i18n import _LI, _LE\n'), ((163, 22, 163, 59), 'nca47.common.exception.ParamFormatError', 'ParamFormatError', (), '', False, 'from nca47.common.exception import ParamFormatError\n'), ((167, 23, 167, 53), 'nca47.api.controllers.v1.tools.ret_info', 'tools.ret_info', ({(167, 38, 167, 43): '"""400"""', (167, 45, 167, 52): 'message'}, {}), "('400', message)", False, 'from nca47.api.controllers.v1 import tools\n'), ((177, 19, 177, 52), 'nca47.api.controllers.v1.tools.ret_info', 'tools.ret_info', ({(177, 34, 177, 40): 'e.code', (177, 42, 177, 51): 'e.message'}, {}), '(e.code, e.message)', False, 'from nca47.api.controllers.v1 import tools\n'), ((181, 19, 181, 64), 'nca47.api.controllers.v1.tools.ret_info', 'tools.ret_info', ({(181, 34, 181, 54): 'self.response.status', (181, 56, 181, 63): 'message'}, {}), '(self.response.status, message)', False, 'from nca47.api.controllers.v1 import tools\n'), ((185, 19, 185, 66), 'nca47.api.controllers.v1.tools.ret_info', 'tools.ret_info', ({(185, 34, 185, 54): 'self.response.status', (185, 56, 185, 65): 'e.message'}, {}), '(self.response.status, e.message)', False, 'from nca47.api.controllers.v1 import tools\n'), ((191, 22, 191, 75), 'nca47.common.exception.BadRequest', 'BadRequest', (), '', False, 'from nca47.common.exception import BadRequest\n'), ((198, 21, 199, 38), 'nca47.common.i18n._LI', '_LI', ({(198, 25, 199, 37): '"""input the SecurityZone values with dic format                         is %(json)s"""'}, {}), "('input the SecurityZone values with dic format                         is %(json)s'\n    )", False, 'from nca47.common.i18n import _LI, _LE\n'), ((203, 22, 203, 59), 'nca47.common.exception.ParamFormatError', 'ParamFormatError', (), '', False, 'from nca47.common.exception import ParamFormatError\n'), ((207, 23, 207, 53), 'nca47.api.controllers.v1.tools.ret_info', 'tools.ret_info', ({(207, 38, 207, 43): '"""400"""', (207, 45, 207, 52): 'message'}, {}), "('400', message)", False, 'from nca47.api.controllers.v1 import tools\n'), ((217, 19, 217, 52), 'nca47.api.controllers.v1.tools.ret_info', 'tools.ret_info', ({(217, 34, 217, 40): 'e.code', (217, 42, 217, 51): 'e.message'}, {}), '(e.code, e.message)', False, 'from nca47.api.controllers.v1 import tools\n'), ((221, 19, 221, 64), 'nca47.api.controllers.v1.tools.ret_info', 'tools.ret_info', ({(221, 34, 221, 54): 'self.response.status', (221, 56, 221, 63): 'message'}, {}), '(self.response.status, message)', False, 'from nca47.api.controllers.v1 import tools\n'), ((225, 19, 225, 66), 'nca47.api.controllers.v1.tools.ret_info', 'tools.ret_info', ({(225, 34, 225, 54): 'self.response.status', (225, 56, 225, 65): 'e.message'}, {}), '(self.response.status, e.message)', False, 'from nca47.api.controllers.v1 import tools\n'), ((43, 22, 43, 71), 'nca47.common.i18n._LE', '_LE', ({(43, 26, 43, 70): "('Error exception! error info: %' + e.message)"}, {}), "('Error exception! error info: %' + e.message)", False, 'from nca47.common.i18n import _LI, _LE\n'), ((73, 22, 73, 71), 'nca47.common.i18n._LE', '_LE', ({(73, 26, 73, 70): "('Error exception! error info: %' + e.message)"}, {}), "('Error exception! error info: %' + e.message)", False, 'from nca47.common.i18n import _LI, _LE\n'), ((104, 22, 104, 71), 'nca47.common.i18n._LE', '_LE', ({(104, 26, 104, 70): "('Error exception! error info: %' + e.message)"}, {}), "('Error exception! error info: %' + e.message)", False, 'from nca47.common.i18n import _LI, _LE\n'), ((134, 22, 134, 71), 'nca47.common.i18n._LE', '_LE', ({(134, 26, 134, 70): "('Error exception! error info: %' + e.message)"}, {}), "('Error exception! error info: %' + e.message)", False, 'from nca47.common.i18n import _LI, _LE\n'), ((174, 22, 174, 71), 'nca47.common.i18n._LE', '_LE', ({(174, 26, 174, 70): "('Error exception! error info: %' + e.message)"}, {}), "('Error exception! error info: %' + e.message)", False, 'from nca47.common.i18n import _LI, _LE\n'), ((214, 22, 214, 71), 'nca47.common.i18n._LE', '_LE', ({(214, 26, 214, 70): "('Error exception! error info: %' + e.message)"}, {}), "('Error exception! error info: %' + e.message)", False, 'from nca47.common.i18n import _LI, _LE\n')]