2018-04-17 14:11:13 +00:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
2018-04-18 14:56:13 +00:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path"
|
2018-04-17 14:11:13 +00:00
|
|
|
"reflect"
|
|
|
|
|
|
|
|
cda "git.loafle.net/commons/di-go/annotation"
|
|
|
|
cdr "git.loafle.net/commons/di-go/registry"
|
2018-04-26 08:39:32 +00:00
|
|
|
ocmsc "git.loafle.net/overflow/commons-go/model/sensorconfig"
|
2018-04-26 08:43:40 +00:00
|
|
|
ocsp "git.loafle.net/overflow/commons-go/service/probe"
|
2018-04-18 14:56:13 +00:00
|
|
|
"git.loafle.net/overflow/probe/config"
|
|
|
|
|
|
|
|
// For annotation
|
2018-04-17 14:11:13 +00:00
|
|
|
_ "git.loafle.net/overflow/commons-go/core/annotation"
|
|
|
|
)
|
|
|
|
|
|
|
|
var SensorConfigServiceType = reflect.TypeOf((*SensorConfigService)(nil))
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
cdr.RegisterType(SensorConfigServiceType)
|
|
|
|
}
|
|
|
|
|
|
|
|
type SensorConfigService struct {
|
2018-04-26 08:43:40 +00:00
|
|
|
ocsp.SensorConfigService
|
2018-04-17 14:11:13 +00:00
|
|
|
cda.TypeAnnotation `annotation:"@overflow:RPCService()"`
|
2018-04-18 14:56:13 +00:00
|
|
|
|
2018-04-26 08:39:32 +00:00
|
|
|
sensorConfigs map[string]*ocmsc.SensorConfig
|
2018-04-17 14:11:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SensorConfigService) InitService() error {
|
2018-04-26 08:39:32 +00:00
|
|
|
s.sensorConfigs = make(map[string]*ocmsc.SensorConfig)
|
2018-04-18 14:56:13 +00:00
|
|
|
|
2018-04-17 14:11:13 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SensorConfigService) StartService() error {
|
2018-04-18 14:56:13 +00:00
|
|
|
if err := s.loadConfigAll(); nil != err {
|
|
|
|
return err
|
|
|
|
}
|
2018-04-17 14:11:13 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SensorConfigService) StopService() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SensorConfigService) DestroyService() {
|
|
|
|
|
|
|
|
}
|
2018-04-18 14:56:13 +00:00
|
|
|
|
|
|
|
func (s *SensorConfigService) AddConfig(tempFilePath string) error {
|
|
|
|
sc, buf, err := s.loadConfigFile(tempFilePath)
|
|
|
|
if nil != err {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
targetPath := config.SensorConfigFilePath(sc)
|
|
|
|
ioutil.WriteFile(targetPath, buf, 0644)
|
|
|
|
|
|
|
|
// tempfile remove
|
|
|
|
err = os.Remove(tempFilePath)
|
|
|
|
if nil != err {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
s.sensorConfigs[sc.ID.String()] = sc
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SensorConfigService) RemoveConfig(sensorConfigID string) error {
|
|
|
|
sc, ok := s.sensorConfigs[sensorConfigID]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("SensorConfig[%s] is not exist", sensorConfigID)
|
|
|
|
}
|
|
|
|
|
|
|
|
targetPath := config.SensorConfigFilePath(sc)
|
|
|
|
err := os.Remove(targetPath)
|
|
|
|
if nil != err {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
delete(s.sensorConfigs, sensorConfigID)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SensorConfigService) loadConfigAll() error {
|
|
|
|
configDirPath := config.ConfigDir()
|
|
|
|
files, err := ioutil.ReadDir(configDirPath)
|
|
|
|
if nil != err {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, file := range files {
|
|
|
|
if file.IsDir() == true {
|
|
|
|
if err := s.loadConfigDir(path.Join(configDirPath, file.Name())); nil != err {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SensorConfigService) loadConfigDir(dirPath string) error {
|
|
|
|
files, err := ioutil.ReadDir(dirPath)
|
|
|
|
if nil != err {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, file := range files {
|
|
|
|
filePath := path.Join(dirPath, file.Name())
|
|
|
|
|
|
|
|
if file.IsDir() == true {
|
|
|
|
if err := s.loadConfigDir(filePath); nil != err {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
sc, _, err := s.loadConfigFile(filePath)
|
|
|
|
if nil != err {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
s.sensorConfigs[file.Name()] = sc
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-04-26 08:39:32 +00:00
|
|
|
func (s *SensorConfigService) loadConfigFile(filePath string) (*ocmsc.SensorConfig, []byte, error) {
|
2018-04-18 14:56:13 +00:00
|
|
|
buf, err := ioutil.ReadFile(filePath)
|
|
|
|
if nil != err {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2018-04-26 08:39:32 +00:00
|
|
|
var m = &ocmsc.SensorConfig{}
|
2018-04-18 14:56:13 +00:00
|
|
|
if err := json.Unmarshal(buf, m); nil != err {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return m, buf, nil
|
|
|
|
}
|