Non-blocking RMR init ...

Change-Id: Ibe051b2601e0947e93742b20aaa4ebeffc7aaadb
Signed-off-by: Mohamed Abukar <abukar.mohamed@nokia.com>
diff --git a/adapter/build_adapter_ubuntu.sh b/adapter/build_adapter_ubuntu.sh
index d1a8a10..b6e5dcc 100755
--- a/adapter/build_adapter_ubuntu.sh
+++ b/adapter/build_adapter_ubuntu.sh
@@ -22,11 +22,11 @@
 echo "--> build_adapter_ubuntu.sh starts"
 
 # Install RMR from deb packages at packagecloud.io
-rmr=rmr_3.2.4_amd64.deb
+rmr=rmr_3.6.3_amd64.deb
 wget --content-disposition  https://packagecloud.io/o-ran-sc/staging/packages/debian/stretch/$rmr/download.deb
 sudo dpkg -i $rmr
 rm $rmr
-rmrdev=rmr-dev_3.2.4_amd64.deb
+rmrdev=rmr-dev_3.6.3_amd64.deb
 wget --content-disposition https://packagecloud.io/o-ran-sc/staging/packages/debian/stretch/$rmrdev/download.deb
 sudo dpkg -i $rmrdev
 rm $rmrdev
@@ -63,4 +63,4 @@
 # And for the Alarm Adapter
 cd ../adapter && go test -v -p 1 -coverprofile cover.out ./cmd/ -c -o ./adapter_test && ./adapter_test
 
-echo "--> build_adapter_ubuntu.sh ends"
\ No newline at end of file
+echo "--> build_adapter_ubuntu.sh ends"
diff --git a/adapter/cmd/adapter_test.go b/adapter/cmd/adapter_test.go
index dbb53e7..0dbc387 100755
--- a/adapter/cmd/adapter_test.go
+++ b/adapter/cmd/adapter_test.go
@@ -55,6 +55,7 @@
 	}
 
 	alarmer, _ = alarm.InitAlarm("my-pod", "my-app")
+	time.Sleep(time.Duration(5) * time.Second)
 	eventChan = make(chan string)
 
 	os.Exit(M.Run())
diff --git a/adapter/container-tag.yaml b/adapter/container-tag.yaml
index 2af355a..cfdbfb8 100644
--- a/adapter/container-tag.yaml
+++ b/adapter/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.4.3
+tag: 0.4.4
diff --git a/alarm/alarm.go b/alarm/alarm.go
index 3291621..cb18dd7 100755
--- a/alarm/alarm.go
+++ b/alarm/alarm.go
@@ -31,7 +31,7 @@
 
 /*
 #cgo CFLAGS: -I../
-#cgo LDFLAGS: -lrmr_nng -lnng
+#cgo LDFLAGS: -lrmr_si
 
 #include "utils.h"
 */
@@ -41,17 +41,13 @@
 // The MO and APP identities are given as a parameters.
 // The identities are used when raising/clearing alarms, unless provided by the applications.
 func InitAlarm(mo, id string) (*RICAlarm, error) {
-	if ctx := C.rmrInit(); ctx != nil {
-		r := &RICAlarm{
-			moId:   mo,
-			appId:  id,
-			rmrCtx: ctx,
-		}
-
-		return r, nil
+	r := &RICAlarm{
+		moId:  mo,
+		appId: id,
 	}
+	go InitRMR(r)
 
-	return nil, errors.New("rmrInit failed!")
+	return r, nil
 }
 
 // Create a new Alarm instance
@@ -128,8 +124,11 @@
 }
 
 func (r *RICAlarm) sendAlarmUpdateReq(a AlarmMessage) error {
-	log.Println("Sending alarm: ", r.AlarmString(a))
+	if r.rmrCtx == nil || !r.rmrReady {
+		return errors.New("RMR no ready yet!")
+	}
 
+	log.Println("Sending alarm: ", r.AlarmString(a))
 	payload, err := json.Marshal(a)
 	if err != nil {
 		return err
@@ -157,3 +156,13 @@
 	}
 	return errors.New("rmrRcv failed!")
 }
+
+func InitRMR(r *RICAlarm) error {
+	if ctx := C.rmrInit(); ctx != nil {
+		r.rmrCtx = ctx
+		r.rmrReady = true
+		return nil
+	}
+
+	return errors.New("rmrInit failed!")
+}
diff --git a/alarm/alarm_test.go b/alarm/alarm_test.go
index ab5953c..1f7a071 100755
--- a/alarm/alarm_test.go
+++ b/alarm/alarm_test.go
@@ -37,6 +37,7 @@
 	assert.Equal(t, false, a == nil)
 
 	alarmer = a
+	time.Sleep(time.Duration(5 * time.Second))
 }
 
 func TestAlarmRaiseSuccess(t *testing.T) {
diff --git a/alarm/types.go b/alarm/types.go
index 54a681b..ffe5d39 100755
--- a/alarm/types.go
+++ b/alarm/types.go
@@ -69,10 +69,11 @@
 
 // RICAlarm is an alarm instance
 type RICAlarm struct {
-	moId   string
-	appId  string
-	rmrCtx unsafe.Pointer
-	mutex  sync.Mutex
+	moId     string
+	appId    string
+	rmrCtx   unsafe.Pointer
+	rmrReady bool
+	mutex    sync.Mutex
 }
 
 const (
diff --git a/alarm/utils.c b/alarm/utils.c
index 7f29260..0869282 100755
--- a/alarm/utils.c
+++ b/alarm/utils.c
@@ -30,8 +30,8 @@
 
     // Must have a route table before we can send, so wait til RMR is ready
     while(!rmr_ready(mrc)) {
-        fprintf(stderr, "Waiting for RMR to be ready ...\n");
-        sleep(1);
+        //fprintf(stderr, "Waiting for RMR to be ready ...\n");
+        sleep(2);
     }
     fprintf(stderr, "RMR is ready now ...\n");
 
diff --git a/go.mod b/go.mod
index 811a353..cc1fa1d 100644
--- a/go.mod
+++ b/go.mod
@@ -4,7 +4,7 @@
 
 replace gerrit.o-ran-sc.org/r/ric-plt/alarm-go/alarm => ./alarm/
 
-replace gerrit.o-ran-sc.org/r/ric-plt/xapp-frame => gerrit.o-ran-sc.org/r/ric-plt/xapp-frame.git v0.0.30
+replace gerrit.o-ran-sc.org/r/ric-plt/xapp-frame => gerrit.o-ran-sc.org/r/ric-plt/xapp-frame.git v0.4.0
 
 replace gerrit.o-ran-sc.org/r/ric-plt/sdlgo => gerrit.o-ran-sc.org/r/ric-plt/sdlgo.git v0.5.2