Initial commit for Xapp Orchestration
[ric-plt/appmgr.git] / pkg / resthooks / resthooks.go
index 5076b06..6b405fc 100755 (executable)
@@ -22,23 +22,34 @@ package resthooks
 import (
        "bytes"
        "encoding/json"
-       sdl "gerrit.oran-osc.org/r/ric-plt/sdlgo"
+       "fmt"
+       sdl "gerrit.o-ran-sc.org/r/ric-plt/sdlgo"
        cmap "github.com/orcaman/concurrent-map"
        "github.com/segmentio/ksuid"
        "net/http"
+       "strings"
        "time"
 
-       "gerrit.oran-osc.org/r/ric-plt/appmgr/pkg/appmgr"
-       "gerrit.oran-osc.org/r/ric-plt/appmgr/pkg/models"
+       "gerrit.o-ran-sc.org/r/ric-plt/appmgr/pkg/appmgr"
+       "gerrit.o-ran-sc.org/r/ric-plt/appmgr/pkg/models"
 )
 
-func NewResthook() *Resthook {
+func NewResthook(restoreData bool) *Resthook {
+       return createResthook(restoreData, sdl.NewSdlInstance("appmgr", sdl.NewDatabase()),sdl.NewSdlInstance("appdb", sdl.NewDatabase()))
+}
+
+func createResthook(restoreData bool, sdlInst iSdl, sdlInst2 iSdl) *Resthook {
        rh := &Resthook{
                client: &http.Client{},
-               db:     sdl.NewSdlInstance("appmgr", sdl.NewDatabase()),
+               db:     sdlInst,
+               db2:    sdlInst2,
        }
 
-       rh.subscriptions = rh.RestoreSubscriptions()
+       if restoreData {
+               rh.subscriptions = rh.RestoreSubscriptions()
+       } else {
+               rh.subscriptions = cmap.New()
+       }
        return rh
 }
 
@@ -47,7 +58,8 @@ func (rh *Resthook) AddSubscription(sr models.SubscriptionRequest) *models.Subsc
                r := v.Val.(SubscriptionInfo).req
                if *r.Data.TargetURL == *sr.Data.TargetURL && r.Data.EventType == sr.Data.EventType {
                        appmgr.Logger.Info("Similar subscription already exists!")
-                       return &models.SubscriptionResponse{}
+                       resp := v.Val.(SubscriptionInfo).resp
+                       return &resp
                }
        }
 
@@ -122,7 +134,14 @@ func (rh *Resthook) NotifyClients(xapps models.AllDeployedXapps, et models.Event
 }
 
 func (rh *Resthook) notify(xapps models.AllDeployedXapps, et models.EventType, s SubscriptionInfo, seq int64) error {
-       notif := models.SubscriptionNotification{ID: s.Id, Version: seq, EventType: et, XApps: xapps}
+       xappData, err := json.Marshal(xapps)
+       if err != nil {
+               appmgr.Logger.Info("json.Marshal failed: %v", err)
+               return err
+       }
+
+       // TODO: Use models.SubscriptionNotification instead of internal ...
+       notif := SubscriptionNotification{ID: s.Id, Version: seq, Event: string(et), XApps: string(xappData)}
        jsonData, err := json.Marshal(notif)
        if err != nil {
                appmgr.Logger.Info("json.Marshal failed: %v", err)
@@ -164,7 +183,6 @@ func (rh *Resthook) retry(s SubscriptionInfo, fn func() error) error {
 func (rh *Resthook) StoreSubscriptions(m cmap.ConcurrentMap) {
        for v := range m.Iter() {
                s := v.Val.(SubscriptionInfo)
-
                data, err := json.Marshal(s.req)
                if err != nil {
                        appmgr.Logger.Error("json.marshal failed: %v ", err.Error())
@@ -222,3 +240,68 @@ func (rh *Resthook) FlushSubscriptions() {
        rh.db.RemoveAll()
        rh.subscriptions = cmap.New()
 }
+
+func (rh *Resthook) UpdateAppData(params models.RegisterRequest, updateflag bool) {
+       appmgr.Logger.Info("Endpoint to be added in SDL: %s", *params.HTTPEndpoint)
+       if updateflag == false {
+               return
+       }
+
+       value, err := rh.db2.Get([]string{"endpoints"})
+       if err != nil {
+               appmgr.Logger.Error("DB.session.Get failed: %v ", err.Error())
+               return
+       }
+
+       appmgr.Logger.Info("List of Apps in SDL: %v", value["endpoints"])
+       var appsindb []string
+       var data string
+       dbflag := false
+
+       if value["endpoints"] != nil {
+               formstring := fmt.Sprintf("%s", value["endpoints"])
+               newstring := strings.Split(formstring, " ")
+               for i, _ := range newstring {
+                       if len(newstring) == 1 && strings.Contains(newstring[i], *params.HTTPEndpoint) {
+                               appmgr.Logger.Info("Removing Key %s", *params.HTTPEndpoint)
+                               rh.db2.Remove([]string{"endpoints"})
+                               dbflag = true
+                               break
+                       }
+                       if strings.Contains(newstring[i], *params.HTTPEndpoint) {
+                               appmgr.Logger.Info("Removing entry %s", *params.HTTPEndpoint)
+                               dbflag = true
+                               continue
+                       }
+                       appsindb = append(appsindb, newstring[i])
+                       data = strings.Join(appsindb, " ")
+               }
+               rh.db2.Set("endpoints", strings.TrimSpace(data))
+       }
+
+       if dbflag == false {
+               xappData, err := json.Marshal(params)
+               if err != nil {
+                       appmgr.Logger.Info("json.Marshal failed: %v", err)
+                       return
+               }
+               appsindb = append(appsindb, string(xappData))
+               data = strings.Join(appsindb, " ")
+               rh.db2.Set("endpoints", strings.TrimSpace(data))
+       }
+}
+
+func (rh *Resthook) GetAppsInSDL() *string {
+       value, err := rh.db2.Get([]string{"endpoints"})
+       if err != nil {
+               appmgr.Logger.Error("DB.session.Get failed: %v ", err.Error())
+               return nil
+       }
+       appmgr.Logger.Info("List of Apps in SDL: %v", value["endpoints"])
+       if value["endpoints"] == nil || value["endpoints"] == "" {
+               return nil
+       } else {
+               apps := fmt.Sprintf("%s", value["endpoints"])
+               return &apps
+       }
+}