openconfig / ygot

A YANG-centric Go toolkit - Go/Protobuf Code Generation; Validation; Marshaling/Unmarshaling
Apache License 2.0
286 stars 107 forks source link

xmlName field in the generated go structs #144

Open mjoshiNetElastic opened 6 years ago

mjoshiNetElastic commented 6 years ago

Hi, Is there any way I can generate xml namespace tags and XMLName xml.name field as a part of the auto-generated go structs?

Thanks.

robshakir commented 6 years ago

Hi,

Could you clarify the question a little bit please? At the moment, we do not store the YANG module's namespace statement in the tags for struct fields - we do store the module's name since this is required for rendering to JSON).

I'm not sure what the XMLName xml.name field is - do you have a reference?

If you are planning to expand ygot to marshal and unmarshal to and from XML, then I'm happy to discuss how the design for this might look. I think that it would need a few steps:

Thanks, r.

mjoshiNetElastic commented 6 years ago

Hi Rob, what I am trying to get at here is to be able to use the generated go structs as a base for for constructing xml marshal calls so as to generate netconf edit-config requests.

type editConfig struct { XMLName xml.Namexml:"edit-config" Target stringxml:"target" Config interface{} } type Config struct { XMLName xml.Namexml:"config"` Config interface{} }

type IETFRouting_Router struct { XMLName xml.Name xml:"urn:ietf:params:xml:ns:yang:ietf-routing router" Config interface{} }`

Need this - **// IETFRouting_Router_Bgp represents the /ietf-routing/router/bgp YANG schema element. type IETFRouting_Router_Bgp struct { _XMLName xml.Namexml:"bgp-config bgp"_ LocalAs uint32xml:"local-as" }**

generated by ygot: **// IETFRouting_Router_Bgp represents the /ietf-routing/router/bgp YANG schema element. type IETFRouting_Router_Bgp struct { LocalAs *uint32path:"local-as" module:"bgp-config"` }**

bgp := &IETFRouting_Router_Bgp{LocalAs: 1233} rtr := IETFRouting_Router{Config: bgp} ed := &editConfig{Target: "", Config: Config{Config: rtr}} op, err := xml.MarshalIndent(ed, " ", " ") if err != nil { fmt.Println("Marshal; error") return }`

the end result is to generate a xml string looking like this: `

              <config>
                <router xmlns="urn:ietf:params:xml:ns:yang:ietf-routing">
                      <bgp xmlns="bgp-config">
                     <local-as>888</local-as>
            </bgp>
              </router>
           </config>
         </edit-config>`
robshakir commented 6 years ago

This isn't something that is implemented today in ygot. The current recommended approach to get data instances to a device is to use RFC7951 JSON (a number of platforms already support this, and I believe that they do this via NETCONF, although we'd encourage you to look at gNMI).

Alternatively, a marshalling approach needs to be written for XML. You could look at how the JSON marshalling is done in ygot/render.go to give a view as to how to this -- since the layout of the structs will need some wrangling to serialise to XML, and can't just be done directly. As a part of this implementation, you'd need to look into the way to have the namespace available as per my previous comment.