Submgr v0.7.2

* Fix to handle E2AP PDU
* Fix to initialize transaction table
* Feature to handle RICsubscriptionDeleteRequest

Change-Id: If870ff82763e1af57204299cf2ea44fcf54e0664
Signed-off-by: Balint Uveges <balint.uveges@nokia.com>
diff --git a/RELNOTES b/RELNOTES
index 36f8eac..83b6eca 100644
--- a/RELNOTES
+++ b/RELNOTES
@@ -1,3 +1,17 @@
+### v0.7.2
+* Correction for E2AP PDU handling
+* Correction for Transaction Table handling
+
+### v0.7.1
+* Verification of RICsubscriptionDeleteRequest message handling
+
+### v0.7.0
+* Handle RICsubscriptionDeleteRequest message
+
+### v0.6.0
+* Capability to track and retrieve transactions
+* Signal subscription related route creation towards Rtmgr
+
 ### v0.5.0
 * Introduces ASN1-C generated E2AP encode/decode
 * Introduces RCO_RAWDATA and E2T_RAWDATA env variables on order to inject initial encoded messge in hex format into stub component for testing purposes
diff --git a/container-tag.yaml b/container-tag.yaml
index 87e4654..540b2fe 100644
--- a/container-tag.yaml
+++ b/container-tag.yaml
@@ -2,4 +2,4 @@
 # By default this file is in the docker build directory,
 # but the location can configured in the JJB template.
 ---
-tag: 0.7.1
+tag: 0.7.2
diff --git a/e2ap/wrapper.c b/e2ap/wrapper.c
index e0f90e4..015693d 100644
--- a/e2ap/wrapper.c
+++ b/e2ap/wrapper.c
@@ -1,6 +1,29 @@
 #include <errno.h>
 #include "wrapper.h"
 
