[SDNC] Change SDNR DB from elasticsearch to maria DB

Enable sdnrdb mariaDB as default

Elasticsearch remains as legacy support

Issue-ID: OOM-3210
Signed-off-by: demskeq8 <alexander.dehn@highstreet-technologies.com>
Change-Id: I16298ef13cd8eb6f6848ec5dbcd63e22f29f3922
diff --git a/kubernetes/sdnc/Chart.yaml b/kubernetes/sdnc/Chart.yaml
index 393a090..24f58b1 100644
--- a/kubernetes/sdnc/Chart.yaml
+++ b/kubernetes/sdnc/Chart.yaml
@@ -48,7 +48,7 @@
   - name: elasticsearch
     version: ~13.x-0
     repository: '@local'
-    condition: config.sdnr.enabled
+    condition: sdnc.elasticsearch.enabled,elasticsearch.enabled
   # conditions for sdnc-subcharts
   - name: dmaap-listener
     version: ~13.x-0
diff --git a/kubernetes/sdnc/resources/config/bin/createSdnrDb.sh b/kubernetes/sdnc/resources/config/bin/createSdnrDb.sh
new file mode 100644
index 0000000..28c703b
--- /dev/null
+++ b/kubernetes/sdnc/resources/config/bin/createSdnrDb.sh
@@ -0,0 +1,30 @@
+#!/bin/sh
+
+###
+# ============LICENSE_START=======================================================
+# ONAP : SDN-C
+# ================================================================================
+# Copyright (C) 2023 highstreet technologies  Intellectual Property. All rights reserved.
+# ================================================================================
+# 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.
+# ============LICENSE_END=========================================================
+###
+
+echo "Create '${SDNRDBDATABASE}' and user '${SDNRDBUSERNAME}' within maria-galera database cluster"
+root_user=root
+mysql -v -v -u $root_user -p${MYSQL_ROOT_PASSWORD} -Bse "\
+CREATE DATABASE IF NOT EXISTS ${SDNRDBDATABASE}; \
+CREATE USER IF NOT EXISTS ${SDNRDBUSERNAME}@'%' IDENTIFIED BY '${SDNRDBPASSWORD}'; \
+GRANT ALL PRIVILEGES ON ${SDNRDBDATABASE}.* TO '${SDNRDBUSERNAME}'@'%'; \
+FLUSH PRIVILEGES; "
+
diff --git a/kubernetes/sdnc/templates/sdnrdb-init-job.yaml b/kubernetes/sdnc/templates/sdnrdb-init-job.yaml
index 6c6e33a..4b2f773 100755
--- a/kubernetes/sdnc/templates/sdnrdb-init-job.yaml
+++ b/kubernetes/sdnc/templates/sdnrdb-init-job.yaml
@@ -27,12 +27,17 @@
         command:
         - /app/ready.py
         args:
+        {{- if .Values.config.sdnr.mariadb.enabled }}
+        - --container-name
+        - {{ include "common.mariadbService" . }}
+        {{- else -}}
         - --container-name
         - {{.Values.elasticsearch.nameOverride}}-elasticsearch
         - --container-name
         - {{.Values.elasticsearch.nameOverride}}-nginx
         - --container-name
         - {{.Values.elasticsearch.nameOverride}}-master
+        {{- end }}
         env:
         - name: NAMESPACE
           valueFrom:
@@ -49,6 +54,9 @@
         args:
           - -c
           - |
+            {{- if .Values.config.sdnr.mariadb.enabled }}
+            "{{ .Values.config.binDir }}/createSdnrDb.sh";
+            {{- end -}}
             sleep 90; "{{ .Values.config.binDir }}/startODL.sh"
         env:
           - name: SDNC_AAF_ENABLED
@@ -62,10 +70,33 @@
           ## start sdnrdb parameter
           - name: SDNRINIT
             value: "true"
+          {{- if .Values.config.sdnr.mariadb.enabled }}
+          - name: SDNRDBTYPE
+            value: MARIADB
+          - name: MYSQL_HOST
+            value: {{ include "common.mariadbService" . }}
+          - name: MYSQL_ROOT_PASSWORD
+            {{- include "common.secret.envFromSecretFast" (dict "global" . "uid" "db-root-password" "key" "password") | indent 12 }}
+          - name: SDNRDBURL
+            value: "jdbc:mysql://{{ include "common.mariadbService" . }}:3306/{{ .Values.config.sdnr.mariadb.databaseName }}"
+          - name: SDNRDBDATABASE
+            value: "{{ .Values.config.sdnr.mariadb.databaseName }}"
+          - name: SDNRDBUSERNAME
+            {{- include "common.secret.envFromSecretFast" (dict "global" . "uid" "sdnrdb-secret" "key" "login") | indent 12 }}
+          - name: SDNRDBPASSWORD
+            {{- include "common.secret.envFromSecretFast" (dict "global" . "uid" "sdnrdb-secret" "key" "password") | indent 12 }}
+        {{- else -}}
           - name: SDNRDBURL
             value: "http://{{ .Values.elasticsearch.service.name | default "sdnrdb"}}.{{.Release.Namespace}}:{{.Values.elasticsearch.service.port | default "9200"}}"
           - name: SDNRDBPARAMETER
             value: "-k"
