c2f8561e551ade4f848497f26df5c5d26353c02c
[aquarium] / logic / src / main / scala / gr / grnet / aquarium / logic / events / EventProcessor.scala
1 /*
2  * Copyright 2011 GRNET S.A. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or
5  * without modification, are permitted provided that the following
6  * conditions are met:
7  *
8  *   1. Redistributions of source code must retain the above
9  *      copyright notice, this list of conditions and the following
10  *      disclaimer.
11  *
12  *   2. Redistributions in binary form must reproduce the above
13  *      copyright notice, this list of conditions and the following
14  *      disclaimer in the documentation and/or other materials
15  *      provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
18  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
21  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
24  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  *
30  * The views and conclusions contained in the software and
31  * documentation are those of the authors and should not be
32  * interpreted as representing official policies, either expressed
33  * or implied, of GRNET S.A.
34  */
35
36 package gr.grnet.aquarium.logic.events
37
38 import gr.grnet.aquarium.logic.accounting.{AccountingEventType, AccountingEvent}
39 import java.util.Date
40
41 object EventProcessor {
42
43   def process(events: List[Event])
44              (evtFilter: (Event) => Boolean): List[AccountingEvent] = {
45
46     val evts = events.filter(evtFilter)
47     val dummy = new AccountingEvent(AccountingEventType.VMTime, new Date(), new Date(), 0, 0, List())
48
49     evts.sortBy(_.when).map {
50       e =>
51         val u = e.who()
52         e match {
53           case v: VMStarted => //Find stop event and calculate resource usage
54             val stop = findVMStopEvent(evts, v)
55             val time = stop match {
56               case Some(x) => x.when
57               case None => new Date() //Now
58             }
59             val stopid = stop match {
60               case Some(x) => x.id
61               case None => -1 
62             }
63             val totaltime = time.getTime - v.w.getTime
64             assert(totaltime > 0)
65             new AccountingEvent(AccountingEventType.VMTime, e.when, time, u, totaltime, List(v.id, stopid))
66           //          case v : VMStarted =>None
67           //          case v : VMStopped =>None
68           case v: DiskSpaceChanged =>
69             assert(v.bytes > 0)
70             new AccountingEvent(AccountingEventType.DiskSpace, e.when, e.when, u, v.bytes, List(v.id()))
71           case v: DataUploaded =>
72             assert(v.bytes > 0)
73             new AccountingEvent(AccountingEventType.NetDataUp, e.when, e.when, u, v.bytes, List(v.id()))
74           case v: DataDownloaded =>
75             assert(v.bytes > 0)
76             new AccountingEvent(AccountingEventType.NetDataDown, e.when, e.when, u, v.bytes, List(v.id()))
77           //          case v : SSaasVMCreated => None
78           //          case v : SSaasVMStarted =>None
79           //          case v : SSaasVMStopped =>None
80           case _ => dummy
81         }
82     }.filter(p => p != dummy) //Remove dummy events
83   }
84
85   /**Find a the first corresponding VM stop event in a list of messages*/
86   def findVMStopEvent(events: List[Event], v: VMStarted): Option[VMStopped] = {
87     events.find {
88       f => (
89         f.id() == v.id() &&
90           f.who() == v.who &&
91           f.when().compareTo(v.when()) > 0) &&
92         f.isInstanceOf[VMStopped]
93     }.asInstanceOf[Option[VMStopped]]
94   }
95 }