RICPLT-2985 Route update via registry/subscription entry
[ric-plt/submgr.git] / pkg / control / registry.go
index 1adb4f7..2c5bd8c 100644 (file)
 package control
 
 import (
+       "fmt"
        "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp"
-       "strconv"
        "sync"
 )
 
-//-----------------------------------------------------------------------------
-//
-//-----------------------------------------------------------------------------
-type Subscription struct {
-       mutex  sync.Mutex
-       Seq    uint16
-       Active bool
-       //
-       Meid        *xapp.RMRMeid
-       RmrEndpoint // xapp endpoint
-       Trans       *Transaction
-}
-
-func (s *Subscription) String() string {
-       s.mutex.Lock()
-       defer s.mutex.Unlock()
-       return strconv.FormatUint(uint64(s.Seq), 10) + "/" + s.RmrEndpoint.String() + "/" + s.Meid.RanName
-}
-
-func (s *Subscription) Confirmed() {
-       s.mutex.Lock()
-       defer s.mutex.Unlock()
-       s.Active = true
-}
-
-func (s *Subscription) UnConfirmed() {
-       s.mutex.Lock()
-       defer s.mutex.Unlock()
-       s.Active = false
-}
-
-func (s *Subscription) IsConfirmed() bool {
-       s.mutex.Lock()
-       defer s.mutex.Unlock()
-       return s.Active
-}
-
-func (s *Subscription) SetTransaction(trans *Transaction) bool {
-       s.mutex.Lock()
-       defer s.mutex.Unlock()
-       if s.Trans == nil {
-               s.Trans = trans
-               return true
-       }
-       return false
-}
-
-func (s *Subscription) UnSetTransaction(trans *Transaction) bool {
-       s.mutex.Lock()
-       defer s.mutex.Unlock()
-       if trans == nil || trans == s.Trans {
-               s.Trans = nil
-               return true
-       }
-       return false
-}
-
-func (s *Subscription) GetTransaction() *Transaction {
-       s.mutex.Lock()
-       defer s.mutex.Unlock()
-       return s.Trans
-}
-
-func (s *Subscription) SubRouteInfo(act Action) SubRouteInfo {
-       s.mutex.Lock()
-       defer s.mutex.Unlock()
-       return SubRouteInfo{act, s.RmrEndpoint.Addr, s.RmrEndpoint.Port, s.Seq}
-}
-
 //-----------------------------------------------------------------------------
 //
 //-----------------------------------------------------------------------------
 type Registry struct {
-       register map[uint16]*Subscription
-       counter  uint16
-       mutex    sync.Mutex
+       mutex       sync.Mutex
+       register    map[uint16]*Subscription
+       counter     uint16
+       rtmgrClient *RtmgrClient
 }
 
 // This method should run as a constructor
@@ -110,7 +42,7 @@ func (r *Registry) Initialize(seedsn uint16) {
 }
 
 // Reserves and returns the next free sequence number
-func (r *Registry) ReserveSubscription(endPoint RmrEndpoint, meid *xapp.RMRMeid) *Subscription {
+func (r *Registry) ReserveSubscription(endPoint RmrEndpoint, meid *xapp.RMRMeid) (*Subscription, error) {
        // Check is current SequenceNumber valid
        // Allocate next SequenceNumber value and retry N times
        r.mutex.Lock()
@@ -125,17 +57,29 @@ func (r *Registry) ReserveSubscription(endPoint RmrEndpoint, meid *xapp.RMRMeid)
                        r.counter++
                }
                if _, ok := r.register[sequenceNumber]; ok == false {
-                       r.register[sequenceNumber] = &Subscription{
+                       subs := &Subscription{
                                Seq:         sequenceNumber,
                                Active:      false,
                                RmrEndpoint: endPoint,
                                Meid:        meid,
                                Trans:       nil,
                        }
-                       return r.register[sequenceNumber]
+                       r.register[sequenceNumber] = subs
+
+                       // Update routing
+                       r.mutex.Unlock()
+                       err := subs.UpdateRoute(CREATE, r.rtmgrClient)
+                       r.mutex.Lock()
+                       if err != nil {
+                               if _, ok := r.register[sequenceNumber]; ok {
+                                       delete(r.register, sequenceNumber)
+                               }
+                               return nil, err
+                       }
+                       return subs, nil
                }
        }
-       return nil
+       return nil, fmt.Errorf("Registry: Failed to reserves subcription. RmrEndpoint: %s, Meid: %s", endPoint, meid.RanName)
 }
 
 func (r *Registry) GetSubscription(sn uint16) *Subscription {
@@ -148,14 +92,21 @@ func (r *Registry) GetSubscription(sn uint16) *Subscription {
        return nil
 }
 
-//This function releases the given id as unused in the register
-func (r *Registry) releaseSequenceNumber(sn uint16) bool {
+func (r *Registry) DelSubscription(sn uint16) bool {
        r.mutex.Lock()
        defer r.mutex.Unlock()
        if _, ok := r.register[sn]; ok {
+               subs := r.register[sn]
                delete(r.register, sn)
+
+               // Update routing
+               r.mutex.Unlock()
+               err := subs.UpdateRoute(DELETE, r.rtmgrClient)
+               r.mutex.Lock()
+               if err != nil {
+                       xapp.Logger.Error("Registry: Failed to del route. SubId: %d, RmrEndpoint: %s", subs.Seq, subs.RmrEndpoint)
+               }
                return true
-       } else {
-               return false
        }
+       return false
 }