Skip to content
Snippets Groups Projects
client.go 4.08 KiB
Newer Older
  • Learn to ignore specific revisions
  • Manuel Kieweg's avatar
    Manuel Kieweg committed
    package ciena
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    
    import (
    
    	"code.fbi.h-da.de/cocsn/gosdn/database"
    	"code.fbi.h-da.de/cocsn/gosdn/log"
    	"code.fbi.h-da.de/cocsn/gosdn/nucleus/interfaces"
    	"code.fbi.h-da.de/cocsn/gosdn/sbi/restconf/util"
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    	apiclient "code.fbi.h-da.de/cocsn/swagger/apis/mcp/client"
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    	"code.fbi.h-da.de/cocsn/yang-modules/generated/tapi"
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    	"crypto/tls"
    	"github.com/go-openapi/runtime"
    	httptransport "github.com/go-openapi/runtime/client"
    	"github.com/go-openapi/strfmt"
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    	"github.com/openconfig/ygot/ygot"
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    	"net/http"
    )
    
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    //MCPClient handles requests to a Ciena MCP RESTCONF endpoint
    type MCPClient struct {
    	transport *httptransport.Runtime
    	client    *apiclient.ServiceTopologyTAPI
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    	database  *database.Database
    
    	buffer    *bytes.Buffer
    
    	config    *interfaces.ClientConfig
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    	device    ygot.GoStruct
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    }
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    
    
    // GetConfig returns a ClientConfig struct containing
    // the current configuration stat of the Ciena SBI client
    
    func (c MCPClient) GetConfig() interfaces.ClientConfig {
    	return *c.config
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    }
    
    
    //NewMCPClient creates a Ciena flavores TAPI client
    
    func NewMCPClient(endpoint, username, password string, database *database.Database, config *interfaces.ClientConfig) *MCPClient {
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    	// create the transport
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    	transport := httptransport.New(endpoint, "/", nil)
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    	transport.Transport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
    	// create the API client, with the transport
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    	basicAuth := httptransport.BasicAuth(username, password)
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    	// authenticate client
    	transport.DefaultAuthentication = basicAuth
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    	client := apiclient.New(transport, strfmt.Default)
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    
    
    	buffer := new(bytes.Buffer)
    	transport.Consumers[runtime.JSONMime] = util.YANGConsumer{Data: buffer}
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    
    
    	return &MCPClient{
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    		transport: transport,
    		client:    client,
    
    		database:  database,
    		buffer:    buffer,
    
    		config:    config,
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    		device:    &tapi.Device{},
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    	}
    }
    
    // GetConnections implements the TAPI Connectivity GetConnections call with a grain of
    // Ciena salt. The response is written to the client's buffer and passed to the database
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    func (c *MCPClient) GetConnections() error {
    
    	defer c.buffer.Reset()
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    	_, err := c.client.TapiConnectivityCore.GetTapiCoreContextConnection(nil)
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    	if err != nil { return err}
    	dest := &tapi.TapiCommon_Context_ConnectivityContext_Connection{}
    	if err := tapi.Unmarshal(c.buffer.Bytes(), dest); err != nil { return err }
    
    	c.database.StoreConnections(c.buffer.String())
    
    	log.Debug(c.buffer.Next(25))
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    	return err
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    }
    
    
    // GetLinks implements the TAPI Topology GetLinks call with a grain of
    // Ciena salt. The response is written to the client's buffer and passed to the database
    
    func (c *MCPClient) GetLinks() error {
    	defer c.buffer.Reset()
    	_, err := c.client.TapiTopologyCore.GetTapiCoreContextTopologyMcpBaseTopologyLink(nil)
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    	if err != nil { return err}
    	dest := &tapi.TapiCommon_Context_TopologyContext_Topology_Link{}
    	if err := tapi.Unmarshal(c.buffer.Bytes(), dest); err != nil { return err }
    
    	c.database.StoreLinks(c.buffer.String())
    
    	log.Debug(c.buffer.Next(25))
    	return err
    }
    
    
    // GetNodes implements the TAPI Topology GetNodes call with a grain of
    // Ciena salt. The response is written to the client's buffer and passed to the database
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    func (c *MCPClient) GetNodes() error {
    
    	defer c.buffer.Reset()
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    	_, err := c.client.TapiTopologyCore.GetTapiCoreContextTopologyMcpBaseTopologyNode(nil)
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    	if err != nil { return err}
    	dest := &tapi.TapiCommon_Context_TopologyContext_Topology_Node{}
    	if err := tapi.Unmarshal(c.buffer.Bytes(), dest); err != nil { return err }
    
    	c.database.StoreNodes(c.buffer.String())
    
    	log.Debug(c.buffer.Next(25))
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    	return err
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    }
    
    
    // GetNodeEdgePoints implements the TAPI Topology GetNodeEdgePoints call with a grain of
    // Ciena salt. The response is written to the client's buffer and passed to the database
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    func (c *MCPClient) GetNodeEdgePoints() error {
    
    	defer c.buffer.Reset()
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    	_, err := c.client.TapiTopologyCore.GetTapiCoreContextTopologyMcpBaseTopologyNodeEdgePoint(nil)
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    	if err != nil { return err}
    	dest := &tapi.TapiCommon_Context_TopologyContext_Topology_Link_NodeEdgePoint{}
    	if err := tapi.Unmarshal(c.buffer.Bytes(), dest); err != nil { return err }
    
    	c.database.StoreNodeEdgePoints(c.buffer.String())
    
    	log.Debug(c.buffer.Next(25))
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    	return err
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    }