Skip to content
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

get metadata bug fix #49

Merged
merged 3 commits into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion server/ServerSignalSpecification.vspec
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ Server.Config.Protocol.Mqtt.Protobuf:
Server.Config.Protocol.Mqtt.Protobuf.Topic:
type: attribute
datatype: string
description: Postfix "/protobuf" added to the MQTT topic name for the protobuf encoded payload format.
description: Postfix '/protobuf' added to the MQTT topic name for the protobuf encoded payload format.

Server.Config.Protocol.Grpc:
type: branch
Expand Down
Binary file modified server/vissv2server/forest/server.binary
Binary file not shown.
69 changes: 22 additions & 47 deletions server/vissv2server/vissv2server.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,22 @@ import (
// "fmt"

"fmt"
"io"
// "io"
// "log"
"os"
// "bufio"

"github.com/akamensky/argparse"
// "github.com/gorilla/mux"

"bytes"
// "bytes"
"encoding/json"
//"io/ioutil"
"net/http"
// "net/http"
// "sort"
"strconv"
"strings"
"time"
// "time"

"github.com/covesa/vissr/server/vissv2server/atServer"
"github.com/covesa/vissr/server/vissv2server/grpcMgr"
Expand Down Expand Up @@ -300,7 +300,7 @@ func jsonifyTreeNode(nodeHandle *utils.Node_t, jsonBuffer string, depth int, max
nodeName := utils.VSSgetName(nodeHandle)
newJsonBuffer += `"` + nodeName + `":{`
nodeType := int(utils.VSSgetType(nodeHandle))
utils.Info.Printf("nodeType=%d", nodeType)
// utils.Info.Printf("nodeType=%d", nodeType)
newJsonBuffer += `"type":` + `"` + nodeTypesToString(nodeType) + `",`
nodeDescr := utils.VSSgetDescr(nodeHandle)
newJsonBuffer += `"description":` + `"` + nodeDescr + `",`
Expand Down Expand Up @@ -344,43 +344,14 @@ func countPathSegments(path string) int {
}

func getNoScopeList(tokenContext string) ([]string, int) {
// call ATS to get list
hostIp := utils.GetServerIP()
url := "http://" + hostIp + ":8600/atserver"

data := []byte(`{"context":"` + tokenContext + `"}`)

req, err := http.NewRequest("POST", url, bytes.NewBuffer(data))
if err != nil {
utils.Error.Print("getNoScopeList: Error reading request. ", err)
return nil, 0
}

// Set headers
req.Header.Set("Access-Control-Allow-Origin", "*")
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Host", hostIp+":8600")

// Set client timeout
client := &http.Client{Timeout: time.Second * 10}

// Send request
resp, err := client.Do(req)
if err != nil {
utils.Error.Print("getNoScopeList: Error sending request. ", err)
return nil, 0
}
defer resp.Body.Close()

body, err := io.ReadAll(resp.Body)
if err != nil {
utils.Error.Print("getNoScopeList:Error reading response. ", err)
return nil, 0
}
// call ATS to get noscope list
request := `{"context":"` + tokenContext + `"}`
atsChannel[0] <- request
body := <-atsChannel[0]
var noScopeMap map[string]interface{}
err = json.Unmarshal(body, &noScopeMap)
err := json.Unmarshal([]byte(body), &noScopeMap)
if err != nil {
utils.Error.Printf("initPurposelist:error data=%s, err=%s", data, err)
utils.Error.Printf("initPurposelist:error body=%s, err=%s", body, err)
return nil, 0
}
return extractNoScopeElementsLevel1(noScopeMap)
Expand All @@ -390,12 +361,12 @@ func extractNoScopeElementsLevel1(noScopeMap map[string]interface{}) ([]string,
for k, v := range noScopeMap {
switch vv := v.(type) {
case string:
utils.Info.Println(k, "is string", vv)
// utils.Info.Println(k, "is string", vv)
noScopeList := make([]string, 1)
noScopeList[0] = vv
return noScopeList, 1
case []interface{}:
utils.Info.Println(k, "is an array:, len=", strconv.Itoa(len(vv)))
// utils.Info.Println(k, "is an array:, len=", strconv.Itoa(len(vv)))
return extractNoScopeElementsLevel2(vv)
default:
utils.Info.Println(k, "is of an unknown type")
Expand All @@ -410,7 +381,7 @@ func extractNoScopeElementsLevel2(noScopeMap []interface{}) ([]string, int) {
for k, v := range noScopeMap {
switch vv := v.(type) {
case string:
utils.Info.Println(k, "is string", vv)
// utils.Info.Println(k, "is string", vv)
noScopeList[i] = vv
default:
utils.Info.Println(k, "is of an unknown type")
Expand All @@ -427,11 +398,11 @@ func synthesizeJsonTree(path string, depth int, tokenContext string, VSSTreeRoot
var matches int
noScopeList, numOfListElem := getNoScopeList(tokenContext)
//utils.Info.Printf("noScopeList[0]=%s", noScopeList[0])
matches, searchData = searchTree(VSSTreeRoot, path, false, false, numOfListElem, noScopeList, nil)
matches, searchData = searchTree(VSSTreeRoot, path+".*", true, false, numOfListElem, noScopeList, nil)
if matches < countPathSegments(path) {
return ""
}
subTreeRoot := searchData[matches-1].NodeHandle
subTreeRoot := searchData[0].NodeHandle
utils.Info.Printf("synthesizeJsonTree:subTreeRoot-name=%s", utils.VSSgetName(subTreeRoot))
if depth == 0 {
depth = 100
Expand Down Expand Up @@ -637,16 +608,20 @@ func issueServiceRequest(requestMap map[string]interface{}, tDChanIndex int, sDC
if len(tokenContext) == 0 {
tokenContext = "Undefined+Undefined+Undefined"
}
//utils.Info.Printf("filterList[%d].Type=%s, filterList[%d].Parameter=%s", i, filterList[i].Type, i, filterList[i].Parameter)
treedepth, err := strconv.Atoi(filterList[i].Parameter)
if err == nil {
metadata := ""
metadata = synthesizeJsonTree(requestMap["path"].(string), 2, tokenContext, VSSTreeRoot) // TODO: depth setting via filtering?
metadata = synthesizeJsonTree(requestMap["path"].(string), treedepth, tokenContext, VSSTreeRoot)
if len(metadata) > 0 {
delete(requestMap, "path")
delete(requestMap, "filter")
requestMap["ts"] = utils.GetRfcTime()
backendChan[tDChanIndex] <- utils.AddKeyValue(utils.FinalizeMessage(requestMap), "metadata", metadata)
return
}
utils.Error.Printf("Metadata not available.")
}
utils.Error.Printf("Metadata error")
utils.SetErrorResponse(requestMap, errorResponseMap, 0, "") //bad_request
backendChan[tDChanIndex] <- utils.FinalizeMessage(errorResponseMap)
return
Expand Down