Skip to content

vAPICall

Bases: vSoftwareEntity

A vAPICall is a software entity that represents an API call.

Source code in PyCloudSim\entity\v_apicall.py
 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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
class vAPICall(vSoftwareEntity):
    """A vAPICall is a software entity that represents an API call."""

    def __init__(
        self,
        src: vMicroservice | vUser,
        dst: vMicroservice | vUser,
        priority: int | Callable[..., int],
        src_process_length: int | Callable[..., int],
        dst_process_length: int | Callable[..., int],
        ack_process_length: int | Callable[..., int],
        num_src_packets: int | Callable[..., int],
        src_packet_size: int | Callable[..., int],
        num_ret_packets: int | Callable[..., int],
        ret_packet_size: int | Callable[..., int],
        num_ack_packets: int | Callable[..., int],
        ack_packet_size: int | Callable[..., int],
        label: str | None = None,
        create_at: int
        | float
        | Callable[..., int]
        | Callable[..., float]
        | None = None,
        terminate_at: int
        | float
        | Callable[..., int]
        | Callable[..., float]
        | None = None,
        precursor: Entity | List[Entity] | None = None,
    ) -> None:
        """Create a new vAPICall.

        Args:
            src (vMicroservice | vUser): the source of the API call.
            dst (vMicroservice | vUser): the destination of the API call.
            priority (int | Callable[..., int]): the priority of the API call.
            src_process_length (int | Callable[..., int]): the length of the source process.
            dst_process_length (int | Callable[..., int]): the length of the destination process.
            ack_process_length (int | Callable[..., int]): the length of the ack process.
            num_src_packets (int | Callable[..., int]): the number of packets from source.
            src_packet_size (int | Callable[..., int]): the size of the packets from source.
            num_ret_packets (int | Callable[..., int]): the number of packets returned from destination.
            ret_packet_size (int | Callable[..., int]): the size of the packets returned from destination.
            num_ack_packets (int | Callable[..., int]): the number of packets from source for ACK.
            ack_packet_size (int | Callable[..., int]): the size of the packets from source for ACK.
            label (str | None, optional): short description of this vAPICall. Defaults to None.
            create_at (int | float | Callable[..., int] | Callable[..., float] | None, optional): when this vAPICall should be created. Defaults to None.
            terminate_at (int | float | Callable[..., int] | Callable[..., float] | None, optional): when this vAPICall shoudl be terminated. Defaults to None.
            precursor (Entity | List[Entity] | None, optional): the precursor of this vAPICall. Defaults to None.
        """
        super().__init__(label, create_at, terminate_at, precursor)
        self._src = src
        self._dst = dst
        self._type = type
        if callable(priority):
            self._priority = round(priority())
        else:
            self._priority = priority
        if callable(src_process_length):
            self._src_process_length = round(src_process_length())
        else:
            self._src_process_length = src_process_length
        if callable(dst_process_length):
            self._dst_process_length = round(dst_process_length())
        else:
            self._dst_process_length = dst_process_length
        if callable(ack_process_length):
            self._ack_process_length = round(ack_process_length())
        else:
            self._ack_process_length = ack_process_length
        if callable(num_src_packets):
            self._num_src_packets = round(num_src_packets())
        else:
            self._num_src_packets = num_src_packets
        if callable(src_packet_size):
            self._src_packet_size = round(src_packet_size())
        else:
            self._src_packet_size = src_packet_size
        if callable(num_ret_packets):
            self._num_ret_packets = round(num_ret_packets())
        else:
            self._num_ret_packets = num_ret_packets
        if callable(ret_packet_size):
            self._ret_packet_size = round(ret_packet_size())
        else:
            self._ret_packet_size = ret_packet_size
        if callable(num_ack_packets):
            self._num_ack_packets = round(num_ack_packets())
        else:
            self._num_ack_packets = num_ack_packets
        if callable(ack_packet_size):
            self._ack_packet_size = round(ack_packet_size())
        else:
            self._ack_packet_size = ack_packet_size

        self._packets: List[vPacket] = [] # EntityList(label=f"{self} Packets")
        self._processes: List[vContainerProcess] = [] # EntityList(label=f"{self} Processes")
        simulation.api_calls.append(self)

    def on_creation(self):
        super().on_creation()

    def on_initiate(self) -> None:
        """Initiate the vAPICall.
        """
        super().on_initiate()
        if isinstance(self.src, vUser) and isinstance(self.dst, vUser):
            if self.src_process_length > 0:
                logger.warning(
                    f"{simulation.now}:\t{self} source is a vUser, src_instruction_length is ignored."
                )
            if self.dst_process_length > 0:
                logger.warning(
                    f"{simulation.now}:\t{self} destination is a vUser, dst_instruction_length is ignored."
                )
            if self.ack_process_length > 0:
                logger.warning(
                    f"{simulation.now}:\t{self} source is a vUser, ack_instruction_length is ignored."
                )

            if self.num_src_packets <= 0:
                logger.error(
                    f"{simulation.now}:\t{self} invalid vAPICall configuration."
                )
                self.terminate(simulation.now)
                return

            src_packets: List[Entity] = list()
            for i in range(self.num_src_packets):
                src_packet = vPacket(
                    src=self.src,
                    dst=self.dst,
                    size=self.src_packet_size,
                    priority=self.priority,
                    create_at=simulation.now,
                    label=f"{self}-SRC-{i}",
                )
                self.packets.append(src_packet)
                src_packets.append(src_packet)

            ret_packets: List[Entity] = list()
            for i in range(self.num_ret_packets):
                ret_packet = vPacket(
                    src=self.dst,
                    dst=self.src,
                    size=self.ret_packet_size,
                    priority=self.priority,
                    create_at=simulation.now,
                    label=f"{self}-RET-{i}",
                    precursor=src_packets,
                )
                self.packets.append(ret_packet)
                ret_packets.append(ret_packet)

            ack_packets: List[Entity] = list()
            for i in range(self.num_ack_packets):
                ack_packet = vPacket(
                    src=self.src,
                    dst=self.dst,
                    size=self.ack_packet_size,
                    priority=self.priority,
                    create_at=simulation.now,
                    label=f"{self}-ACK-{i}",
                    precursor=ret_packets,
                )
                self.packets.append(ack_packet)
                ack_packets.append(ack_packet)

        elif isinstance(self.src, vUser) and not isinstance(self.dst, vUser):
            if self.src_process_length > 0:
                logger.warning(
                    f"{simulation.now}:\t{self} source is a vUser, src_instruction_length is ignored."
                )
            if self.ack_process_length > 0:
                logger.warning(
                    f"{simulation.now}:\t{self} source is a vUser, ack_instruction_length is ignored."
                )

            if self.num_src_packets <= 0:
                logger.error(
                    f"{simulation.now}:\t{self} invalid vAPICall configuration."
                )
                self.terminate(simulation.now)
                return

            dst_container = self.dst.getContainer()

            src_packets: List[Entity] = list()
            for i in range(self.num_src_packets):
                src_packet = vPacket(
                    src=self.src,
                    dst=dst_container,
                    size=self.src_packet_size,
                    priority=self.priority,
                    create_at=simulation.now,
                    label=f"{self}-SRC-{i}",
                )
                self.packets.append(src_packet)
                src_packets.append(src_packet)

            dst_process = vContainerProcess(
                container=self.dst.getContainer(),
                length=self.dst_process_length,
                priority=self.priority,
                precursor=src_packets,
                create_at=simulation.now,
                label=f"{self}-DST",
            )

            ret_packets: List[Entity] = list()
            for i in range(self.num_ret_packets):
                ret_packet = vPacket(
                    src=dst_container,
                    dst=self.src,
                    size=self.ret_packet_size,
                    priority=self.priority,
                    create_at=simulation.now,
                    label=f"{self}-RET-{i}",
                    precursor=dst_process,
                )
                self.packets.append(ret_packet)
                ret_packets.append(ret_packet)

            for i in range(self.num_ack_packets):
                ack_packet = vPacket(
                    src=self.src,
                    dst=dst_container,
                    size=self.ack_packet_size,
                    priority=self.priority,
                    create_at=simulation.now,
                    label=f"{self}-ACK-{i}",
                    precursor=ret_packets,
                )
                self.packets.append(ack_packet)

        elif not isinstance(self.src, vUser) and isinstance(self.dst, vUser):
            if self.dst_process_length > 0:
                logger.warning(
                    f"{simulation.now}:\t{self} destination is a vUser, dst_instruction_length is ignored."
                )

            src_container = self.src.getContainer()

            src_process = vContainerProcess(
                container=src_container,
                length=self.src_process_length,
                priority=self.priority,
                create_at=simulation.now,
                label=f"{self}-SRC",
            )
            self.processes.append(src_process)

            src_packets: List[Entity] = list()
            for i in range(self.num_ack_packets):
                src_packet = vPacket(
                    src=src_container,
                    dst=self.dst,
                    size=self.src_packet_size,
                    priority=self.priority,
                    create_at=simulation.now,
                    label=f"{self}-SRC-{i}",
                    precursor=src_process,
                )
                self.packets.append(src_packet)
                src_packets.append(src_packet)

            ret_packets: List[Entity] = list()
            for i in range(self.num_ret_packets):
                ret_packet = vPacket(
                    src=self.dst,
                    dst=src_container,
                    size=self.ret_packet_size,
                    priority=self.priority,
                    create_at=simulation.now,
                    label=f"{self}-RET-{i}",
                    precursor=src_packets,
                )
                self.packets.append(ret_packet)
                ret_packets.append(ret_packet)

            ack_process = vContainerProcess(
                container=src_container,
                length=self.ack_process_length,
                priority=self.priority,
                precursor=ret_packets,
                create_at=simulation.now,
                label=f"{self}-ACK",
            )
            self.processes.append(ack_process)

            for i in range(self.num_ack_packets):
                ack_packet = vPacket(
                    src=src_container,
                    dst=self.dst,
                    size=self.ack_packet_size,
                    priority=self.priority,
                    create_at=simulation.now,
                    label=f"{self}-ACK-{i}",
                    precursor=ack_process,
                )
                self.packets.append(ack_packet)

        elif not isinstance(self.src, vUser) and not isinstance(self.dst, vUser):

            src_container = self.src.getContainer()
            dst_container = self.dst.getContainer()

            src_process = vContainerProcess(
                container=src_container,
                length=self.src_process_length,
                priority=self.priority,
                create_at=simulation.now,
                label=f"{self}-SRC",
            )
            self.processes.append(src_process)

            src_packets: List[Entity] = list()
            for i in range(self.num_src_packets):
                src_packet = vPacket(
                    src=src_container,
                    dst=dst_container,
                    size=self.src_packet_size,
                    priority=self.priority,
                    create_at=simulation.now,
                    label=f"{self}-SRC-{i}",
                    precursor=src_process,
                )
                self.packets.append(src_packet)
                src_packets.append(src_packet)

            dst_process = vContainerProcess(
                container=dst_container,
                length=self.dst_process_length,
                priority=self.priority,
                precursor=src_packets,
                create_at=simulation.now,
                label=f"{self}-DST",
            )
            self.processes.append(dst_process)

            ret_packets: List[Entity] = list()
            for i in range(self.num_ret_packets):
                ret_packet = vPacket(
                    src=dst_container,
                    dst=src_container,
                    size=self.ret_packet_size,
                    priority=self.priority,
                    create_at=simulation.now,
                    label=f"{self}-RET-{i}",
                    precursor=dst_process,
                )
                self.packets.append(ret_packet)
                ret_packets.append(ret_packet)

            ack_process = vContainerProcess(
                container=src_container,
                length=self.ack_process_length,
                priority=self.priority,
                precursor=ret_packets,
                create_at=simulation.now,
                label=f"{self}-ACK",
            )
            self.processes.append(ack_process)

            for i in range(self.num_ack_packets):
                ack_packet = vPacket(
                    src=src_container,
                    dst=dst_container,
                    size=self.ack_packet_size,
                    priority=self.priority,
                    create_at=simulation.now,
                    label=f"{self}-ACK-{i}",
                    precursor=ack_process,
                )
                self.packets.append(ack_packet)

        @self.continuous_event(
            at=simulation.now,
            interval=simulation.min_time_unit,
            duration=inf,
            label=f"{self}-Monitor",
        )
        def monitoring():
            if any([process.failed for process in self.processes]) or any(
                [packet.failed for packet in self.packets]
            ):
                self.fail(simulation.now)
                return

            if all([process.succeed for process in self.processes]) and all(
                [packet.succeed for packet in self.packets]
            ):
                self.success(simulation.now)
                return

        logger.info(
            f"{simulation.now}:\t{self} is initiated {self} between {self.src} and {self.dst}."
        )

    def on_termination(self):
        super().on_termination()
        for process in self.processes:
            process.fail(simulation.now)
        logger.info(f"{simulation.now}:\t{self} terminated.")

    def on_destruction(self):
        super().on_termination()
        for process in self.processes:
            process.fail(simulation.now)

    def on_success(self):
        super().on_success()
        logger.info(f"{simulation.now}:\t{self} succeeded.")

    def on_fail(self):
        super().on_fail()
        logger.info(f"{simulation.now}:\t{self} failed.")

    @property
    def src(self) -> vMicroservice | vUser:
        """Return the source of the API call."""
        return self._src

    @property
    def dst(self) -> vMicroservice | vUser:
        """Return the destination of the API call."""
        return self._dst

    @property
    def priority(self) -> int:
        """Return the priority of the API call."""
        return self._priority

    @property
    def packets(self) -> List[vPacket]:
        """Return the packets of the API call."""
        return self._packets

    @property
    def processes(self) -> List[vContainerProcess]:
        """Return the processes of the API call."""
        return self._processes

    @property
    def src_process_length(self) -> int:
        """Return the src_instruction_length"""
        return self._src_process_length

    @property
    def dst_process_length(self) -> int:
        """Return the dst_instruction_length"""
        return self._dst_process_length

    @property
    def ack_process_length(self) -> int:
        """Return the ack_instruction_length"""
        return self._ack_process_length

    @property
    def num_src_packets(self) -> int:
        """Return the num_src_packets"""
        return self._num_src_packets

    @property
    def src_packet_size(self) -> int:
        """Return the src_packet_size"""
        return self._src_packet_size

    @property
    def num_ret_packets(self) -> int:
        """Return the num_ret_packets"""
        return self._num_ret_packets

    @property
    def ret_packet_size(self) -> int:
        """Return the ret_packet_size"""
        return self._ret_packet_size

    @property
    def num_ack_packets(self) -> int:
        """Return the num_ack_packets"""
        return self._num_ack_packets

    @property
    def ack_packet_size(self) -> int:
        """Return the ack_packet_size"""
        return self._ack_packet_size

