1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements.  See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership.  The ASF licenses this file
// to you 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.

use std::collections::hash_map::{IntoIter, Iter, IterMut};
use std::collections::HashMap;

use serde::{Deserialize, Serialize};
use url::Url;
use uuid::Uuid;

use crate::{
    Executor, ExecutorType, FileAuthTag, FileCrypto, FunctionArguments, Storable,
    TeaclaveInputFile, TeaclaveOutputFile,
};

const STAGED_TASK_PREFIX: &str = "staged-"; // staged-task-uuid
pub const QUEUE_KEY: &str = "staged-task";

#[derive(Debug, Deserialize, Serialize, Clone, Default)]
pub struct FunctionInputFiles {
    inner: HashMap<String, FunctionInputFile>,
}

impl FunctionInputFiles {
    pub fn new(entries: HashMap<String, FunctionInputFile>) -> Self {
        entries.into()
    }
    pub fn iter(&self) -> Iter<String, FunctionInputFile> {
        self.inner.iter()
    }
}

impl IntoIterator for FunctionInputFiles {
    type Item = (String, FunctionInputFile);
    type IntoIter = IntoIter<String, FunctionInputFile>;

    fn into_iter(self) -> IntoIter<String, FunctionInputFile> {
        self.inner.into_iter()
    }
}

impl<V> std::iter::FromIterator<(String, V)> for FunctionInputFiles
where
    V: Into<FunctionInputFile>,
{
    fn from_iter<T: IntoIterator<Item = (String, V)>>(iter: T) -> Self {
        FunctionInputFiles {
            inner: iter.into_iter().map(|(k, v)| (k, v.into())).collect(),
        }
    }
}

impl std::convert::From<HashMap<String, FunctionInputFile>> for FunctionInputFiles {
    fn from(entries: HashMap<String, FunctionInputFile>) -> FunctionInputFiles {
        entries.into_iter().collect()
    }
}

#[derive(Debug, Deserialize, Serialize, Clone, Default)]
pub struct FunctionOutputFiles {
    inner: HashMap<String, FunctionOutputFile>,
}

impl IntoIterator for FunctionOutputFiles {
    type Item = (String, FunctionOutputFile);
    type IntoIter = IntoIter<String, FunctionOutputFile>;

    fn into_iter(self) -> IntoIter<String, FunctionOutputFile> {
        self.inner.into_iter()
    }
}

impl<V> std::iter::FromIterator<(String, V)> for FunctionOutputFiles
where
    V: Into<FunctionOutputFile>,
{
    fn from_iter<T: IntoIterator<Item = (String, V)>>(iter: T) -> Self {
        FunctionOutputFiles {
            inner: iter.into_iter().map(|(k, v)| (k, v.into())).collect(),
        }
    }
}

impl std::convert::From<HashMap<String, FunctionOutputFile>> for FunctionOutputFiles {
    fn from(entries: HashMap<String, FunctionOutputFile>) -> FunctionOutputFiles {
        entries.into_iter().collect()
    }
}

impl FunctionOutputFiles {
    pub fn new(entries: HashMap<String, FunctionOutputFile>) -> Self {
        entries.into()
    }

    pub fn iter(&self) -> Iter<String, FunctionOutputFile> {
        self.inner.iter()
    }

    pub fn iter_mut(&mut self) -> IterMut<String, FunctionOutputFile> {
        self.inner.iter_mut()
    }

    pub fn len(&self) -> usize {
        self.inner.len()
    }

    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }
}

#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct FunctionInputFile {
    pub url: Url,
    pub cmac: FileAuthTag,
    pub crypto_info: FileCrypto,
}

impl FunctionInputFile {
    pub fn new(url: Url, cmac: FileAuthTag, crypto: impl Into<FileCrypto>) -> Self {
        Self {
            url,
            cmac,
            crypto_info: crypto.into(),
        }
    }
}

impl From<TeaclaveInputFile> for FunctionInputFile {
    fn from(file: TeaclaveInputFile) -> Self {
        Self {
            url: file.url,
            cmac: file.cmac,
            crypto_info: file.crypto_info,
        }
    }
}

#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct FunctionOutputFile {
    pub url: Url,
    pub crypto_info: FileCrypto,
}

impl FunctionOutputFile {
    pub fn new(url: Url, crypto: impl Into<FileCrypto>) -> Self {
        Self {
            url,
            crypto_info: crypto.into(),
        }
    }
}

impl From<TeaclaveOutputFile> for FunctionOutputFile {
    fn from(file: TeaclaveOutputFile) -> Self {
        Self {
            url: file.url,
            crypto_info: file.crypto_info,
        }
    }
}

#[derive(Debug, Default, Deserialize, Serialize)]
pub struct StagedTask {
    pub task_id: Uuid,
    pub function_id: Uuid,
    pub user_id: String,
    pub executor: Executor,
    pub executor_type: ExecutorType,
    pub function_name: String,
    pub function_arguments: FunctionArguments,
    pub function_payload: Vec<u8>,
    pub input_data: FunctionInputFiles,
    pub output_data: FunctionOutputFiles,
}

impl Storable for StagedTask {
    fn key_prefix() -> &'static str {
        STAGED_TASK_PREFIX
    }

    fn uuid(&self) -> Uuid {
        self.task_id
    }
}

impl StagedTask {
    pub fn get_queue_key() -> &'static str {
        QUEUE_KEY
    }
}

#[derive(Default)]
pub struct StagedTaskBuilder {
    task: StagedTask,
}

impl StagedTaskBuilder {
    pub fn new() -> Self {
        Self {
            task: StagedTask::default(),
        }
    }

    pub fn task_id(mut self, task_id: Uuid) -> Self {
        self.task.task_id = task_id;
        self
    }

    pub fn function_id(mut self, function_id: Uuid) -> Self {
        self.task.function_id = function_id;
        self
    }

    pub fn user_id(mut self, user_id: impl ToString) -> Self {
        self.task.user_id = user_id.to_string();
        self
    }

    pub fn executor(mut self, executor: Executor) -> Self {
        self.task.executor = executor;
        self
    }

    pub fn function_name(mut self, name: impl ToString) -> Self {
        self.task.function_name = name.to_string();
        self
    }

    pub fn function_arguments(mut self, function_arguments: impl Into<FunctionArguments>) -> Self {
        self.task.function_arguments = function_arguments.into();
        self
    }

    pub fn function_payload(mut self, function_payload: Vec<u8>) -> Self {
        self.task.function_payload = function_payload;
        self
    }

    pub fn input_data(mut self, input_data: impl Into<FunctionInputFiles>) -> Self {
        self.task.input_data = input_data.into();
        self
    }

    pub fn output_data(mut self, output_data: impl Into<FunctionOutputFiles>) -> Self {
        self.task.output_data = output_data.into();
        self
    }

    pub fn executor_type(mut self, executor_type: ExecutorType) -> Self {
        self.task.executor_type = executor_type;
        self
    }

    pub fn build(self) -> StagedTask {
        self.task
    }
}