Skip to content

Commit 1d45f03

Browse files
authored
Moved mnedc logic to discoverymgr, which fixes #178 (#180)
Signed-off-by: ayush.kumar <[email protected]> - Since MNEDC responsibility is similar to the Discovery Manager, the mnedcmgr package has now been moved inside discoverymgr package and renamed to mnedc.
1 parent a18e10e commit 1d45f03

22 files changed

+182
-73
lines changed

GoMain/src/main/main.go

+26-25
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,15 @@ package main
2121
import (
2222
"errors"
2323
"log"
24-
"strings"
2524
"os"
25+
"strings"
2626

2727
"common/logmgr"
2828
"common/sigmgr"
2929

3030
configuremgr "controller/configuremgr/container"
3131
"controller/discoverymgr"
32-
"controller/mnedcmgr"
32+
mnedcmgr "controller/discoverymgr/mnedc"
3333
"controller/scoringmgr"
3434
"controller/securemgr/authenticator"
3535
"controller/securemgr/authorizer"
@@ -72,10 +72,10 @@ const (
7272
passPhraseJWTPath = edgeDir + "/data/jwt"
7373
rbacRulePath = edgeDir + "/data/rbac"
7474

75-
cipherKeyFilePath = edgeDir + "/user/orchestration_userID.txt"
76-
deviceIDFilePath = edgeDir + "/device/orchestration_deviceID.txt"
77-
dataStorageFilePath = edgeDir + "/datastorage/configuration.toml"
78-
mnedcServerConfig = edgeDir + "/mnedc/client.config"
75+
cipherKeyFilePath = edgeDir + "/user/orchestration_userID.txt"
76+
deviceIDFilePath = edgeDir + "/device/orchestration_deviceID.txt"
77+
dataStorageFilePath = edgeDir + "/datastorage/configuration.toml"
78+
mnedcServerConfig = edgeDir + "/mnedc/client.config"
7979
)
8080

8181
var (
@@ -104,8 +104,8 @@ func orchestrationInit() error {
104104
mnedc := os.Getenv("MNEDC")
105105

106106
isSecured := false
107-
if len(secure)>0 {
108-
if strings.Compare(strings.ToLower(secure), "true")==0 {
107+
if len(secure) > 0 {
108+
if strings.Compare(strings.ToLower(secure), "true") == 0 {
109109
log.Println("Orchestration init with secure option")
110110
isSecured = true
111111
}
@@ -130,6 +130,7 @@ func orchestrationInit() error {
130130

131131
servicemgr.GetInstance().SetClient(restIns)
132132
discoverymgr.GetInstance().SetClient(restIns)
133+
mnedcmgr.GetClientInstance().SetClient(restIns)
133134

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

183184
restEdgeRouter.Start()
184185

185-
if _, err := os.Stat(dataStorageFilePath); err==nil {
186+
if _, err := os.Stat(dataStorageFilePath); err == nil {
186187
sd := storagedriver.StorageDriver{}
187188
go startup.Bootstrap(dataStorageService, device.Version, &sd)
188189
}
189190

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

192193
if len(mnedc) > 0 {
193-
if strings.Compare(strings.ToLower(mnedc), "server") == 0 {
194-
if isSecured {
195-
mnedcmgr.GetServerInstance().SetCipher(dummy.GetCipher(cipherKeyFilePath))
196-
mnedcmgr.GetServerInstance().SetCertificateFilePath(certificateFilePath)
197-
} else {
198-
mnedcmgr.GetServerInstance().SetCipher(sha256.GetCipher(cipherKeyFilePath))
199-
}
200-
go mnedcmgr.GetServerInstance().StartMNEDCServer(deviceIDFilePath)
201-
} else if strings.Compare(strings.ToLower(mnedc), "client") == 0 {
202-
if isSecured {
203-
mnedcmgr.GetClientInstance().SetCertificateFilePath(certificateFilePath)
204-
}
205-
go mnedcmgr.GetClientInstance().StartMNEDCClient(deviceIDFilePath, mnedcServerConfig)
206-
}
194+
if strings.Compare(strings.ToLower(mnedc), "server") == 0 {
195+
if isSecured {
196+
mnedcmgr.GetServerInstance().SetCipher(dummy.GetCipher(cipherKeyFilePath))
197+
mnedcmgr.GetServerInstance().SetCertificateFilePath(certificateFilePath)
198+
} else {
199+
mnedcmgr.GetServerInstance().SetCipher(sha256.GetCipher(cipherKeyFilePath))
200+
}
201+
go discoverymgr.GetInstance().StartMNEDCServer(deviceIDFilePath)
202+
} else if strings.Compare(strings.ToLower(mnedc), "client") == 0 {
203+
if isSecured {
204+
mnedcmgr.GetClientInstance().SetCertificateFilePath(certificateFilePath)
205+
}
206+
go discoverymgr.GetInstance().StartMNEDCClient(deviceIDFilePath, mnedcServerConfig)
207+
}
207208
} else {
208209
if strings.Contains(buildTags, "mnedcserver") {
209210
if isSecured {
@@ -212,12 +213,12 @@ func orchestrationInit() error {
212213
} else {
213214
mnedcmgr.GetServerInstance().SetCipher(sha256.GetCipher(cipherKeyFilePath))
214215
}
215-
go mnedcmgr.GetServerInstance().StartMNEDCServer(deviceIDFilePath)
216+
go discoverymgr.GetInstance().StartMNEDCServer(deviceIDFilePath)
216217
} else if strings.Contains(buildTags, "mnedcclient") {
217218
if isSecured {
218219
mnedcmgr.GetClientInstance().SetCertificateFilePath(certificateFilePath)
219220
}
220-
go mnedcmgr.GetClientInstance().StartMNEDCClient(deviceIDFilePath, mnedcServerConfig)
221+
go discoverymgr.GetInstance().StartMNEDCClient(deviceIDFilePath, mnedcServerConfig)
221222
}
222223
}
223224

build.sh

+5-5
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,11 @@ PKG_LIST=(
3434
"controller/servicemgr/executor/containerexecutor"
3535
"controller/servicemgr/executor/nativeexecutor"
3636
"controller/servicemgr/notification"
37-
"controller/mnedcmgr"
38-
"controller/mnedcmgr/client"
39-
"controller/mnedcmgr/connectionutil"
40-
"controller/mnedcmgr/server"
41-
"controller/mnedcmgr/tunmgr"
37+
"controller/discoverymgr/mnedc"
38+
"controller/discoverymgr/mnedc/client"
39+
"controller/discoverymgr/mnedc/connectionutil"
40+
"controller/discoverymgr/mnedc/server"
41+
"controller/discoverymgr/mnedc/tunmgr"
4242
"controller/storagemgr/storagedriver"
4343
"db/bolt/common"
4444
"db/bolt/configuration"

glide.yaml

+5-5
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@ ignore:
3333
- controller/servicemgr/executor/containerexecutor
3434
- controller/servicemgr/executor/nativeexecutor
3535
- controller/servicemgr/notification
36-
- controller/mnedcmgr
37-
- controller/mnedcmgr/client
38-
- controller/mnedcmgr/connectionutil
39-
- controller/mnedcmgr/server
40-
- controller/mnedcmgr/tunmgr
36+
- controller/discoverymgr/mnedc
37+
- controller/discoverymgr/mnedc/client
38+
- controller/discoverymgr/mnedc/connectionutil
39+
- controller/discoverymgr/mnedc/server
40+
- controller/discoverymgr/mnedc/tunmgr
4141
- controller/storagemgr/storagedriver
4242
- db/helper
4343
- db/bolt/common

src/common/sigmgr/sigmgr.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ import (
2323
"os/signal"
2424
"syscall"
2525

26-
"controller/mnedcmgr/client"
27-
"controller/mnedcmgr/server"
26+
"controller/discoverymgr/mnedc/client"
27+
"controller/discoverymgr/mnedc/server"
2828
)
2929

3030
const (

src/controller/discoverymgr/discovery.go

+14-2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828

2929
errors "common/errors"
3030
networkhelper "common/networkhelper"
31+
mnedc "controller/discoverymgr/mnedc"
3132
wrapper "controller/discoverymgr/wrapper"
3233

3334
configurationdb "db/bolt/configuration"
@@ -56,6 +57,8 @@ type Discovery interface {
5657
NotifyMNEDCBroadcastServer() error
5758
MNEDCReconciledCallback()
5859
GetDeviceID() (id string, err error)
60+
StartMNEDCClient(string, string)
61+
StartMNEDCServer(string)
5962
client.Setter
6063
cipher.Setter
6164
}
@@ -383,7 +386,6 @@ func setDeviceID(UUIDPath string) (UUIDstr string, err error) {
383386
return UUIDstr, err
384387
}
385388

386-
387389
func getPlatform() (platform string, err error) {
388390
platform, err = getSystemDB(systemdb.Platform)
389391
if err != nil {
@@ -686,6 +688,16 @@ func (d *DiscoveryImpl) MNEDCReconciledCallback() {
686688
}
687689
}
688690

691+
//StartMNEDCClient Starts MNEDC client
692+
func (d *DiscoveryImpl) StartMNEDCClient(deviceIDFilePath, mnedcServerConfig string) {
693+
mnedc.GetClientInstance().StartMNEDCClient(deviceIDFilePath, mnedcServerConfig)
694+
}
695+
696+
//StartMNEDCServer Starts MNEDC server
697+
func (d *DiscoveryImpl) StartMNEDCServer(deviceIDFilePath string) {
698+
mnedc.GetServerInstance().StartMNEDCServer(deviceIDFilePath)
699+
}
700+
689701
// ClearMap makes map empty and only leaves my device info
690702
func clearMap() {
691703
log.Println(logPrefix, "[clearMap]")
@@ -819,4 +831,4 @@ func activeDiscovery() {
819831
// It Clears Map
820832
func resetServer(ips []net.IP) {
821833
wrapperIns.ResetServer(ips)
822-
}
834+
}

src/controller/mnedcmgr/client/client.go renamed to src/controller/discoverymgr/mnedc/client/client.go

+76-9
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,11 @@ import (
2626
"sync"
2727
"time"
2828

29-
"controller/discoverymgr"
30-
"controller/mnedcmgr/connectionutil"
31-
"controller/mnedcmgr/tunmgr"
29+
restclient "restinterface/client"
30+
//"controller/discoverymgr"
31+
networkhelper "common/networkhelper"
32+
"controller/discoverymgr/mnedc/connectionutil"
33+
"controller/discoverymgr/mnedc/tunmgr"
3234

3335
"github.com/songgao/water"
3436
)
@@ -61,18 +63,21 @@ type Client struct {
6163
serverPort string
6264
deviceID string
6365
configPath string
66+
clientAPI restclient.Clienter
6467
}
6568

6669
var (
6770
clientIns *Client
6871
tunIns tunmgr.Tun
6972
networkUtilIns connectionutil.NetworkUtil
70-
discoveryIns discoverymgr.Discovery
73+
networkIns networkhelper.Network
74+
//discoveryIns discoverymgr.Discovery
7175
)
7276

7377
const (
74-
waitDelay = 150 * time.Millisecond
75-
retryDelay = 5 * time.Second
78+
waitDelay = 150 * time.Millisecond
79+
retryDelay = 5 * time.Second
80+
mnedcBroadcastServerPort = 3333
7681
)
7782

7883
//MNEDCClient declares methods related to MNEDC client
@@ -88,13 +93,16 @@ type MNEDCClient interface {
8893
ParseVirtualIP(string) error
8994
TunReadRoutine()
9095
TunWriteRoutine()
96+
NotifyBroadcastServer(configPath string) error
97+
SetClient(clientAPI restclient.Clienter)
9198
}
9299

93100
func init() {
94101
clientIns = &Client{}
95102
tunIns = tunmgr.GetInstance()
96103
networkUtilIns = connectionutil.GetInstance()
97-
discoveryIns = discoverymgr.GetInstance()
104+
networkIns = networkhelper.GetInstance()
105+
//discoveryIns = discoverymgr.GetInstance()
98106
}
99107

100108
//GetInstance returns MNEDCClient interface instance
@@ -403,14 +411,73 @@ func (c *Client) TunWriteRoutine() {
403411
func (c *Client) NotifyClose() {
404412
logPrefix := "[NotifyClose]"
405413
log.Println(logPrefix, "MNEDC connection closed")
406-
discoveryIns.MNEDCClosedCallback()
414+
//discoveryIns.MNEDCClosedCallback()
407415
}
408416

409417
//ConnectionReconciled handles the case when MNEDC connection is re-established
410418
func (c *Client) ConnectionReconciled() {
411419
logPrefix := "[connectionReIstablish]"
412420
log.Println(logPrefix, "MNEDC connection reistablished")
413-
discoveryIns.MNEDCReconciledCallback()
421+
//discoveryIns.MNEDCReconciledCallback()
422+
//notifyBroadcastServer()
423+
c.NotifyBroadcastServer(c.configPath)
424+
}
425+
426+
//NotifyBroadcastServer sends request to broadcast server
427+
func (c *Client) NotifyBroadcastServer(configPath string) error {
428+
logPrefix := "[RegisterBroadcast]"
429+
log.Println(logTag, "Registering to Broadcast server")
430+
c.configPath = configPath
431+
virtualIP, err := networkIns.GetVirtualIP()
432+
if err != nil {
433+
log.Println(logPrefix, "Cant register to Broadcast server, virtual IP error", err.Error())
434+
return err
435+
}
436+
437+
privateIP, err := networkIns.GetOutboundIP()
438+
if err != nil {
439+
log.Println(logPrefix, "Cant register to Broadcast server, outbound IP error", err.Error())
440+
return err
441+
}
442+
443+
file, err := os.Open(configPath)
444+
445+
if err != nil {
446+
log.Println(logPrefix, "cant read config file from", configPath, err.Error())
447+
return err
448+
}
449+
defer file.Close()
450+
451+
scanner := bufio.NewScanner(file)
452+
scanner.Split(bufio.ScanLines)
453+
454+
scanner.Scan()
455+
serverIP := scanner.Text()
456+
457+
go func() {
458+
459+
if c.clientAPI == nil {
460+
log.Println(logPrefix, "Client is nil, returning")
461+
err = errors.New("Client is nil")
462+
return
463+
}
464+
err = c.clientAPI.DoNotifyMNEDCBroadcastServer(serverIP, mnedcBroadcastServerPort, c.deviceID, privateIP, virtualIP)
465+
if err != nil {
466+
log.Println(logPrefix, "Cannot register to Broadcast server", err.Error())
467+
}
468+
}()
469+
470+
time.Sleep(5 * time.Second)
471+
if err != nil {
472+
return err
473+
}
474+
475+
return nil
476+
}
477+
478+
//SetClient sets the rest client
479+
func (c *Client) SetClient(clientAPI restclient.Clienter) {
480+
c.clientAPI = clientAPI
414481
}
415482

416483
func getMNEDCServerAddress(path string) (string, string, error) {

src/controller/mnedcmgr/client/client_test.go renamed to src/controller/discoverymgr/mnedc/client/client_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ import (
2828
"github.com/golang/mock/gomock"
2929
"github.com/songgao/water"
3030

31+
networkUtilMocks "controller/discoverymgr/mnedc/connectionutil/mocks"
3132
discoveryMocks "controller/discoverymgr/mocks"
32-
networkUtilMocks "controller/mnedcmgr/connectionutil/mocks"
33-
tunMocks "controller/mnedcmgr/tunmgr/mocks"
33+
tunMocks "controller/discoverymgr/mnedc/tunmgr/mocks"
3434
)
3535

3636
const (

src/controller/mnedcmgr/client/mocks/mocks_client.go renamed to src/controller/discoverymgr/mnedc/client/mocks/mocks_client.go

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)