ack_packet_size: int property

Return the ack_packet_size

ack_process_length: int property

Return the ack_instruction_length

dst: vMicroservice | vUser property

Return the destination of the API call.

dst_process_length: int property

Return the dst_instruction_length

num_ack_packets: int property

Return the num_ack_packets

num_ret_packets: int property

Return the num_ret_packets

num_src_packets: int property

Return the num_src_packets

packets: List[vPacket] property

Return the packets of the API call.

priority: int property

Return the priority of the API call.

processes: List[vContainerProcess] property

Return the processes of the API call.

ret_packet_size: int property

Return the ret_packet_size

src: vMicroservice | vUser property

Return the source of the API call.

src_packet_size: int property

Return the src_packet_size

src_process_length: int property

Return the src_instruction_length

__init__(src, dst, priority, src_process_length, dst_process_length, ack_process_length, num_src_packets, src_packet_size, num_ret_packets, ret_packet_size, num_ack_packets, ack_packet_size, label=None, create_at=None, terminate_at=None, precursor=None)

Create a new vAPICall.

Parameters:

Name Type Description Default
src vMicroservice | vUser

the source of the API call.

required
dst vMicroservice | vUser

the destination of the API call.

required
priority int | Callable[..., int]

the priority of the API call.

required
src_process_length int | Callable[..., int]

the length of the source process.

required
dst_process_length int | Callable[..., int]

the length of the destination process.

required
ack_process_length int | Callable[..., int]

the length of the ack process.

required
num_src_packets int | Callable[..., int]

the number of packets from source.

required
src_packet_size int | Callable[..., int]

the size of the packets from source.

required
num_ret_packets int | Callable[..., int]

the number of packets returned from destination.

required
ret_packet_size int | Callable[..., int]

the size of the packets returned from destination.

required
num_ack_packets int | Callable[..., int]

the number of packets from source for ACK.

required
ack_packet_size int | Callable[..., int]

the size of the packets from source for ACK.

required
label str | None

short description of this vAPICall. Defaults to None.

None
create_at int | float | Callable[..., int] | Callable[..., float] | None

when this vAPICall should be created. Defaults to None.

None
terminate_at int | float | Callable[..., int] | Callable[..., float] | None

when this vAPICall shoudl be terminated. Defaults to None.

None
precursor Entity | List[Entity] | None

the precursor of this vAPICall. Defaults to None.

None
Source code in PyCloudSim\entity\v_apicall.py
 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
def __init__(
    self,
    src: vMicroservice | vUser,
    dst: vMicroservice | vUser,
    priority: int | Callable[..., int],
    src_process_length: int | Callable[..., int],
    dst_process_length: int | Callable[..., int],
    ack_process_length: int | Callable[..., int],
    num_src_packets: int | Callable[..., int],
    src_packet_size: int | Callable[..., int],
    num_ret_packets: int | Callable[..., int],
    ret_packet_size: int | Callable[..., int],
    num_ack_packets: int | Callable[..., int],
    ack_packet_size: int | Callable[..., int],
    label: str | None = None,
    create_at: int
    | float
    | Callable[..., int]
    | Callable[..., float]
    | None = None,
    terminate_at: int
    | float
    | Callable[..., int]
    | Callable[..., float]
    | None = None,
    precursor: Entity | List[Entity] | None = None,
) -> None:
    """Create a new vAPICall.

    Args:
        src (vMicroservice | vUser): the source of the API call.
        dst (vMicroservice | vUser): the destination of the API call.
        priority (int | Callable[..., int]): the priority of the API call.
        src_process_length (int | Callable[..., int]): the length of the source process.
        dst_process_length (int | Callable[..., int]): the length of the destination process.
        ack_process_length (int | Callable[..., int]): the length of the ack process.
        num_src_packets (int | Callable[..., int]): the number of packets from source.
        src_packet_size (int | Callable[..., int]): the size of the packets from source.
        num_ret_packets (int | Callable[..., int]): the number of packets returned from destination.
        ret_packet_size (int | Callable[..., int]): the size of the packets returned from destination.
        num_ack_packets (int | Callable[..., int]): the number of packets from source for ACK.
        ack_packet_size (int | Callable[..., int]): the size of the packets from source for ACK.
        label (str | None, optional): short description of this vAPICall. Defaults to None.
        create_at (int | float | Callable[..., int] | Callable[..., float] | None, optional): when this vAPICall should be created. Defaults to None.
        terminate_at (int | float | Callable[..., int] | Callable[..., float] | None, optional): when this vAPICall shoudl be terminated. Defaults to None.
        precursor (Entity | List[Entity] | None, optional): the precursor of this vAPICall. Defaults to None.
    """
    super().__init__(label, create_at, terminate_at, precursor)
    self._src = src
    self._dst = dst
    self._type = type
    if callable(priority):
        self._priority = round(priority())
    else:
        self._priority = priority
    if callable(src_process_length):
        self._src_process_length = round(src_process_length())
    else:
        self._src_process_length = src_process_length
    if callable(dst_process_length):
        self._dst_process_length = round(dst_process_length())
    else:
        self._dst_process_length = dst_process_length
    if callable(ack_process_length):
        self._ack_process_length = round(ack_process_length())
    else:
        self._ack_process_length = ack_process_length
    if callable(num_src_packets):
        self._num_src_packets = round(num_src_packets())
    else:
        self._num_src_packets = num_src_packets
    if callable(src_packet_size):
        self._src_packet_size = round(src_packet_size())
    else:
        self._src_packet_size = src_packet_size
    if callable(num_ret_packets):
        self._num_ret_packets = round(num_ret_packets())
    else:
        self._num_ret_packets = num_ret_packets
    if callable(ret_packet_size):
        self._ret_packet_size = round(ret_packet_size())
    else:
        self._ret_packet_size = ret_packet_size
    if callable(num_ack_packets):
        self._num_ack_packets = round(num_ack_packets())
    else:
        self._num_ack_packets = num_ack_packets
    if callable(ack_packet_size):
        self._ack_packet_size = round(ack_packet_size())
    else:
        self._ack_packet_size = ack_packet_size

    self._packets: List[vPacket] = [] # EntityList(label=f"{self} Packets")
    self._processes: List[vContainerProcess] = [] # EntityList(label=f"{self} Processes")
    simulation.api_calls.append(self)

