Submgr restart improvement

Change-Id: I02388dea48b826eac09aec0308d85bcdcf812a6f
Signed-off-by: Anssi Mannila <anssi.mannila@nokia.com>
diff --git a/pkg/control/registry.go b/pkg/control/registry.go
index 7ad54c1..e07116c 100644
--- a/pkg/control/registry.go
+++ b/pkg/control/registry.go
@@ -42,8 +42,8 @@
 func (r *Registry) Initialize() {
 	r.register = make(map[uint32]*Subscription)
 	var i uint32
-	for i = 0; i < 65535; i++ {
-		r.subIds = append(r.subIds, i+1)
+	for i = 1; i < 65535; i++ {
+		r.subIds = append(r.subIds, i)
 	}
 }
 
@@ -60,7 +60,7 @@
 	return resp, nil
 }
 
-func (r *Registry) allocateSubs(trans *TransactionXapp, subReqMsg *e2ap.E2APSubscriptionRequest) (*Subscription, error) {
+func (r *Registry) allocateSubs(trans *TransactionXapp, subReqMsg *e2ap.E2APSubscriptionRequest, resetTestFlag bool) (*Subscription, error) {
 	if len(r.subIds) > 0 {
 		subId := r.subIds[0]
 		r.subIds = r.subIds[1:]
@@ -69,19 +69,26 @@
 			return nil, fmt.Errorf("Registry: Failed to reserve subscription exists")
 		}
 		subs := &Subscription{
-			registry:  r,
-			Meid:      trans.Meid,
-			SubReqMsg: subReqMsg,
-			valid:     true,
+			registry:         r,
+			Meid:             trans.Meid,
+			SubReqMsg:        subReqMsg,
+			valid:            true,
+			RetryFromXapp:    false,
+			SubRespRcvd:      false,
+			DeleteFromDb:     false,
+			NoRespToXapp:     false,
+			DoNotWaitSubResp: false,
 		}
 		subs.ReqId.Id = 123
 		subs.ReqId.InstanceId = subId
+		if resetTestFlag == true {
+			subs.DoNotWaitSubResp = true
+		}
 
 		if subs.EpList.AddEndpoint(trans.GetEndpoint()) == false {
 			r.subIds = append(r.subIds, subs.ReqId.InstanceId)
 			return nil, fmt.Errorf("Registry: Endpoint existing already in subscription")
 		}
-
 		return subs, nil
 	}
 	return nil, fmt.Errorf("Registry: Failed to reserve subscription no free ids")
@@ -121,7 +128,7 @@
 	return nil, false
 }
 
-func (r *Registry) AssignToSubscription(trans *TransactionXapp, subReqMsg *e2ap.E2APSubscriptionRequest) (*Subscription, error) {
+func (r *Registry) AssignToSubscription(trans *TransactionXapp, subReqMsg *e2ap.E2APSubscriptionRequest, resetTestFlag bool) (*Subscription, error) {
 	var err error
 	var newAlloc bool
 	r.mutex.Lock()
@@ -151,15 +158,16 @@
 
 	subs, endPointFound := r.findExistingSubs(trans, subReqMsg)
 	if subs == nil {
-		subs, err = r.allocateSubs(trans, subReqMsg)
+		subs, err = r.allocateSubs(trans, subReqMsg, resetTestFlag)
 		if err != nil {
 			return nil, err
 		}
 		newAlloc = true
 	} else if endPointFound == true {
 		// Requesting endpoint is already present in existing subscription. This can happen if xApp is restarted.
+		subs.RetryFromXapp = true
 		xapp.Logger.Debug("CREATE: subscription already exists. %s", subs.String())
-		xapp.Logger.Debug("Registry: substable=%v", r.register)
+		//xapp.Logger.Debug("Registry: substable=%v", r.register)
 		return subs, nil
 	}
 
@@ -231,7 +239,7 @@
 }
 
 // TODO: Works with concurrent calls, but check if can be improved
-func (r *Registry) RemoveFromSubscription(subs *Subscription, trans *TransactionXapp, waitRouteClean time.Duration) error {
+func (r *Registry) RemoveFromSubscription(subs *Subscription, trans *TransactionXapp, waitRouteClean time.Duration, c *Control) error {
 
 	r.mutex.Lock()
 	defer r.mutex.Unlock()
@@ -276,7 +284,6 @@
 				xapp.Logger.Debug("Registry: substable=%v", r.register)
 			}
 			r.subIds = append(r.subIds, subId)
-
 		} else if subs.EpList.Size() > 0 {
 			//
 			// Subscription route updates
@@ -284,12 +291,30 @@
 			subRouteAction := SubRouteInfo{subs.EpList, uint16(subId)}
 			r.rtmgrClient.SubscriptionRequestUpdate(subRouteAction)
 		}
-
 	}()
 
 	return nil
 }
 
+func (r *Registry) UpdateSubscriptionToDb(subs *Subscription, c *Control) {
+	r.mutex.Lock()
+	defer r.mutex.Unlock()
+	subs.mutex.Lock()
+	defer subs.mutex.Unlock()
+
+	epamount := subs.EpList.Size()
+	if epamount == 0 {
+		if _, ok := r.register[subs.ReqId.InstanceId]; ok {
+			// Not merged subscription is being deleted
+			c.RemoveSubscriptionFromDb(subs)
+
+		}
+	} else if subs.EpList.Size() > 0 {
+		// Endpoint of merged subscription is being deleted
+		c.WriteSubscriptionToDb(subs)
+	}
+}
+
 func (r *Registry) GetSubscription(subId uint32) *Subscription {
 	r.mutex.Lock()
 	defer r.mutex.Unlock()