Skip to content
Snippets Groups Projects
model.go 3.5 KiB
Newer Older
  • Learn to ignore specific revisions
  • /* Copyright 2017 Google Inc.
    
    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at
    
        https://www.apache.org/licenses/LICENSE-2.0
    
    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
    */
    
    package gnmi
    
    import (
    	oc "code.fbi.h-da.de/cocsn/yang-models/generated/arista"
    	"errors"
    	"fmt"
    	log "github.com/sirupsen/logrus"
    	"net"
    	"reflect"
    	"sort"
    
    	"github.com/openconfig/goyang/pkg/yang"
    	"github.com/openconfig/ygot/ygot"
    	"github.com/openconfig/ygot/ytypes"
    
    	pb "github.com/openconfig/gnmi/proto/gnmi"
    )
    
    // JSONUnmarshaler is the signature of the Unmarshal() function in the GoStruct code generated by openconfig ygot library.
    type JSONUnmarshaler func([]byte, ygot.GoStruct, ...ytypes.UnmarshalOpt) error
    
    // GoStructEnumData is the data type to maintain GoStruct enum type.
    type GoStructEnumData map[string]map[int64]ygot.EnumDefinition
    
    // Model contains the model data and GoStruct information for the device to config.
    type Model struct {
    	modelData       []*pb.ModelData
    	structRootType  reflect.Type
    	schemaTreeRoot  *yang.Entry
    	jsonUnmarshaler JSONUnmarshaler
    	enumData        GoStructEnumData
    }
    
    // NewModel returns an instance of Model struct.
    func NewModel(m []*pb.ModelData, t reflect.Type, r *yang.Entry, f JSONUnmarshaler, e GoStructEnumData) *Model {
    	return &Model{
    		modelData:       m,
    		structRootType:  t,
    		schemaTreeRoot:  r,
    		jsonUnmarshaler: f,
    		enumData:        e,
    	}
    }
    
    func (m *Model) newRootValue() interface{} {
    	return reflect.New(m.structRootType.Elem()).Interface()
    }
    
    // NewConfigStruct creates a ValidatedGoStruct of this model from jsonConfig. If jsonConfig is nil, creates an empty GoStruct.
    func (m *Model) NewConfigStruct(jsonConfig []byte) (ygot.ValidatedGoStruct, error) {
    	rootStruct, ok := m.newRootValue().(ygot.ValidatedGoStruct)
    	if !ok {
    		return nil, errors.New("root node is not a ygot.ValidatedGoStruct")
    	}
    	ifaces, err := getInterfaces()
    	if err != nil {
    		return nil, err
    	}
    	device, ok := rootStruct.(*oc.Device)
    	if !ok {
    		return nil, errors.New("root node is not a oc.Device")
    	}
    	device.Interfaces = ifaces
    	return device, nil
    }
    
    // SupportedModels returns a list of supported models.
    func (m *Model) SupportedModels() []string {
    	mDesc := make([]string, len(m.modelData))
    	for i, m := range m.modelData {
    		mDesc[i] = fmt.Sprintf("%s %s", m.Name, m.Version)
    	}
    	sort.Strings(mDesc)
    	return mDesc
    }
    
    func getInterfaces() (*oc.OpenconfigInterfaces_Interfaces, error) {
    	ifaces, err := net.Interfaces()
    	if err != nil {
    		log.Fatal()
    	}
    	interfaces := &oc.OpenconfigInterfaces_Interfaces{
    		Interface: make(map[string]*oc.OpenconfigInterfaces_Interfaces_Interface),
    	}
    
    	for _, tInterface := range ifaces {
    		var mtu *uint16
    		var name *string
    		var index *uint32
    
    		rmtu := uint16(tInterface.MTU)
    		rname := tInterface.Name
    		rindex := uint32(tInterface.Index)
    
    		mtu = &rmtu
    		name = &rname
    		index = &rindex
    
    		iface, err := interfaces.NewInterface(tInterface.Name)
    		if err != nil {
    			return nil, err
    		}
    		iface.State = &oc.OpenconfigInterfaces_Interfaces_Interface_State{
    			Ifindex: &rindex,
    			Mtu:     &rmtu,
    			Name:    &rname,
    		}
    		iface.State.Name = name
    		iface.State.Mtu = mtu
    		iface.State.Ifindex = index
    	}
    	return interfaces, nil
    }