@@ -22,14 +22,15 @@ import akka.actor.{ActorRef, ActorSystem}
2222import javax .inject .Inject
2323import play .api .{Configuration , Logger }
2424import play .api .libs .concurrent .CustomExecutionContext
25- import play .api .libs .json ._
2625import play .api .libs .ws .WSClient
2726import akka .stream .Materializer
2827import play .api .libs .streams .ActorFlow
2928import actors .{ClientSocketActor , PublishSocketMessageActor }
3029import play .api .mvc ._
31-
3230import scala .concurrent .ExecutionContext
31+ import authorization .AuthProvider
32+ import play .api .libs .json .Json
33+
3334
3435
3536trait MyExecutionContext extends ExecutionContext
@@ -63,9 +64,15 @@ class InstanceRegistryController @Inject()(implicit system: ActorSystem, mat: Ma
6364 val instanceRegistryUri = config.get[String ](" app.instanceRegistryUri" )
6465 val instanceRegistryBasePath = config.get[String ](" app.instanceRegistryBasePath" )
6566
67+ /** This method maps list of instances with specific componentType.
68+ *
69+ * @param componentType
70+ * @return
71+ */
6672 def instances (componentType : String ): Action [AnyContent ] = Action .async {
67-
68- ws.url(instanceRegistryUri + " /instances" ).addQueryStringParameters(" ComponentType" -> componentType).get().map { response =>
73+ ws.url(instanceRegistryUri).addQueryStringParameters(" ComponentType" -> componentType)
74+ .withHttpHeaders((" Authorization" , s " Bearer ${AuthProvider .generateJwt()}" ))
75+ .get().map { response =>
6976 // TODO: possible handling of parsing the data can be done here
7077
7178 Ok (response.body)
@@ -80,8 +87,15 @@ class InstanceRegistryController @Inject()(implicit system: ActorSystem, mat: Ma
8087 }
8188 }
8289
90+ /** Called to fetch network graph of current registry. Contains a list of all instances and all links
91+ * currently registered.
92+ *
93+ * @return
94+ */
95+
8396 def getNetwork (): Action [AnyContent ] = Action .async {
84- ws.url(instanceRegistryUri + " /network" ).get().map { response =>
97+ ws.url(instanceRegistryUri + " /instances/network" ).withHttpHeaders((" Authorization" , s " Bearer ${AuthProvider .generateJwt()}" ))
98+ .get().map { response =>
8599 // TODO: possible handling of parsing the data can be done here
86100 Logger .debug(response.body)
87101 if (response.status == 200 ) {
@@ -92,10 +106,20 @@ class InstanceRegistryController @Inject()(implicit system: ActorSystem, mat: Ma
92106 }(myExecutionContext)
93107 }
94108
95- def numberOfInstances (componentType : String ) : Action [AnyContent ] = Action .async {
109+ /**
110+ * Fetches the number of instances for the specified ComponentType. The ComponentType is an optional parameter which is passed as an query
111+ * argument named 'ComponentType'
112+ *
113+ * @param componentType
114+ * @return
115+ */
116+
117+ def numberOfInstances (componentType : String ): Action [AnyContent ] = Action .async {
96118 // TODO: handle what should happen if the instance registry is not reachable.
97119 // TODO: create constants for the urls
98- ws.url(instanceRegistryUri + " /numberOfInstances" ).addQueryStringParameters(" ComponentType" -> componentType).get().map { response =>
120+ ws.url(instanceRegistryUri + " /count" ).addQueryStringParameters(" ComponentType" -> componentType)
121+ .withHttpHeaders((" Authorization" , s " Bearer ${AuthProvider .generateJwt()}" ))
122+ .get().map { response =>
99123 // TODO: possible handling of parsing the data can be done here
100124 if (response.status == 200 ) {
101125 Ok (response.body)
@@ -106,31 +130,51 @@ class InstanceRegistryController @Inject()(implicit system: ActorSystem, mat: Ma
106130 }
107131
108132 /**
109- * This function is for handling all(start, stop, play, pause, resume) POST request.
110- * To control the instance State
111- * @param componentId
112- */
133+ * This function is for handling all(start, stop, play, pause, resume) POST request.
134+ * To control the instance State (E.g. /instances/42/stop )
135+ *
136+ * @param componentId
137+ */
113138
114139
115140 def handleRequest (action : String , instanceID : String ): Action [AnyContent ] = Action .async { request =>
116- ws.url(instanceRegistryUri + action)
117- .addQueryStringParameters( " Id " -> instanceID )
141+ ws.url(instanceRegistryUri + " /instances/ " + instanceID + action)
142+ .withHttpHeaders(( " Authorization " , s " Bearer ${ AuthProvider .generateJwt()} " ) )
118143 .post(" " )
119144 .map { response =>
120145 new Status (response.status)
121146 }(myExecutionContext)
122147 }
123148
149+ def reconnect (from : Int , to : Int ): Action [AnyContent ] = Action .async { request =>
150+
151+ ws.url(instanceRegistryUri + " /instances/" + from + " /assignInstance"
152+ )
153+ .withHttpHeaders((" Authorization" , s " Bearer ${AuthProvider .generateJwt()}" ))
154+ .post(Json .obj(" AssignedInstanceId" -> to))
155+ .map { response =>
156+ response.status match {
157+ case 200 =>
158+ Ok (response.body)
159+ case x =>
160+ new Status (x)
161+ }
162+ }(myExecutionContext)
163+ }
124164 /**
125- * This function is for handling an POST request for adding an instance to the Scala web server
126- *
127- * @param componentType
128- * @param name
129- */
130- def postInstance (compType : String , name : String ): Action [AnyContent ] = Action .async { request =>
131- ws.url(instanceRegistryUri + " /deploy" )
132- .addQueryStringParameters(" ComponentType" -> compType, " InstanceName" -> name)
133- .post(" " )
165+ * This function is for handling an POST request for adding an instance to the Scala web server
166+ * (E.g. .../instances/deploy
167+ *
168+ * @param componentType
169+ * @param name
170+ */
171+
172+ def postInstance (compType : String , name : String ): Action [AnyContent ] = Action .async
173+ {
174+ request =>
175+ ws.url(instanceRegistryUri + " /instances/deploy" )
176+ .withHttpHeaders((" Authorization" , s " Bearer ${AuthProvider .generateJwt()}" ))
177+ .post(Json .obj(" ComponentType" -> compType, " InstanceName" -> name))
134178 .map { response =>
135179 response.status match {
136180 // scalastyle:off magic.number
@@ -142,5 +186,4 @@ class InstanceRegistryController @Inject()(implicit system: ActorSystem, mat: Ma
142186 }
143187 }(myExecutionContext)
144188 }
145-
146189}
0 commit comments