package main import ( "encoding/json" "fmt" "net/url" "os" "strings" "cloud.o-forge.io/core/oc-catalog/models" "github.com/beego/beego/v2/core/logs" "github.com/sbabiv/xml2map" "github.com/tidwall/gjson" ) type Link struct { Src string Dst string } type Graph struct { Datas []models.DataModel Computings []models.ComputingModel Datacenters []models.DatacenterModel Storages []models.StorageModel Links map[string]models.Link ws HttpQuery } // Create a dictionnaries with each each existing workflow from a workspace, associated to the JSON representation of its content func (g *Graph) GetGraphList(apiurl string) (map[string]string, error) { g.ws.Init(apiurl) body, err := g.ws.Get("v1/workspace/list") if err != nil { return nil, err } workspaces := make(map[string]string) result := gjson.Get(string(body), "Workflows") result.ForEach(func(key, value gjson.Result) bool { workspaces[key.Str] = value.String() return true // keep iterating }) return workspaces, nil } // Create the objects from the mxgraphxml stored in the workflow given as a parameter func (g *Graph) LoadFrom(workspace string) error { // Extract the xmlgraph from the given workspace xml := gjson.Get(workspace, "MxgraphXML").String() decodedValue, err := url.QueryUnescape(xml) if err != nil { return err } os.WriteFile("graph.xml", []byte(decodedValue), 0660) decoder := xml2map.NewDecoder(strings.NewReader(decodedValue)) result, err := decoder.Decode() if err != nil { return err } // Retrieve the content of mxcell and object elements cells := result["mxGraphModel"].(map[string]interface{})["root"].(map[string]interface{})["mxCell"].([]map[string]interface{}) obj := result["mxGraphModel"].(map[string]interface{})["root"].(map[string]interface{})["object"] // Append the content of obj to cells depending on if it's a single key/value pair or a list of it switch v := obj.(type) { case map[string]interface{}: cells = append(cells, obj.(map[string]interface{})) _ = v // fmt.Printf("One: %v", v) case []map[string]interface{}: cells = append(cells, obj.([]map[string]interface{})...) // fmt.Printf("Many: %v", v) } // dictionnary := make(map[string]string) var idlinks []Link g.GetWorkflowComponents(workspace) g.GetLinks(workspace) for _, element := range cells { // id := element["@id"].(string) // // Case MXCell // if _, ok := element["@style"]; ok { // if _, ok2 := element["@rID"]; ok2 { // // Resolve elements // // fmt.Print(id + ": ") // // fmt.Println(element["@rID"], element["@rType"]) // // fmt.Println(element) // dictionnary[id] = element["@rID"].(string) // g.addElementByType(element) // } // } else { // // Case object : contains user's input through the GUI // if _, ok := element["mxCell"]; ok { // // Attribute values // // Extracts the cell ids // element = element["mxCell"].(map[string]interface{}) // if _, ok := element["@style"]; ok { // if _, ok2 := element["@rID"]; ok2 { // // Resolve elements // // fmt.Print(id + ": ") // // fmt.Println(element["@rID"], element["@rType"]) // // fmt.Println(element) // dictionnary[id] = element["@rID"].(string) // g.addElementByType(element) // } // } // } // } // register links if src, ok := element["@source"]; ok { //src = element["@source"].(string) idlinks = append(idlinks, Link{Src: src.(string), Dst: element["@target"].(string)}) // fmt.Println("Link: " + src.(string) + " " + element["@target"].(string)) } } // translate links // for _, link := range idlinks { // g.Links = append(g.Links, Link{Src: dictionnary[link.Src], Dst: dictionnary[link.Dst]}) // fmt.Println("Link: " + link.Src + " " + link.Dst + " : " + dictionnary[link.Src] + " " + dictionnary[link.Dst]) // } return nil } // func (g *Graph) addElementByType(element map[string]interface{}) { // if element["@rType"] == "data" { // g.AddDataModel(element["@rID"].(string)) // } // if element["@rType"] == "datacenter" { // g.AddDatacenterModel(element["@rID"].(string)) // } // if element["@rType"] == "computing" { // g.AddComputingModel(element["@rID"].(string)) // } // if element["@rType"] == "storage" { // g.AddStorageModel(element["@rID"].(string)) // } // } // TODO : extract all the JSON/data processing to a new object that takes // the current graph as an attribute and deals with adding new objects // to it, depending on their type // Create the objects that correspond to each component // in a workflow, combining the user input and the base components attributes func (g *Graph) GetWorkflowComponents(workflow string){ types := []string{"computing","datacenter","data","storage"} // create a constant for more maintainability OR even better get the list of all component's type for this WF for _, component_type := range types { // Retrieve the dict of component for a specific type in the workflow result := gjson.Get(workflow, component_type) if (result.Type != gjson.Null) { result.ForEach(func(id, value gjson.Result) bool{ comp_id := value.Get("referenceID").Str if (comp_id != "") { switch component_type { case "computing": g.AddComputingModel(comp_id, value, id.Str) case "data": g.AddDataModel(comp_id, value, id.Str) case "datacenter": g.AddDatacenterModel(comp_id, value, id.Str) case "storage": g.AddStorageModel(comp_id, value, id.Str) default : logs.Critical("Component type doesn't match a know type : " + component_type) } } return true }) } } } func (g *Graph) GetLinks(workflow string){ g.Links = make(map[string]models.Link) result := gjson.Get(workflow, "link") if (result.Type != gjson.Null) { result.ForEach(func(id, value gjson.Result) bool{ var l models.Link fmt.Print(id) fmt.Println(value.Str) json.Unmarshal([]byte(value.Raw),&l) g.Links[id.Str] = l return true }) } } func (g *Graph) AddDataModel(id string, user_input gjson.Result, wf_id string) error { var d models.DataModel resp, err := g.ws.Get("v1/data/" + id) if err != nil { return err } json.Unmarshal(resp, &d) json.Unmarshal([]byte(user_input.Raw),&d.DataNEWModel) d.ID = wf_id g.Datas = append(g.Datas, d) return nil } func (g *Graph) AddDatacenterModel(id string, user_input gjson.Result, wf_id string) error { var d models.DatacenterModel resp, err := g.ws.Get("v1/datacenter/" + id) if err != nil { return err } json.Unmarshal(resp, &d) json.Unmarshal([]byte(user_input.Raw),&d.DatacenterNEWModel) d.ID = wf_id g.Datacenters = append(g.Datacenters, d) return nil } func (g *Graph) AddComputingModel(id string, user_input gjson.Result, wf_id string) error { var c models.ComputingModel resp, err := g.ws.Get("v1/computing/" + id) if err != nil { return err } json.Unmarshal(resp, &c) json.Unmarshal([]byte(user_input.Raw),&c.ComputingNEWModel) c.ID = wf_id g.Computings = append(g.Computings, c) return nil } func (g *Graph) AddStorageModel(id string, user_input gjson.Result, wf_id string) error { var s models.StorageModel resp, err := g.ws.Get("v1/data/" + id) if err != nil { return err } json.Unmarshal(resp, &s) json.Unmarshal([]byte(user_input.Raw),&s.StorageNEWModel) s.ID = wf_id g.Storages = append(g.Storages, s) return nil } func (g *Graph) ExportToArgo(id string) error { end_links := make(map[string]models.Link) editable_link_list := make(map[string]models.Link) editable_link_list = g.Links for i, link := range g.Links { if (!link.DCLink && !g.isSource(link.Destination,i)){ end_links[i] = link } } for index, end_link := range end_links { has_parent := true j := index // index_list := make([]int, len(g.Links)) current_link := end_link current_src := current_link.Source _ = current_src str_links := g.getComponentName(current_link.Source) + " linked with " + g.getComponentName(current_link.Destination) for has_parent { previous, previous_index := g.hasPreviousLink(j,editable_link_list) if previous { current_link = g.Links[previous_index] str_links = g.getComponentName(current_link.Source) + " linked with " + g.getComponentName(current_link.Destination) + " --> " + str_links delete(editable_link_list,j) j = previous_index } else { has_parent = false } } fmt.Println(str_links) } return nil } func (g *Graph) ExportToHelm(id string) error { return nil } // Return if it exists a link where Destination is the same as comp_id func (g *Graph) isDestination(comp_id string,link_id string) bool { for i, link := range g.Links{ if(i !=link_id && link.Destination == comp_id){ return true } } return false } // Return if it exists a link where Source is the same as comp_id func (g *Graph) isSource(comp_id string,link_id string) bool { for i, link := range g.Links{ if(i !=link_id && link.Source == comp_id && !link.DCLink){ return true } } return false } // Returns an index number if their is a link in g.Links // with the same Destination id that the Source id in g.Links[linkIndex] // or nil if not func (g *Graph) hasPreviousLink(link_id string,list_link map[string]models.Link) (hasPrevious bool, previous_id string) { for i, link := range list_link{ if(i != link_id && link.Destination == g.Links[link_id].Source){ return true, i } } return false, "" } func (g *Graph) getComponentName(id string) string { for _, comp := range g.Computings{ if comp.ID == id { return comp.Name } } for _, storage := range g.Storages{ if storage.ID == id { return storage.Name } } for _, data := range g.Datas{ if data.ID == id { return data.Name } } return "" } func getMapKeys(m map[int]interface{}) []int { var list_keys []int for key := range m { fmt.Println("Key:", key) } return list_keys }