RICPLT-3014 Subscription multiple endpoints
[ric-plt/submgr.git] / pkg / control / registry.go
index 4f5c4fd..5edc3eb 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"
        "sync"
 )
 
+//-----------------------------------------------------------------------------
+//
+//-----------------------------------------------------------------------------
 type Registry struct {
-       register map[uint16]bool
-       counter  uint16
-       mutex    sync.Mutex
+       mutex       sync.Mutex
+       register    map[uint16]*Subscription
+       subIds      []uint16
+       rtmgrClient *RtmgrClient
 }
 
 // This method should run as a constructor
-func (r *Registry) Initialize(seedsn uint16) {
-       r.register = make(map[uint16]bool)
-       r.counter = seedsn
+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)
+       }
 }
 
 // Reserves and returns the next free sequence number
-func (r *Registry) ReserveSequenceNumber() (uint16, bool) {
-       // Check is current SequenceNumber valid
+func (r *Registry) AssignToSubscription(trans *Transaction, subReqMsg *e2ap.E2APSubscriptionRequest) (*Subscription, error) {
        r.mutex.Lock()
        defer r.mutex.Unlock()
-       sequenceNumber := r.counter
-       if _, ok := r.register[sequenceNumber]; ok {
-               xapp.Logger.Error("Invalid SeqenceNumber sequenceNumber: %v", sequenceNumber)
-               return sequenceNumber, false
-       }
-       r.register[sequenceNumber] = false
+       if len(r.subIds) > 0 {
+               sequenceNumber := r.subIds[0]
+               r.subIds = r.subIds[1:]
+               if _, ok := r.register[sequenceNumber]; ok == false {
+                       subs := &Subscription{
+                               registry: r,
+                               Seq:      sequenceNumber,
+                               Meid:     trans.Meid,
+                       }
+                       err := subs.AddEndpoint(trans.GetEndpoint())
+                       if err != nil {
+                               return nil, err
+                       }
+                       subs.SubReqMsg = subReqMsg
 
-       // Allocate next SequenceNumber value
-       if r.counter == 65535 {
-               r.counter = 0
-       } else {
-               r.counter++
+                       r.register[sequenceNumber] = subs
+                       xapp.Logger.Debug("Registry: Create %s", subs.String())
+                       xapp.Logger.Debug("Registry: substable=%v", r.register)
+                       return subs, nil
+               }
        }
-       return sequenceNumber, true
+       return nil, fmt.Errorf("Registry: Failed to reserves subscription")
 }
 
-// This function checks the validity of the given subscription id
-func (r *Registry) IsValidSequenceNumber(sn uint16) bool {
+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 true
+               return r.register[sn]
        }
-       return false
-}
-
-// This function sets the give id as confirmed in the register
-func (r *Registry) setSubscriptionToConfirmed(sn uint16) {
-       r.mutex.Lock()
-       defer r.mutex.Unlock()
-       r.register[sn] = true
+       return nil
 }
 
-//This function sets the given id as unused in the register
-func (r *Registry) deleteSubscription(sn uint16) {
+func (r *Registry) GetSubscriptionFirstMatch(ids []uint16) (*Subscription, error) {
        r.mutex.Lock()
        defer r.mutex.Unlock()
-       r.register[sn] = 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)
 }
 
-//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]
+               xapp.Logger.Debug("Registry: Delete %s", subs.String())
+               r.subIds = append(r.subIds, sn)
                delete(r.register, sn)
+               xapp.Logger.Debug("Registry: substable=%v", r.register)
                return true
-       } else {
-               return false
        }
+       return false
 }