+        {{- end }}
+        {{- if .Values.config.sdnr.mariadb.enabled }}
+        volumeMounts:
+          - mountPath: {{ .Values.config.binDir }}/createSdnrDb.sh
+            name: bin
+            subPath: createSdnrDb.sh
+        {{- end }}
         resources: {{ include "common.resources" . | nindent 10 }}
       {{- if include "common.onServiceMesh" . }}
       - name: sdnrdb-service-mesh-wait-for-job-container
diff --git a/kubernetes/sdnc/templates/statefulset.yaml b/kubernetes/sdnc/templates/statefulset.yaml
index b714775..20f82e0 100644
--- a/kubernetes/sdnc/templates/statefulset.yaml
+++ b/kubernetes/sdnc/templates/statefulset.yaml
@@ -296,12 +296,31 @@
           {{- end }}
           - name: SDNRONLY
             value: "{{ .Values.config.sdnr.sdnronly | default "false" }}"
+          {{- if .Values.config.sdnr.mariadb.enabled }}
+          - name: SDNRCONTROLLERID
+            value: {{ uuidv4 }}
+          - name: SDNRDBTYPE
+            value: MARIADB
+          - name: SDNRDBURL
+            value: "jdbc:mysql://{{ include "common.mariadbService" . }}:3306/{{ .Values.config.sdnr.mariadb.databaseName}}"
+          - name: SDNR_DB_DATABASE
+            value: {{ .Values.config.sdnr.mariadb.databaseName }}
+          - name: SDNRDBUSERNAME
+            {{- include "common.secret.envFromSecretFast" (dict "global" . "uid" "sdnrdb-secret" "key" "login") | indent 12 }}
+          - name: SDNRDBPASSWORD
+            {{- include "common.secret.envFromSecretFast" (dict "global" . "uid" "sdnrdb-secret" "key" "password") | indent 12 }}
+          - name: SDNR_ASYNC_HANDLING
+            value: {{ .Values.config.sdnr.mariadb.asyncHandling | default "false" | quote }}
+          - name: SDNR_ASYNC_POOLSIZE
+            value: {{ .Values.config.sdnr.mariadb.asyncPoolSize | default 200 | quote }}
+          {{- else -}}
           - name: SDNRDBURL
             value: "http://{{ .Values.elasticsearch.service.name | default "sdnrdb"}}.{{.Release.Namespace}}:{{.Values.elasticsearch.service.port | default "9200"}}"
           {{- if .Values.config.sdnr.sdnrdbTrustAllCerts }}
           - name: SDNRDBTRUSTALLCERTS
             value: "true"
           {{- end }}
+          {{- end }}
           {{- if .Values.global.cmpv2Enabled }}
           - name: ODL_CERT_DIR
             value: {{ (mustFirst (.Values.certificates)).mountPath }}
diff --git a/kubernetes/sdnc/values.yaml b/kubernetes/sdnc/values.yaml
index 9a00798..e35dfde 100644
--- a/kubernetes/sdnc/values.yaml
+++ b/kubernetes/sdnc/values.yaml
@@ -177,6 +177,11 @@
     type: basicAuth
     login: '{{ .Values.config.sdnr.vesCollector.username }}'
     password: '{{ .Values.config.sdnr.vesCollector.password }}'
+  - uid: sdnrdb-secret
+    name: &sdnrdbSecretName '{{ include "common.release" . }}-sdnc-sdnrdb-secret'
+    type: basicAuth
+    login: '{{ index .Values "config" "sdnr" "mariadb" "user" }}'
+    password: '{{ index .Values "config" "sdnr" "mariadb" "password" }}'
 #################################################################
 # Certificates
 #################################################################
@@ -316,6 +321,19 @@
     # sdnronly: true starts sdnc container with odl and sdnrwt features only
     sdnronly: false
     sdnrdbTrustAllCerts: true
+    elasticsearch:
+    ## for legacy eleasticsearch database
+    # enabled: &esdbenabled true
+      enabled: &esdbenabled false
+    mariadb:
+      ## for legacy eleasticsearch database
+      # enabled: false
+      enabled: true
+      databaseName: sdnrdb
+      user: sdnrdb
+      externalSecret: *sdnrdbSecretName
+      asyncHandling: true
+      asyncPoolSize: 200
     kafka:
       enabled: false
       consumerGroupPrefix: &consumerGroupPrefix sdnr
@@ -401,7 +419,7 @@
     user: *dbUser
     externalSecret: *dbSecretName
   service:
-    name: sdnc-dbhost
+    name: sdnc-db
   sdnctlPrefix: sdnc
   persistence:
     mountSubPath: sdnc/mariadb-galera
@@ -495,6 +513,7 @@
 # local elasticsearch cluster
 localElasticCluster: true
 elasticsearch:
+  enabled: *esdbenabled
   nameOverride: &elasticSearchName sdnrdb
   name: sdnrdb-cluster
   service: