# -*- coding: utf-8 -*-
#
# 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.

import os
from oslo_log import log as logging
import oslo_messaging as messaging
from oslo_service import periodic_task

from cyborg.accelerator.drivers.fpga.base import FPGADriver
from cyborg.agent.resource_tracker import ResourceTracker
from cyborg.agent.rpcapi import AgentAPI
from cyborg.conductor import rpcapi as cond_api
from cyborg.conf import CONF
from cyborg.image.api import API as ImageAPI


LOG = logging.getLogger(__name__)


class AgentManager(periodic_task.PeriodicTasks):
    """Cyborg Agent manager main class."""

    RPC_API_VERSION = '1.0'
    target = messaging.Target(version=RPC_API_VERSION)

    def __init__(self, topic, host=None):
        super(AgentManager, self).__init__(CONF)
        self.topic = topic
        self.host = host or CONF.host
        self.fpga_driver = FPGADriver()
        self.cond_api = cond_api.ConductorAPI()
        self.agent_api = AgentAPI()
        self.image_api = ImageAPI()
        self._rt = ResourceTracker(host, self.cond_api)

    def periodic_tasks(self, context, raise_on_error=False):
        return self.run_periodic_tasks(context, raise_on_error=raise_on_error)

    def fpga_program_v2(self, context, controlpath_id,
                        bitstream_uuid, driver_name):
        # TODO() Use tempfile module?
        download_path = "/tmp/" + bitstream_uuid + ".gbs"
        self.image_api.download(context,
                                bitstream_uuid,
                                dest_path=download_path)
        driver = self.fpga_driver.create(driver_name)
        ret = driver.program_v2(controlpath_id, download_path)
        LOG.info('Driver program() API returned code %s', ret)
        os.remove(download_path)

    @periodic_task.periodic_task(run_immediately=True)
    def update_available_resource(self, context, startup=True):
        """Update all kinds of accelerator resources from their drivers."""
        self._rt.update_usage(context)

@@ -575,16 +575,33 @@
 port os%0A
+import tempfile%0A%0A
 from osl
@@ -701,16 +701,49 @@
 dic_task
+%0Afrom oslo_utils import uuidutils
 %0A%0Afrom c
@@ -895,16 +895,52 @@
 gentAPI%0A
+from cyborg.common import exception%0A
 from cyb
@@ -1903,72 +1903,287 @@
     
-# TODO() Use tempfile module?%0A        download_path = %22/tmp/%22 + 
+bitstream_uuid = str(bitstream_uuid)%0A        if not uuidutils.is_uuid_like(bitstream_uuid):%0A            raise exception.InvalidUUID(uuid=bitstream_uuid)%0A        download_path = tempfile.NamedTemporaryFile(suffix=%22.gbs%22,%0A                                                    prefix=
 bits
@@ -2196,17 +2196,9 @@
 uuid
- + %22.gbs%22
+)
 %0A   
@@ -2330,32 +2330,37 @@
 th=download_path
+.name
 )%0A        driver
@@ -2460,16 +2460,21 @@
 oad_path
+.name
 )%0A      
@@ -2561,16 +2561,21 @@
 oad_path
+.name
 )%0A%0A    @

Fix bandit error: [B108:hardcoded_tmp_directory]