+size_t encode_E2AP_PDU(E2AP_PDU_t* pdu, void* buffer, size_t buf_size)
+{
+    asn_enc_rval_t encode_result;
+    encode_result = aper_encode_to_buffer(&asn_DEF_E2AP_PDU, NULL, pdu, buffer, buf_size);
+    if(encode_result.encoded == -1) {
+        return -1;
+    }
+    return encode_result.encoded;
+}
+
+E2AP_PDU_t* decode_E2AP_PDU(const void* buffer, size_t buf_size)
+{
+    asn_dec_rval_t decode_result;
+    E2AP_PDU_t *pdu = 0;
+    decode_result = aper_decode_complete(NULL, &asn_DEF_E2AP_PDU, (void **)&pdu, buffer, buf_size);
+    if(decode_result.code == RC_OK) {
+        return pdu;
+    } else {
+        ASN_STRUCT_FREE(asn_DEF_E2AP_PDU, pdu);
+        return 0;
+    }
+}
+
 ssize_t encode_RIC_subscription_request(RICsubscriptionRequest_t* pdu, void* buffer, size_t buf_size)
 {
     asn_enc_rval_t encode_result;
@@ -26,14 +49,16 @@
 
 long e2ap_get_ric_subscription_request_sequence_number(void *buffer, size_t buf_size)
 {
-    RICsubscriptionRequest_t *pdu = decode_RIC_subscription_request(buffer, buf_size);
-    if ( pdu != NULL )
+    E2AP_PDU_t *pdu = decode_E2AP_PDU(buffer, buf_size);
+    if ( pdu != NULL && pdu->present == E2AP_PDU_PR_initiatingMessage)
     {
-        for (int i = 0; i < pdu->protocolIEs.list.count; ++i )
+        InitiatingMessageE2_t* initiatingMessage = pdu->choice.initiatingMessage;
+        RICsubscriptionRequest_t *ric_subscription_request = &(initiatingMessage->value.choice.RICsubscriptionRequest);
+        for (int i = 0; i < ric_subscription_request->protocolIEs.list.count; ++i )
         {
-            if ( pdu->protocolIEs.list.array[i]->id == ProtocolIE_ID_id_RICrequestID )
+            if ( ric_subscription_request->protocolIEs.list.array[i]->id == ProtocolIE_ID_id_RICrequestID )
             {
-                return pdu->protocolIEs.list.array[i]->value.choice.RICrequestID.ricRequestSequenceNumber;
+                return ric_subscription_request->protocolIEs.list.array[i]->value.choice.RICrequestID.ricRequestSequenceNumber;
             }
         }
     }
@@ -42,15 +67,17 @@
 
 ssize_t  e2ap_set_ric_subscription_request_sequence_number(void *buffer, size_t buf_size, long sequence_number)
 {
-    RICsubscriptionRequest_t *pdu = decode_RIC_subscription_request(buffer, buf_size);
-    if ( pdu != NULL )
+    E2AP_PDU_t *pdu = decode_E2AP_PDU(buffer, buf_size);
+    if ( pdu != NULL && pdu->present == E2AP_PDU_PR_initiatingMessage)
     {
-        for (int i = 0; i < pdu->protocolIEs.list.count; ++i )
+        InitiatingMessageE2_t* initiatingMessage = pdu->choice.initiatingMessage;
+        RICsubscriptionRequest_t *ric_subscription_request = &(initiatingMessage->value.choice.RICsubscriptionRequest);
+        for (int i = 0; i < ric_subscription_request->protocolIEs.list.count; ++i )
         {
-            if ( pdu->protocolIEs.list.array[i]->id == ProtocolIE_ID_id_RICrequestID )
+            if ( ric_subscription_request->protocolIEs.list.array[i]->id == ProtocolIE_ID_id_RICrequestID )
             {
-                pdu->protocolIEs.list.array[i]->value.choice.RICrequestID.ricRequestSequenceNumber = sequence_number;
-                return encode_RIC_subscription_request(pdu, buffer, buf_size);
+                ric_subscription_request->protocolIEs.list.array[i]->value.choice.RICrequestID.ricRequestSequenceNumber = sequence_number;
+                return encode_E2AP_PDU(pdu, buffer, buf_size);
             }
         }
     }
@@ -132,6 +159,7 @@
     RICsubscriptionDeleteRequest_t *pdu = 0;
     decode_result = aper_decode_complete(NULL, &asn_DEF_RICsubscriptionDeleteRequest, (void **)&pdu, buffer, buf_size);
     if(decode_result.code == RC_OK) {
+        fprintf(stdout, "decoded bytes: %ld\n", decode_result.consumed);
         return pdu;
     } else {
         ASN_STRUCT_FREE(asn_DEF_RICsubscriptionDeleteRequest, pdu);
diff --git a/e2ap/wrapper.h b/e2ap/wrapper.h
index 6aa2bb5..026e676 100644
--- a/e2ap/wrapper.h
+++ b/e2ap/wrapper.h
@@ -5,9 +5,13 @@
 #include "RICsubscriptionResponse.h"
 #include "RICsubscriptionDeleteRequest.h"
 #include "RICsubscriptionDeleteResponse.h"
+#include "E2AP-PDU.h"
+#include "InitiatingMessageE2.h"
 #include "ProtocolIE-Container.h"
 #include "ProtocolIE-Field.h"
 
+size_t encode_E2AP_PDU(E2AP_PDU_t* pdu, void* buffer, size_t buf_size);
+E2AP_PDU_t* decode_E2AP_PDU(const void* buffer, size_t buf_size);
 
 /* RICsubscriptionRequest */
 ssize_t encode_RIC_subscription_request(RICsubscriptionRequest_t* pdu, void* buffer, size_t buf_size);
@@ -40,4 +44,4 @@
 
 
 
-#endif /* _WRAPPER_H_ */
\ No newline at end of file
+#endif /* _WRAPPER_H_ */
diff --git a/pkg/control/control.go b/pkg/control/control.go
index 27e3fbe..f6cd771 100644
--- a/pkg/control/control.go
+++ b/pkg/control/control.go
@@ -92,12 +92,15 @@
 	registry := new(Registry)
 	registry.Initialize(SEEDSN)
 
+	tracker := new(Tracker)
+	tracker.Init()
+
 	transport := httptransport.New(viper.GetString("rtmgr.HostAddr") + ":" + viper.GetString("rtmgr.port"), viper.GetString("rtmgr.baseUrl"), []string{"http"})
 	client := rtmgrclient.New(transport, strfmt.Default)
 	handle := rtmgrhandle.NewProvideXappSubscriptionHandleParamsWithTimeout(10 * time.Second)
 	rtmgrClient := RtmgrClient{client, handle}
 
-	return Control{new(E2ap), registry, &rtmgrClient, new(Tracker)}
+	return Control{new(E2ap), registry, &rtmgrClient, tracker}
 }
 
 func (c *Control) Run() {
@@ -151,7 +154,7 @@
 	/* Create transatcion records for every subscription request */
 	xact_key := Transaction_key{new_sub_id, CREATE}
 	xact_value := Transaction{*src_addr, *src_port, params.Payload}
-	err = c.tracker.Track_transaction(xact_key, &xact_value)
+	err = c.tracker.Track_transaction(xact_key, xact_value)
 	if err != nil {
 		xapp.Logger.Error("Failed to create a transaction record due to %v", err)
 		return
diff --git a/pkg/control/tracker.go b/pkg/control/tracker.go
index bd062da..fdbbeaf 100644
--- a/pkg/control/tracker.go
+++ b/pkg/control/tracker.go
@@ -28,14 +28,18 @@
 Implements a record of ongoing transactions and helper functions to CRUD the records.
 */
 type Tracker struct {
-	transaction_table map[Transaction_key]*Transaction
+	transaction_table map[Transaction_key]Transaction
+}
+
+func (t *Tracker) Init() {
+	t.transaction_table = make(map[Transaction_key]Transaction)
 }
 
 /*
 Checks if a tranascation with similar type has been ongoing. If not then creates one.
 Returns error if there is similar transatcion ongoing.
 */
-func (t *Tracker) Track_transaction(key Transaction_key, xact *Transaction) error{
+func (t *Tracker) Track_transaction(key Transaction_key, xact Transaction) error{
 	if _, ok := t.transaction_table[key]; ok {
 		// TODO: Implement merge related check here. If the key is same but the value is different.
 		err := fmt.Errorf("Transaction tracker: Similar transaction with sub id %d and type %s is ongoing", key.SubID, key.trans_type )
@@ -49,25 +53,28 @@
 Retreives the transaction table entry for the given request.
 Returns error in case the transaction cannot be found.
 */
-func (t *Tracker) Retrive_transaction(subID uint16, act Action) (*Transaction, error){
+func (t *Tracker) Retrive_transaction(subID uint16, act Action) (Transaction, error){
 	key := Transaction_key{subID, act}
+	var xact Transaction
 	if xact, ok := t.transaction_table[key]; ok {
 		return xact, nil
 	}
 	err := fmt.Errorf("Tranaction record for Subscription ID %d and action %s does not exist", subID, act)
-	return nil, err
+	return xact, err
 }
 
 /*
 Deletes the transaction table entry for the given request and returns the deleted xapp's address and port for reference.
 Returns error in case the transaction cannot be found.
 */
-func (t *Tracker) complete_transaction(subID uint16, act Action) (*string, *uint16, error){
+func (t *Tracker) complete_transaction(subID uint16, act Action) (string, uint16, error){
 	key := Transaction_key{subID, act}
+	var empty_address string
+	var empty_port uint16
 	if xact, ok := t.transaction_table[key]; ok {
 		delete(t.transaction_table, key)
-		return &(xact.Xapp_instance_address), &(xact.Xapp_port), nil
+		return xact.Xapp_instance_address, xact.Xapp_port, nil
 	}
 	err := fmt.Errorf("Tranaction record for Subscription ID %d and action %s does not exist", subID, act)
-	return nil, nil, err
+	return empty_address, empty_port, err
 }