-
Notifications
You must be signed in to change notification settings - Fork 0
/
core.go
101 lines (89 loc) · 2.93 KB
/
core.go
1
2
3
4
5
6
7
8
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package connector
import (
"encoding/json"
log "github.com/sirupsen/logrus"
"github.com/thingsplex/tpflow/connector/model"
"github.com/thingsplex/tpflow/connector/plugins"
"github.com/thingsplex/tpflow/utils"
"io/ioutil"
"path/filepath"
"strings"
)
// Adapter instance has to be created from Flow manager .
// Node should refuse to start if Adapter
type Registry struct {
instances []*model.Instance
configsDir string
}
func NewRegistry(configDir string) *Registry {
reg := Registry{configsDir: configDir}
return ®
}
// Adds existing connection as connector instance into the registry
func (reg *Registry) AddConnection(id string, name string, connType string, conn model.ConnInterface) {
inst := model.Instance{ID: id, Name: name, Plugin: connType, Connection: conn}
reg.instances = append(reg.instances, &inst)
}
// Adds existing instance to registry
func (reg *Registry) AddInstance(inst *model.Instance) {
reg.instances = append(reg.instances, inst)
log.Info("<ConnRegistry> Instance was added , id = %s , name = %s ", inst.ID, inst.Name)
}
// Creates instance of connector using one of registered plugins
func (reg *Registry) CreateInstance(id string, name string, plugin string, config interface{}) model.ConnInterface {
connPlugin := plugins.GetPlugin(plugin)
if connPlugin != nil {
if id == "" {
id = utils.GenerateId(15)
}
connInstance := connPlugin.Constructor(name, config)
reg.AddConnection(id, name, plugin, connInstance)
return connInstance
}
return nil
}
// Returns pointer to existing instance of connector
func (reg *Registry) GetInstance(id string) *model.Instance {
for i := range reg.instances {
if reg.instances[i].ID == id {
return reg.instances[i]
}
}
return nil
}
// Returns pointer to existing instance of connector
func (reg *Registry) GetAllInstances() []model.InstanceView {
var instList []model.InstanceView
for i := range reg.instances {
inst := model.InstanceView{ID: reg.instances[i].ID, Name: reg.instances[i].Name, Plugin: reg.instances[i].Plugin, State: reg.instances[i].Connection.GetState(), Config: reg.instances[i].Config}
instList = append(instList, inst)
}
return instList
}
func (reg *Registry) LoadInstancesFromDisk() error {
log.Info("<ConnRegistry> Loading connectors from disk ")
files, err := ioutil.ReadDir(reg.configsDir)
if err != nil {
log.Error(err)
return err
}
for _, file := range files {
if strings.Contains(file.Name(), ".json") {
fileName := filepath.Join(reg.configsDir, file.Name())
log.Info("<ConnRegistry> Loading connector instance from file : ", fileName)
file, err := ioutil.ReadFile(fileName)
if err != nil {
log.Error("<ConnRegistry> Can't open connector config file.")
continue
}
inst := model.Instance{}
err = json.Unmarshal(file, &inst)
if err != nil {
log.Error("<ConnRegistry> Can't unmarshel connector file.")
continue
}
reg.CreateInstance(inst.ID, inst.Name, inst.Plugin, inst.Config)
}
}
return nil
}