on_initiate()

Initiate the vAPICall.

Source code in PyCloudSim\entity\v_apicall.py
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
def on_initiate(self) -> None:
    """Initiate the vAPICall.
    """
    super().on_initiate()
    if isinstance(self.src, vUser) and isinstance(self.dst, vUser):
        if self.src_process_length > 0:
            logger.warning(
                f"{simulation.now}:\t{self} source is a vUser, src_instruction_length is ignored."
            )
        if self.dst_process_length > 0:
            logger.warning(
                f"{simulation.now}:\t{self} destination is a vUser, dst_instruction_length is ignored."
            )
        if self.ack_process_length > 0:
            logger.warning(
                f"{simulation.now}:\t{self} source is a vUser, ack_instruction_length is ignored."
            )

        if self.num_src_packets <= 0:
            logger.error(
                f"{simulation.now}:\t{self} invalid vAPICall configuration."
            )
            self.terminate(simulation.now)
            return

        src_packets: List[Entity] = list()
        for i in range(self.num_src_packets):
            src_packet = vPacket(
                src=self.src,
                dst=self.dst,
                size=self.src_packet_size,
                priority=self.priority,
                create_at=simulation.now,
                label=f"{self}-SRC-{i}",
            )
            self.packets.append(src_packet)
            src_packets.append(src_packet)

        ret_packets: List[Entity] = list()
        for i in range(self.num_ret_packets):
            ret_packet = vPacket(
                src=self.dst,
                dst=self.src,
                size=self.ret_packet_size,
                priority=self.priority,
                create_at=simulation.now,
                label=f"{self}-RET-{i}",
                precursor=src_packets,
            )
            self.packets.append(ret_packet)
            ret_packets.append(ret_packet)

        ack_packets: List[Entity] = list()
        for i in range(self.num_ack_packets):
            ack_packet = vPacket(
                src=self.src,
                dst=self.dst,
                size=self.ack_packet_size,
                priority=self.priority,
                create_at=simulation.now,
                label=f"{self}-ACK-{i}",
                precursor=ret_packets,
            )
            self.packets.append(ack_packet)
            ack_packets.append(ack_packet)

    elif isinstance(self.src, vUser) and not isinstance(self.dst, vUser):
        if self.src_process_length > 0:
            logger.warning(
                f"{simulation.now}:\t{self} source is a vUser, src_instruction_length is ignored."
            )
        if self.ack_process_length > 0:
            logger.warning(
                f"{simulation.now}:\t{self} source is a vUser, ack_instruction_length is ignored."
            )

        if self.num_src_packets <= 0:
            logger.error(
                f"{simulation.now}:\t{self} invalid vAPICall configuration."
            )
            self.terminate(simulation.now)
            return

        dst_container = self.dst.getContainer()

        src_packets: List[Entity] = list()
        for i in range(self.num_src_packets):
            src_packet = vPacket(
                src=self.src,
                dst=dst_container,
                size=self.src_packet_size,
                priority=self.priority,
                create_at=simulation.now,
                label=f"{self}-SRC-{i}",
            )
            self.packets.append(src_packet)
            src_packets.append(src_packet)

        dst_process = vContainerProcess(
            container=self.dst.getContainer(),
            length=self.dst_process_length,
            priority=self.priority,
            precursor=src_packets,
            create_at=simulation.now,
            label=f"{self}-DST",
        )

        ret_packets: List[Entity] = list()
        for i in range(self.num_ret_packets):
            ret_packet = vPacket(
                src=dst_container,
                dst=self.src,
                size=self.ret_packet_size,
                priority=self.priority,
                create_at=simulation.now,
                label=f"{self}-RET-{i}",
                precursor=dst_process,
            )
            self.packets.append(ret_packet)
            ret_packets.append(ret_packet)

        for i in range(self.num_ack_packets):
            ack_packet = vPacket(
                src=self.src,
                dst=dst_container,
                size=self.ack_packet_size,
                priority=self.priority,
                create_at=simulation.now,
                label=f"{self}-ACK-{i}",
                precursor=ret_packets,
            )
            self.packets.append(ack_packet)

    elif not isinstance(self.src, vUser) and isinstance(self.dst, vUser):
        if self.dst_process_length > 0:
            logger.warning(
                f"{simulation.now}:\t{self} destination is a vUser, dst_instruction_length is ignored."
            )

        src_container = self.src.getContainer()

        src_process = vContainerProcess(
            container=src_container,
            length=self.src_process_length,
            priority=self.priority,
            create_at=simulation.now,
            label=f"{self}-SRC",
        )
        self.processes.append(src_process)

        src_packets: List[Entity] = list()
        for i in range(self.num_ack_packets):
            src_packet = vPacket(
                src=src_container,
                dst=self.dst,
                size=self.src_packet_size,
                priority=self.priority,
                create_at=simulation.now,
                label=f"{self}-SRC-{i}",
                precursor=src_process,
            )
            self.packets.append(src_packet)
            src_packets.append(src_packet)

        ret_packets: List[Entity] = list()
        for i in range(self.num_ret_packets):
            ret_packet = vPacket(
                src=self.dst,
                dst=src_container,
                size=self.ret_packet_size,
                priority=self.priority,
                create_at=simulation.now,
                label=f"{self}-RET-{i}",
                precursor=src_packets,
            )
            self.packets.append(ret_packet)
            ret_packets.append(ret_packet)

        ack_process = vContainerProcess(
            container=src_container,
            length=self.ack_process_length,
            priority=self.priority,
            precursor=ret_packets,
            create_at=simulation.now,
            label=f"{self}-ACK",
        )
        self.processes.append(ack_process)

        for i in range(self.num_ack_packets):
            ack_packet = vPacket(
                src=src_container,
                dst=self.dst,
                size=self.ack_packet_size,
                priority=self.priority,
                create_at=simulation.now,
                label=f"{self}-ACK-{i}",
                precursor=ack_process,
            )
            self.packets.append(ack_packet)

    elif not isinstance(self.src, vUser) and not isinstance(self.dst, vUser):

        src_container = self.src.getContainer()
        dst_container = self.dst.getContainer()

        src_process = vContainerProcess(
            container=src_container,
            length=self.src_process_length,
            priority=self.priority,
            create_at=simulation.now,
            label=f"{self}-SRC",
        )
        self.processes.append(src_process)

        src_packets: List[Entity] = list()
        for i in range(self.num_src_packets):
            src_packet = vPacket(
                src=src_container,
                dst=dst_container,
                size=self.src_packet_size,
                priority=self.priority,
                create_at=simulation.now,
                label=f"{self}-SRC-{i}",
                precursor=src_process,
            )
            self.packets.append(src_packet)
            src_packets.append(src_packet)

        dst_process = vContainerProcess(
            container=dst_container,
            length=self.dst_process_length,
            priority=self.priority,
            precursor=src_packets,
            create_at=simulation.now,
            label=f"{self}-DST",
        )
        self.processes.append(dst_process)

        ret_packets: List[Entity] = list()
        for i in range(self.num_ret_packets):
            ret_packet = vPacket(
                src=dst_container,
                dst=src_container,
                size=self.ret_packet_size,
                priority=self.priority,
                create_at=simulation.now,
                label=f"{self}-RET-{i}",
                precursor=dst_process,
            )
            self.packets.append(ret_packet)
            ret_packets.append(ret_packet)

        ack_process = vContainerProcess(
            container=src_container,
            length=self.ack_process_length,
            priority=self.priority,
            precursor=ret_packets,
            create_at=simulation.now,
            label=f"{self}-ACK",
        )
        self.processes.append(ack_process)

        for i in range(self.num_ack_packets):
            ack_packet = vPacket(
                src=src_container,
                dst=dst_container,
                size=self.ack_packet_size,
                priority=self.priority,
                create_at=simulation.now,
                label=f"{self}-ACK-{i}",
                precursor=ack_process,
            )
            self.packets.append(ack_packet)

    @self.continuous_event(
        at=simulation.now,
        interval=simulation.min_time_unit,
        duration=inf,
        label=f"{self}-Monitor",
    )
    def monitoring():
        if any([process.failed for process in self.processes]) or any(
            [packet.failed for packet in self.packets]
        ):
            self.fail(simulation.now)
            return

        if all([process.succeed for process in self.processes]) and all(
            [packet.succeed for packet in self.packets]
        ):
            self.success(simulation.now)
            return

    logger.info(
        f"{simulation.now}:\t{self} is initiated {self} between {self.src} and {self.dst}."
    )