-
Notifications
You must be signed in to change notification settings - Fork 12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Initial skeleton for Tr1d1um (WIP) #1
Merged
Merged
Changes from 7 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
9601536
Initial skeleton for Tr1d1um
joe94 e396f49
Conversion should be from http Request to xpc message.
joe94 86f4289
WDMP format to WRP requires extracting data from http.Request
joe94 fa3cf48
Clean up + finish minor tasks.
joe94 2c83fed
Correct message type.
joe94 5898418
Reorg + Unit tests of written functions.
joe94 8c464b7
All the flavors of SET and GET commands should be working. Next: Unit…
joe94 1ccd1ee
Support for DELETE command + start of REPLACE
joe94 c44ed05
All main methods should be working now + Redesigned Conversion + Work…
joe94 6f0c952
More Unit tests + Next: More unit tests and research of new go valida…
joe94 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package main | ||
|
||
const( | ||
COMMAND_GET = "GET" | ||
COMMAND_GET_ATTRS = "GET_ATTRIBUTES" | ||
COMMAND_SET = "SET" | ||
COMMAND_SET_ATTRS = "SET_ATTRIBUTES" | ||
COMMAND_TEST_SET = "TEST_AND_SET" | ||
|
||
HEADER_WPA_SYNC_OLD_CID = "X-Webpa-Sync-Old-Cid" | ||
HEADER_WPA_SYNC_NEW_CID = "X-Webpa-Sync-New-Cid" | ||
HEADER_WPA_SYNC_CMC = "X-Webpa-Sync-Cmc" | ||
|
||
ERR_UNSUCCESSFUL_DATA_PARSE = "Unsuccessful Data Parse" | ||
ERR_UNSUCCESSFUL_DATA_WRAP = "Unsuccessful WDMP data transfer into wrp message" | ||
|
||
) | ||
|
||
type GetWDMP struct { | ||
Command string `json:"command"` | ||
Names []string `json:"names,omitempty"` | ||
Attribute string `json:"attributes,omitempty"` | ||
} | ||
|
||
type SetParam struct { | ||
Name* string `json:"name"` | ||
DataType* int32 `json:"dataType,omitempty"` | ||
Value interface{} `json:"value,omitempty"` | ||
Attributes Attr `json:"attributes,omitempty"` | ||
} | ||
|
||
type SetWDMP struct { | ||
Command string `json:"command"` | ||
OldCid string `json:"old-cid,omitempty"` | ||
NewCid string `json:"new-cid,omitempty"` | ||
SyncCmc string `json:"sync-cmc,omitempty"` | ||
Parameters []SetParam `json:"parameters,omitempty"` | ||
} | ||
|
||
type Attr map[string]interface{} |
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,138 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/Comcast/webpa-common/wrp" | ||
"encoding/json" | ||
"strings" | ||
"net/http" | ||
"io" | ||
"errors" | ||
) | ||
|
||
var ( | ||
ErrJsonEmpty = errors.New("JSON payload is empty") | ||
) | ||
|
||
//Given some wdmp data, wraps it into a wrp object, returns the resulting payload | ||
func WrapInWrp(wdmpData []byte) (payload []byte, err error){ | ||
wrpMessage := wrp.Message{Type:wrp.SimpleRequestResponseMessageType, Payload:wdmpData} | ||
payload, err = json.Marshal(wrpMessage) | ||
return | ||
} | ||
|
||
// All we care about is the payload. Method helps abstract away work done with the WDMP object | ||
func GetFlavorFormat(req *http.Request, attr, formValKey, sep string) (payload[]byte, err error){ | ||
wdmp := new(GetWDMP) | ||
|
||
if names := strings.Split(req.FormValue(formValKey),sep); len(names) > 0 { | ||
wdmp.Command = COMMAND_GET | ||
wdmp.Names = names | ||
} else{ | ||
err = errors.New("names is a required property for GET") | ||
return | ||
} | ||
|
||
if attributes := req.FormValue(attr); attributes != "" { | ||
wdmp.Command = COMMAND_GET_ATTRS | ||
} | ||
|
||
payload, err = json.Marshal(wdmp) | ||
return | ||
} | ||
|
||
func SetFlavorFormat(req *http.Request, ReadEntireBody func(io.Reader)(payload []byte, err error)) (payload[]byte, err error){ | ||
wdmp := new(SetWDMP) | ||
DecodeJsonPayload(req, wdmp, ReadEntireBody) | ||
|
||
wdmp.Command, err = ValidateAndGetCommand(req, wdmp) | ||
|
||
if err != nil { | ||
return | ||
} | ||
|
||
payload, err = json.Marshal(wdmp) | ||
return | ||
} | ||
|
||
func ValidateAndGetCommand(req *http.Request, wdmp *SetWDMP) (command string, err error){ | ||
if newCid := req.Header.Get(HEADER_WPA_SYNC_NEW_CID); newCid != "" { | ||
wdmp.OldCid = req.Header.Get(HEADER_WPA_SYNC_OLD_CID) | ||
wdmp.NewCid = newCid | ||
wdmp.SyncCmc = req.Header.Get(HEADER_WPA_SYNC_CMC) | ||
command, err = validateSETParams(false, wdmp, COMMAND_TEST_SET) | ||
} else { | ||
command, err = validateSETParams(true, wdmp, "") | ||
} | ||
return | ||
} | ||
|
||
|
||
/* -Inputs-: | ||
**checkingForSetAttr**: true if we're checking for the required parameter properties for the SET_ATTRIBUTES command | ||
These properties are: attributes and name | ||
|
||
**wdmp**: the WDMP object from which we retrieve the parameters | ||
|
||
**override**: overrides the final suggested command if non-empty. Useful if one just wants to check for SET command | ||
parameter properties (value, dataType, name) | ||
|
||
-Outputs-: | ||
**command**: the final command based on the analysis of the parameters | ||
**err**: it is non-nil if any required property is violated | ||
*/ | ||
func validateSETParams(checkingForSetAttr bool, wdmp *SetWDMP, override string) (command string, err error){ | ||
for _, sp := range wdmp.Parameters { | ||
if sp.Name == nil { | ||
err = errors.New("name is required for parameters") | ||
return | ||
} | ||
|
||
if checkingForSetAttr { | ||
if sp.Value != nil || sp.Attributes == nil { | ||
checkingForSetAttr = false | ||
} | ||
} else { //in this case, we are just checking valid parameters for SET | ||
if sp.DataType == nil || sp.Value == nil { | ||
err = errors.New("dataType and value are required for SET command") | ||
} | ||
} | ||
} | ||
|
||
if override != "" { | ||
command = override | ||
return | ||
} | ||
|
||
if checkingForSetAttr { // checked for SET_ATTRS properties until the end and found no violation | ||
command = COMMAND_SET_ATTRS | ||
return | ||
} | ||
|
||
command = COMMAND_SET | ||
return | ||
} | ||
|
||
func DecodeJsonPayload(req *http.Request, v interface{}, ReadEntireBody func(io.Reader)([]byte, error)) (err error) { | ||
if ReadEntireBody == nil { | ||
err = errors.New("method ReadEntireBody is undefined") | ||
return | ||
} | ||
payload, err := ReadEntireBody(req.Body) | ||
req.Body.Close() | ||
|
||
if err != nil { | ||
return | ||
} | ||
|
||
if len(payload) == 0 { | ||
err = ErrJsonEmpty | ||
return | ||
} | ||
|
||
err = json.Unmarshal(payload, v) | ||
if err != nil { | ||
return | ||
} | ||
return | ||
} | ||
|
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,41 @@ | ||
package main | ||
|
||
import ( | ||
"testing" | ||
"encoding/json" | ||
"github.com/Comcast/webpa-common/wrp" | ||
"github.com/stretchr/testify/assert" | ||
"net/http" | ||
) | ||
|
||
//Some tests are trivial but worth having | ||
func TestWrapInWrp(t *testing.T) { | ||
assert := assert.New(t) | ||
input := []byte("data") | ||
wrpMsg := wrp.Message{Type:wrp.SimpleRequestResponseMessageType, Payload:input} | ||
expected, expectedErr := json.Marshal(wrpMsg) | ||
|
||
actual, actualErr := WrapInWrp(input) | ||
assert.EqualValues(expected, actual) | ||
assert.EqualValues(expectedErr, actualErr) | ||
} | ||
|
||
func TestGetFormattedData(t *testing.T) { | ||
assert := assert.New(t) | ||
req, err := http.NewRequest("GET", "api/device/config?names=param1,param2,param3", nil) | ||
|
||
if err != nil { | ||
assert.FailNow("Could not make new request") | ||
} | ||
|
||
wdmp := &GetWDMP{Command:COMMAND_GET} | ||
wdmp.Names = []string{"param1","param2","param3"} | ||
|
||
expected, expectedErr := json.Marshal(wdmp) | ||
|
||
actual, actualErr := GetFlavorFormat(req,"attributes", "names", ",") | ||
|
||
assert.EqualValues(expected, actual) | ||
assert.EqualValues(expectedErr, actualErr) | ||
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did this method having in mind this description: "If you provide both value and attributes by default it will do set values" from swagger
If this description referred to the logic that applies on the device side and not in TR1D1UM, then this was a misunderstanding. @schmidtw