Use go module as dependency tool instead of glide
[src/xds/xds-agent.git] / lib / agent / xdsserver.go
index 7020ef0..40ee57b 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2017 "IoT.bzh"
+ * Copyright (C) 2017-2018 "IoT.bzh"
  * Author Sebastien Douheret <sebastien@iot.bzh>
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
@@ -21,17 +21,16 @@ import (
        "encoding/json"
        "fmt"
        "io"
-       "io/ioutil"
        "net/http"
        "strings"
        "sync"
        "time"
 
+       "gerrit.automotivelinux.org/gerrit/src/xds/xds-agent.git/lib/xaapiv1"
+       "gerrit.automotivelinux.org/gerrit/src/xds/xds-agent.git/lib/xdsconfig"
+       common "gerrit.automotivelinux.org/gerrit/src/xds/xds-common.git"
+       "gerrit.automotivelinux.org/gerrit/src/xds/xds-server.git/lib/xsapiv1"
        "github.com/gin-gonic/gin"
-       "github.com/iotbzh/xds-agent/lib/xaapiv1"
-       "github.com/iotbzh/xds-agent/lib/xdsconfig"
-       common "github.com/iotbzh/xds-common/golib"
-       "github.com/iotbzh/xds-server/lib/xsapiv1"
        uuid "github.com/satori/go.uuid"
        sio_client "github.com/sebd71/go-socket.io-client"
 )
