Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Moved mnedc logic to discoverymgr, which fixes #178 #180

Merged
merged 1 commit into from
Nov 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 26 additions & 25 deletions GoMain/src/main/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ package main
import (
"errors"
"log"
"strings"
"os"
"strings"

"common/logmgr"
"common/sigmgr"

configuremgr "controller/configuremgr/container"
"controller/discoverymgr"
"controller/mnedcmgr"
mnedcmgr "controller/discoverymgr/mnedc"
"controller/scoringmgr"
"controller/securemgr/authenticator"
"controller/securemgr/authorizer"
Expand Down Expand Up @@ -72,10 +72,10 @@ const (
passPhraseJWTPath = edgeDir + "/data/jwt"
rbacRulePath = edgeDir + "/data/rbac"

cipherKeyFilePath = edgeDir + "/user/orchestration_userID.txt"
deviceIDFilePath = edgeDir + "/device/orchestration_deviceID.txt"
dataStorageFilePath = edgeDir + "/datastorage/configuration.toml"
mnedcServerConfig = edgeDir + "/mnedc/client.config"
cipherKeyFilePath = edgeDir + "/user/orchestration_userID.txt"
deviceIDFilePath = edgeDir + "/device/orchestration_deviceID.txt"
dataStorageFilePath = edgeDir + "/datastorage/configuration.toml"
mnedcServerConfig = edgeDir + "/mnedc/client.config"
)

var (
Expand Down Expand Up @@ -104,8 +104,8 @@ func orchestrationInit() error {
mnedc := os.Getenv("MNEDC")

isSecured := false
if len(secure)>0 {
if strings.Compare(strings.ToLower(secure), "true")==0 {
if len(secure) > 0 {
if strings.Compare(strings.ToLower(secure), "true") == 0 {
log.Println("Orchestration init with secure option")
isSecured = true
}
Expand All @@ -130,6 +130,7 @@ func orchestrationInit() error {

servicemgr.GetInstance().SetClient(restIns)
discoverymgr.GetInstance().SetClient(restIns)
mnedcmgr.GetClientInstance().SetClient(restIns)

builder := orchestrationapi.OrchestrationBuilder{}
builder.SetWatcher(configuremgr.GetInstance(configPath))
Expand Down Expand Up @@ -182,28 +183,28 @@ func orchestrationInit() error {

restEdgeRouter.Start()

if _, err := os.Stat(dataStorageFilePath); err==nil {
if _, err := os.Stat(dataStorageFilePath); err == nil {
sd := storagedriver.StorageDriver{}
go startup.Bootstrap(dataStorageService, device.Version, &sd)
}

log.Println(logPrefix, "orchestration init done")

if len(mnedc) > 0 {
if strings.Compare(strings.ToLower(mnedc), "server") == 0 {
if isSecured {
mnedcmgr.GetServerInstance().SetCipher(dummy.GetCipher(cipherKeyFilePath))
mnedcmgr.GetServerInstance().SetCertificateFilePath(certificateFilePath)
} else {
mnedcmgr.GetServerInstance().SetCipher(sha256.GetCipher(cipherKeyFilePath))
}
go mnedcmgr.GetServerInstance().StartMNEDCServer(deviceIDFilePath)
} else if strings.Compare(strings.ToLower(mnedc), "client") == 0 {
if isSecured {
mnedcmgr.GetClientInstance().SetCertificateFilePath(certificateFilePath)
}
go mnedcmgr.GetClientInstance().StartMNEDCClient(deviceIDFilePath, mnedcServerConfig)
}
if strings.Compare(strings.ToLower(mnedc), "server") == 0 {
if isSecured {
mnedcmgr.GetServerInstance().SetCipher(dummy.GetCipher(cipherKeyFilePath))
mnedcmgr.GetServerInstance().SetCertificateFilePath(certificateFilePath)
} else {
mnedcmgr.GetServerInstance().SetCipher(sha256.GetCipher(cipherKeyFilePath))
}
go discoverymgr.GetInstance().StartMNEDCServer(deviceIDFilePath)
} else if strings.Compare(strings.ToLower(mnedc), "client") == 0 {
if isSecured {
mnedcmgr.GetClientInstance().SetCertificateFilePath(certificateFilePath)
}
go discoverymgr.GetInstance().StartMNEDCClient(deviceIDFilePath, mnedcServerConfig)
}
} else {
if strings.Contains(buildTags, "mnedcserver") {
if isSecured {
Expand All @@ -212,12 +213,12 @@ func orchestrationInit() error {
} else {
mnedcmgr.GetServerInstance().SetCipher(sha256.GetCipher(cipherKeyFilePath))
}
go mnedcmgr.GetServerInstance().StartMNEDCServer(deviceIDFilePath)
go discoverymgr.GetInstance().StartMNEDCServer(deviceIDFilePath)
} else if strings.Contains(buildTags, "mnedcclient") {
if isSecured {
mnedcmgr.GetClientInstance().SetCertificateFilePath(certificateFilePath)
}
go mnedcmgr.GetClientInstance().StartMNEDCClient(deviceIDFilePath, mnedcServerConfig)
go discoverymgr.GetInstance().StartMNEDCClient(deviceIDFilePath, mnedcServerConfig)
}
}

Expand Down
10 changes: 5 additions & 5 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ PKG_LIST=(
"controller/servicemgr/executor/containerexecutor"
"controller/servicemgr/executor/nativeexecutor"
"controller/servicemgr/notification"
"controller/mnedcmgr"
"controller/mnedcmgr/client"
"controller/mnedcmgr/connectionutil"
"controller/mnedcmgr/server"
"controller/mnedcmgr/tunmgr"
"controller/discoverymgr/mnedc"
"controller/discoverymgr/mnedc/client"
"controller/discoverymgr/mnedc/connectionutil"
"controller/discoverymgr/mnedc/server"
"controller/discoverymgr/mnedc/tunmgr"
"controller/storagemgr/storagedriver"
"db/bolt/common"
"db/bolt/configuration"
Expand Down
10 changes: 5 additions & 5 deletions glide.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ ignore:
- controller/servicemgr/executor/containerexecutor
- controller/servicemgr/executor/nativeexecutor
- controller/servicemgr/notification
- controller/mnedcmgr
- controller/mnedcmgr/client
- controller/mnedcmgr/connectionutil
- controller/mnedcmgr/server
- controller/mnedcmgr/tunmgr
- controller/discoverymgr/mnedc
- controller/discoverymgr/mnedc/client
- controller/discoverymgr/mnedc/connectionutil
- controller/discoverymgr/mnedc/server
- controller/discoverymgr/mnedc/tunmgr
- controller/storagemgr/storagedriver
- db/helper
- db/bolt/common
Expand Down
4 changes: 2 additions & 2 deletions src/common/sigmgr/sigmgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ import (
"os/signal"
"syscall"

"controller/mnedcmgr/client"
"controller/mnedcmgr/server"
"controller/discoverymgr/mnedc/client"
"controller/discoverymgr/mnedc/server"
)

const (
Expand Down
16 changes: 14 additions & 2 deletions src/controller/discoverymgr/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (

errors "common/errors"
networkhelper "common/networkhelper"
mnedc "controller/discoverymgr/mnedc"
wrapper "controller/discoverymgr/wrapper"

configurationdb "db/bolt/configuration"
Expand Down Expand Up @@ -56,6 +57,8 @@ type Discovery interface {
NotifyMNEDCBroadcastServer() error
MNEDCReconciledCallback()
GetDeviceID() (id string, err error)
StartMNEDCClient(string, string)
StartMNEDCServer(string)
client.Setter
cipher.Setter
}
Expand Down Expand Up @@ -383,7 +386,6 @@ func setDeviceID(UUIDPath string) (UUIDstr string, err error) {
return UUIDstr, err
}


func getPlatform() (platform string, err error) {
platform, err = getSystemDB(systemdb.Platform)
if err != nil {
Expand Down Expand Up @@ -686,6 +688,16 @@ func (d *DiscoveryImpl) MNEDCReconciledCallback() {
}
}

//StartMNEDCClient Starts MNEDC client
func (d *DiscoveryImpl) StartMNEDCClient(deviceIDFilePath, mnedcServerConfig string) {
mnedc.GetClientInstance().StartMNEDCClient(deviceIDFilePath, mnedcServerConfig)
}

//StartMNEDCServer Starts MNEDC server
func (d *DiscoveryImpl) StartMNEDCServer(deviceIDFilePath string) {
mnedc.GetServerInstance().StartMNEDCServer(deviceIDFilePath)
}

// ClearMap makes map empty and only leaves my device info
func clearMap() {
log.Println(logPrefix, "[clearMap]")
Expand Down Expand Up @@ -819,4 +831,4 @@ func activeDiscovery() {
// It Clears Map
func resetServer(ips []net.IP) {
wrapperIns.ResetServer(ips)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@ import (
"sync"
"time"

"controller/discoverymgr"
"controller/mnedcmgr/connectionutil"
"controller/mnedcmgr/tunmgr"
restclient "restinterface/client"
//"controller/discoverymgr"
networkhelper "common/networkhelper"
"controller/discoverymgr/mnedc/connectionutil"
"controller/discoverymgr/mnedc/tunmgr"

"github.com/songgao/water"
)
Expand Down Expand Up @@ -61,18 +63,21 @@ type Client struct {
serverPort string
deviceID string
configPath string
clientAPI restclient.Clienter
}

var (
clientIns *Client
tunIns tunmgr.Tun
networkUtilIns connectionutil.NetworkUtil
discoveryIns discoverymgr.Discovery
networkIns networkhelper.Network
//discoveryIns discoverymgr.Discovery
)

const (
waitDelay = 150 * time.Millisecond
retryDelay = 5 * time.Second
waitDelay = 150 * time.Millisecond
retryDelay = 5 * time.Second
mnedcBroadcastServerPort = 3333
)

//MNEDCClient declares methods related to MNEDC client
Expand All @@ -88,13 +93,16 @@ type MNEDCClient interface {
ParseVirtualIP(string) error
TunReadRoutine()
TunWriteRoutine()
NotifyBroadcastServer(configPath string) error
SetClient(clientAPI restclient.Clienter)
}

func init() {
clientIns = &Client{}
tunIns = tunmgr.GetInstance()
networkUtilIns = connectionutil.GetInstance()
discoveryIns = discoverymgr.GetInstance()
networkIns = networkhelper.GetInstance()
//discoveryIns = discoverymgr.GetInstance()
}

//GetInstance returns MNEDCClient interface instance
Expand Down Expand Up @@ -403,14 +411,73 @@ func (c *Client) TunWriteRoutine() {
func (c *Client) NotifyClose() {
logPrefix := "[NotifyClose]"
log.Println(logPrefix, "MNEDC connection closed")
discoveryIns.MNEDCClosedCallback()
//discoveryIns.MNEDCClosedCallback()
}

//ConnectionReconciled handles the case when MNEDC connection is re-established
func (c *Client) ConnectionReconciled() {
logPrefix := "[connectionReIstablish]"
log.Println(logPrefix, "MNEDC connection reistablished")
discoveryIns.MNEDCReconciledCallback()
//discoveryIns.MNEDCReconciledCallback()
//notifyBroadcastServer()
c.NotifyBroadcastServer(c.configPath)
}

//NotifyBroadcastServer sends request to broadcast server
func (c *Client) NotifyBroadcastServer(configPath string) error {
logPrefix := "[RegisterBroadcast]"
log.Println(logTag, "Registering to Broadcast server")
c.configPath = configPath
virtualIP, err := networkIns.GetVirtualIP()
if err != nil {
log.Println(logPrefix, "Cant register to Broadcast server, virtual IP error", err.Error())
return err
}

privateIP, err := networkIns.GetOutboundIP()
if err != nil {
log.Println(logPrefix, "Cant register to Broadcast server, outbound IP error", err.Error())
return err
}

file, err := os.Open(configPath)

if err != nil {
log.Println(logPrefix, "cant read config file from", configPath, err.Error())
return err
}
defer file.Close()

scanner := bufio.NewScanner(file)
scanner.Split(bufio.ScanLines)

scanner.Scan()
serverIP := scanner.Text()

go func() {

if c.clientAPI == nil {
log.Println(logPrefix, "Client is nil, returning")
err = errors.New("Client is nil")
return
}
err = c.clientAPI.DoNotifyMNEDCBroadcastServer(serverIP, mnedcBroadcastServerPort, c.deviceID, privateIP, virtualIP)
if err != nil {
log.Println(logPrefix, "Cannot register to Broadcast server", err.Error())
}
}()

time.Sleep(5 * time.Second)
if err != nil {
return err
}

return nil
}

//SetClient sets the rest client
func (c *Client) SetClient(clientAPI restclient.Clienter) {
c.clientAPI = clientAPI
}

func getMNEDCServerAddress(path string) (string, string, error) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ import (
"github.com/golang/mock/gomock"
"github.com/songgao/water"

networkUtilMocks "controller/discoverymgr/mnedc/connectionutil/mocks"
discoveryMocks "controller/discoverymgr/mocks"
networkUtilMocks "controller/mnedcmgr/connectionutil/mocks"
tunMocks "controller/mnedcmgr/tunmgr/mocks"
tunMocks "controller/discoverymgr/mnedc/tunmgr/mocks"
)

const (
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading