Skip to content

Commit

Permalink
Merge pull request #54 from plumgrid/sagheer-dev
Browse files Browse the repository at this point in the history
add checks against router and bridge options
  • Loading branch information
msagheer authored Sep 2, 2016
2 parents 73f14d7 + b923f75 commit 3e07c67
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 14 deletions.
44 changes: 33 additions & 11 deletions plugin/driver/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,12 @@ func (driver *driver) createNetwork(w http.ResponseWriter, r *http.Request) {
neName := create.Options[netlabel.GenericData].(map[string]interface{})["bridge"]

if neName != nil {
AddNetworkInfo(create.NetworkID, neName.(string), domainid.(string))
if err := AddNetworkInfo(create.NetworkID, neName.(string), domainid.(string)); err != nil {
Log.Error(err)
DomainDelete(domainid.(string))
errorResponse(w, fmt.Sprintf("Bridge (%s) doesnot exist in doamin (%s).", neName.(string), domainid.(string)))
return
}
AddGatewayInfo(create.NetworkID, domainid.(string), gatewayip)
emptyResponse(w)

Expand All @@ -155,7 +160,13 @@ func (driver *driver) createNetwork(w http.ResponseWriter, r *http.Request) {
tm, _ := hex.DecodeString(ipnet.Mask.String())
netmask := fmt.Sprintf("%v.%v.%v.%v", tm[0], tm[1], tm[2], tm[3])
Log.Infof("Adding router interface for : ", router, gatewayip, netmask)
CreateNetworkLink(router.(string), domainid.(string), create.NetworkID, gatewayip, netmask)
if err := CreateNetworkLink(router.(string), domainid.(string), create.NetworkID, gatewayip, netmask); err != nil {
Log.Error(err)
BridgeDelete(create.NetworkID, domainid.(string))
DomainDelete(domainid.(string))
errorResponse(w, fmt.Sprintf("Router (%s) doesnot exist in doamin (%s).", router.(string), domainid.(string)))
return
}
}

emptyResponse(w)
Expand Down Expand Up @@ -202,7 +213,7 @@ func (driver *driver) createEndpoint(w http.ResponseWriter, r *http.Request) {
local := vethPair(endID[:5])
if err := netlink.LinkAdd(local); err != nil {
Log.Error(err)
errorResponsef(w, "could not create veth pair")
errorResponse(w, "could not create veth pair")
return
}
link, _ := netlink.LinkByName(local.PeerName)
Expand Down Expand Up @@ -273,7 +284,6 @@ func (driver *driver) joinEndpoint(w http.ResponseWriter, r *http.Request) {
mac := link.Attrs().HardwareAddr.String()
Log.Infof("mac address: %s\n", mac)

//first command {adding port on plumgrid}
cmdStr := "sudo /opt/pg/bin/ifc_ctl gateway add_port " + if_local_name
Log.Infof("addport cmd: %s", cmdStr)
cmd := exec.Command("/bin/sh", "-c", cmdStr)
Expand All @@ -282,9 +292,12 @@ func (driver *driver) joinEndpoint(w http.ResponseWriter, r *http.Request) {
if err := cmd.Run(); err != nil {
Log.Error("Error thrown: ", err)
}
Log.Infof("output: %+v\n", addport.String())
if addport.String() != "" {
Log.Error(fmt.Errorf(addport.String()))
errorResponse(w, "Unable to on-board container onto PLUMgrid")
return
}

//second command {up the port on plumgrid}
cmdStr = "sudo /opt/pg/bin/ifc_ctl gateway ifup " + if_local_name + " access_container cont_" + endID[:8] + " " + mac + " pgtag2=" + bridgeID + " pgtag1=" + domainid
Log.Infof("ifup cmd: %s", cmdStr)
cmd = exec.Command("/bin/sh", "-c", cmdStr)
Expand All @@ -293,10 +306,14 @@ func (driver *driver) joinEndpoint(w http.ResponseWriter, r *http.Request) {
if err := cmd.Run(); err != nil {
Log.Error("Error thrown: ", err)
}
Log.Infof("output: %+v\n", ifup.String())
if ifup.String() != "" {
Log.Error(fmt.Errorf(ifup.String()))
errorResponse(w, "Unable to on-board container onto PLUMgrid")
return
}

if netlink.LinkSetUp(local) != nil {
errorResponsef(w, `unable to bring veth up`)
errorResponse(w, "unable to bring veth up")
return
}

Expand Down Expand Up @@ -329,7 +346,6 @@ func (driver *driver) leaveEndpoint(w http.ResponseWriter, r *http.Request) {

if_local_name := "tap" + l.EndpointID[:5]

//first command {adding port on plumgrid}
cmdStr := "sudo /opt/pg/bin/ifc_ctl gateway ifdown " + if_local_name
Log.Infof("ifdown cmd: %s", cmdStr)
cmd := exec.Command("/bin/sh", "-c", cmdStr)
Expand All @@ -338,9 +354,11 @@ func (driver *driver) leaveEndpoint(w http.ResponseWriter, r *http.Request) {
if err := cmd.Run(); err != nil {
Log.Error("Error thrown: ", err)
}
Log.Infof("output: %+v\n", ifdown.String())
if ifdown.String() != "" {
Log.Error(fmt.Errorf(ifdown.String()))
errorResponse(w, "Unable to off-board container from PLUMgrid")
}

//second command {up the port on plumgrid}
cmdStr = "sudo /opt/pg/bin/ifc_ctl gateway del_port " + if_local_name
Log.Infof("delport cmd: %s", cmdStr)
cmd = exec.Command("/bin/sh", "-c", cmdStr)
Expand All @@ -350,6 +368,10 @@ func (driver *driver) leaveEndpoint(w http.ResponseWriter, r *http.Request) {
Log.Error("Error thrown: ", err)
}
Log.Infof("output: %+v\n", delport.String())
if delport.String() != "" {
Log.Error(fmt.Errorf(delport.String()))
errorResponse(w, "Unable to off-board container from PLUMgrid")
}

RemoveMetaconfig(domainid, bridgeID, l.EndpointID)

Expand Down
38 changes: 36 additions & 2 deletions plugin/driver/pg_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package driver

import (
"encoding/json"
"fmt"
"strings"
)

Expand Down Expand Up @@ -77,14 +78,19 @@ func BridgeDelete(ID string, domainid string) {
RestCall("DELETE", url, nil)
}

func AddNetworkInfo(dNetwork string, neName string, domain string) {
func AddNetworkInfo(dNetwork string, neName string, domain string) error {

id := GetNeId(neName, domain)
var neId string
if id == "" {
neId = neName
} else {
neId = id
}
if !CheckNeExists(neId, domain) {
return fmt.Errorf("Bridge (%s) doesnot exist in doamin (%s).", neName, domain)
}

url := "/0/connectivity/domain/" + domain + "/ne/" + neId
body, _ := RestCall("GET", url+"?configonly=true", nil)
var netData map[string]interface{}
Expand All @@ -95,6 +101,8 @@ func AddNetworkInfo(dNetwork string, neName string, domain string) {
netData["metadata"] = dNetwork
finalData, _ := json.Marshal(netData)
RestCall("PUT", url, finalData)

return nil
}

func FindDomainFromNetwork(ID string) (domainid string, netid string) {
Expand Down Expand Up @@ -238,9 +246,13 @@ func GetNeId(NeName string, DomainID string) (NeID string) {
return ""
}

func CreateNetworkLink(NeName string, DomainID string, NetworkID string, IP string, Netmask string) {
func CreateNetworkLink(NeName string, DomainID string, NetworkID string, IP string, Netmask string) error {

ne_ID := GetNeId(NeName, DomainID)
if !CheckNeExists(ne_ID, DomainID) {
return fmt.Errorf("Router (%s) doesnot exist in doamin (%s).", NeName, DomainID)
}

CheckNeChildList(ne_ID, DomainID, "ifc")
CheckNeChildList(NetworkName(NetworkID), DomainID, "ifc")
ne_ifc := NetworkID
Expand Down Expand Up @@ -271,6 +283,8 @@ func CreateNetworkLink(NeName string, DomainID string, NetworkID string, IP stri
"attachment1": "/ne/` + ne_ID + `/ifc/` + ne_ifc + `",
"attachment2": "/ne/` + NetworkName(NetworkID) + `/ifc/` + net_ifc + `"}`)
RestCall("PUT", url, data)

return nil
}

func DeleteNetworkLinks(DomainID string, NetworkID string) {
Expand Down Expand Up @@ -409,3 +423,23 @@ func DeleteAttachedLB(DomainID string, NetworkID string) {
}
}
}

func CheckNeExists(ne_name, domain string) bool {

url := "/0/connectivity/domain/" + domain + "/ne?configonly=true"

body, _ := RestCall("GET", url+"?configonly=true", nil)
var domain_data map[string]interface{}
err := json.Unmarshal([]byte(body), &domain_data)
if err != nil {
panic(err)
}

for ne, _ := range domain_data {
if ne_name == ne {
return true
}
}

return false
}
2 changes: 1 addition & 1 deletion plugin/driver/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func sendError(w http.ResponseWriter, msg string, code int) {
http.Error(w, msg, code)
}

func errorResponsef(w http.ResponseWriter, fmtString string, item ...interface{}) {
func errorResponse(w http.ResponseWriter, fmtString string, item ...interface{}) {
json.NewEncoder(w).Encode(map[string]string{
"Err": fmt.Sprintf(fmtString, item...),
})
Expand Down

0 comments on commit 3e07c67

Please sign in to comment.