Newer
Older
"code.fbi.h-da.de/cocsn/gosdn/database"
"code.fbi.h-da.de/cocsn/gosdn/nucleus"
"github.com/go-openapi/runtime"
httptransport "github.com/go-openapi/runtime/client"
"github.com/go-openapi/strfmt"
//Mcp handles requests to a Ciena MCP RESTCONF endpoint
type Mcp struct {
transport *httptransport.Runtime
client *apiclient.ServiceTopologyTAPI
// GetConfig returns a ClientConfig struct containing
// the current configuration stat of the Ciena SBI ciena
func (c Mcp) GetConfig() nucleus.ClientConfig {
// ListPorts is a stub to satisfy the interface
// TODO: Implement
return nil
}
// PushReceiver is a stub to satisfy the interface
// TODO: Implement
//NewMCPClient creates a Ciena flavores TAPI ciena
func NewMCPClient(endpoint, username, password string, database *database.Database, config *nucleus.ClientConfig) *Mcp {
transport.Transport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
basicAuth := httptransport.BasicAuth(username, password)
buffer := new(bytes.Buffer)
transport.Consumers[runtime.JSONMime] = nucleus.YANGConsumer{Data: buffer}
database: database,
buffer: buffer,
// 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 {
_, 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))
// 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))
// 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 {
_, 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))
// 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 {
_, 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))
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
}
//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
}
}