RICPLT-3007 Replace msg Mtype numbers with variables defined in xapp frame

Change-Id: Ibc35c4d82ce7fb126c1949b1a304fdd04be2e923
Signed-off-by: Juha Hyttinen <juha.hyttinen@nokia.com>
diff --git a/pkg/control/control.go b/pkg/control/control.go
index b189071..52571aa 100755
--- a/pkg/control/control.go
+++ b/pkg/control/control.go
@@ -22,7 +22,6 @@
 import (
 	"fmt"
 	"gerrit.o-ran-sc.org/r/ric-plt/e2ap/pkg/e2ap"
-	"gerrit.o-ran-sc.org/r/ric-plt/e2ap/pkg/packer"
 	rtmgrclient "gerrit.o-ran-sc.org/r/ric-plt/submgr/pkg/rtmgr_client"
 	rtmgrhandle "gerrit.o-ran-sc.org/r/ric-plt/submgr/pkg/rtmgr_client/handle"
 	"gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp"
@@ -135,29 +134,29 @@
 	return
 }
 
-func (c *Control) rmrSend(desc string, subs *Subscription, trans *Transaction, payload *packer.PackedData) (err error) {
+func (c *Control) rmrSend(desc string, subs *Subscription, trans *Transaction) (err error) {
 	params := &RMRParams{&xapp.RMRParams{}}
 	params.Mtype = trans.GetMtype()
 	params.SubId = int(subs.GetSubId())
 	params.Xid = ""
 	params.Meid = subs.GetMeid()
 	params.Src = ""
-	params.PayloadLen = len(payload.Buf)
-	params.Payload = payload.Buf
+	params.PayloadLen = len(trans.Payload.Buf)
+	params.Payload = trans.Payload.Buf
 	params.Mbuf = nil
 
 	return c.rmrSendRaw(desc, params)
 }
 
-func (c *Control) rmrReplyToSender(desc string, subs *Subscription, trans *Transaction, mType int, payload *packer.PackedData) (err error) {
+func (c *Control) rmrReplyToSender(desc string, subs *Subscription, trans *Transaction) (err error) {
 	params := &RMRParams{&xapp.RMRParams{}}
-	params.Mtype = mType
+	params.Mtype = trans.GetMtype()
 	params.SubId = int(subs.GetSubId())
 	params.Xid = trans.GetXid()
 	params.Meid = trans.GetMeid()
 	params.Src = ""
-	params.PayloadLen = len(payload.Buf)
-	params.Payload = payload.Buf
+	params.PayloadLen = len(trans.Payload.Buf)
+	params.Payload = trans.Payload.Buf
 	params.Mbuf = nil
 
 	return c.rmrSendRaw(desc, params)
@@ -195,7 +194,6 @@
 	//
 	//
 	trans, err := c.tracker.TrackTransaction(NewRmrEndpoint(params.Src),
-		params.Mtype,
 		params.Xid,
 		params.Meid,
 		false,
@@ -244,7 +242,7 @@
 	//
 	//       This is intermediate solution while improving message handling
 	//
-	trans.Payload, err = c.e2ap.PackSubscriptionRequest(trans.SubReqMsg)
+	trans.Mtype, trans.Payload, err = c.e2ap.PackSubscriptionRequest(trans.SubReqMsg)
 	if err != nil {
 		xapp.Logger.Error("SubReq: %s for trans %s", err.Error(), trans)
 		subs.Release()
@@ -252,7 +250,7 @@
 		return
 	}
 
-	c.rmrSend("SubReq: SubReq to E2T", subs, trans, trans.Payload)
+	c.rmrSend("SubReq: SubReq to E2T", subs, trans)
 
 	c.timerMap.StartTimer("RIC_SUB_REQ", int(subs.GetSubId()), subReqTime, FirstTry, c.handleSubscriptionRequestTimer)
 	xapp.Logger.Debug("SubReq: Debugging trans table = %v", c.tracker.transactionXappTable)
@@ -307,7 +305,7 @@
 		return
 	}
 
-	trans.Payload, err = c.e2ap.PackSubscriptionResponse(trans.SubRespMsg)
+	trans.Mtype, trans.Payload, err = c.e2ap.PackSubscriptionResponse(trans.SubRespMsg)
 	if err != nil {
 		xapp.Logger.Error("SubResp: %s for trans %s", err.Error(), trans)
 		trans.Release()
@@ -316,7 +314,7 @@
 
 	subs.Confirmed()
 	trans.Release()
-	c.rmrReplyToSender("SubResp: SubResp to xapp", subs, trans, 12011, trans.Payload)
+	c.rmrReplyToSender("SubResp: SubResp to xapp", subs, trans)
 	return
 }
 
@@ -371,9 +369,9 @@
 		return
 	}
 
-	trans.Payload, err = c.e2ap.PackSubscriptionFailure(trans.SubFailMsg)
+	trans.Mtype, trans.Payload, err = c.e2ap.PackSubscriptionFailure(trans.SubFailMsg)
 	if err == nil {
-		c.rmrReplyToSender("SubFail: SubFail to xapp", subs, trans, 12012, trans.Payload)
+		c.rmrReplyToSender("SubFail: SubFail to xapp", subs, trans)
 		time.Sleep(3 * time.Second)
 	} else {
 		//TODO error handling improvement
@@ -408,11 +406,11 @@
 	}
 
 	if tryCount < maxSubReqTryCount {
-		xapp.Logger.Info("SubReq timeout: Resending SubReq to E2T: Mtype: %v, SubId: %v, Xid %s, Meid %v", trans.GetMtype(), subs.GetSubId(), trans.GetXid(), trans.GetMeid())
+		xapp.Logger.Info("SubReq timeout: subs: %s trans: %s", subs, trans)
 
 		trans.RetryTransaction()
 
-		c.rmrSend("SubReq timeout: SubReq to E2T", subs, trans, trans.Payload)
+		c.rmrSend("SubReq timeout: SubReq to E2T", subs, trans)
 
 		tryCount++
 		c.timerMap.StartTimer("RIC_SUB_REQ", int(subs.GetSubId()), subReqTime, tryCount, c.handleSubscriptionRequestTimer)
@@ -424,7 +422,6 @@
 
 	// Create DELETE transaction (internal and no messages toward xapp)
 	deltrans, err := c.tracker.TrackTransaction(&trans.RmrEndpoint,
-		12020, // RIC SUBSCRIPTION DELETE
 		trans.GetXid(),
 		trans.GetMeid(),
 		false,
@@ -441,7 +438,7 @@
 	deltrans.SubDelReqMsg.RequestId.Id = trans.SubReqMsg.RequestId.Id
 	deltrans.SubDelReqMsg.RequestId.Seq = uint32(subs.GetSubId())
 	deltrans.SubDelReqMsg.FunctionId = trans.SubReqMsg.FunctionId
-	deltrans.Payload, err = c.e2ap.PackSubscriptionDeleteRequest(deltrans.SubDelReqMsg)
+	deltrans.Mtype, deltrans.Payload, err = c.e2ap.PackSubscriptionDeleteRequest(deltrans.SubDelReqMsg)
 	if err != nil {
 		xapp.Logger.Error("SubReq timeout: Packing SubDelReq failed. Err: %v", err)
 		//TODO improve error handling. Important at least in merge
@@ -458,7 +455,7 @@
 		return
 	}
 
-	c.rmrSend("SubReq timer: SubDelReq to E2T", subs, deltrans, deltrans.Payload)
+	c.rmrSend("SubReq timer: SubDelReq to E2T", subs, deltrans)
 	c.timerMap.StartTimer("RIC_SUB_DEL_REQ", int(subs.GetSubId()), subDelReqTime, FirstTry, c.handleSubscriptionDeleteRequestTimer)
 	return
 }
@@ -470,7 +467,6 @@
 	//
 	//
 	trans, err := c.tracker.TrackTransaction(NewRmrEndpoint(params.Src),
-		params.Mtype,
 		params.Xid,
 		params.Meid,
 		false,
@@ -521,7 +517,7 @@
 	//
 	//       This is intermediate solution while improving message handling
 	//
-	trans.Payload, err = c.e2ap.PackSubscriptionDeleteRequest(trans.SubDelReqMsg)
+	trans.Mtype, trans.Payload, err = c.e2ap.PackSubscriptionDeleteRequest(trans.SubDelReqMsg)
 	if err != nil {
 		xapp.Logger.Error("SubDelReq: %s for trans %s", err.Error(), trans)
 		trans.Release()
@@ -530,7 +526,7 @@
 
 	subs.UnConfirmed()
 
-	c.rmrSend("SubDelReq: SubDelReq to E2T", subs, trans, trans.Payload)
+	c.rmrSend("SubDelReq: SubDelReq to E2T", subs, trans)
 
 	c.timerMap.StartTimer("RIC_SUB_DEL_REQ", int(subs.GetSubId()), subDelReqTime, FirstTry, c.handleSubscriptionDeleteRequestTimer)
 	return
@@ -663,7 +659,7 @@
 	if tryCount < maxSubDelReqTryCount {
 		// Set possible to handle new response for the subId
 		trans.RetryTransaction()
-		c.rmrSend("SubDelReq timeout: SubDelReq to E2T", subs, trans, trans.Payload)
+		c.rmrSend("SubDelReq timeout: SubDelReq to E2T", subs, trans)
 		tryCount++
 		c.timerMap.StartTimer("RIC_SUB_DEL_REQ", int(subs.GetSubId()), subReqTime, tryCount, c.handleSubscriptionDeleteRequestTimer)
 		return
@@ -683,9 +679,9 @@
 		trans.SubDelRespMsg.FunctionId = trans.SubDelReqMsg.FunctionId
 
 		var err error
-		trans.Payload, err = c.e2ap.PackSubscriptionDeleteResponse(trans.SubDelRespMsg)
+		trans.Mtype, trans.Payload, err = c.e2ap.PackSubscriptionDeleteResponse(trans.SubDelRespMsg)
 		if err == nil {
-			c.rmrReplyToSender(desc+": SubDelResp to xapp", subs, trans, 12021, trans.Payload)
+			c.rmrReplyToSender(desc+": SubDelResp to xapp", subs, trans)
 			time.Sleep(3 * time.Second)
 		} else {
 			//TODO error handling improvement
diff --git a/pkg/control/e2ap.go b/pkg/control/e2ap.go
index 8f7fb63..30f1ef2 100644
--- a/pkg/control/e2ap.go
+++ b/pkg/control/e2ap.go
@@ -30,6 +30,7 @@
 	"gerrit.o-ran-sc.org/r/ric-plt/e2ap/pkg/e2ap"
 	"gerrit.o-ran-sc.org/r/ric-plt/e2ap/pkg/e2ap_wrapper"
 	"gerrit.o-ran-sc.org/r/ric-plt/e2ap/pkg/packer"
+	"gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp"
 )
 
 var packerif e2ap.E2APPackerIf = e2ap_wrapper.NewAsn1E2Packer()
@@ -55,17 +56,17 @@
 	return subReq, nil
 }
 
-func (c *E2ap) PackSubscriptionRequest(req *e2ap.E2APSubscriptionRequest) (*packer.PackedData, error) {
+func (c *E2ap) PackSubscriptionRequest(req *e2ap.E2APSubscriptionRequest) (int, *packer.PackedData, error) {
 	e2SubReq := packerif.NewPackerSubscriptionRequest()
 	err := e2SubReq.Set(req)
 	if err != nil {
-		return nil, err
+		return 0, nil, err
 	}
 	err, packedData := e2SubReq.Pack(nil)
 	if err != nil {
-		return nil, err
+		return 0, nil, err
 	}
-	return packedData, nil
+	return xapp.RIC_SUB_REQ, packedData, nil
 }
 
 //-----------------------------------------------------------------------------
@@ -86,17 +87,17 @@
 	return subResp, nil
 }
 
-func (c *E2ap) PackSubscriptionResponse(req *e2ap.E2APSubscriptionResponse) (*packer.PackedData, error) {
+func (c *E2ap) PackSubscriptionResponse(req *e2ap.E2APSubscriptionResponse) (int, *packer.PackedData, error) {
 	e2SubResp := packerif.NewPackerSubscriptionResponse()
 	err := e2SubResp.Set(req)
 	if err != nil {
-		return nil, err
+		return 0, nil, err
 	}
 	err, packedData := e2SubResp.Pack(nil)
 	if err != nil {
-		return nil, err
+		return 0, nil, err
 	}
-	return packedData, nil
+	return xapp.RIC_SUB_RESP, packedData, nil
 }
 
 //-----------------------------------------------------------------------------
@@ -117,17 +118,17 @@
 	return subFail, nil
 }
 
-func (c *E2ap) PackSubscriptionFailure(req *e2ap.E2APSubscriptionFailure) (*packer.PackedData, error) {
+func (c *E2ap) PackSubscriptionFailure(req *e2ap.E2APSubscriptionFailure) (int, *packer.PackedData, error) {
 	e2SubFail := packerif.NewPackerSubscriptionFailure()
 	err := e2SubFail.Set(req)
 	if err != nil {
-		return nil, err
+		return 0, nil, err
 	}
 	err, packedData := e2SubFail.Pack(nil)
 	if err != nil {
-		return nil, err
+		return 0, nil, err
 	}
-	return packedData, nil
+	return xapp.RIC_SUB_FAILURE, packedData, nil
 }
 
 //-----------------------------------------------------------------------------
@@ -148,17 +149,17 @@
 	return subDelReq, nil
 }
 
-func (c *E2ap) PackSubscriptionDeleteRequest(req *e2ap.E2APSubscriptionDeleteRequest) (*packer.PackedData, error) {
+func (c *E2ap) PackSubscriptionDeleteRequest(req *e2ap.E2APSubscriptionDeleteRequest) (int, *packer.PackedData, error) {
 	e2SubDelReq := packerif.NewPackerSubscriptionDeleteRequest()
 	err := e2SubDelReq.Set(req)
 	if err != nil {
-		return nil, err
+		return 0, nil, err
 	}
 	err, packedData := e2SubDelReq.Pack(nil)
 	if err != nil {
-		return nil, err
+		return 0, nil, err
 	}
-	return packedData, nil
+	return xapp.RIC_SUB_DEL_REQ, packedData, nil
 }
 
 //-----------------------------------------------------------------------------
@@ -179,17 +180,17 @@
 	return subDelResp, nil
 }
 
-func (c *E2ap) PackSubscriptionDeleteResponse(req *e2ap.E2APSubscriptionDeleteResponse) (*packer.PackedData, error) {
+func (c *E2ap) PackSubscriptionDeleteResponse(req *e2ap.E2APSubscriptionDeleteResponse) (int, *packer.PackedData, error) {
 	e2SubDelResp := packerif.NewPackerSubscriptionDeleteResponse()
 	err := e2SubDelResp.Set(req)
 	if err != nil {
-		return nil, err
+		return 0, nil, err
 	}
 	err, packedData := e2SubDelResp.Pack(nil)
 	if err != nil {
-		return nil, err
+		return 0, nil, err
 	}
-	return packedData, nil
+	return xapp.RIC_SUB_DEL_RESP, packedData, nil
 }
 
 //-----------------------------------------------------------------------------
@@ -210,15 +211,15 @@
 	return subDelFail, nil
 }
 
-func (c *E2ap) PackSubscriptionDeleteFailure(req *e2ap.E2APSubscriptionDeleteFailure) (*packer.PackedData, error) {
+func (c *E2ap) PackSubscriptionDeleteFailure(req *e2ap.E2APSubscriptionDeleteFailure) (int, *packer.PackedData, error) {
 	e2SubDelFail := packerif.NewPackerSubscriptionDeleteFailure()
 	err := e2SubDelFail.Set(req)
 	if err != nil {
-		return nil, err
+		return 0, nil, err
 	}
 	err, packedData := e2SubDelFail.Pack(nil)
 	if err != nil {
-		return nil, err
+		return 0, nil, err
 	}
-	return packedData, nil
+	return xapp.RIC_SUB_DEL_FAILURE, packedData, nil
 }
diff --git a/pkg/control/tracker.go b/pkg/control/tracker.go
index 75127a7..11d2cf7 100644
--- a/pkg/control/tracker.go
+++ b/pkg/control/tracker.go
@@ -39,7 +39,6 @@
 
 func (t *Tracker) TrackTransaction(
 	endpoint *RmrEndpoint,
-	mtype int,
 	xid string,
 	meid *xapp.RMRMeid,
 	respReceived bool,
@@ -54,7 +53,6 @@
 		tracker:           nil,
 		Subs:              nil,
 		RmrEndpoint:       *endpoint,
-		Mtype:             mtype,
 		Xid:               xid,
 		Meid:              meid,
 		RespReceived:      respReceived,
diff --git a/pkg/control/transaction.go b/pkg/control/transaction.go
index b0da077..867b854 100644
--- a/pkg/control/transaction.go
+++ b/pkg/control/transaction.go
@@ -47,7 +47,6 @@
 	tracker           *Tracker                             //tracker instance
 	Subs              *Subscription                        //related subscription
 	RmrEndpoint       RmrEndpoint                          //xapp endpoint
-	Mtype             int                                  //type of initiating message
 	Xid               string                               //xapp xid in req
 	Meid              *xapp.RMRMeid                        //meid transaction related
 	SubReqMsg         *e2ap.E2APSubscriptionRequest        //SubReq TODO: maybe own transactions per type
@@ -56,7 +55,8 @@
 	SubDelReqMsg      *e2ap.E2APSubscriptionDeleteRequest  //SubDelReq TODO: maybe own transactions per type
 	SubDelRespMsg     *e2ap.E2APSubscriptionDeleteResponse //SubDelResp TODO: maybe own transactions per type
 	SubDelFailMsg     *e2ap.E2APSubscriptionDeleteFailure  //SubDelFail TODO: maybe own transactions per type
-	Payload           *packer.PackedData                   //Encoded message to be send. Optimized
+	Mtype             int                                  //Encoded message type to be send
+	Payload           *packer.PackedData                   //Encoded message to be send
 	RespReceived      bool
 	ForwardRespToXapp bool
 }