Skip to content
Snippets Groups Projects
controller.go 1.25 KiB
Newer Older
  • Learn to ignore specific revisions
  • package nucleus
    
    import (
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    	yangPro "code.fbi.h-da.de/cocsn/yang-modules/generated/ciena"
    
    	"github.com/openconfig/ygot/ygot"
    
    // This is a test function in order to see how to generate JSON encoded openconfig stuff
    
    
    func AssembleJSON() {
    	// Build my device
    
    Martin Stiemerling's avatar
    Martin Stiemerling committed
    	d := &yangPro.Device{}
    
    
    	interfaces, _ := net.Interfaces()
    	for _, iface := range interfaces {
    		fmt.Println(iface.Name)
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    		i, err := d.WaveserverInterfaces.NewLogicalInterface("en0")
    
    		if err != nil {
    			panic(err)
    		}
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    		i.State = &yangPro.CienaWaveserverInterfaces_WaveserverInterfaces_LogicalInterface_State{
    			AdminState:       0,
    			OperationalState: 0,
    		}
    
    		//i.Mtu = ygot.Uint16(iface.MTU)
    	}
    
    	// EmitJSON from the ygot library directly does .Validate() and outputs JSON in
    	// the specified format.
    	json, err := ygot.EmitJSON(d, &ygot.EmitJSONConfig{
    		Format: ygot.RFC7951,
    		Indent: "  ",
    		RFC7951Config: &ygot.RFC7951JSONConfig{
    			AppendModuleName: true,
    		},
    	})
    	if err != nil {
    		panic(fmt.Sprintf("JSON demo error: %v", err))
    	}
    
    Martin Stiemerling's avatar
    Martin Stiemerling committed
    
    	// and now try to read the data from it...
    	// Device struct to unmarshal into.
    	loadd := &yangPro.Device{}
    	if err := yangPro.Unmarshal([]byte(json), loadd); err != nil {
    		panic(fmt.Sprintf("Cannot unmarshal JSON: %v", err))
    	}
    
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    }