RICPLT-2989 Submgr routing manager client code to support multiple endpoints
[ric-plt/submgr.git] / pkg / control / registry.go
index 1adb4f7..2750b78 100644 (file)
 package control
 
 import (
+       "fmt"
+       "gerrit.o-ran-sc.org/r/ric-plt/e2ap/pkg/e2ap"
        "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp"
-       "strconv"
        "sync"
+       "time"
 )
 
 //-----------------------------------------------------------------------------
 //
 //-----------------------------------------------------------------------------
-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
+type Registry struct {
+       mutex       sync.Mutex
+       register    map[uint16]*Subscription
+       subIds      []uint16
+       rtmgrClient *RtmgrClient
 }
 
-func (s *Subscription) Confirmed() {
-       s.mutex.Lock()
-       defer s.mutex.Unlock()
-       s.Active = true
+func (r *Registry) Initialize() {
+       r.register = make(map[uint16]*Subscription)
+       var i uint16
+       for i = 0; i < 65535; i++ {
+               r.subIds = append(r.subIds, i+1)
+       }
 }
 
-func (s *Subscription) UnConfirmed() {
-       s.mutex.Lock()
-       defer s.mutex.Unlock()
-       s.Active = false
-}
+func (r *Registry) AssignToSubscription(trans *Transaction, subReqMsg *e2ap.E2APSubscriptionRequest) (*Subscription, error) {
+       r.mutex.Lock()
+       defer r.mutex.Unlock()
 
-func (s *Subscription) IsConfirmed() bool {
-       s.mutex.Lock()
-       defer s.mutex.Unlock()
-       return s.Active
-}
+       var sequenceNumber uint16
 
-func (s *Subscription) SetTransaction(trans *Transaction) bool {
-       s.mutex.Lock()
-       defer s.mutex.Unlock()
-       if s.Trans == nil {
-               s.Trans = trans
-               return true
+       //
+       // Allocate subscription
+       //
+       if len(r.subIds) > 0 {
+               sequenceNumber = r.subIds[0]
+               r.subIds = r.subIds[1:]
+               if _, ok := r.register[sequenceNumber]; ok == true {
+                       r.subIds = append(r.subIds, sequenceNumber)
+                       return nil, fmt.Errorf("Registry: Failed to reserves subscription")
+               }
+       } else {
+               return nil, fmt.Errorf("Registry: Failed to reserves subscription no free ids")
        }
-       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
+       subs := &Subscription{
+               registry: r,
+               Seq:      sequenceNumber,
+               Meid:     trans.Meid,
        }
-       return false
-}
 
-func (s *Subscription) GetTransaction() *Transaction {
-       s.mutex.Lock()
-       defer s.mutex.Unlock()
-       return s.Trans
-}
+       //
+       // Add to subscription
+       //
+       subs.mutex.Lock()
+       defer subs.mutex.Unlock()
 
-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}
-}
+       if subs.EpList.AddEndpoint(trans.GetEndpoint()) == false {
+               r.subIds = append(r.subIds, sequenceNumber)
+               return nil, fmt.Errorf("Registry: Endpoint existing already in subscription")
+       }
+       epamount := subs.EpList.Size()
 
-//-----------------------------------------------------------------------------
-//
-//-----------------------------------------------------------------------------
-type Registry struct {
-       register map[uint16]*Subscription
-       counter  uint16
-       mutex    sync.Mutex
-}
+       r.mutex.Unlock()
+       //
+       // Subscription route updates
+       //
+       var err error
+       if epamount == 1 {
+               subRouteAction := SubRouteInfo{CREATE, subs.EpList, subs.Seq}
+               err = r.rtmgrClient.SubscriptionRequestUpdate(subRouteAction)
+       } else {
+               subRouteAction := SubRouteInfo{UPDATE, subs.EpList, subs.Seq}
+               err = r.rtmgrClient.SubscriptionRequestUpdate(subRouteAction)
+       }
+       r.mutex.Lock()
 
-// This method should run as a constructor
-func (r *Registry) Initialize(seedsn uint16) {
-       r.register = make(map[uint16]*Subscription)
-       r.counter = seedsn
+       if err != nil {
+               r.subIds = append(r.subIds, sequenceNumber)
+               return nil, err
+       }
+       subs.SubReqMsg = subReqMsg
+
+       r.register[sequenceNumber] = subs
+       xapp.Logger.Debug("Registry: Create %s", subs.String())
+       xapp.Logger.Debug("Registry: substable=%v", r.register)
+       return subs, nil
 }
 
-// Reserves and returns the next free sequence number
-func (r *Registry) ReserveSubscription(endPoint RmrEndpoint, meid *xapp.RMRMeid) *Subscription {
-       // Check is current SequenceNumber valid
-       // Allocate next SequenceNumber value and retry N times
+func (r *Registry) RemoveFromSubscription(subs *Subscription, trans *Transaction, waitRouteClean time.Duration) error {
        r.mutex.Lock()
        defer r.mutex.Unlock()
-       var subs *Subscription = nil
-       var retrytimes uint16 = 1000
-       for ; subs == nil && retrytimes > 0; retrytimes-- {
-               sequenceNumber := r.counter
-               if r.counter == 65535 {
-                       r.counter = 0
-               } else {
-                       r.counter++
+       subs.mutex.Lock()
+       defer subs.mutex.Unlock()
+
+       delStatus := subs.EpList.DelEndpoint(trans.GetEndpoint())
+       epamount := subs.EpList.Size()
+
+       //
+       // If last endpoint remove from register map
+       //
+       if epamount == 0 {
+               if _, ok := r.register[subs.Seq]; ok {
+                       xapp.Logger.Debug("Registry: Delete %s", subs.String())
+                       delete(r.register, subs.Seq)
+                       xapp.Logger.Debug("Registry: substable=%v", r.register)
                }
-               if _, ok := r.register[sequenceNumber]; ok == false {
-                       r.register[sequenceNumber] = &Subscription{
-                               Seq:         sequenceNumber,
-                               Active:      false,
-                               RmrEndpoint: endPoint,
-                               Meid:        meid,
-                               Trans:       nil,
-                       }
-                       return r.register[sequenceNumber]
+       }
+       r.mutex.Unlock()
+
+       //
+       // Wait some time before really do route updates
+       //
+       if waitRouteClean > 0 {
+               subs.mutex.Unlock()
+               time.Sleep(waitRouteClean)
+               subs.mutex.Lock()
+       }
+
+       xapp.Logger.Info("Registry: Cleaning %s", subs.String())
+
+       //
+       // Subscription route updates
+       //
+       if delStatus {
+               if epamount == 0 {
+                       tmpList := RmrEndpointList{}
+                       tmpList.AddEndpoint(trans.GetEndpoint())
+                       subRouteAction := SubRouteInfo{DELETE, tmpList, subs.Seq}
+                       r.rtmgrClient.SubscriptionRequestUpdate(subRouteAction)
+               } else {
+                       subRouteAction := SubRouteInfo{UPDATE, subs.EpList, subs.Seq}
+                       r.rtmgrClient.SubscriptionRequestUpdate(subRouteAction)
                }
        }
+
+       r.mutex.Lock()
+       //
+       // If last endpoint free seq nro
+       //
+       if epamount == 0 {
+               r.subIds = append(r.subIds, subs.Seq)
+       }
+
        return nil
 }
 
 func (r *Registry) GetSubscription(sn uint16) *Subscription {
        r.mutex.Lock()
        defer r.mutex.Unlock()
-       xapp.Logger.Debug("Registry map: %v", r.register)
        if _, ok := r.register[sn]; ok {
                return r.register[sn]
        }
        return nil
 }
 
-//This function releases the given id as unused in the register
-func (r *Registry) releaseSequenceNumber(sn uint16) bool {
+func (r *Registry) GetSubscriptionFirstMatch(ids []uint16) (*Subscription, error) {
        r.mutex.Lock()
        defer r.mutex.Unlock()
-       if _, ok := r.register[sn]; ok {
-               delete(r.register, sn)
-               return true
-       } else {
-               return false
+       for _, id := range ids {
+               if _, ok := r.register[id]; ok {
+                       return r.register[id], nil
+               }
        }
+       return nil, fmt.Errorf("No valid subscription found with ids %v", ids)
 }