From 0c78a827da7922e88d420eae08c51d00fadf2bb3 Mon Sep 17 00:00:00 2001 From: Joel Unzain Date: Tue, 17 Oct 2017 15:40:37 -0700 Subject: [PATCH] Transaction UUID is required in Talaria and TR1D1UM should take care of that. --- src/tr1d1um/conversion_utils.go | 23 ++++++++++++++++++++--- src/tr1d1um/conversion_utils_test.go | 15 +++++++++++++++ 2 files changed, 35 insertions(+), 3 deletions(-) diff --git a/src/tr1d1um/conversion_utils.go b/src/tr1d1um/conversion_utils.go index c3831451..be12dbc6 100644 --- a/src/tr1d1um/conversion_utils.go +++ b/src/tr1d1um/conversion_utils.go @@ -8,6 +8,8 @@ import ( "io/ioutil" "net/http" "strings" + "crypto/rand" + "encoding/base64" "github.com/Comcast/webpa-common/wrp" "github.com/go-ozzo/ozzo-validation" @@ -132,9 +134,9 @@ func (cw *ConversionWDMP) ReplaceFlavorFormat(input io.Reader, urlVars Vars, tab return } -//ValidateAndDeduceSET attempts at defaulting to the SET command given that all the command property requirements are satisfied. +//ValidateAndDeduceSET attempts at defaulting to the SET command given all command property requirements are satisfied // (name, value, dataType). Then, if the new_cid is provided, it is deduced that the command should be TEST_SET -//else, +//If the SET command properties are not satisfied, we attempt at validating the input for the SET_ATTRS command func (cw *ConversionWDMP) ValidateAndDeduceSET(header http.Header, wdmp *SetWDMP) (err error) { if err = validation.Validate(wdmp.Parameters, validation.Required); err == nil { wdmp.Command = CommandSet @@ -176,7 +178,7 @@ func (cw *ConversionWDMP) GetConfiguredWRP(wdmp []byte, pathVars Vars, header ht Payload: wdmp, Source: WRPSource + "/" + service, Destination: deviceID + "/" + service, - TransactionUUID: header.Get(HeaderWPATID), + TransactionUUID: GetOrGenTID(header), } return } @@ -216,3 +218,18 @@ func (helper *EncodingHelper) GenericEncode(v interface{}, f wrp.Format) (data [ data = tmp.Bytes() return } + + +//GetOrGenTID returns a Transaction ID for a given request. +//If a TID was provided in the headers, such is used. Otherwise, +//a new TID is generated and returned +func GetOrGenTID(requestHeader http.Header) (tid string){ + if tid = requestHeader.Get(HeaderWPATID); tid == ""{ + buf := make([]byte, 16) + if _, err := rand.Read(buf); err == nil{ + tid = base64.RawURLEncoding.EncodeToString(buf) + } + } + //TODO: any validation on the provided TID? + return +} diff --git a/src/tr1d1um/conversion_utils_test.go b/src/tr1d1um/conversion_utils_test.go index 49a257a7..90330ee6 100644 --- a/src/tr1d1um/conversion_utils_test.go +++ b/src/tr1d1um/conversion_utils_test.go @@ -432,3 +432,18 @@ func TestGetConfiguredWRP(t *testing.T) { assert.EqualValues(expectedSource, wrpMsg.Source) assert.EqualValues(tid, wrpMsg.TransactionUUID) } + + +func TestGetOrGenTID(t *testing.T) { + assert := assert.New(t) + t.Run("UseGivenTID", func(t *testing.T) { + header := http.Header{} + header.Set(HeaderWPATID, "SomeTID") + assert.EqualValues("SomeTID", GetOrGenTID(header)) + }) + + t.Run("GenerateTID", func(t *testing.T) { + tid := GetOrGenTID(http.Header{}) + assert.NotEmpty(tid) + }) +}