# Copyright (C) 2022 Fujitsu
# All Rights Reserved.
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

from tacker.sol_refactored.api import wsgi as sol_wsgi
from tacker.sol_refactored.common import config as cfg
from tacker.sol_refactored.common import exceptions as sol_ex
from tacker.sol_refactored.common import monitoring_plugin_base as mon_base

CONF = cfg.CONF


class ServerNotificationController(sol_wsgi.SolAPIController):
    def notify(self, request, vnf_instance_id, server_id, body):
        if not CONF.server_notification.server_notification:
            raise sol_ex.ServerNotificationNotEnabled()
        cls = mon_base.get_class(
            CONF.server_notification.server_notification_package,
            CONF.server_notification.server_notification_class)
        mon_base.MonitoringPlugin.get_instance(cls).alert(
            request=request, vnf_instance_id=vnf_instance_id,
            server_id=server_id, body=body)
        return sol_wsgi.SolResponse(204, None)
