Prepare Dockerfile and script for SONAR

Change-Id: Idb3cc5da4a3388ea7484b40f63775b29eff3461c
Signed-off-by: Mohamed Abukar <abukar.mohamed@nokia.com>
diff --git a/adapter/Dockerfile b/adapter/Dockerfile
index ffb9ede..f8c275f 100755
--- a/adapter/Dockerfile
+++ b/adapter/Dockerfile
@@ -18,51 +18,25 @@
 #   platform project (RICP).
 #==================================================================================
 
-# The CI system creates and publishes the alarm-adapter Docker image
-# from the last step in this multi-stage build and applies
-# a Docker tag from the string in file container-tag.yaml
+FROM nexus3.o-ran-sc.org:10004/bldr-ubuntu18-c-go:4-u18.04-nng as ubuntu-alarmadapter
 
-#FROM golang:1.12.1 as alarmadapter
-FROM nexus3.o-ran-sc.org:10004/bldr-ubuntu18-c-go:3-u18.04-nng as alarmadapter
+# Install utilities
+RUN apt update && apt install -y iputils-ping net-tools curl sudo
 
-RUN apt update && apt install -y iputils-ping net-tools curl
-
-# Install RMr shared library & development header files
-ARG RMRVERSION=3.2.4
-RUN wget --content-disposition https://packagecloud.io/o-ran-sc/staging/packages/debian/stretch/rmr_${RMRVERSION}_amd64.deb/download.deb && dpkg -i rmr_${RMRVERSION}_amd64.deb && rm -rf rmr_${RMRVERSION}_amd64.deb
-RUN wget --content-disposition https://packagecloud.io/o-ran-sc/staging/packages/debian/stretch/rmr-dev_${RMRVERSION}_amd64.deb/download.deb && dpkg -i rmr-dev_${RMRVERSION}_amd64.deb && rm -rf rmr-dev_${RMRVERSION}_amd64.deb
-
-ENV PATH="/usr/local/go/bin:${PATH}"
-ENV GOPATH /go
-
-RUN mkdir -p /go/bin
-RUN cd /go/bin \
-    && wget --quiet https://github.com/go-swagger/go-swagger/releases/download/v0.19.0/swagger_linux_amd64 \
-    && mv swagger_linux_amd64 swagger \
-    && chmod +x swagger
-
+# Install dependencies, compile and test the module
 RUN mkdir -p /go/src/alarm-adapter
-WORKDIR "/go/src/alarm-adapter"
-COPY go.sum .
-COPY go.mod .
-
-RUN go mod download
-
 COPY . /go/src/alarm-adapter
+
 WORKDIR "/go/src/alarm-adapter/adapter"
+RUN ./build_adapter_ubuntu.sh
 
-# build and run UT
-RUN ldconfig
-RUN ./build_adapter.sh
+# Final, executable and deployable container
+FROM ubuntu:18.04
 
