freeconf / restconf

Implementation of RESTCONF Management protocol - IETF RFC8040

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Question: how to view full config after change

b-kamphorst opened this issue · comments

commented

Hi!

I'm trying to set up a RESTCONF client through FREECONF. I'm working through the Car example and aim to present the user with configuration, update the configuration, and display the new configuration. Unfortunately, after a configuration update I fail to display the full configuration again.

My client code:

// Courtesy to https://freeconf.org/docs/examples/restconf-client/

package main

import (
	"github.com/freeconf/restconf"
	"github.com/freeconf/restconf/client"
	"github.com/freeconf/yang/nodeutil"
)

func connectClient() {

	// YANG: just need YANG file ietf-yang-library.yang, not the yang of remote system as that will
	// be downloaded as needed
	ypath := restconf.InternalYPath

	// Connect
	proto := client.ProtocolHandler(ypath)
	dev, err := proto("http://localhost:8080/restconf")
	if err != nil {
		panic(err)
	}

	// Get a browser to walk server's management API for car
	car, err := dev.Browser("car")
	if err != nil {
		panic(err)
	}
	root := car.Root()
	defer root.Release()

	actual, err := nodeutil.WritePrettyJSON(car.Root())
	if err != nil {
		panic(err)
	}
	println("========== initial config ==========")
	println(actual)

	n, err := nodeutil.ReadJSON(`{"speed":50}`)
	if err != nil {
		panic(err)
	}

	err = root.UpsertFrom(n)
	if err != nil {
		panic(err)
	}

	actual, err = nodeutil.WritePrettyJSON(car.Root())
	if err != nil {
		panic(err)
	}
	println("========== config after setting speed ==========")
	println(actual)

	n, err = nodeutil.ReadJSON(`{"pollInterval":50}`)
	if err != nil {
		panic(err)
	}
	err = root.UpsertFrom(n)
	if err != nil {
		panic(err)
	}
	actual, err = nodeutil.WritePrettyJSON(car.Root())
	if err != nil {
		panic(err)
	}
	println("========== config after setting pollInterval ==========")
	println(actual)
}

func main() {
	connectClient()
}

The output:

========== initial config ==========
{
"speed":50,
"pollInterval":50,
"running":false,
"miles":0,
"lastRotation":0,
"tire":[
  {
    "pos":0,
    "size":"H15",
    "worn":false,
    "wear":100,
    "flat":false},
  {
    "pos":1,
    "size":"H15",
    "worn":false,
    "wear":100,
    "flat":false},
  {
    "pos":2,
    "size":"H15",
    "worn":false,
    "wear":100,
    "flat":false},
  {
    "pos":3,
    "size":"H15",
    "worn":false,
    "wear":100,
    "flat":false}]}
========== config after setting speed ==========
{
"speed":50,
"tire":[
  {
    "size":"H15"},
  {
    "size":"H15"},
  {
    "size":"H15"},
  {
    "size":"H15"}]}
========== config after setting pollInterval ==========
{
"pollInterval":50,
"tire":[
  {
    "size":"H15"},
  {
    "size":"H15"},
  {
    "size":"H15"},
  {
    "size":"H15"}]}

I had expected that the nodeutil.WritePrettyJSON(car.Root()) calls would return the full config, not just the updated part + all configuration further dow the hierarchy. I'd be happy to learn how to obtain the full config (and why the above output is sensible).

Could you help me out? Looking forward to your (usually impressively swift) reply!

commented

I confirm that master (3972ca6) fixes this issue (rather than #62 as referenced by the commit message). Thank you!

commented

Perhaps we are not done entirely. This time, I select and print the value of a leaf after the config change. After that, I don't get the full config again if I print car.Root(). It appears that the root has been changed?

// Courtesy to https://freeconf.org/docs/examples/restconf-client/

package main

import (
	"github.com/freeconf/restconf"
	"github.com/freeconf/restconf/client"
	"github.com/freeconf/yang/nodeutil"
)

func connectClient() {

	// YANG: just need YANG file ietf-yang-library.yang, not the yang of remote system as that will
	// be downloaded as needed
	ypath := restconf.InternalYPath

	// Connect
	proto := client.ProtocolHandler(ypath)
	dev, err := proto("http://localhost:8080/restconf")
	if err != nil {
		panic(err)
	}

	// Get a browser to walk server's management API for car
	car, err := dev.Browser("car")
	if err != nil {
		panic(err)
	}
	root := car.Root()
	defer root.Release()

	actual, err := nodeutil.WritePrettyJSON(car.Root())
	if err != nil {
		panic(err)
	}
	println("========== initial config ==========")
	println(actual)

	n, err := nodeutil.ReadJSON(`{"speed":50}`)
	if err != nil {
		panic(err)
	}

	err = root.UpsertFrom(n)
	if err != nil {
		panic(err)
	}

	actual, err = nodeutil.WritePrettyJSON(car.Root())
	if err != nil {
		panic(err)
	}
	println("========== config after setting speed ==========")
	println(actual)

	speed, err := root.Find("speed")
	if err != nil {
		panic(err)
	}
	speed_json, err := nodeutil.WritePrettyJSON(speed)
	if err != nil {
		panic(err)
	}
	println("========== just showing the value of speed ==========")
	println(speed_json)
        
        actual, err := nodeutil.WritePrettyJSON(car.Root())
	if err != nil {
		panic(err)
	}
	println("========== full config ==========")
	println(actual)
}

func main() {
	connectClient()
}

Output:

========== initial config ==========
{
"speed":1000,
"pollInterval":1000,
... // actual full config
========== config after setting speed ==========
{
"speed":50,
"pollInterval":1000,
... // actual full config
========== just showing the value of speed ==========
{
"speed":50}
========== full config ==========
{
"speed":50}

Not sure whether this is related though, as in the original example I didn't make a call to root.Find.

commented

Note: the example runs as expected when I add speed.Release() after println(speed_json). Still, it is counterintuitive to me that root prints only partial information while a reference a speed selection is locked (and why that was locked to begin with).

commented