-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
So that ota-app can use both directors Signed-off-by: Simão Mata <[email protected]>
- Loading branch information
Showing
9 changed files
with
177 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
src/main/scala/com/advancedtelematic/director/http/LegacyRoutes.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package com.advancedtelematic.director.http | ||
|
||
import java.time.Instant | ||
|
||
import akka.http.scaladsl.model.StatusCodes | ||
import akka.http.scaladsl.server.Directives.{complete, delete, path, put} | ||
import akka.http.scaladsl.server.{Directive1, Route} | ||
import com.advancedtelematic.libats.data.DataType.{MultiTargetUpdateId, Namespace} | ||
import com.advancedtelematic.libats.messaging.MessageBusPublisher | ||
import com.advancedtelematic.libats.messaging_datatype.DataType.{DeviceId, UpdateId} | ||
import com.advancedtelematic.libats.messaging_datatype.Messages.{DeviceUpdateAssigned, DeviceUpdateEvent} | ||
import slick.jdbc.MySQLProfile.api._ | ||
import com.advancedtelematic.libats.http.UUIDKeyAkka._ | ||
import akka.http.scaladsl.server.Directives._ | ||
import scala.concurrent.{ExecutionContext, Future} | ||
import de.heikoseeberger.akkahttpcirce.FailFastCirceSupport._ | ||
|
||
// Implements routes provided by old director that ota-web-app still uses | ||
class LegacyRoutes(extractNamespace: Directive1[Namespace])(implicit val db: Database, ec: ExecutionContext, messageBusPublisher: MessageBusPublisher) { | ||
private val deviceAssignments = new DeviceAssignments() | ||
|
||
private def createDeviceAssignment(ns: Namespace, deviceId: DeviceId, mtuId: UpdateId): Future[Unit] = { | ||
val correlationId = MultiTargetUpdateId(mtuId.uuid) | ||
val assignment = deviceAssignments.createForDevice(ns, correlationId, deviceId, mtuId) | ||
|
||
assignment.map { a => | ||
val msg: DeviceUpdateEvent = DeviceUpdateAssigned(ns, Instant.now(), correlationId, a.deviceId) | ||
messageBusPublisher.publishSafe(msg) | ||
} | ||
} | ||
|
||
val route: Route = | ||
extractNamespace { ns => | ||
path("admin" / "devices" / DeviceId.Path / "multi_target_update" / UpdateId.Path) { (deviceId, updateId) => | ||
put { | ||
val f = createDeviceAssignment(ns, deviceId, updateId).map(_ => StatusCodes.Created) | ||
complete(f) | ||
} | ||
} ~ | ||
path("assignments" / DeviceId.Path) { deviceId => | ||
delete { | ||
val a = deviceAssignments.cancel(ns, List(deviceId)) | ||
complete(a.map(_.map(_.deviceId))) | ||
} | ||
} | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
src/main/scala/com/advancedtelematic/director/http/RepositoryCreation.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package com.advancedtelematic.director.http | ||
|
||
import com.advancedtelematic.director.db.{DeviceRepositorySupport, RepoNamespaceRepositorySupport} | ||
import com.advancedtelematic.libats.data.DataType.Namespace | ||
import com.advancedtelematic.libtuf.data.TufDataType.{Ed25519KeyType, RepoId} | ||
import com.advancedtelematic.libtuf_server.keyserver.KeyserverClient | ||
import slick.jdbc.MySQLProfile.api._ | ||
import com.advancedtelematic.libats.http.UUIDKeyAkka._ | ||
|
||
import scala.concurrent.{ExecutionContext, Future} | ||
|
||
class RepositoryCreation(keyserverClient: KeyserverClient)(implicit val db: Database, val ec: ExecutionContext) | ||
extends DeviceRepositorySupport with RepoNamespaceRepositorySupport { | ||
|
||
def create(ns: Namespace): Future[Unit] = { | ||
val repoId = RepoId.generate() | ||
|
||
for { | ||
_ <- keyserverClient.createRoot(repoId, Ed25519KeyType, forceSync = true) | ||
_ <- repoNamespaceRepo.persist(repoId, ns) | ||
} yield () | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
src/test/scala/com/advancedtelematic/director/http/LegacyApiResourceSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package com.advancedtelematic.director.http | ||
|
||
import akka.http.scaladsl.model.StatusCodes | ||
import com.advancedtelematic.director.data.AdminDataType.{MultiTargetUpdate, QueueResponse} | ||
import com.advancedtelematic.director.util.{DirectorSpec, MockMessageBus, RepositorySpec, RouteResourceSpec} | ||
import com.advancedtelematic.libats.messaging_datatype.DataType.{DeviceId, UpdateId} | ||
import com.advancedtelematic.director.data.Generators._ | ||
import com.advancedtelematic.libats.data.DataType.MultiTargetUpdateId | ||
import com.advancedtelematic.director.data.GeneratorOps._ | ||
import com.advancedtelematic.director.data.Codecs._ | ||
import com.advancedtelematic.libats.codecs.CirceCodecs._ | ||
import de.heikoseeberger.akkahttpcirce.FailFastCirceSupport._ | ||
import cats.syntax.show._ | ||
import org.scalatest.OptionValues._ | ||
import com.advancedtelematic.libats.messaging_datatype.Messages._ | ||
|
||
class LegacyApiResourceSpec extends DirectorSpec | ||
with RouteResourceSpec | ||
with AdminResources | ||
with RepositorySpec | ||
with AssignmentResources { | ||
|
||
override implicit val msgPub = new MockMessageBus | ||
|
||
testWithRepo("creates an assignment for the given update id for the specified device") { implicit ns => | ||
val regDev = registerAdminDeviceWithSecondariesOk() | ||
|
||
val targetUpdate = GenTargetUpdateRequest.generate | ||
val mtu = MultiTargetUpdate(Map(regDev.primary.hardwareId -> targetUpdate)) | ||
|
||
val mtuId = Post(apiUri("multi_target_updates"), mtu).namespaced ~> routes ~> check { | ||
status shouldBe StatusCodes.Created | ||
responseAs[UpdateId] | ||
} | ||
|
||
Put(apiUri(s"admin/devices/${regDev.deviceId.show}/multi_target_update/${mtuId.show}")).namespaced ~> routes ~> check { | ||
status shouldBe StatusCodes.Created | ||
} | ||
|
||
val queue = Get(apiUri(s"assignments/${regDev.deviceId.show}")).namespaced ~> routes ~> check { | ||
status shouldBe StatusCodes.OK | ||
responseAs[List[QueueResponse]] | ||
} | ||
|
||
queue.head.correlationId shouldBe MultiTargetUpdateId(mtuId.uuid) | ||
queue.head.targets.get(regDev.primary.ecuSerial).value.image.filepath shouldBe targetUpdate.to.target | ||
queue.head.targets.get(regDev.secondaries.keys.head) shouldBe empty | ||
|
||
val msg = msgPub.wasReceived[DeviceUpdateEvent] { msg: DeviceUpdateEvent => | ||
msg.deviceUuid == regDev.deviceId | ||
} | ||
|
||
msg.value shouldBe a [DeviceUpdateAssigned] | ||
} | ||
|
||
testWithRepo("DELETE assignments cancels assigned updates") { implicit ns => | ||
val regDev = registerAdminDeviceOk() | ||
createAssignmentOk(regDev.deviceId, regDev.primary.hardwareId) | ||
|
||
val queue0 = getDeviceAssignmentOk(regDev.deviceId) | ||
queue0 shouldNot be(empty) | ||
|
||
Delete(apiUri("assignments/" + regDev.deviceId.show)).namespaced ~> routes ~> check { | ||
status shouldBe StatusCodes.OK | ||
responseAs[Seq[DeviceId]] | ||
} | ||
|
||
val queue = getDeviceAssignmentOk(regDev.deviceId) | ||
queue shouldBe empty | ||
|
||
val msg = msgPub.wasReceived[DeviceUpdateEvent] { msg: DeviceUpdateEvent => | ||
msg.deviceUuid == regDev.deviceId | ||
} | ||
|
||
msg shouldBe defined | ||
msg.get shouldBe a [DeviceUpdateCanceled] | ||
} | ||
} |