Add new packages to the container
[ric-plt/rtmgr.git] / pkg / nbi / httpgetter.go
index 6780403..bdcc435 100644 (file)
@@ -19,7 +19,7 @@
 /*
   Mnemonic:    httpgetter.go
   Abstract:    HTTPgetter NBI implementation
-               Simple HTTP getter solution. Only for testing purpose.
+               Simple HTTP getter solution.
   Date:                15 March 2019
 */
 
@@ -28,23 +28,53 @@ package nbi
 import (
        "encoding/json"
        "net/http"
-       "rtmgr"
+       "routing-manager/pkg/rpe"
+       "routing-manager/pkg/rtmgr"
+       "routing-manager/pkg/sdl"
        "time"
 )
 
-var myClient = &http.Client{Timeout: 1 * time.Second}
+type HttpGetter struct {
+       Engine
+       FetchAllXApps FetchAllXAppsHandler
+}
+
+func NewHttpGetter() *HttpGetter {
+       instance := new(HttpGetter)
+       instance.FetchAllXApps = fetchAllXApps
+       return instance
+}
 
-func fetchXappList(url string) (*[]rtmgr.XApp, error) {
-       rtmgr.Logger.Debug("Invoked httpgetter.fetchXappList")
-       r, err := myClient.Get(url)
+var myClient = &http.Client{Timeout: 5 * time.Second}
+
+func fetchAllXApps(xmurl string) (*[]rtmgr.XApp, error) {
+       rtmgr.Logger.Info("Invoked httpGetter.fetchXappList: " + xmurl)
+       r, err := myClient.Get(xmurl)
        if err != nil {
                return nil, err
        }
        defer r.Body.Close()
-       rtmgr.Logger.Debug("http client raw response: %v", r)
-       var xapps []rtmgr.XApp
-       json.NewDecoder(r.Body).Decode(&xapps)
-       rtmgr.Logger.Info("HTTP GET: OK")
-       rtmgr.Logger.Debug("httpgetter.fetchXappList returns: %v", xapps)
-       return &xapps, err
+
+       if r.StatusCode == 200 {
+               rtmgr.Logger.Debug("http client raw response: %v", r)
+               var xApps []rtmgr.XApp
+               err = json.NewDecoder(r.Body).Decode(&xApps)
+               if err != nil {
+                       rtmgr.Logger.Warn("Json decode failed: " + err.Error())
+               }
+               rtmgr.Logger.Info("HTTP GET: OK")
+               rtmgr.Logger.Debug("httpGetter.fetchXappList returns: %v", xApps)
+               return &xApps, err
+       }
+       rtmgr.Logger.Warn("httpGetter got an unexpected http status code: %v", r.StatusCode)
+       return nil, nil
+}
+
+func (g *HttpGetter) Initialize(xmurl string, nbiif string, fileName string, configfile string,
+       sdlEngine sdl.Engine, rpeEngine rpe.Engine, triggerSBI chan<- bool) error {
+       return nil
+}
+
+func (g *HttpGetter) Terminate() error {
+       return nil
 }