|
| 1 | +package de.upb.cs.swt.delphi.instanceregistry |
| 2 | + |
| 3 | +import java.util.concurrent.TimeUnit |
| 4 | + |
| 5 | +import akka.actor.ActorSystem |
| 6 | +import akka.http.scaladsl.server |
| 7 | +import akka.http.scaladsl.model.{HttpResponse, StatusCodes} |
| 8 | +import akka.http.scaladsl.server.HttpApp |
| 9 | +import akka.http.scaladsl.unmarshalling.Unmarshal |
| 10 | +import akka.stream.ActorMaterializer |
| 11 | +import akka.util.Timeout |
| 12 | +import de.upb.cs.swt.delphi.instanceregistry.io.swagger.client.model.InstanceEnums.ComponentType |
| 13 | +import io.swagger.client.model.{Instance, JsonSupport} |
| 14 | + |
| 15 | +import scala.collection.mutable |
| 16 | +import scala.concurrent.{Await, ExecutionContext} |
| 17 | +import scala.concurrent.duration.Duration |
| 18 | + |
| 19 | + |
| 20 | +/** |
| 21 | + * Web server configuration for Instance Registry API. |
| 22 | + */ |
| 23 | +object Server extends HttpApp with JsonSupport with AppLogging { |
| 24 | + |
| 25 | + //Default ES instance for testing |
| 26 | + private val instances = mutable.HashSet (Instance(Some(0), "elasticsearch://localhost", 9200, "Default ElasticSearch Instance", ComponentType.ElasticSearch)) |
| 27 | + |
| 28 | + implicit val system : ActorSystem = ActorSystem("delphi-registry") |
| 29 | + implicit val materializer : ActorMaterializer = ActorMaterializer() |
| 30 | + implicit val ec : ExecutionContext = system.dispatcher |
| 31 | + implicit val timeout : Timeout = Timeout(5, TimeUnit.SECONDS) |
| 32 | + |
| 33 | + override def routes : server.Route = |
| 34 | + path("register") {entity(as[String]) { jsonString => addInstance(jsonString) }} ~ |
| 35 | + path("deregister") { deleteInstance() } ~ |
| 36 | + path("instances" ) { fetchInstancesOfType() } ~ |
| 37 | + path("numberOfInstances" ) { numberOfInstances() } ~ |
| 38 | + path("matchingInstance" ) { getMatchingInstance()} ~ |
| 39 | + path("matchingResult" ) {matchInstance()} |
| 40 | + |
| 41 | + |
| 42 | + def addInstance(InstanceString: String) : server.Route = { |
| 43 | + post |
| 44 | + { |
| 45 | + log.debug(s"POST /register has been called, parameter is: $InstanceString") |
| 46 | + Await.result(Unmarshal(InstanceString).to[Instance] map {paramInstance => |
| 47 | + val name = paramInstance.name |
| 48 | + val newID : Long = { |
| 49 | + if(instances.isEmpty){ |
| 50 | + 0L |
| 51 | + } |
| 52 | + else{ |
| 53 | + (instances map( instance => instance.iD.get) max) + 1L |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + val instanceToRegister = Instance(iD = Some(newID), host = paramInstance.host, portnumber = paramInstance.portnumber, name = paramInstance.name, componentType = paramInstance.componentType) |
| 58 | + |
| 59 | + instances += instanceToRegister |
| 60 | + log.info(s"Instance with name $name registered, ID $newID assigned.") |
| 61 | + |
| 62 | + complete {newID.toString()} |
| 63 | + } recover {case ex => |
| 64 | + log.warning(s"Failed to read registering instance, exception: $ex") |
| 65 | + complete(HttpResponse(StatusCodes.InternalServerError, entity = "Failed to unmarshal parameter.")) |
| 66 | + }, Duration.Inf) |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + def deleteInstance() : server.Route = parameters('Id.as[Long]){ Id => |
| 71 | + post { |
| 72 | + log.debug(s"POST /deregister?Id=$Id has been called") |
| 73 | + |
| 74 | + val instanceToRemove = instances find(instance => instance.iD.get == Id) |
| 75 | + |
| 76 | + if(instanceToRemove.isEmpty){ |
| 77 | + log.warning(s"Cannot remove instance with id $Id, that id is not present on the server") |
| 78 | + complete{HttpResponse(StatusCodes.NotFound, entity = s"Id $Id not present on the server")} |
| 79 | + } |
| 80 | + else{ |
| 81 | + instances remove instanceToRemove.get |
| 82 | + log.info(s"Successfully removed instance with id $Id") |
| 83 | + complete {s"Successfully removed instance with id $Id"} |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + def fetchInstancesOfType () : server.Route = parameters('ComponentType.as[String]) { compTypeString => |
| 88 | + get { |
| 89 | + log.debug(s"GET /instances?ComponentType=$compTypeString has been called") |
| 90 | + val compType : ComponentType = ComponentType.values.find(v => v.toString == compTypeString).orNull |
| 91 | + val matchingInstancesList = List() ++ instances filter {instance => instance.componentType == compType} |
| 92 | + |
| 93 | + complete {matchingInstancesList} |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + def numberOfInstances() : server.Route = parameters('ComponentType.as[String]) { compTypeString => |
| 98 | + get { |
| 99 | + log.debug(s"GET /numberOfInstances?ComponentType=$compTypeString has been called") |
| 100 | + val compType : ComponentType = ComponentType.values.find(v => v.toString == compTypeString).orNull |
| 101 | + val count : Int = instances count {instance => instance.componentType == compType} |
| 102 | + complete{count.toString()} |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + def getMatchingInstance() : server.Route = parameters('ComponentType.as[String]){ compTypeString => |
| 107 | + get{ |
| 108 | + log.debug(s"GET /matchingInstance?ComponentType=$compTypeString has been called") |
| 109 | + val compType : ComponentType = ComponentType.values.find(v => v.toString == compTypeString).orNull |
| 110 | + log.info(s"Looking for instance of type $compType ...") |
| 111 | + val matchingInstances = instances filter {instance => instance.componentType == compType} |
| 112 | + if(matchingInstances.isEmpty){ |
| 113 | + log.warning(s"Could not find matching instance for type $compType .") |
| 114 | + complete(HttpResponse(StatusCodes.NotFound, entity = s"Could not find matching instance for type $compType")) |
| 115 | + } |
| 116 | + else { |
| 117 | + val matchedInstance = matchingInstances.iterator.next() |
| 118 | + log.info(s"Matched to $matchedInstance.") |
| 119 | + complete(matchedInstance) |
| 120 | + } |
| 121 | + |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + def matchInstance() : server.Route = parameters('Id.as[Long], 'MatchingSuccessful.as[Boolean]){ (Id, MatchingResult) => |
| 126 | + post { |
| 127 | + //TODO: Need to keep track of matching, maybe remove instances if not reachable! |
| 128 | + log.debug(s"POST /matchingResult?Id=$Id&MatchingSuccessful=$MatchingResult has been called") |
| 129 | + if(MatchingResult){ |
| 130 | + log.info(s"Instance with Id $Id was successfully matched.") |
| 131 | + } |
| 132 | + else{ |
| 133 | + log.warning(s"A client was not able to reach matched instance with Id $Id !") |
| 134 | + } |
| 135 | + complete {s"Matching result $MatchingResult processed."} |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + def main(args: Array[String]): Unit = { |
| 140 | + val configuration = new Configuration() |
| 141 | + Server.startServer(configuration.bindHost, configuration.bindPort) |
| 142 | + system.terminate() |
| 143 | + } |
| 144 | + |
| 145 | + |
| 146 | +} |
| 147 | + |
| 148 | + |
0 commit comments