Skip to content

Commit

Permalink
Merge pull request #9 from Comcast/feature/TUUID
Browse files Browse the repository at this point in the history
Transaction UUID is required in Talaria and TR1D1UM should take care …
  • Loading branch information
joe94 authored Oct 18, 2017
2 parents 8af040f + 0c78a82 commit fe97435
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
23 changes: 20 additions & 3 deletions src/tr1d1um/conversion_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
15 changes: 15 additions & 0 deletions src/tr1d1um/conversion_utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
}

0 comments on commit fe97435

Please sign in to comment.