Skip to content
Snippets Groups Projects
Commit bca99cb4 authored by Malte Bauch's avatar Malte Bauch
Browse files

removed plugins folder

parent 907bb41c
Branches
Tags
2 merge requests!120Resolve "Code Quality",!90Develop
package ciena
import (
"bytes"
"code.fbi.h-da.de/cocsn/gosdn/database"
"code.fbi.h-da.de/cocsn/gosdn/nucleus"
apiclient "code.fbi.h-da.de/cocsn/swagger/apis/mcp/client"
"code.fbi.h-da.de/cocsn/yang-models/generated/tapi"
"crypto/tls"
"encoding/json"
"github.com/go-openapi/runtime"
httptransport "github.com/go-openapi/runtime/client"
"github.com/go-openapi/strfmt"
"github.com/openconfig/ygot/ygot"
log "github.com/sirupsen/logrus"
"github.com/tidwall/gjson"
"net/http"
"strings"
)
//Mcp handles requests to a Ciena MCP RESTCONF endpoint
type Mcp struct {
transport *httptransport.Runtime
client *apiclient.ServiceTopologyTAPI
database *database.Database
buffer *bytes.Buffer
config *nucleus.ClientConfig
device ygot.GoStruct
}
// GetConfig returns a ClientConfig struct containing
// the current configuration stat of the Ciena SBI ciena
func (c Mcp) GetConfig() nucleus.ClientConfig {
return *c.config
}
// ListPorts is a stub to satisfy the interface
// TODO: Implement
func (c Mcp) ListPorts() interface{} {
return nil
}
// PushReceiver is a stub to satisfy the interface
// TODO: Implement
func (c Mcp) PushReceiver() error {
return nil
}
//NewMCPClient creates a Ciena flavores TAPI ciena
func NewMCPClient(endpoint, username, password string, database *database.Database, config *nucleus.ClientConfig) *Mcp {
// create the transport
transport := httptransport.New(endpoint, "/", nil)
transport.Transport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
// create the API ciena, with the transport
basicAuth := httptransport.BasicAuth(username, password)
// authenticate ciena
transport.DefaultAuthentication = basicAuth
client := apiclient.New(transport, strfmt.Default)
buffer := new(bytes.Buffer)
transport.Consumers[runtime.JSONMime] = nucleus.YANGConsumer{Data: buffer}
return &Mcp{
transport: transport,
client: client,
database: database,
buffer: buffer,
config: config,
device: &tapi.Device{},
}
}
// GetConnections implements the TAPI Connectivity GetConnections call with a grain of
// Ciena salt. The response is written to the ciena's buffer and passed to the database
func (c *Mcp) GetConnections() error {
defer c.buffer.Reset()
_, err := c.client.TapiConnectivityCore.GetTapiCoreContextConnection(nil)
if err != nil {
return err
}
json := preformatJSON(c.buffer.String(), c.config.GjsonConnectionsPath)
for _, jsonEntry := range json.Array() {
dest := &tapi.TapiCommon_Context_ConnectivityContext_Connection{}
if err := tapi.Unmarshal([]byte(jsonEntry.String()), dest); err != nil {
//TODO: think about a way how to handle this.
//logging every error is kinda ugly, since ciena tapi throws a
//lot of them.
log.Info(err)
}
log.Info(*dest.Uuid)
}
// c.database.StoreConnections(c.buffer.String())
// log.Debug(c.buffer.Next(25))
return err
}
// GetLinks implements the TAPI Topology GetLinks call with a grain of
// Ciena salt. The response is written to the ciena's buffer and passed to the database
func (c *Mcp) GetLinks() error {
defer c.buffer.Reset()
_, err := c.client.TapiTopologyCore.GetTapiCoreContextTopologyMcpBaseTopologyLink(nil)
if err != nil {
return err
}
json := preformatJSON(c.buffer.String(), c.config.GjsonDefaultPath)
for _, jsonEntry := range json.Array() {
dest := &tapi.TapiCommon_Context_TopologyContext_Topology_Link{}
if err := tapi.Unmarshal([]byte(jsonEntry.String()), dest); err != nil {
//TODO: think about a way how to handle this.
//logging every error is kinda ugly, since ciena tapi throws a
//lot of them.
log.Info(err)
}
log.Info(*dest.Uuid)
}
// 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 ciena's buffer and passed to the database
func (c *Mcp) GetNodes() error {
defer c.buffer.Reset()
_, err := c.client.TapiTopologyCore.GetTapiCoreContextTopologyMcpBaseTopologyNode(nil)
if err != nil {
return err
}
json := preformatJSON(c.buffer.String(), c.config.GjsonDefaultPath)
for _, jsonEntry := range json.Array() {
dest := &tapi.TapiCommon_Context_TopologyContext_Topology_Node{}
if err := tapi.Unmarshal([]byte(jsonEntry.String()), dest); err != nil {
//TODO: think about a way how to handle this.
//logging every error is kinda ugly, since ciena tapi throws a
//lot of them.
log.Info(err)
}
log.Info(*dest.Uuid)
}
// c.database.StoreNodes(c.buffer.String())
// log.Debug(c.buffer.Next(25))
return err
}
// GetNodeEdgePoints implements the TAPI Topology GetNodeEdgePoints call with a grain of
// Ciena salt. The response is written to the ciena's buffer and passed to the database
func (c *Mcp) GetNodeEdgePoints() error {
defer c.buffer.Reset()
_, err := c.client.TapiTopologyCore.GetTapiCoreContextTopologyMcpBaseTopologyNodeEdgePoint(nil)
if err != nil {
return err
}
//TODO: there is no tapi ygot struct that fits the ciena node-edge-point
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))
return err
}
//preformatJSON preformats the recieved JSON for further processing
func preformatJSON(jsn string, path string) *gjson.Result {
//TODO: move this!
modifierName := "uppercase"
gjson.AddModifier(modifierName, func(jsonString, arg string) string {
var jsonMap interface{}
err := json.Unmarshal([]byte(jsonString), &jsonMap)
if err != nil {
log.Info("failed unmarshal for JSON")
}
jsonMap = uppercaseJSONValues(jsonMap)
result, err := json.Marshal(jsonMap)
if err != nil {
log.Info("failed marshal for JSON")
}
return string(result)
})
formattedJSON := gjson.Get(jsn, path+"|@"+modifierName)
return &formattedJSON
}
//uppercaseJSONValues takes a interface{} created with json.Unmarshal() and changes the containing
//string values to uppercase
//returns a interface{} which can be changed back into a JSON string via
//json.Marshal()
func uppercaseJSONValues(jsonMap interface{}) interface{} {
switch jsonMap := jsonMap.(type) {
//check if []interface{} and go through every entry recursively
case []interface{}:
for i := range jsonMap {
jsonMap[i] = uppercaseJSONValues(jsonMap[i])
}
return jsonMap
//check if map[string]interface, handle ciena tapi json specific fixes
//and go on recursively
case map[string]interface{}:
tmpInterfaceMap := make(map[string]interface{}, len(jsonMap))
for k, v := range jsonMap {
//TODO: maybe we can uppercase them too, but for now:
//DO NOT uppercase uuid's
if strings.Contains(k, "uuid") {
tmpInterfaceMap[k] = v
} else {
tmpInterfaceMap[k] = uppercaseJSONValues(v)
}
}
return tmpInterfaceMap
//ygot: requires enums in uppercase and since CIENA TAPI does sometimes
//provide faulty JSON values we need to uppercase them before we process
//them further
case string:
return strings.ToUpper(jsonMap)
//default: do nothing (like for bool or other stuff)
default:
return jsonMap
}
}
package plugins
import (
"fmt"
"os"
"plugin"
)
type SBIGreeter interface {
SBIHello()
}
func SBILoader() {
modPath := "/Users/mls/go/src/code.fbi.h-da.de/cocsn/byowsbi/byowsbi.o"
// open the so file that contains the SBI-plugin as step before loading the symbols
plug, err := plugin.Open(modPath)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
// loading the symbols
sbiModule, err := plug.Lookup("SBIGreeter")
if err != nil {
fmt.Println(err)
os.Exit(1)
}
// Assert the loaded symbol
var sbigreeter SBIGreeter
sbigreeter, ok := sbiModule.(SBIGreeter)
if !ok {
fmt.Println("unexpected type from module symbol")
os.Exit(1)
}
// use me!
sbigreeter.SBIHello()
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment