Skip to content

Commit

Permalink
Add new CorrelationId type for offline updates/lockboxes
Browse files Browse the repository at this point in the history
  • Loading branch information
simao committed Nov 3, 2023
1 parent a84f8be commit f54833d
Showing 1 changed file with 18 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package com.advancedtelematic.libats.data

import java.util.UUID

import com.advancedtelematic.libats.data.DataType.HashMethod.HashMethod
import eu.timepit.refined.api.{Refined, Validate}
import eu.timepit.refined.api.{RefType, Refined, Validate}
import eu.timepit.refined.boolean.And
import eu.timepit.refined.collection.Size
import eu.timepit.refined.generic.Equal
import eu.timepit.refined.string.HexStringSpec
import io.circe.{Decoder, Encoder}
import cats.syntax.either.*

import scala.language.postfixOps

Expand Down Expand Up @@ -44,18 +48,29 @@ object DataType {
override def toString: String = s"urn:here-ota:auto-update:$value"
}

type ValidLockboxHash = String Refined (HexStringSpec And Size[Equal[12]])

final case class OfflineUpdateId(name: String, version: Long, hash: ValidLockboxHash) extends CorrelationId {
override def toString: String = s"urn:tdx-ota:lockbox:$name:$version:${hash.value}"
}
object CorrelationId {
private[this] val CorrelationIdRe = """^urn:here-ota:(mtu|auto-update|campaign):([0-9a-fA-F]{8}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{12})$"""r

private [this] val TrxCorrelationIdRe = """^urn:tdx-ota:lockbox:([A-Za-z0-9_-]+):([0-9]+):([0-9a-fA-F]{12})$"""r

def fromString(s: String): Either[String, CorrelationId] = s match {
case CorrelationIdRe("mtu", uuid) =>
Right(MultiTargetUpdateId(UUID.fromString(uuid)))
case CorrelationIdRe("campaign", uuid) =>
Right(CampaignId(UUID.fromString(uuid)))
case CorrelationIdRe("auto-update", uuid) =>
Right(AutoUpdateId(UUID.fromString(uuid)))
case TrxCorrelationIdRe(name, version, hash) =>
RefType.applyRef[ValidLockboxHash](hash).map { hash =>
OfflineUpdateId(name, version.toLong, hash)
}
case x =>
Left(s"Invalid correlationId: '$x'.")
Left(s"Invalid correlationId: '$x'")
}

implicit val DecoderInstance: Decoder[CorrelationId] = Decoder.decodeString.emap(CorrelationId.fromString)
Expand Down

0 comments on commit f54833d

Please sign in to comment.