Skip to content

Commit

Permalink
Allow provision of device with same deviceId but different ecus
Browse files Browse the repository at this point in the history
This should most likely not be allowed, but the old director allows it
and it's not possible to determine if anyone is doing this.

Signed-off-by: Simão Mata <[email protected]>
  • Loading branch information
simao committed Feb 4, 2020
1 parent 9b2ddec commit 0a5a4e2
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/main/scala/com/advancedtelematic/director/db/Repository.scala
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,21 @@ trait DeviceRepositorySupport extends DatabaseSupport {

protected class DeviceRepository()(implicit val db: Database, val ec: ExecutionContext) {
def create(ns: Namespace, deviceId: DeviceId, primaryEcuId: EcuIdentifier, ecus: Seq[Ecu]): Future[Unit] = {
val ecusDeleteIO =
Schema.ecus
.filter(_.namespace === ns)
.filter(_.deviceId === deviceId)
.filter(_.ecuSerial.inSet(ecus.map(_.ecuSerial)))
.delete

val deviceDeleteIo =
Schema.devices
.filter(_.namespace === ns)
.filter(_.id === deviceId)
.delete

val io = for {
_ <- ecusDeleteIO.andThen(deviceDeleteIo) // This is a bad idea and will fail if device has assignments, see DeviceResourceSpec
_ <- Schema.ecus ++= ecus
_ <- Schema.devices += Device(ns, deviceId, primaryEcuId)
} yield ()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,30 @@ class DeviceResourceSpec extends DirectorSpec
registerDeviceOk()
}

// TODO: Legacy, this should not be possible
// https://saeljira.it.here.com/browse/OTA-441
// https://saeljira.it.here.com/browse/OTA-2517
testWithNamespace("registering the same device id with different ecus works") { implicit ns =>
createRepoOk()

val deviceId = DeviceId.generate()
val ecus = GenRegisterEcu.generate
val primaryEcu = ecus.ecu_serial
val req = RegisterDevice(deviceId.some, primaryEcu, Seq(ecus))

val ecus2 = GenRegisterEcu.generate
val primaryEcu2 = ecus2.ecu_serial
val req2 = RegisterDevice(deviceId.some, primaryEcu2, Seq(ecus2))

Post(apiUri(s"device/${deviceId.show}/ecus"), req).namespaced ~> routes ~> check {
status shouldBe StatusCodes.Created
}

Post(apiUri(s"device/${deviceId.show}/ecus"), req2).namespaced ~> routes ~> check {
status shouldBe StatusCodes.Created
}
}

testWithRepo("fails when primary ecu is not defined in ecus") { implicit ns =>
val ecus = GenRegisterEcu.generate
val primaryEcu = GenEcuIdentifier.generate
Expand Down

0 comments on commit 0a5a4e2

Please sign in to comment.