Skip to content

Question: how to re-retrieve server configuration #62

@b-kamphorst

Description

@b-kamphorst

Hi!

I'm still tweaking the "Next step" and "RESTCONF client" example. This time I want to start the car through the RPC and then see that status reflected in the car:running leaf:

import (
	"fmt"
	"strings"
	"testing"

	"github.com/freeconf/examples/car"
	"github.com/freeconf/restconf"
	"github.com/freeconf/restconf/client"
	"github.com/freeconf/restconf/device"
	"github.com/freeconf/yang/fc"
	"github.com/freeconf/yang/node"
	"github.com/freeconf/yang/nodeutil"
	"github.com/freeconf/yang/source"
)

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:9998/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()

        // Subscribe to notifications
        sel, err = root.Find("update")
	if err != nil {
		panic(err)
	}
	defer sel.Release()
	unsub, err := sel.Notifications(func(n node.Notification) {
		msg, err := nodeutil.WriteJSON(n.Event)
		if err != nil {
			panic(err)
		}
		fmt.Println(msg)
	})
	if err != nil {
		panic(err)
	}
	defer unsub()

	// Wait a bit just to ensure we receive the car started notification
	time.Sleep(time.Second)

	// Start car
	sel, err = root.Find("start")
	if err != nil {
		panic(err)
	}
	if _, err = sel.Action(nil); err != nil {
		panic(err)
	}

	println("Waiting for some notifications...")
	for i := 0; i < 10; i++ {
		sel, err = root.Find("running")
		if err != nil {
			panic(err)
		}
		running, err := sel.Get()
		if err != nil {
			panic(err)
		}
		fmt.Printf("Is the car running? %v\n", running)
		time.Sleep(time.Second)
	}
}

func main() {
	connectClient()
}

Output:

Waiting for some notifications...
Is the car running? false
{"event":"carStarted"}
Is the car running? false
Is the car running? false
Is the car running? false
Is the car running? false
Is the car running? false
{"event":"flatTire"}
{"event":"carStopped"}
Is the car running? false
Is the car running? false
Is the car running? false
Is the car running? false

I understand that the configuration is retrieved from the server and cached locally. I just wonder how I can forcibly re-retrieve the configuration? I appreciate your time!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions