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

Question: how to view full config after change #61

Open
b-kamphorst opened this issue May 1, 2024 · 9 comments
Open

Question: how to view full config after change #61

b-kamphorst opened this issue May 1, 2024 · 9 comments

Comments

@b-kamphorst
Copy link
Contributor

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!

@dhubler
Copy link
Collaborator

dhubler commented May 1, 2024 via email

@dhubler
Copy link
Collaborator

dhubler commented May 7, 2024 via email

@dhubler
Copy link
Collaborator

dhubler commented May 7, 2024 via email

@b-kamphorst
Copy link
Contributor Author

b-kamphorst commented May 7, 2024

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

@b-kamphorst
Copy link
Contributor Author

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.

@b-kamphorst b-kamphorst reopened this May 7, 2024
@dhubler
Copy link
Collaborator

dhubler commented May 7, 2024 via email

@b-kamphorst
Copy link
Contributor Author

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).

@dhubler
Copy link
Collaborator

dhubler commented May 16, 2024 via email

@b-kamphorst
Copy link
Contributor Author

b-kamphorst commented May 16, 2024 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants