Add device capability and get authenticated device list
[platform/core/system/edge-orchestration.git] / patches / devicedetails.patch
1 diff --git a/src/controller/servicemgr/devicedetails.go b/src/controller/servicemgr/devicedetails.go
2 new file mode 100644
3 index 0000000..739ff37
4 --- /dev/null
5 +++ b/src/controller/servicemgr/devicedetails.go
6 @@ -0,0 +1,84 @@
7 +package servicemgr
8 +
9 +import (
10 +       "common/utils"
11 +       configurationdb "db/bolt/configuration"
12 +       networkdb "db/bolt/network"
13 +       servicedb "db/bolt/service"
14 +       "errors"
15 +       "log"
16 +       "restinterface/resthelper"
17 +)
18 +
19 +const (
20 +       pingAPI      = "/api/v1/ping"
21 +       internalPort = 56002
22 +)
23 +
24 +var (
25 +       logTag         = logPrefix + "[GetAuthenticatedDevices]"
26 +       helper         resthelper.RestHelper
27 +       netDBExecutor  networkdb.DBInterface
28 +       servDBExecutor servicedb.DBInterface
29 +       confDBExecutor configurationdb.DBInterface
30 +       util           utils.UtilsHelper
31 +)
32 +
33 +func find(slice []string, val string) bool {
34 +       for _, item := range slice {
35 +               if item == val {
36 +                       return true
37 +               }
38 +       }
39 +       return false
40 +}
41 +
42 +// GetAuthenticatedDevices returns list of Authenticated devices that are having the requested service and have the requested execution type.
43 +func (sm *SMMgrImpl) GetAuthenticatedDevices(service string, exType string) ([]string, error) {
44 +       log.Println(logTag, "getAuthenticatedDevices -- API Called.", service, exType)
45 +       var deviceIps []string
46 +       var allDevicesList []string
47 +
48 +       servInfos, err := servDBExecutor.GetList()
49 +
50 +       if len(servInfos) == 0 || err != nil {
51 +               log.Println(logTag, "Could not fetch from service db")
52 +               if err != nil {
53 +                       log.Println(logTag, "ERROR:", err.Error())
54 +               }
55 +               return deviceIps, errors.New("service not available")
56 +
57 +       }
58 +
59 +       for _, servInfo := range servInfos {
60 +
61 +               if service == "" || find(servInfo.Services, service) {
62 +                       allDevicesList = append(allDevicesList, servInfo.ID)
63 +                       log.Println(logTag, "Service found in device ", servInfo.ID)
64 +               }
65 +       }
66 +
67 +       for _, device := range allDevicesList {
68 +               devInfo, err := confDBExecutor.Get(device)
69 +               if err != nil {
70 +                       log.Println(logTag, "Could not fetch from config db", err.Error())
71 +                       continue
72 +               }
73 +               if exType == "" || devInfo.ExecType == exType {
74 +                       log.Println(logTag, "Adding Device "+device+" As execution type is "+ devInfo.ExecType + " And required Ex type is "+exType )
75 +                       netInfo, err := netDBExecutor.Get(device)
76 +                       if err != nil {
77 +                               log.Println(logTag, "Could not fetch from Network db", err.Error())
78 +                               continue
79 +                       }
80 +                       if len(netInfo.IPv4) != 0 && util.PingDevice(netInfo.IPv4[0]) == true {
81 +                               deviceIps = append(deviceIps, netInfo.IPv4[0])
82 +                       }
83 +               }
84 +       }
85 +       if len(deviceIps)>0{
86 +               log.Println(logTag, "FINAL DEVICE IPS:", deviceIps)
87 +               return deviceIps, nil
88 +       }
89 +       return deviceIps , errors.New("no device found")
90 +}
91 \ No newline at end of file