Newer
Older
package sdnclient
import (
"encoding/json"
"fmt"
"net/http"
l2smv1 "github.com/Networks-it-uc3m/L2S-M/api/v1"
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
)
// ExternalClient is part of the Client interface, and implements the SessionClient, which is a wrapper of the http function
// this type of client is for the specific idco onos app, which manages inter cluster networks.
type ExternalClient struct {
Session *SessionClient
}
func (c *ExternalClient) beginSessionController() bool {
//TODO: implement healthcheck in idco onos app
resp, err := c.Session.Get("/idco/mscs/status")
if err != nil {
return false
}
defer resp.Body.Close()
// Check if the status code indicates success (HTTP 200 OK).
return resp.StatusCode == http.StatusOK
}
// CreateNetwork creates a new network in the SDN controller
func (c *ExternalClient) CreateNetwork(networkType l2smv1.NetworkType, config interface{}) error {
jsonData, err := json.Marshal(config)
if err != nil {
return err
}
response, err := c.Session.Post("/idco/mscs", jsonData)
if err != nil {
return err
}
defer response.Body.Close()
if response.StatusCode != http.StatusNoContent {
return fmt.Errorf("failed to create network, status code: %d", response.StatusCode)
}
return nil
}
// CheckNetworkExists checks if the specified network exists in the SDN controller
func (c *ExternalClient) CheckNetworkExists(networkType l2smv1.NetworkType, networkID string) (bool, error) {
response, err := c.Session.Get(fmt.Sprintf("/idco/mscs/%s", networkID))
if err != nil {
return false, err
}
defer response.Body.Close()
return response.StatusCode == http.StatusOK, nil
}
// DeleteNetwork deletes an existing network from the SDN controller
func (c *ExternalClient) DeleteNetwork(networkType l2smv1.NetworkType, networkID string) error {
response, err := c.Session.Delete(fmt.Sprintf("/idco/mscs/%s", networkID))
if err != nil {
return err
}
defer response.Body.Close()
if response.StatusCode != http.StatusNoContent {
return fmt.Errorf("SDN controller responded with status code: %d", response.StatusCode)
}
return nil
}
// AttachPodToNetwork attaches a pod to a network, using the configuration file
func (c *ExternalClient) AttachPodToNetwork(networkType l2smv1.NetworkType, config interface{}) error {
// jsonData, err := json.Marshal(config)
// if err != nil {
// return err
// }
// response, err := c.Session.Post("/idco/mscs", jsonData)
// if err != nil {
// return err
// }
// defer response.Body.Close()
// if response.StatusCode != http.StatusNoContent {
// return fmt.Errorf("failed to create network, status code: %d", response.StatusCode)
// }
return nil
}