Add device capability and get authenticated device list
[platform/core/system/edge-orchestration.git] / patches / main.patch
1 diff --git a/src/interfaces/capi/main.go b/src/interfaces/capi/main.go
2 index 0a1d997..bbf1c24 100644
3 --- a/src/interfaces/capi/main.go
4 +++ b/src/interfaces/capi/main.go
5 @@ -62,11 +62,23 @@ typedef struct {
6         TargetInfo RemoteTargetInfo;
7  } ResponseService;
8  
9 +typedef struct {
10 +       char*   Message;
11 +       char*   Endpoints;
12 +} DeviceList;
13 +
14 +typedef struct {
15 +       char*   Message;
16 +       char*   CapabilityJson;
17 +} DeviceCapability;
18 +
19  typedef char* (*identityGetterFunc)();
20  typedef char* (*keyGetterFunc)(char* id);
21 +typedef int (*executeCb) (char* pkg_name, char* args);
22  
23 -identityGetterFunc iGetter;
24 -keyGetterFunc kGetter;
25 +static identityGetterFunc iGetter;
26 +static keyGetterFunc kGetter;
27 +static executeCb execCb;
28  
29  static void setPSKHandler(identityGetterFunc ihandle, keyGetterFunc khandle){
30         iGetter = ihandle;
31 @@ -80,6 +92,14 @@ static char* bridge_iGetter(){
32  static char* bridge_kGetter(char* id){
33         return kGetter(id);
34  }
35 +
36 +static void setExecCb(executeCb exechandle) {
37 +        execCb = exechandle;
38 +}
39 +
40 +static int bridge_execCb(char *pkg_name, char* args){
41 +        return execCb(pkg_name, args);
42 +}
43  #ifdef __cplusplus
44  }
45  
46 @@ -99,7 +119,7 @@ import (
47         "unsafe"
48  
49         "common/logmgr"
50 -
51 +       capabilitymgr "controller/configuremgr"
52         configuremgr "controller/configuremgr/native"
53         "controller/discoverymgr"
54         scoringmgr "controller/scoringmgr"
55 @@ -127,10 +147,12 @@ const (
56  
57         edgeDir = "/var/edge-orchestration"
58  
59 -       logPath             = edgeDir + "/log"
60 -       configPath          = edgeDir + "/apps"
61 -       dbPath              = edgeDir + "/data/db"
62 -       certificateFilePath = edgeDir + "/data/cert"
63 +       logPath                = edgeDir + "/log"
64 +       configPath             = edgeDir + "/apps"
65 +       dbPath                 = edgeDir + "/data/db"
66 +       certificateFilePath    = edgeDir + "/data/cert"
67 +       containerWhiteListPath = edgeDir + "/data/cwl"
68 +       passPhraseJWTPath      = edgeDir + "/data/jwt"
69  
70         cipherKeyFilePath = edgeDir + "/user/orchestration_userID.txt"
71         deviceIDFilePath  = edgeDir + "/device/orchestration_deviceID.txt"
72 @@ -140,12 +162,23 @@ var (
73         flagVersion                  bool
74         commitID, version, buildTime string
75         buildTags                    string
76 +       keySet                       bool
77  
78         orcheEngine orchestrationapi.Orche
79  )
80  
81 +type execHandler struct{}
82 +
83 +func (eHandler execHandler) CmdExecute(pkgName string, args string) int {
84 +       var cPkg *C.char
85 +       var cArgs *C.char
86 +       cPkg = C.CString(pkgName)
87 +       cArgs = C.CString(args)
88 +       return int(C.bridge_execCb(cPkg, cArgs))
89 +}
90 +
91  //export OrchestrationInit
92 -func OrchestrationInit() C.int {
93 +func OrchestrationInit(execCb C.executeCb) C.int {
94         flag.BoolVar(&flagVersion, "v", false, "if true, print version and exit")
95         flag.BoolVar(&flagVersion, "version", false, "if true, print version and exit")
96         flag.Parse()
97 @@ -180,6 +213,7 @@ func OrchestrationInit() C.int {
98         builder.SetScoring(scoringmgr.GetInstance())
99         builder.SetService(servicemgr.GetInstance())
100         builder.SetExecutor(nativeexecutor.GetInstance())
101 +       builder.SetCapability(capabilitymgr.GetInstance())
102         builder.SetClient(restIns)
103         orcheEngine = builder.Build()
104         if orcheEngine == nil {
105 @@ -187,6 +221,9 @@ func OrchestrationInit() C.int {
106                 return -1
107         }
108  
109 +       C.setExecCb(execCb)
110 +       nativeexecutor.GetInstance().SetExecuteHandler(execHandler{})
111 +
112         orcheEngine.Start(deviceIDFilePath, platform, executionType)
113  
114         var restEdgeRouter *route.RestRouter
115 @@ -271,6 +308,138 @@ func OrchestrationRequestService(cAppName *C.char, cSelfSelection C.int, cReques
116         return ret
117  }
118  
119 +//export OrchestrationRequestServiceOnDevice
120 +func OrchestrationRequestServiceOnDevice(cAppName *C.char, cSelfSelection C.int, cRequester *C.char, serviceInfo *C.RequestServiceInfo, cIP *C.char, count C.int) C.ResponseService {
121 +       log.Printf("[%s] OrchestrationRequestService", logPrefix)
122 +
123 +       appName := C.GoString(cAppName)
124 +       ip := C.GoString(cIP)
125 +
126 +       requestInfos := make([]orchestrationapi.RequestServiceInfo, count)
127 +       CServiceInfo := (*[(math.MaxInt16 - 1) / unsafe.Sizeof(serviceInfo)]C.RequestServiceInfo)(unsafe.Pointer(serviceInfo))[:count:count]
128 +
129 +       for idx, requestInfo := range CServiceInfo {
130 +               requestInfos[idx].ExecutionType = C.GoString(requestInfo.ExecutionType)
131 +
132 +               args := strings.Split(C.GoString(requestInfo.ExeCmd), " ")
133 +               if strings.Compare(args[0], "") == 0 {
134 +                       args = nil
135 +               }
136 +               requestInfos[idx].ExeCmd = append([]string{}, args...)
137 +       }
138 +
139 +       externalAPI, err := orchestrationapi.GetExternalAPI()
140 +       if err != nil {
141 +               log.Fatalf("[%s] Orchestaration external api : %s", logPrefix, err.Error())
142 +       }
143 +
144 +       selfSel := true
145 +       if cSelfSelection == 0 {
146 +               selfSel = false
147 +       }
148 +
149 +       requester := C.GoString(cRequester)
150 +
151 +       log.Printf("[OrchestrationRequestService] appName:%s", appName)
152 +       log.Printf("[OrchestrationRequestService] selfSel:%v", selfSel)
153 +       log.Printf("[OrchestrationRequestService] requester:%s", requester)
154 +       log.Printf("[OrchestrationRequestService] infos:%v", requestInfos)
155 +
156 +       res := externalAPI.RequestServiceOnDevice(orchestrationapi.ReqeustService{
157 +               ServiceName:      appName,
158 +               SelfSelection:    selfSel,
159 +               ServiceInfo:      requestInfos,
160 +               ServiceRequester: requester,
161 +       }, ip)
162 +       log.Println("requestService handle : ", res)
163 +
164 +       ret := C.ResponseService{}
165 +       ret.Message = C.CString(res.Message)
166 +       ret.ServiceName = C.CString(res.ServiceName)
167 +       ret.RemoteTargetInfo.ExecutionType = C.CString(res.RemoteTargetInfo.ExecutionType)
168 +       ret.RemoteTargetInfo.Target = C.CString(res.RemoteTargetInfo.Target)
169 +
170 +       return ret
171 +}
172 +
173 +//export OrchestrationGetDeviceList
174 +func OrchestrationGetDeviceList(cServiceName, cExecutionType *C.char) *C.DeviceList {
175 +       log.Printf("[%s] OrchestrationGetDeviceList", logPrefix)
176 +
177 +       svcName := C.GoString(cServiceName)
178 +       execType := C.GoString(cExecutionType)
179 +
180 +       externalAPI, err := orchestrationapi.GetExternalAPI()
181 +       if err != nil {
182 +               log.Fatalf("[%s] Orchestaration external api : %s", logPrefix, err.Error())
183 +       }
184 +
185 +       deviceList, err := externalAPI.GetAuthenticatedDeviceList(svcName, execType)
186 +       log.Println(logPrefix, "Response from orch.GetDeviceList : ", deviceList)
187 +       if err != nil {
188 +               log.Println(logPrefix, "Response : ", deviceList, err.Error())
189 +               devices := (*C.DeviceList)(C.malloc(C.size_t(unsafe.Sizeof(C.struct_DeviceList{}))))
190 +               devices.Message = C.CString(err.Error())
191 +               devices.Endpoints = C.CString("")
192 +               return devices
193 +       }
194 +
195 +       endpoints := strings.Join(deviceList, ",")
196 +       log.Println(logPrefix, "Endpoints: ", endpoints)
197 +
198 +       devices := (*C.DeviceList)(C.malloc(C.size_t(unsafe.Sizeof(C.struct_DeviceList{}))))
199 +       devices.Message = C.CString("ERROR_NONE")
200 +       devices.Endpoints = C.CString(endpoints)
201 +
202 +       return devices
203 +}
204 +
205 +//export OrchestrationReadCapability
206 +func OrchestrationReadCapability(cIP *C.char) *C.DeviceCapability {
207 +       log.Printf("[%s] OrchestrationReadCapability", logPrefix)
208 +
209 +       ip := C.GoString(cIP)
210 +
211 +       externalAPI, err := orchestrationapi.GetExternalAPI()
212 +       if err != nil {
213 +               log.Fatalf("[%s] Orchestaration external api : %s", logPrefix, err.Error())
214 +       }
215 +
216 +       capability := (*C.DeviceCapability)(C.malloc(C.size_t(unsafe.Sizeof(C.struct_DeviceCapability{}))))
217 +
218 +       capJson, err := externalAPI.ReadCapability(ip)
219 +       if err != nil {
220 +               log.Println(logPrefix, "ReadCapability Error:", err.Error())
221 +               capability.Message = C.CString(err.Error())
222 +               capability.CapabilityJson = C.CString("")
223 +       } else {
224 +               capability.Message = C.CString("ERROR_NONE")
225 +               capability.CapabilityJson = C.CString(capJson)
226 +       }
227 +
228 +       return capability
229 +}
230 +
231 +//export OrchestrationWriteCapability
232 +func OrchestrationWriteCapability(cCapability *C.char) *C.char {
233 +       log.Printf("[%s] OrchestrationWriteCapability", logPrefix)
234 +
235 +       capJson := C.GoString(cCapability)
236 +
237 +       externalAPI, err := orchestrationapi.GetExternalAPI()
238 +       if err != nil {
239 +               log.Fatalf("[%s] Orchestaration external api : %s", logPrefix, err.Error())
240 +       }
241 +
242 +       er := externalAPI.WriteCapability(capJson)
243 +       if er != nil {
244 +               log.Println(logPrefix, "Error:", er.Error())
245 +               return C.CString(er.Error())
246 +       }
247 +
248 +       return C.CString("SUCCESS")
249 +}
250 +
251  type customPSKHandler struct{}
252  
253  func (cHandler customPSKHandler) GetIdentity() string {
254 @@ -288,7 +457,13 @@ func (cHandler customPSKHandler) GetKey(id string) ([]byte, error) {
255         cKey = C.bridge_kGetter(cStr)
256         key := C.GoString(cKey)
257         if len(key) == 0 {
258 +               keySet = false
259                 return nil, errors.New("key is empty")
260 +       } else {
261 +               if (keySet == false) {
262 +                       discoverymgr.GetInstance().ForceDiscovery()
263 +                       keySet = true
264 +               }
265         }
266         return []byte(key), nil
267  }