-# Final, executable container
-FROM ubuntu:16.04
-
-COPY --from=alarmadapter /go/src/alarm-adapter/adapter/run_adapter.sh /
-COPY --from=alarmadapter /go/src/alarm-adapter/adapter/alarm-adapter /
-COPY --from=alarmadapter /go/src/alarm-adapter/config/* /
-COPY --from=alarmadapter /usr/local/include /usr/local/include
-COPY --from=alarmadapter /usr/local/lib /usr/local/lib
+COPY --from=ubuntu-alarmadapter /go/src/alarm-adapter/adapter/run_adapter.sh /
+COPY --from=ubuntu-alarmadapter /go/src/alarm-adapter/adapter/alarm-adapter /
+COPY --from=ubuntu-alarmadapter /go/src/alarm-adapter/config/* /
+COPY --from=ubuntu-alarmadapter /usr/local/lib /usr/local/lib
 
 RUN ldconfig
 
diff --git a/adapter/build_adapter.sh b/adapter/build_adapter.sh
deleted file mode 100755
index 511fd76..0000000
--- a/adapter/build_adapter.sh
+++ /dev/null
@@ -1,44 +0,0 @@
-#==================================================================================
-#   Copyright (c) 2020 AT&T Intellectual Property.
-#   Copyright (c) 2020 Nokia
-#
-#   Licensed under the Apache License, Version 2.0 (the "License");
-#   you may not use this file except in compliance with the License.
-#   You may obtain a copy of the License at
-#
-#       http://www.apache.org/licenses/LICENSE-2.0
-#
-#   Unless required by applicable law or agreed to in writing, software
-#   distributed under the License is distributed on an "AS IS" BASIS,
-#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-#   See the License for the specific language governing permissions and
-#   limitations under the License.
-#==================================================================================
-
-set -e
-set -x
-
-# setup version tag
-if [ -f container-tag.yaml ]
-then
-    tag=$(grep "tag:" container-tag.yaml | awk '{print $2}')
-else
-    tag="-"
-fi
-
-hash=$(git rev-parse --short HEAD || true)
-
-export GOPATH=$HOME/go
-export PATH=$GOPATH/bin:$GOROOT/bin:$PATH
-export CFG_FILE=../config/config-file.json
-export RMR_SEED_RT=../config/uta_rtg.rt
-
-GO111MODULE=on GO_ENABLED=0 GOOS=linux
-
-# Build
-go build -a -installsuffix cgo -ldflags "-X main.Version=$tag -X main.Hash=$hash" -o alarm-adapter ./cmd/*.go
-
-# Run UT
-cd ../alarm && RMR_SEED_RT=../config/uta_rtg_lib.r go-acc ./
-#go test -v -p 1 -coverprofile cover.out ./cmd/ -c -o ./adapter_test && ./adapter_test
-#cd ../alarm && RMR_SEED_RT=../config/uta_rtg_lib.rt go test . -v -coverprofile cover.out
diff --git a/adapter/build_adapter_ubuntu.sh b/adapter/build_adapter_ubuntu.sh
new file mode 100755
index 0000000..d1a8a10
--- /dev/null
+++ b/adapter/build_adapter_ubuntu.sh
@@ -0,0 +1,66 @@
+#!/bin/bash
+
+#==================================================================================
+#   Copyright (c) 2020 AT&T Intellectual Property.
+#   Copyright (c) 2020 Nokia
+#
+#   Licensed under the Apache License, Version 2.0 (the "License");
+#   you may not use this file except in compliance with the License.
+#   You may obtain a copy of the License at
+#
+#       http://www.apache.org/licenses/LICENSE-2.0
+#
+#   Unless required by applicable law or agreed to in writing, software
+#   distributed under the License is distributed on an "AS IS" BASIS,
+#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+#   See the License for the specific language governing permissions and
+#   limitations under the License.
+#==================================================================================
+
+set -eux
+
+echo "--> build_adapter_ubuntu.sh starts"
+
+# Install RMR from deb packages at packagecloud.io
+rmr=rmr_3.2.4_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
+wget --content-disposition https://packagecloud.io/o-ran-sc/staging/packages/debian/stretch/$rmrdev/download.deb
+sudo dpkg -i $rmrdev
+rm $rmrdev
+
+# Required to find nng and rmr libs
+export LD_LIBRARY_PATH=/usr/local/lib
+
+# Go install, build, etc
+export GOPATH=$HOME/go
+export PATH=$GOPATH/bin:$PATH
+
+# xApp-framework stuff
+export CFG_FILE=../config/config-file.json
+export RMR_SEED_RT=../config/uta_rtg.rt
+
+GO111MODULE=on GO_ENABLED=0 GOOS=linux
+
+# setup version tag
+if [ -f container-tag.yaml ]
+then
+    tag=$(grep "tag:" container-tag.yaml | awk '{print $2}')
+else
+    tag="no-tag-found"
+fi
+
+hash=$(git rev-parse --short HEAD || true)
+
+# Build
+go build -a -installsuffix cgo -ldflags "-X main.Version=$tag -X main.Hash=$hash" -o alarm-adapter ./cmd/*.go
+
+# Execute UT and measure coverage for the Alarm Library
+cd ../alarm && RMR_SEED_RT=../config/uta_rtg_lib.rt go test . -v -coverprofile cover.out
+
+# 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
diff --git a/adapter/cmd/adapter_test.go b/adapter/cmd/adapter_test.go
index 857e826..dbb53e7 100755
--- a/adapter/cmd/adapter_test.go
+++ b/adapter/cmd/adapter_test.go
@@ -67,7 +67,7 @@
 	a := alarmer.NewAlarm(alarm.RIC_RT_DISTRIBUTION_FAILED, alarm.SeverityMajor, "Some App data", "eth 0 1")
 	assert.Nil(t, alarmer.Raise(a), "raise failed")
 
-	VerifyAlarm(t, a, 1, 0)
+	VerifyAlarm(t, a, 1)
 }
 
 func TestAlarmClearedSucess(t *testing.T) {
@@ -78,7 +78,7 @@
 	a := alarmer.NewAlarm(alarm.RIC_RT_DISTRIBUTION_FAILED, alarm.SeverityMajor, "Some App data", "eth 0 1")
 	assert.Nil(t, alarmer.Raise(a), "raise failed")
 
-	VerifyAlarm(t, a, 1, 0)
+	VerifyAlarm(t, a, 1)
 
 	// Now Clear the alarm and check alarm is removed
 	a = alarmer.NewAlarm(alarm.RIC_RT_DISTRIBUTION_FAILED, alarm.SeverityCleared, "Some App data", "eth 0 1")
@@ -99,8 +99,8 @@
 	b := alarmer.NewAlarm(alarm.TCP_CONNECTIVITY_LOST_TO_DBAAS, alarm.SeverityMinor, "Hello", "abcd 11")
 	assert.Nil(t, alarmer.Raise(b), "raise failed")
 
-	VerifyAlarm(t, a, 2, 0)
-	VerifyAlarm(t, b, 2, 1)
+	VerifyAlarm(t, a, 2)
+	VerifyAlarm(t, b, 2)
 }
 
 func TestMultipleAlarmsClearedSucess(t *testing.T) {
@@ -127,7 +127,7 @@
 	assert.Nil(t, alarmer.Raise(a), "raise failed")
 	assert.Nil(t, alarmer.Raise(a), "raise failed")
 
-	VerifyAlarm(t, a, 1, 0)
+	VerifyAlarm(t, a, 1)
 }
 
 func TestInvalidAlarms(t *testing.T) {
@@ -151,18 +151,12 @@
 	assert.Equal(t, true, alarmAdapter.StatusCB())
 }
 
-func VerifyAlarm(t *testing.T, a alarm.Alarm, count, idx int) string {
+func VerifyAlarm(t *testing.T, a alarm.Alarm, expectedCount int) string {
 	receivedAlert := waitForEvent()
 
-	assert.Equal(t, len(alarmAdapter.activeAlarms), count)
-
-	b := alarmAdapter.activeAlarms[idx]
-	assert.Equal(t, b.ManagedObjectId, a.ManagedObjectId)
-	assert.Equal(t, b.ApplicationId, a.ApplicationId)
-	assert.Equal(t, b.SpecificProblem, a.SpecificProblem)
-	assert.Equal(t, b.PerceivedSeverity, a.PerceivedSeverity)
-	assert.Equal(t, b.AdditionalInfo, a.AdditionalInfo)
-	assert.Equal(t, b.IdentifyingInfo, a.IdentifyingInfo)
+	assert.Equal(t, len(alarmAdapter.activeAlarms), expectedCount)
+	_, ok := alarmAdapter.IsMatchFound(a)
+	assert.True(t, ok)
 
 	return receivedAlert
 }