Example #1
1
func main() {
	var t toscalib.ServiceTemplateDefinition
	err := t.Parse(os.Stdin)
	if err != nil {
		log.Fatal(err)
	}
	// Creates a new graph
	g := gographviz.NewGraph()
	g.AddAttr("", "rankdir", "LR")
	g.SetName("G")
	g.SetDir(true)
	e := toscaexec.GeneratePlaybook(t)
	for i, p := range e.Index {
		g.AddNode("G", fmt.Sprintf("%v", i),
			map[string]string{
				"id":    fmt.Sprintf("\"%v\"", i),
				"label": fmt.Sprintf("\"%v|%v\"", p.NodeTemplate.Name, p.OperationName),
				"shape": "\"record\"",
			})
	}
	l := e.AdjacencyMatrix.Dim()
	for r := 0; r < l; r++ {
		for c := 0; c < l; c++ {
			if e.AdjacencyMatrix.At(r, c) == 1 {
				g.AddEdge(fmt.Sprintf("%v", r), fmt.Sprintf("%v", c), true, nil)

			}

		}

	}
	log.Println("here")
	s := g.String()
	fmt.Println(s)
	/*
		d, _ := yaml.Marshal(e)
		fmt.Println(string(d))
	*/
	/*
		for i, n := range e.Index {
			log.Printf("[%v] %v:%v -> %v %v",
				i,
				n.NodeTemplate.Name,
				n.OperationName,
				n.NodeTemplate.Interfaces[n.InterfaceName].Operations[n.OperationName].Implementation,
				n.NodeTemplate.Interfaces[n.InterfaceName].Operations[n.OperationName].Inputs,
			)
		}
	*/
}
Example #2
0
func togorch(t toscalib.ServiceTemplateDefinition, operations []string) orchestrator.Graph {

	e := toscaexec.GeneratePlaybook(t)
	var g orchestrator.Graph
	g.Digraph = structure.Matrix(e.AdjacencyMatrix)
	g.Name = t.Description
	for i, n := range e.Index {
		var node orchestrator.Node
		node.ID = i
		node.Name = fmt.Sprintf("%v:%v", n.NodeTemplate.Name, n.OperationName)
		if n.OperationName == "noop" {
			node.Engine = "nil"
		} else {
			node.Engine = "toscassh"
		}
		torun := false
		for _, operation := range operations {
			if operation == n.OperationName {
				torun = true
			}
		}
		if !torun {
			node.Engine = "nil"
		}

		// Sets the target

		node.Target = getComputeTarget(t, n)
		attrTarget := getAttributeTarget(t, n)

		ctxlog := log.WithFields(logrus.Fields{
			"Node":   node.Name,
			"ID":     node.ID,
			"Target": node.Target,
		})
		node.Artifact = n.NodeTemplate.Interfaces[n.InterfaceName].Operations[n.OperationName].Implementation
		// Get inputs from the node type
		for argName, argValue := range t.NodeTypes[n.NodeTemplate.Type].Interfaces[n.InterfaceName][n.OperationName].Inputs {
			for get, val := range argValue.Value {
				switch get {
				case "value":
					node.Args = append(node.Args, fmt.Sprintf("%v=%v", argName, val[0]))
				case "get_input":
					value := e.Inputs[val[0]]
					node.Args = append(node.Args, fmt.Sprintf("%v=%v", argName, value))
				case "get_property":
					tgt := val[0]
					switch val[0] {
					case "SELF":
						tgt = n.NodeTemplate.Name
					case "HOST":
						tgt = node.Target
					}
					prop, err := t.GetProperty(tgt, val[1])
					vals, err := t.EvaluateStatement(prop)
					node.Args = append(node.Args, fmt.Sprintf("%v=%v", argName, vals))
					if err != nil {
						ctxlog.Warnf("Cannot find property %v on %v", val[1], val[0])
					}
				case "get_attribute":
					tgt := val[0]
					switch val[0] {
					case "SELF":
						tgt = n.NodeTemplate.Name
					case "HOST":
						tgt = attrTarget
					}
					node.Args = append(node.Args, fmt.Sprintf("%v=get_attribute %v.*:%v", argName, tgt, val[1]))
				default:
					node.Args = append(node.Args, fmt.Sprintf("DEBUG: %v=%v", argName, val))

				}
			}
		}
		// Get inputs from the node template
		for argName, argValue := range n.NodeTemplate.Interfaces[n.InterfaceName].Operations[n.OperationName].Inputs {
			for get, val := range argValue {
				switch get {
				case "value":
					node.Args = append(node.Args, fmt.Sprintf("%v=%v", argName, val[0]))
				case "get_input":
					value := e.Inputs[val[0]]
					node.Args = append(node.Args, fmt.Sprintf("%v=%v", argName, value))
				case "get_property":
					prop, err := t.GetProperty(val[0], val[1])
					node.Args = append(node.Args, fmt.Sprintf("%v=%v", argName, prop))
					if err != nil {
						ctxlog.Warnf("Cannot find property %v on %v", val[1], val[0])
					}
				case "get_attribute":
					node.Args = append(node.Args, fmt.Sprintf("%v=get_attribute %v*:%v", argName, val[0], val[1]))
				default:
					node.Args = append(node.Args, fmt.Sprintf("DEBUG: %v=%v", argName, val))

				}
			}
		}
		// Sets the output
		// For every node, the attributes of the node or its type is an output
		node.Outputs = make(map[string]string, 0)
		for k, _ := range n.NodeTemplate.Refs.Type.Attributes {
			node.Outputs[k] = ""
		}
		//for k, v := range n.NodeTemplate.Attributes {
		//node.Outputs[k] = v
		//}
		g.Nodes = append(g.Nodes, node)
	}
	return g
}