sourceMeta.go 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. package plugins
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "github.com/emqx/kuiper/common"
  6. "io/ioutil"
  7. "path"
  8. "reflect"
  9. "strings"
  10. )
  11. type (
  12. fileSource struct {
  13. About *fileAbout `json:"about"`
  14. Libs []string `json:"libs"`
  15. ConfKeys map[string][]*fileField `json:"properties"`
  16. }
  17. uiSource struct {
  18. About *about `json:"about"`
  19. Libs []string `json:"libs"`
  20. ConfKeys map[string][]*field `json:"properties"`
  21. }
  22. sourceProperty struct {
  23. cf map[string]map[string]interface{}
  24. meta *uiSource
  25. }
  26. )
  27. func isInternalSource(fiName string) bool {
  28. internal := []string{`edgex.json`, `httppull.json`, `mqtt.json`}
  29. for _, v := range internal {
  30. if v == fiName {
  31. return true
  32. }
  33. }
  34. return false
  35. }
  36. func newUiSource(fi *fileSource) (*uiSource, error) {
  37. if nil == fi {
  38. return nil, nil
  39. }
  40. var err error
  41. ui := new(uiSource)
  42. ui.Libs = fi.Libs
  43. ui.About = newAbout(fi.About)
  44. ui.ConfKeys = make(map[string][]*field)
  45. for k, fields := range fi.ConfKeys {
  46. if ui.ConfKeys[k], err = newField(fields); nil != err {
  47. return nil, err
  48. }
  49. }
  50. return ui, nil
  51. }
  52. var g_sourceProperty map[string]*sourceProperty
  53. func (m *Manager) uninstalSource(name string) {
  54. if v, ok := g_sourceProperty[name+".json"]; ok {
  55. if ui := v.meta; nil != ui {
  56. if nil != ui.About {
  57. ui.About.Installed = false
  58. }
  59. }
  60. }
  61. }
  62. func (m *Manager) readSourceMetaFile(filePath string) error {
  63. fileName := path.Base(filePath)
  64. if "mqtt_source.json" == fileName {
  65. fileName = "mqtt.json"
  66. }
  67. ptrMeta := new(fileSource)
  68. err := common.ReadJsonUnmarshal(filePath, ptrMeta)
  69. if nil != err || 0 == len(ptrMeta.ConfKeys) {
  70. return fmt.Errorf("file:%s err:%v", filePath, err)
  71. }
  72. if 0 == len(ptrMeta.ConfKeys["default"]) {
  73. return fmt.Errorf("not found default confKey %s", filePath)
  74. }
  75. if nil == ptrMeta.About {
  76. return fmt.Errorf("not found about of %s", filePath)
  77. } else if isInternalSource(fileName) {
  78. ptrMeta.About.Installed = true
  79. } else {
  80. _, ptrMeta.About.Installed = m.registry.Get(SOURCE, strings.TrimSuffix(fileName, `.json`))
  81. }
  82. yamlData := make(map[string]map[string]interface{})
  83. filePath = strings.TrimSuffix(filePath, `.json`) + `.yaml`
  84. err = common.ReadYamlUnmarshal(filePath, &yamlData)
  85. if nil != err {
  86. return fmt.Errorf("file:%s err:%v", filePath, err)
  87. }
  88. if 0 == len(yamlData["default"]) {
  89. return fmt.Errorf("not found default confKey from %s", filePath)
  90. }
  91. property := new(sourceProperty)
  92. property.cf = yamlData
  93. property.meta, err = newUiSource(ptrMeta)
  94. if nil != err {
  95. return err
  96. }
  97. g_sourceProperty[fileName] = property
  98. return err
  99. }
  100. func (m *Manager) readSourceMetaDir() error {
  101. g_sourceProperty = make(map[string]*sourceProperty)
  102. confDir, err := common.GetConfLoc()
  103. if nil != err {
  104. return err
  105. }
  106. dir := path.Join(confDir, "sources")
  107. infos, err := ioutil.ReadDir(dir)
  108. if nil != err {
  109. return err
  110. }
  111. if err = m.readSourceMetaFile(path.Join(confDir, "mqtt_source.json")); nil != err {
  112. return err
  113. }
  114. for _, info := range infos {
  115. fileName := info.Name()
  116. if strings.HasSuffix(fileName, ".json") {
  117. filePath := path.Join(dir, fileName)
  118. if err = m.readSourceMetaFile(filePath); nil != err {
  119. return err
  120. }
  121. common.Log.Infof("sourceMeta file : %s", fileName)
  122. }
  123. }
  124. return nil
  125. }
  126. func GetSourceConf(pluginName string) (b []byte, err error) {
  127. property, ok := g_sourceProperty[pluginName+".json"]
  128. if ok {
  129. cf := make(map[string]map[string]interface{})
  130. for key, kvs := range property.cf {
  131. aux := make(map[interface{}]interface{})
  132. for k, v := range kvs {
  133. aux[k] = v
  134. }
  135. cf[key] = common.ConvertMap(aux)
  136. }
  137. return json.Marshal(cf)
  138. }
  139. return nil, fmt.Errorf("not found plugin %s", pluginName)
  140. }
  141. func GetSourceMeta(pluginName string) (ptrSourceProperty *uiSource, err error) {
  142. property, ok := g_sourceProperty[pluginName+".json"]
  143. if ok {
  144. return property.cfToMeta()
  145. }
  146. return nil, fmt.Errorf("not found plugin %s", pluginName)
  147. }
  148. func GetSources() (sources []*pluginfo) {
  149. for fileName, v := range g_sourceProperty {
  150. node := new(pluginfo)
  151. node.Name = strings.TrimSuffix(fileName, `.json`)
  152. if nil == v.meta {
  153. continue
  154. }
  155. if nil == v.meta.About {
  156. continue
  157. }
  158. node.About = v.meta.About
  159. i := 0
  160. for ; i < len(sources); i++ {
  161. if node.Name <= sources[i].Name {
  162. sources = append(sources, node)
  163. copy(sources[i+1:], sources[i:])
  164. sources[i] = node
  165. break
  166. }
  167. }
  168. if len(sources) == i {
  169. sources = append(sources, node)
  170. }
  171. }
  172. return sources
  173. }
  174. func GetSourceConfKeys(pluginName string) (keys []string) {
  175. property := g_sourceProperty[pluginName+".json"]
  176. if nil == property {
  177. return keys
  178. }
  179. for k, _ := range property.cf {
  180. keys = append(keys, k)
  181. }
  182. return keys
  183. }
  184. func DelSourceConfKey(pluginName, confKey string) error {
  185. property := g_sourceProperty[pluginName+".json"]
  186. if nil == property {
  187. return fmt.Errorf("not found plugin %s", pluginName)
  188. }
  189. if nil == property.cf {
  190. return fmt.Errorf("not found confKey %s", confKey)
  191. }
  192. delete(property.cf, confKey)
  193. return property.saveCf(pluginName)
  194. }
  195. func AddSourceConfKey(pluginName, confKey string, content []byte) error {
  196. reqField := make(map[string]interface{})
  197. err := json.Unmarshal(content, &reqField)
  198. if nil != err {
  199. return err
  200. }
  201. property := g_sourceProperty[pluginName+".json"]
  202. if nil == property {
  203. return fmt.Errorf("not found plugin %s", pluginName)
  204. }
  205. if nil == property.cf {
  206. property.cf = make(map[string]map[string]interface{})
  207. }
  208. if 0 != len(property.cf[confKey]) {
  209. return fmt.Errorf("exist confKey %s", confKey)
  210. }
  211. property.cf[confKey] = reqField
  212. g_sourceProperty[pluginName+".json"] = property
  213. return property.saveCf(pluginName)
  214. }
  215. func AddSourceConfKeyField(pluginName, confKey string, content []byte) error {
  216. reqField := make(map[string]interface{})
  217. err := json.Unmarshal(content, &reqField)
  218. if nil != err {
  219. return err
  220. }
  221. property := g_sourceProperty[pluginName+".json"]
  222. if nil == property {
  223. return fmt.Errorf("not found plugin %s", pluginName)
  224. }
  225. if nil == property.cf {
  226. return fmt.Errorf("not found confKey %s", confKey)
  227. }
  228. if nil == property.cf[confKey] {
  229. return fmt.Errorf("not found confKey %s", confKey)
  230. }
  231. for k, v := range reqField {
  232. property.cf[confKey][k] = v
  233. }
  234. return property.saveCf(pluginName)
  235. }
  236. func recursionDelMap(cf, fields map[string]interface{}) error {
  237. for k, v := range fields {
  238. if nil == v {
  239. delete(cf, k)
  240. continue
  241. }
  242. if delKey, ok := v.(string); ok {
  243. if 0 == len(delKey) {
  244. delete(cf, k)
  245. continue
  246. }
  247. var auxCf map[string]interface{}
  248. if err := common.MapToStruct(cf[k], &auxCf); nil != err {
  249. return fmt.Errorf("not found second key:%s.%s", k, delKey)
  250. }
  251. cf[k] = auxCf
  252. delete(auxCf, delKey)
  253. continue
  254. }
  255. if reflect.Map == reflect.TypeOf(v).Kind() {
  256. var auxCf, auxFields map[string]interface{}
  257. if err := common.MapToStruct(cf[k], &auxCf); nil != err {
  258. return fmt.Errorf("not found second key:%s.%v", k, v)
  259. }
  260. cf[k] = auxCf
  261. if err := common.MapToStruct(v, &auxFields); nil != err {
  262. return fmt.Errorf("requestef format err:%s.%v", k, v)
  263. }
  264. if err := recursionDelMap(auxCf, auxFields); nil != err {
  265. return err
  266. }
  267. }
  268. }
  269. return nil
  270. }
  271. func DelSourceConfKeyField(pluginName, confKey string, content []byte) error {
  272. reqField := make(map[string]interface{})
  273. err := json.Unmarshal(content, &reqField)
  274. if nil != err {
  275. return err
  276. }
  277. property := g_sourceProperty[pluginName+".json"]
  278. if nil == property {
  279. return fmt.Errorf("not found plugin %s", pluginName)
  280. }
  281. if nil == property.cf {
  282. return fmt.Errorf("not found confKey %s", confKey)
  283. }
  284. if nil == property.cf[confKey] {
  285. return fmt.Errorf("not found confKey %s", confKey)
  286. }
  287. err = recursionDelMap(property.cf[confKey], reqField)
  288. if nil != err {
  289. return err
  290. }
  291. return property.saveCf(pluginName)
  292. }
  293. func recursionNewFields(template []*field, conf map[string]interface{}, ret *[]*field) error {
  294. for i := 0; i < len(template); i++ {
  295. p := new(field)
  296. *p = *template[i]
  297. *ret = append(*ret, p)
  298. v, ok := conf[template[i].Name]
  299. if ok {
  300. p.Exist = true
  301. } else {
  302. p.Exist = false
  303. continue
  304. }
  305. var auxRet, auxTemplate []*field
  306. p.Default = &auxRet
  307. if nil == v {
  308. p.Default = v
  309. } else {
  310. if reflect.Map == reflect.TypeOf(v).Kind() {
  311. var nextCf map[string]interface{}
  312. if tmp, ok := v.(map[interface{}]interface{}); ok {
  313. nextCf = common.ConvertMap(tmp)
  314. } else {
  315. if err := common.MapToStruct(v, &nextCf); nil != err {
  316. return err
  317. }
  318. }
  319. if err := common.MapToStruct(template[i].Default, &auxTemplate); nil != err {
  320. return err
  321. }
  322. if err := recursionNewFields(auxTemplate, nextCf, &auxRet); nil != err {
  323. return err
  324. }
  325. } else {
  326. p.Default = v
  327. }
  328. }
  329. }
  330. return nil
  331. }
  332. func (this *sourceProperty) cfToMeta() (*uiSource, error) {
  333. fields := this.meta.ConfKeys["default"]
  334. ret := make(map[string][]*field)
  335. for k, kvs := range this.cf {
  336. var sli []*field
  337. err := recursionNewFields(fields, kvs, &sli)
  338. if nil != err {
  339. return nil, err
  340. }
  341. ret[k] = sli
  342. }
  343. meta := new(uiSource)
  344. *meta = *(this.meta)
  345. meta.ConfKeys = ret
  346. return meta, nil
  347. }
  348. func (this *sourceProperty) saveCf(pluginName string) error {
  349. confDir, err := common.GetConfLoc()
  350. if nil != err {
  351. return err
  352. }
  353. dir := path.Join(confDir, "sources")
  354. if "mqtt" == pluginName {
  355. pluginName = "mqtt_source"
  356. dir = confDir
  357. }
  358. filePath := path.Join(dir, pluginName+".yaml")
  359. return common.WriteYamlMarshal(filePath, this.cf)
  360. }