Support for subscription action type Policy

Change-Id: I91c0de12dc84dc51b040647c25c85d8b8d2a8130
Signed-off-by: Anssi Mannila <anssi.mannila@nokia.com>
diff --git a/pkg/control/registry.go b/pkg/control/registry.go
index e00062b..c9abdb8 100644
--- a/pkg/control/registry.go
+++ b/pkg/control/registry.go
@@ -112,8 +112,27 @@
 	r.mutex.Lock()
 	defer r.mutex.Unlock()
 
-	subs := r.findExistingSubs(trans, subReqMsg)
+	//
+	// Check validity of subscription action types
+	//
+	actionType, err := r.CheckActionTypes(subReqMsg)
+	if err != nil {
+		xapp.Logger.Debug("CREATE %s", err)
+		return nil, err
+	}
 
+	//
+	// Find possible existing Policy subscription
+	//
+	if actionType == e2ap.E2AP_ActionTypePolicy {
+		if subs, ok := r.register[subReqMsg.RequestId.Seq]; ok {
+			xapp.Logger.Debug("CREATE %s. Existing subscription for Policy found", subs.String())
+			subs.SetCachedResponse(nil, true)
+			return subs, nil
+		}
+	}
+
+	subs := r.findExistingSubs(trans, subReqMsg)
 	if subs == nil {
 		subs, err = r.allocateSubs(trans, subReqMsg)
 		if err != nil {
@@ -158,6 +177,30 @@
 	return subs, nil
 }
 
+func (r *Registry) CheckActionTypes(subReqMsg *e2ap.E2APSubscriptionRequest) (uint64, error) {
+	var reportFound bool = false
+	var policyFound bool = false
+
+	for _, acts := range subReqMsg.ActionSetups {
+		if acts.ActionType == e2ap.E2AP_ActionTypeReport {
+			reportFound = true
+		}
+		if acts.ActionType == e2ap.E2AP_ActionTypePolicy {
+			policyFound = true
+		}
+	}
+	if reportFound == true && policyFound == true {
+		return e2ap.E2AP_ActionTypeInvalid, fmt.Errorf("Report and Policy in same RICactions-ToBeSetup-List")
+	}
+	if reportFound == true {
+		return e2ap.E2AP_ActionTypeReport, nil
+	}
+	if policyFound == true {
+		return e2ap.E2AP_ActionTypePolicy, nil
+	}
+	return e2ap.E2AP_ActionTypeInvalid, fmt.Errorf("Invalid action type in RICactions-ToBeSetup-List")
+}
+
 // TODO: Works with concurrent calls, but check if can be improved
 func (r *Registry) RemoveFromSubscription(subs *Subscription, trans *TransactionXapp, waitRouteClean time.Duration) error {
 
diff --git a/pkg/control/ut_messaging_test.go b/pkg/control/ut_messaging_test.go
index 3054999..de2458a 100644
--- a/pkg/control/ut_messaging_test.go
+++ b/pkg/control/ut_messaging_test.go
@@ -20,6 +20,7 @@
 package control
 
 import (
+	"gerrit.o-ran-sc.org/r/ric-plt/e2ap/pkg/e2ap"
 	"gerrit.o-ran-sc.org/r/ric-plt/submgr/pkg/teststube2ap"
 	"testing"
 )
@@ -1163,3 +1164,151 @@
 	e2termConn.TestMsgChanEmpty(t)
 	mainCtrl.wait_registry_empty(t, 15)
 }
+
+//-----------------------------  Policy cases ---------------------------------
+//-----------------------------------------------------------------------------
+// TestSubReqPolicyAndSubDelOk
+//
+//   stub                          stub
+// +-------+     +---------+    +---------+
+// | xapp  |     | submgr  |    | e2term  |
+// +-------+     +---------+    +---------+
+//     |              |              |
+//     | SubReq       |              |
+//     |------------->|              |
+//     |              |              |
+//     |              | SubReq       |
+//     |              |------------->|
+//     |              |              |
+//     |              |      SubResp |
+//     |              |<-------------|
+//     |              |              |
+//     |      SubResp |              |
+//     |<-------------|              |
+//     |              |              |
+//     |              |              |
+//     | SubDelReq    |              |
+//     |------------->|              |
+//     |              |              |
+//     |              | SubDelReq    |
+//     |              |------------->|
+//     |              |              |
+//     |              |   SubDelResp |
+//     |              |<-------------|
+//     |              |              |
+//     |   SubDelResp |              |
+//     |<-------------|              |
+//
+//-----------------------------------------------------------------------------
+func TestSubReqPolicyAndSubDelOk(t *testing.T) {
+	CaseBegin("TestSubReqAndSubDelOk")
+
+	rparams1 := &teststube2ap.E2StubSubsReqParams{}
+	rparams1.Init()
+	rparams1.Req.ActionSetups[0].ActionType = e2ap.E2AP_ActionTypePolicy
+	cretrans := xappConn1.SendSubsReq(t, rparams1, nil)
+
+	crereq, cremsg := e2termConn.RecvSubsReq(t)
+	e2termConn.SendSubsResp(t, crereq, cremsg)
+	e2SubsId := xappConn1.RecvSubsResp(t, cretrans)
+	deltrans := xappConn1.SendSubsDelReq(t, nil, e2SubsId)
+	delreq, delmsg := e2termConn.RecvSubsDelReq(t)
+
+	waiter := rtmgrHttp.AllocNextEvent(true)
+	e2termConn.SendSubsDelResp(t, delreq, delmsg)
+	xappConn1.RecvSubsDelResp(t, deltrans)
+	waiter.WaitResult(t)
+
+	//Wait that subs is cleaned
+	mainCtrl.wait_subs_clean(t, e2SubsId, 10)
+
+	xappConn1.TestMsgChanEmpty(t)
+	xappConn2.TestMsgChanEmpty(t)
+	e2termConn.TestMsgChanEmpty(t)
+	mainCtrl.wait_registry_empty(t, 10)
+}
+
+//-----------------------------------------------------------------------------
+// TestSubReqPolicyChangeAndSubDelOk
+//
+//   stub                          stub
+// +-------+     +---------+    +---------+
+// | xapp  |     | submgr  |    | e2term  |
+// +-------+     +---------+    +---------+
+//     |              |              |
+//     | SubReq       |              |
+//     |------------->|              |
+//     |              |              |
+//     |              | SubReq       |
+//     |              |------------->|
+//     |              |              |
+//     |              |      SubResp |
+//     |              |<-------------|
+//     |              |              |
+//     |      SubResp |              |
+//     |<-------------|              |
+//     |              |              |
+//     | SubReq       |              |
+//     |------------->|              |
+//     |              |              |
+//     |              | SubReq       |
+//     |              |------------->|
+//     |              |              |
+//     |              |      SubResp |
+//     |              |<-------------|
+//     |              |              |
+//     |      SubResp |              |
+//     |<-------------|              |
+//     |              |              |
+//     | SubDelReq    |              |
+//     |------------->|              |
+//     |              |              |
+//     |              | SubDelReq    |
+//     |              |------------->|
+//     |              |              |
+//     |              |   SubDelResp |
+//     |              |<-------------|
+//     |              |              |
+//     |   SubDelResp |              |
+//     |<-------------|              |
+//
+//-----------------------------------------------------------------------------
+
+func TestSubReqPolicyChangeAndSubDelOk(t *testing.T) {
+	CaseBegin("TestSubReqAndSubDelOk")
+
+	waiter := rtmgrHttp.AllocNextEvent(true)
+	rparams1 := &teststube2ap.E2StubSubsReqParams{}
+	rparams1.Init()
+	rparams1.Req.ActionSetups[0].ActionType = e2ap.E2AP_ActionTypePolicy
+	cretrans := xappConn1.SendSubsReq(t, rparams1, nil)
+	waiter.WaitResult(t)
+
+	crereq, cremsg := e2termConn.RecvSubsReq(t)
+	e2termConn.SendSubsResp(t, crereq, cremsg)
+	e2SubsId := xappConn1.RecvSubsResp(t, cretrans)
+
+	//Policy change
+	rparams1.Req.RequestId.Seq = e2SubsId
+	rparams1.Req.ActionSetups[0].SubsequentAction.TimetoWait = e2ap.E2AP_TimeToWaitW200ms
+	xappConn1.SendSubsReq(t, rparams1, cretrans)
+
+	crereq, cremsg = e2termConn.RecvSubsReq(t)
+	e2termConn.SendSubsResp(t, crereq, cremsg)
+	e2SubsId = xappConn1.RecvSubsResp(t, cretrans)
+	deltrans := xappConn1.SendSubsDelReq(t, nil, e2SubsId)
+	delreq, delmsg := e2termConn.RecvSubsDelReq(t)
+
+	waiter = rtmgrHttp.AllocNextEvent(true)
+	e2termConn.SendSubsDelResp(t, delreq, delmsg)
+	xappConn1.RecvSubsDelResp(t, deltrans)
+	waiter.WaitResult(t)
+
+	//Wait that subs is cleaned
+	mainCtrl.wait_subs_clean(t, e2SubsId, 10)
+
+	xappConn1.TestMsgChanEmpty(t)
+	xappConn2.TestMsgChanEmpty(t)
+	e2termConn.TestMsgChanEmpty(t)
+	mainCtrl.wait_registry_empty(t, 10)
+}