Add device capability and get authenticated device list
[platform/core/system/edge-orchestration.git] / patches / discovery.patch
1 diff --git a/src/controller/discoverymgr/discovery.go b/src/controller/discoverymgr/discovery.go
2 index 182b642..71e56b2 100644
3 --- a/src/controller/discoverymgr/discovery.go
4 +++ b/src/controller/discoverymgr/discovery.go
5 @@ -21,12 +21,13 @@ import (
6         "io/ioutil"
7         "log"
8         "net"
9 +       "reflect"
10         "time"
11  
12         errors "common/errors"
13         networkhelper "common/networkhelper"
14         wrapper "controller/discoverymgr/wrapper"
15 -
16 +       capabilitydb "db/bolt/capability"
17         configurationdb "db/bolt/configuration"
18         networkdb "db/bolt/network"
19         servicedb "db/bolt/service"
20 @@ -44,13 +45,16 @@ type Discovery interface {
21         AddNewServiceName(serviceName string) error
22         RemoveServiceName(serviceName string) error
23         ResetServiceName()
24 +       AddNewCapability(capability string)
25 +       ForceDiscovery()
26  }
27  
28  type discoveryImpl struct{}
29  
30  var (
31 -       discoveryIns discoveryImpl
32 -       networkIns   networkhelper.Network
33 +       discoveryIns    discoveryImpl
34 +       networkIns      networkhelper.Network
35 +       CapabilityCache string
36  )
37  
38  func init() {
39 @@ -63,6 +67,8 @@ func init() {
40         confQuery = configurationdb.Query{}
41         netQuery = networkdb.Query{}
42         serviceQuery = servicedb.Query{}
43 +       capabilityQuery = capabilitydb.Query{}
44 +       CapabilityCache = ""
45  }
46  
47  // GetInstance returns discovery instaance
48 @@ -79,8 +85,22 @@ func (discoveryImpl) StartDiscovery(UUIDpath string, platform string, executionT
49                 log.Print(logPrefix, "[StartDiscovery]", "UUID ", UUIDStr, " is Temporary")
50         }
51  
52 +       deviceId,err := sysQuery.Get("id")
53 +        if err != nil {
54 +                log.Print(logPrefix, "[StartDiscovery] Could not fetch self device id : ", err)
55 +                CapabilityCache = ""
56 +        }else{
57 +                CapabilityCacheData, err := capabilityQuery.Get(deviceId.Value)
58 +                if err != nil {
59 +                        log.Print(logPrefix, "[StartDiscovery] Could not fetch self Capability")
60 +                        CapabilityCache = ""
61 +                }else{
62 +                        CapabilityCache = CapabilityCacheData.Cap
63 +                }
64 +        }
65 +
66         // NOTE : startServer blocks until server is registered
67 -       startServer(UUIDStr, platform, executionType)
68 +       startServer(UUIDStr, platform, executionType, CapabilityCache)
69  
70         go detectNetworkChgRoutine()
71  
72 @@ -201,6 +221,7 @@ func (discoveryImpl) ResetServiceName() {
73         var serverTXT []string
74         serverTXT = append(serverTXT, confItem.ExecType)
75         serverTXT = append(serverTXT, confItem.Platform)
76 +       serverTXT = append(serverTXT, CapabilityCache)
77  
78         setNewServiceList(serverTXT)
79  }
80 @@ -284,10 +305,10 @@ func getExecType() (execType string, err error) {
81         return
82  }
83  
84 -func startServer(deviceUUID string, platform string, executionType string) {
85 +func startServer(deviceUUID string, platform string, executionType string, capability string) {
86         deviceDetectionRoutine()
87  
88 -       deviceID, hostName, Text := setDeviceArgument(deviceUUID, platform, executionType)
89 +       deviceID, hostName, Text := setDeviceArgument(deviceUUID, platform, executionType, capability)
90  
91         // @Note store system information(id, platform and execution type) to system db
92         setSystemDB(deviceID, platform, executionType)
93 @@ -317,12 +338,13 @@ func startServer(deviceUUID string, platform string, executionType string) {
94         return
95  }
96  
97 -func setDeviceArgument(deviceUUID string, platform string, executionType string) (deviceID string, hostName string, Text []string) {
98 +func setDeviceArgument(deviceUUID string, platform string, executionType string, capability string) (deviceID string, hostName string, Text []string) {
99         deviceID = "edge-orchestration-" + deviceUUID
100         hostName = "edge-" + deviceUUID
101  
102         Text = append(Text, platform)
103         Text = append(Text, executionType)
104 +       Text = append(Text, capability)
105         return
106  }
107  
108 @@ -366,18 +388,28 @@ func deviceDetectionRoutine() {
109  
110                                 _, confInfo, netInfo, serviceInfo := convertToDBInfo(*data)
111  
112 +                               if len(netInfo.IPv4) == 0 {
113 +                                       continue
114 +                               }
115 +
116                                 log.Printf("[deviceDetectionRoutine] %s", data.DeviceID)
117                                 log.Printf("[deviceDetectionRoutine] confInfo    : ExecType(%s), Platform(%s)", confInfo.ExecType, confInfo.Platform)
118 -                               log.Printf("[deviceDetectionRoutine] netInfo     : IPv4(%s)", netInfo.IPv4)
119 +                               log.Printf("[deviceDetectionRoutine] netInfo     : IPv4(%s), RTT(%v)", netInfo.IPv4, netInfo.RTT)
120                                 log.Printf("[deviceDetectionRoutine] serviceInfo : Services(%v)", serviceInfo.Services)
121 +                               log.Printf("[deviceDetectionRoutine] Capability : Capability(%s)", data.OrchestrationInfo.Capability)
122                                 log.Printf("")
123  
124 -                               if len(netInfo.IPv4) != 0 {
125 +                               var info networkdb.NetworkInfo
126 +                               info, err = getNetworkDB(netInfo.ID)
127 +
128 +                               if err != nil || !reflect.DeepEqual(netInfo.IPv4, info.IPv4) {
129                                         setNetworkDB(netInfo)
130                                 }
131 +
132                                 // @Note Is it need to call Update API?
133                                 setConfigurationDB(confInfo)
134                                 setServiceDB(serviceInfo)
135 +                               setCapabilityDB(data.DeviceID, data.OrchestrationInfo.Capability)
136                         }
137                 }
138         }()
139 @@ -451,7 +483,7 @@ func getIndexToDelete(serverTXT []string, serviceName string) (idxToDel int, err
140  
141  func setNewServiceList(serverTXT []string) {
142         // if len(serverTXT) > 2 {
143 -       newServiceList := serverTXT[2:]
144 +       newServiceList := serverTXT[3:]
145  
146         deviceID, err := getDeviceID()
147         if err != nil {
148 @@ -465,6 +497,20 @@ func setNewServiceList(serverTXT []string) {
149         wrapperIns.SetText(serverTXT)
150  }
151  
152 +// AddNewCapability is called when new capability is added
153 +func (discoveryImpl) AddNewCapability(capability string) {
154 +       text := wrapperIns.GetText()
155 +       text[2] = capability
156 +       wrapperIns.SetText(text)
157 +       go activeDiscovery()
158 +}
159 +
160 +// ForceDiscovery is called to trigger discovery forcefully
161 +func (discoveryImpl) ForceDiscovery() {
162 +       // Triggering force discovery
163 +       go activeDiscovery()
164 +}
165 +
166  // ClearMap makes map empty and only leaves my device info
167  func clearMap() {
168         log.Println(logPrefix, "[clearMap]")
169 @@ -528,6 +574,15 @@ func setSystemDB(id string, platform string, execType string) {
170         }
171  }
172  
173 +func setCapabilityDB(deviceId string, capability string) {
174 +       deviceCap := capabilitydb.Capability{}
175 +       deviceCap.Cap = capability
176 +       err := capabilityQuery.Set(deviceId, deviceCap)
177 +       if err != nil {
178 +               log.Println(logPrefix, err.Error())
179 +       }
180 +}
181 +
182  func setConfigurationDB(confInfo configurationdb.Configuration) {
183         err := confQuery.Set(confInfo)
184         if err != nil {
185 @@ -559,6 +614,15 @@ func getSystemDB(name string) (string, error) {
186         return sysInfo.Value, err
187  }
188  
189 +func getNetworkDB(id string) (networkdb.NetworkInfo, error) {
190 +       netInfo, err := netQuery.Get(id)
191 +       if err != nil {
192 +               log.Println(logPrefix, err.Error())
193 +       }
194 +
195 +       return netInfo, err
196 +}
197 +
198  // DeleteDevice deletes device info by key
199  func deleteDevice(deviceID string) {
200         log.Println(logPrefix, "[deleteDevice]", deviceID)