X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=pkg%2Fcontrol%2Fregistry.go;h=1adb4f7253d8dc945ec317f121f843b65256e862;hb=refs%2Fchanges%2F77%2F2177%2F2;hp=8f52f970a6f6cd0d2fa3069b49fece1fd70efa63;hpb=4511475f03107eb53c88545649898e3cfbcc2765;p=ric-plt%2Fsubmgr.git diff --git a/pkg/control/registry.go b/pkg/control/registry.go index 8f52f97..1adb4f7 100644 --- a/pkg/control/registry.go +++ b/pkg/control/registry.go @@ -19,15 +19,143 @@ package control +import ( + "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 { - counter int + register map[uint16]*Subscription + counter uint16 + mutex sync.Mutex +} + +// This method should run as a constructor +func (r *Registry) Initialize(seedsn uint16) { + r.register = make(map[uint16]*Subscription) + r.counter = seedsn +} + +// 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 + 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++ + } + 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] + } + } + return nil } -func (r *Registry) GetSubscriptionId() int { - return r.generateId() +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 } -func (r *Registry) generateId() int { - r.counter += 1 - return r.counter +//This function releases the given id as unused in the register +func (r *Registry) releaseSequenceNumber(sn uint16) bool { + r.mutex.Lock() + defer r.mutex.Unlock() + if _, ok := r.register[sn]; ok { + delete(r.register, sn) + return true + } else { + return false + } }