WIP Resource event handling
[aquarium] / src / main / scala / gr / grnet / aquarium / computation / data / RoleHistory.scala
1 /*
2  * Copyright 2011-2012 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.computation.data
37
38 import gr.grnet.aquarium.logic.accounting.dsl.Timeslot
39 import scala.collection.immutable.{TreeMap, SortedMap}
40 import scala.collection.mutable.ListBuffer
41 import scala.annotation.tailrec
42
43 /**
44  *
45  * @author Christos KK Loverdos <loverdos@gmail.com>
46  */
47
48 case class RoleHistory(
49                         /**
50                          * The head role is the most recent. The same rule applies for the tail.
51                          */
52                        roles: List[RoleHistoryItem]) {
53
54   def roleNamesByTimeslot: SortedMap[Timeslot, String] = {
55     TreeMap(roles.map(role ⇒ (role.timeslot, role.name)): _*)
56   }
57
58   def rolesByTimeslot: SortedMap[Timeslot, RoleHistoryItem] = {
59     TreeMap(roles.map(role ⇒ (role.timeslot, role)): _*)
60   }
61
62   def updateWithRole(role: String, validFrom: Long) = {
63     def fixValidTo(validFrom: Long, validTo: Long): Long = {
64       if(validTo == validFrom) {
65         // Since validTo is exclusive, make at least 1ms gap
66         validFrom + 1
67       } else {
68         validTo
69       }
70     }
71
72     val newItems = roles match {
73       case Nil ⇒
74         RoleHistoryItem(role, validFrom) :: Nil
75
76       case head :: tail ⇒
77         if(head.startsStrictlyAfter(validFrom)) {
78           // must search history items to find where this fits in
79           @tailrec
80           def check(allChecked: ListBuffer[RoleHistoryItem],
81                     lastCheck: RoleHistoryItem,
82                     toCheck: List[RoleHistoryItem]): List[RoleHistoryItem] = {
83
84             toCheck match {
85               case Nil ⇒
86                 allChecked.append(RoleHistoryItem(role, validFrom, fixValidTo(validFrom, lastCheck.validFrom)))
87                 allChecked.toList
88
89               case toCheckHead :: toCheckTail ⇒
90                 if(toCheckHead.startsStrictlyAfter(validFrom)) {
91                   allChecked.append(toCheckHead)
92
93                   check(allChecked, toCheckHead, toCheckTail)
94                 } else {
95                   allChecked.append(RoleHistoryItem(role, validFrom, fixValidTo(validFrom, lastCheck.validFrom)))
96                   allChecked.toList
97                 }
98             }
99           }
100
101           val buffer = new ListBuffer[RoleHistoryItem]
102           buffer.append(head)
103           check(buffer, head, tail)
104         } else {
105           // assume head.validTo goes to infinity,
106           RoleHistoryItem(role, validFrom) :: head.copyWithValidTo(fixValidTo(head.validFrom, validFrom)) :: tail
107         }
108     }
109
110     RoleHistory(newItems)
111   }
112
113   /**
114    * Returns the first, chronologically, role.
115    */
116   def firstRole: Option[RoleHistoryItem] = {
117     rolesByTimeslot.valuesIterator.toList.lastOption
118   }
119
120   /**
121    * Returns the name of the first, chronologically, role.
122    */
123   def firstRoleName: Option[String] = {
124     roleNamesByTimeslot.valuesIterator.toList.lastOption
125   }
126
127   /**
128    * Returns the last, chronologically, role.
129    */
130   def lastRole: Option[RoleHistoryItem] = {
131     rolesByTimeslot.valuesIterator.toList.headOption
132   }
133
134   /**
135    * Returns the name of the last, chronologically, role.
136    */
137   def lastRoleName: Option[String] = {
138     roleNamesByTimeslot.valuesIterator.toList.headOption
139   }
140 }
141
142 object RoleHistory {
143   final val Empty = RoleHistory(Nil)
144
145   def initial(role: String, validFrom: Long): RoleHistory = {
146     RoleHistory(RoleHistoryItem(role, validFrom) :: Nil)
147   }
148 }