Add device capability and get authenticated device list
[platform/core/system/edge-orchestration.git] / patches / capabilitydetails.patch
1 diff --git a/src/controller/configuremgr/capabilitydetails.go b/src/controller/configuremgr/capabilitydetails.go
2 new file mode 100644
3 index 0000000..f04ed2e
4 --- /dev/null
5 +++ b/src/controller/configuremgr/capabilitydetails.go
6 @@ -0,0 +1,92 @@
7 +package configuremgr
8 +
9 +import (
10 +       discovery "controller/discoverymgr"
11 +       capabilitydb "db/bolt/capability"
12 +       networkdb "db/bolt/network"
13 +       systemdb "db/bolt/system"
14 +       "errors"
15 +       "log"
16 +       "sync"
17 +)
18 +
19 +const logPrefix = "[configuremgr]"
20 +const logTag = logPrefix + "[Capability]"
21 +
22 +var (
23 +       capDBExecutor capabilitydb.DBInterface
24 +       netDBExecutor networkdb.DBInterface
25 +       sysDBExecutor systemdb.DBInterface
26 +       discoveryImpl discovery.Discovery
27 +)
28 +
29 +type CAPImpl struct {
30 +       mutex *sync.Mutex
31 +}
32 +
33 +var (
34 +       capImpl *CAPImpl
35 +)
36 +
37 +func init() {
38 +       capImpl = &CAPImpl{}
39 +       netDBExecutor = networkdb.Query{}
40 +       sysDBExecutor = systemdb.Query{}
41 +       capDBExecutor = capabilitydb.Query{}
42 +       capImpl.mutex = &sync.Mutex{}
43 +       discoveryImpl = discovery.GetInstance()
44 +}
45 +
46 +func GetInstance() CapabilityImpl {
47 +       return capImpl
48 +}
49 +
50 +func (sm *CAPImpl) ReadCapability(deviceIP string) (string, error) {
51 +       sm.mutex.Lock()
52 +       defer sm.mutex.Unlock()
53 +
54 +       netInfos, err := netDBExecutor.GetList()
55 +       if err != nil {
56 +               log.Println(logTag, "Could not fetch from Network db", err.Error())
57 +               return "", err
58 +       }
59 +       for _, netInfo := range netInfos {
60 +               if len(netInfo.IPv4) != 0 {
61 +                       if netInfo.IPv4[0] == deviceIP {
62 +                               deviceId := netInfo.ID
63 +                               capInfo, err := capDBExecutor.Get(deviceId)
64 +                               if err != nil {
65 +                                       log.Println(logTag, "Could not fetch from config db", err.Error())
66 +                                       return "", err
67 +                               }
68 +                               return capInfo.Cap, nil
69 +                       }
70 +               }
71 +       }
72 +       return "", errors.New("Device not found!")
73 +}
74 +
75 +func (sm *CAPImpl) WriteCapability(capabilityJson string) error {
76 +       sm.mutex.Lock()
77 +       defer sm.mutex.Unlock()
78 +
79 +       systemInfo, err := sysDBExecutor.Get("id")
80 +       if err != nil {
81 +               log.Println(logTag, "Could not fetch from system db", err.Error())
82 +               return err
83 +       }
84 +
85 +       deviceId := systemInfo.Value
86 +
87 +       cap := capabilitydb.Capability{}
88 +       cap.Cap = capabilityJson
89 +
90 +       err = capDBExecutor.Set(deviceId, cap)
91 +       if err != nil {
92 +               log.Println(logPrefix, "DB error : ", err.Error())
93 +               return err
94 +       }
95 +       discovery.CapabilityCache = capabilityJson
96 +       discoveryImpl.AddNewCapability(capabilityJson)
97 +       return nil
98 +}
99 \ No newline at end of file