X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=pkg%2Fcontrol%2Fregistry.go;h=5edc3eb7182893bd6d145445db72bf24da8bf17b;hb=422d018f94aedd9f4c001176b5ff06c786de28eb;hp=8f52f970a6f6cd0d2fa3069b49fece1fd70efa63;hpb=4511475f03107eb53c88545649898e3cfbcc2765;p=ric-plt%2Fsubmgr.git diff --git a/pkg/control/registry.go b/pkg/control/registry.go index 8f52f97..5edc3eb 100644 --- a/pkg/control/registry.go +++ b/pkg/control/registry.go @@ -19,15 +19,90 @@ 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 { - counter int + mutex sync.Mutex + register map[uint16]*Subscription + subIds []uint16 + rtmgrClient *RtmgrClient +} + +// This method should run as a constructor +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) AssignToSubscription(trans *Transaction, subReqMsg *e2ap.E2APSubscriptionRequest) (*Subscription, error) { + r.mutex.Lock() + defer r.mutex.Unlock() + 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 + + r.register[sequenceNumber] = subs + xapp.Logger.Debug("Registry: Create %s", subs.String()) + xapp.Logger.Debug("Registry: substable=%v", r.register) + return subs, nil + } + } + return nil, fmt.Errorf("Registry: Failed to reserves subscription") +} + +func (r *Registry) GetSubscription(sn uint16) *Subscription { + r.mutex.Lock() + defer r.mutex.Unlock() + if _, ok := r.register[sn]; ok { + return r.register[sn] + } + return nil } -func (r *Registry) GetSubscriptionId() int { - return r.generateId() +func (r *Registry) GetSubscriptionFirstMatch(ids []uint16) (*Subscription, error) { + r.mutex.Lock() + defer r.mutex.Unlock() + 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) } -func (r *Registry) generateId() int { - r.counter += 1 - return r.counter +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 + } + return false }