@@ -40,6 +39,7 @@ import (
 type XdsServer struct {
        *Context
        ID           string
+       URLIndex     string
        BaseURL      string
        APIURL       string
        PartialURL   string
@@ -55,16 +55,20 @@ type XdsServer struct {
        sockEventsLock *sync.Mutex
 
        // Private fields
-       client    *common.HTTPClient
-       ioSock    *sio_client.Client
-       logOut    io.Writer
-       apiRouter *gin.RouterGroup
-       cmdList   map[string]interface{}
+       client      *common.HTTPClient
+       ioSock      *sio_client.Client
+       logOut      io.Writer
+       apiRouter   *gin.RouterGroup
+       cmdList     map[string]interface{}
+       cbOnConnect OnConnectedCB
 }
 
 // EventCB Event emitter callback
 type EventCB func(privData interface{}, evtData interface{}) error
 
+// OnConnectedCB connect callback
+type OnConnectedCB func(svr *XdsServer) error
+
 // caller Used to chain event listeners
 type caller struct {
        id          uuid.UUID
@@ -80,6 +84,7 @@ func NewXdsServer(ctx *Context, conf xdsconfig.XDSServerConf) *XdsServer {
        return &XdsServer{
                Context:    ctx,
                ID:         _IDTempoPrefix + uuid.NewV1().String(),
+               URLIndex:   conf.URLIndex,
                BaseURL:    conf.URL,
                APIURL:     conf.APIBaseURL + conf.APIPartialURL,
                PartialURL: conf.APIPartialURL,
@@ -136,6 +141,12 @@ func (xs *XdsServer) Connect() error {
        return err
 }
 
+// ConnectOn Register a callback on events reception
+func (xs *XdsServer) ConnectOn(f OnConnectedCB) error {
+       xs.cbOnConnect = f
+       return nil
+}
+
 // IsTempoID returns true when server as a temporary id
 func (xs *XdsServer) IsTempoID() bool {
        return strings.HasPrefix(xs.ID, _IDTempoPrefix)
@@ -146,6 +157,19 @@ func (xs *XdsServer) SetLoggerOutput(out io.Writer) {
        xs.logOut = out
 }
 
+// GetConfig return the current server config
+func (xs *XdsServer) GetConfig() xaapiv1.ServerCfg {
+       return xaapiv1.ServerCfg{
+               ID:         xs.ID,
+               URL:        xs.BaseURL,
+               APIURL:     xs.APIURL,
+               PartialURL: xs.PartialURL,
+               ConnRetry:  xs.ConnRetry,
+               Connected:  xs.Connected,
+               Disabled:   xs.Disabled,
+       }
+}
+
 // SendCommand Send a command to XDS Server
 func (xs *XdsServer) SendCommand(cmd string, body []byte, res interface{}) error {
        url := cmd
@@ -199,6 +223,22 @@ func (xs *XdsServer) CommandSignal(args *xsapiv1.ExecSignalArgs, res *xsapiv1.Ex
        return xs.client.Post("/signal", args, res)
 }
 
+// CommandTgtTerminalGet Send GET request to retrieve info of a target terminals
+func (xs *XdsServer) CommandTgtTerminalGet(targetID, termID string, res *xsapiv1.TerminalConfig) error {
+       return xs.client.Get("/targets/"+targetID+"/terminals/"+termID, res)
+}
+
+// CommandTgtTerminalOpen Send POST request to open a target terminal
+func (xs *XdsServer) CommandTgtTerminalOpen(targetID string, termID string, res *xsapiv1.TerminalConfig) error {
+       var empty interface{}
+       return xs.client.Post("/targets/"+targetID+"/terminals/"+termID+"/open", &empty, res)
+}
+
+// CommandTgtTerminalSignal Send POST request to send a signal to a target terminal
+func (xs *XdsServer) CommandTgtTerminalSignal(args *xsapiv1.TerminalSignalArgs, res *xsapiv1.TerminalConfig) error {
+       return xs.client.Post("/signal", args, res)
+}
+
 // SetAPIRouterGroup .
 func (xs *XdsServer) SetAPIRouterGroup(r *gin.RouterGroup) {
        xs.apiRouter = r
@@ -239,8 +279,11 @@ func (xs *XdsServer) PassthroughPost(url string) {
        }
 
        xs.apiRouter.POST(url, func(c *gin.Context) {
-               bodyReq := []byte{}
-               n, err := c.Request.Body.Read(bodyReq)
+               var err error
+               var data interface{}
+
+               // Get raw body
+               body, err := c.GetRawData()
                if err != nil {
                        common.APIError(c, err.Error())
                        return
@@ -253,34 +296,133 @@ func (xs *XdsServer) PassthroughPost(url string) {
                }
 
                // Send Post request
-               body, err := json.Marshal(bodyReq[:n])
+               response, err := xs.client.HTTPPostWithRes(nURL, string(body))
                if err != nil {
-                       common.APIError(c, err.Error())
+                       goto httpError
+               }
+               if response.StatusCode != 200 {
+                       err = fmt.Errorf(response.Status)
                        return
                }
+               err = json.Unmarshal(xs.client.ResponseToBArray(response), &data)
+               if err != nil {
+                       goto httpError
+               }
 
-               response, err := xs.client.HTTPPostWithRes(nURL, string(body))
+               c.JSON(http.StatusOK, data)
+               return
+
+               /* Handle error case */
+       httpError:
+               if strings.Contains(err.Error(), "connection refused") {
+                       xs._Disconnected()
+               }
+               common.APIError(c, err.Error())
+               return
+       })
+}
+
+// PassthroughPut Used to declare a route that sends directly a PUT request to XDS Server
+func (xs *XdsServer) PassthroughPut(url string) {
+       if xs.apiRouter == nil {
+               xs.Log.Errorf("apiRouter not set !")
+               return
+       }
+
+       xs.apiRouter.PUT(url, func(c *gin.Context) {
+               var err error
+               var data interface{}
+
+               // Get raw body
+               body, err := c.GetRawData()
                if err != nil {
                        common.APIError(c, err.Error())
                        return
                }
 
-               bodyRes, err := ioutil.ReadAll(response.Body)
+               // Take care of param (eg. id in /projects/:id)
+               nURL := url
+               if strings.Contains(url, ":") {
+                       nURL = strings.TrimPrefix(c.Request.URL.Path, xs.APIURL)
+               }
+
+               // Send Put request
+               response, err := xs.client.HTTPPutWithRes(nURL, string(body))
                if err != nil {
-                       common.APIError(c, "Cannot read response body")
+                       goto httpError
+               }
+               if response.StatusCode != 200 {
+                       err = fmt.Errorf(response.Status)
                        return
                }
-               c.JSON(http.StatusOK, string(bodyRes))
+               err = json.Unmarshal(xs.client.ResponseToBArray(response), &data)
+               if err != nil {
+                       goto httpError
+               }
+
+               c.JSON(http.StatusOK, data)
+               return
+
+               /* Handle error case */
+       httpError:
+               if strings.Contains(err.Error(), "connection refused") {
+                       xs._Disconnected()
+               }
+               common.APIError(c, err.Error())
+               return
+       })
+}
+
+// PassthroughDelete Used to declare a route that sends directly a Delete request to XDS Server
+func (xs *XdsServer) PassthroughDelete(url string) {
+       if xs.apiRouter == nil {
+               xs.Log.Errorf("apiRouter not set !")
+               return
+       }
+
+       xs.apiRouter.DELETE(url, func(c *gin.Context) {
+               var err error
+               var data interface{}
+
+               // Take care of param (eg. id in /projects/:id)
+               nURL := url
+               if strings.Contains(url, ":") {
+                       nURL = strings.TrimPrefix(c.Request.URL.Path, xs.APIURL)
+               }
+
+               // Send Post request
+               response, err := xs.client.HTTPDeleteWithRes(nURL)
+               if err != nil {
+                       goto httpError
+               }
+               if response.StatusCode != 200 {
+                       err = fmt.Errorf(response.Status)
+                       return
+               }
+               err = json.Unmarshal(xs.client.ResponseToBArray(response), &data)
+               if err != nil {
+                       goto httpError
+               }
+
+               c.JSON(http.StatusOK, data)
+               return
+
+               /* Handle error case */
+       httpError:
+               if strings.Contains(err.Error(), "connection refused") {
+                       xs._Disconnected()
+               }
+               common.APIError(c, err.Error())
+               return
        })
 }
 
 // EventRegister Post a request to register to an XdsServer event
-func (xs *XdsServer) EventRegister(evName string, id string) error {
-       return xs.client.Post(
-               "/events/register",
+func (xs *XdsServer) EventRegister(evName string, filter string) error {
+       return xs.client.Post("/events/register",
                xsapiv1.EventRegisterArgs{
-                       Name:      evName,
-                       ProjectID: id,
+                       Name:   evName,
+                       Filter: filter,
                },
                nil)
 }
@@ -307,31 +449,16 @@ func (xs *XdsServer) EventOn(evName string, privData interface{}, f EventCB) (uu
                // Register listener only the first time
                evn := evName
 
-               // FIXME: use generic type: data interface{} instead of data xsapiv1.EventMsg
-               var err error
-               if evName == "event:folder-state-change" {
-                       err = xs.ioSock.On(evn, func(data xsapiv1.EventMsg) error {
-                               xs.sockEventsLock.Lock()
-                               sEvts := make([]*caller, len(xs.sockEvents[evn]))
-                               copy(sEvts, xs.sockEvents[evn])
-                               xs.sockEventsLock.Unlock()
-                               for _, c := range sEvts {
-                                       c.Func(c.PrivateData, data)
-                               }
-                               return nil
-                       })
-               } else {
-                       err = xs.ioSock.On(evn, func(data interface{}) error {
-                               xs.sockEventsLock.Lock()
-                               sEvts := make([]*caller, len(xs.sockEvents[evn]))
-                               copy(sEvts, xs.sockEvents[evn])
-                               xs.sockEventsLock.Unlock()
-                               for _, c := range sEvts {
-                                       c.Func(c.PrivateData, data)
-                               }
-                               return nil
-                       })
-               }
+               err := xs.ioSock.On(evn, func(data interface{}) error {
+                       xs.sockEventsLock.Lock()
+                       sEvts := make([]*caller, len(xs.sockEvents[evn]))
+                       copy(sEvts, xs.sockEvents[evn])
+                       xs.sockEventsLock.Unlock()
+                       for _, c := range sEvts {
+                               c.Func(c.PrivateData, data)
+                       }
+                       return nil
+               })
                if err != nil {
                        return uuid.Nil, err
                }
@@ -503,11 +630,10 @@ func (xs *XdsServer) _CreateConnectHTTP() error {
 
 // _Reconnect Re-established connection
 func (xs *XdsServer) _Reconnect() error {
+
+       // Note that ConnectOn callback will be called (see apiv1.go file)
        err := xs._Connect(true)
-       if err == nil {
-               // Reload projects list for this server
-               err = xs.projects.Init(xs)
-       }
+
        return err
 }
 
@@ -538,6 +664,12 @@ func (xs *XdsServer) _Connect(reConn bool) error {
        }
 
        xs.Connected = true
+
+       // Call OnConnect callback
+       if xs.cbOnConnect != nil {
+               xs.cbOnConnect(xs)
+       }
+
        xs._NotifyState()
        return nil
 }
@@ -569,7 +701,7 @@ func (xs *XdsServer) _SocketConnect() error {
        })
 
        iosk.On("disconnection", func(err error) {
-               xs.Log.Infof("IO.socket disconnection server %s", xs.ID)
+               xs.Log.Infof("IO.socket disconnection server %s (APIURL %s)", xs.ID, xs.APIURL)
                if xs.CBOnDisconnect != nil {
                        xs.CBOnDisconnect(err)
                }
@@ -612,6 +744,9 @@ func (xs *XdsServer) _SocketConnect() error {
 // _Disconnected Set XDS Server as disconnected
 func (xs *XdsServer) _Disconnected() error {
        // Clear all register events as socket is closed
+       xs.sockEventsLock.Lock()
+       defer xs.sockEventsLock.Unlock()
+
        for k := range xs.sockEvents {
                delete(xs.sockEvents, k)
        }