[COMAC-197] Refactor Accelleran CU

Change-Id: I0b0639ebcc6d0a1c0e9c7fab9aa876da77cf0ba6
diff --git a/ran/accelleran-cu/Chart.yaml b/ran/accelleran-cu/Chart.yaml
new file mode 100644
index 0000000..4b442ae
--- /dev/null
+++ b/ran/accelleran-cu/Chart.yaml
@@ -0,0 +1,22 @@
+---
+# Copyright 2018-present Open Networking Foundation
+# Copyright 2018 Intel Corporation
+#
+# 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.
+
+apiVersion: v1
+name: accelleran-cu
+description: A Helm chart to install Accelleran CU
+icon: https://guide.opencord.org/logos/cord.svg
+
+version: 0.1.0
diff --git a/ran/accelleran-cu/templates/_helpers.tpl b/ran/accelleran-cu/templates/_helpers.tpl
new file mode 100644
index 0000000..6ba93e7
--- /dev/null
+++ b/ran/accelleran-cu/templates/_helpers.tpl
@@ -0,0 +1,67 @@
+{{- /*
+# Copyright 2018-present Open Networking Foundation
+# Copyright 2018 Intel Corporation
+#
+# 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.
+*/ -}}
+
+{{/*
+Renders a set of standardised labels
+*/}}
+{{- define "accelleran-cu.metadata_labels" -}}
+{{- $application := index . 0 -}}
+{{- $context := index . 1 -}}
+release: {{ $context.Release.Name }}
+app: {{ $application }}
+{{- end -}}
+
+{{/*
+Render ServiceAccount, Role, and RoleBinding required for kubernetes-entrypoint.
+*/}}
+{{- define "accelleran-cu.service_account" -}}
+{{- $context := index . 1 -}}
+{{- $saName := index . 0 -}}
+{{- $saNamespace := $context.Release.Namespace }}
+---
+apiVersion: v1
+kind: ServiceAccount
+metadata:
+  name: {{ $saName }}
+  namespace: {{ $saNamespace }}
+  labels:
+{{ tuple $saName $context | include "accelleran-cu.metadata_labels" | indent 4 }}
+---
+apiVersion: rbac.authorization.k8s.io/v1beta1
+kind: RoleBinding
+metadata:
+  name: {{ $saName }}
+  namespace: {{ $saNamespace }}
+  labels:
+{{ tuple $saName $context | include "accelleran-cu.metadata_labels" | indent 4 }}
+roleRef:
+  apiGroup: rbac.authorization.k8s.io
+  kind: Role
+  name: {{ $saName }}
+subjects:
+  - kind: ServiceAccount
+    name: {{ $saName }}
+    namespace: {{ $saNamespace }}
+---
+apiVersion: rbac.authorization.k8s.io/v1beta1
+kind: Role
+metadata:
+  name: {{ $saName }}
+  namespace: {{ $saNamespace }}
+  labels:
+{{ tuple $saName $context | include "accelleran-cu.metadata_labels" | indent 4 }}
+{{- end -}}
diff --git a/ran/accelleran-cu/templates/configmap-accelleran.yaml b/ran/accelleran-cu/templates/configmap-accelleran.yaml
new file mode 100644
index 0000000..7076d95
--- /dev/null
+++ b/ran/accelleran-cu/templates/configmap-accelleran.yaml
@@ -0,0 +1,28 @@
+{{/*
+Copyright 2019-present Open Networking Foundation
+
+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.
+*/}}
+
+---
+apiVersion: v1
+kind: ConfigMap
+metadata:
+  name: accelleran-cu-config
+  labels:
+{{ tuple "accelleran-cu" . | include "accelleran-cu.metadata_labels" | indent 4 }}
+data:
+  bootstrap.txt: |
+    redis.hostname:accelleran-cu
+    redis.port:{{ .Values.config.vran.ports.redis.port }}
+    instance.filter:{{ .Values.config.vran.instId }}
\ No newline at end of file
diff --git a/ran/accelleran-cu/templates/service-accelleran.yaml b/ran/accelleran-cu/templates/service-accelleran.yaml
new file mode 100644
index 0000000..eb179b2
--- /dev/null
+++ b/ran/accelleran-cu/templates/service-accelleran.yaml
@@ -0,0 +1,61 @@
+{{/*
+Copyright 2019-present Open Networking Foundation
+
+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.
+*/}}
+
+---
+apiVersion: v1
+kind: Service
+metadata:
+  name: accelleran-cu
+  labels:
+{{ tuple "accelleran-cu" . | include "accelleran-cu.metadata_labels" | indent 4 }}
+spec:
+  selector:
+{{ tuple "accelleran-cu" . | include "accelleran-cu.metadata_labels" | indent 4 }}
+  ports:
+  - name: s1ap
+    port: {{ .Values.config.vran.ports.s1ap.port }}
+    protocol: SCTP
+  - name: nats
+    port: {{ .Values.config.vran.ports.nats.port }}
+    protocol: TCP
+  - name: redis
+    port: {{ .Values.config.vran.ports.redis.port }}
+    protocol: TCP
+
+---
+apiVersion: v1
+kind: Service
+metadata:
+  name: acceleran-cu-external
+  labels:
+{{ tuple "accelleran-cu" . | include "accelleran-cu.metadata_labels" | indent 4 }}
+spec:
+  selector:
+{{ tuple "accelleran-cu" . | include "accelleran-cu.metadata_labels" | indent 4 }}
+  type: NodePort
+  ports:
+  - name: s1ap
+    port: {{ .Values.config.vran.ports.s1ap.port }}
+    protocol: SCTP
+    nodePort: {{ .Values.config.vran.ports.s1ap.nodePort }}
+  - name: nats
+    port: {{ .Values.config.vran.ports.nats.port }}
+    protocol: TCP
+    nodePort: {{ .Values.config.vran.ports.nats.nodePort }}
+  - name: redis
+    port: {{ .Values.config.vran.ports.redis.port }}
+    protocol: TCP
+    nodePort: {{ .Values.config.vran.ports.redis.nodePort }}
diff --git a/ran/accelleran-cu/templates/statefulset-accelleran.yaml b/ran/accelleran-cu/templates/statefulset-accelleran.yaml
new file mode 100644
index 0000000..31431e9
--- /dev/null
+++ b/ran/accelleran-cu/templates/statefulset-accelleran.yaml
@@ -0,0 +1,145 @@
+{{/*
+Copyright 2019-present Open Networking Foundation
+
+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.
+*/}}
+
+{{ tuple "accelleran-cu" . | include "accelleran-cu.service_account" }}
+
+---
+apiVersion: apps/v1
+kind: StatefulSet
+metadata:
+  name: accelleran-cu
+  labels:
+{{ tuple "accelleran-cu" . | include "accelleran-cu.metadata_labels" | indent 4 }}
+spec:
+  replicas: {{ .Values.config.vran.replicas }}
+  selector:
+    matchLabels:
+{{ tuple "accelleran-cu" . | include "accelleran-cu.metadata_labels" | indent 6 }}
+  serviceName: "accelleran-cu"
+  template:
+    metadata:
+      labels:
+{{ tuple "accelleran-cu" . | include "accelleran-cu.metadata_labels" | indent 8 }}
+    spec:
+      terminationGracePeriodSeconds: 1
+    {{- if .Values.nodeSelectors.enabled }}
+      nodeSelector:
+        {{ .Values.nodeSelectors.accelleranCU.label }}: {{ .Values.nodeSelectors.accelleranCU.value }}
+    {{- end }}
+      serviceAccountName: accelleran-cu
+      initContainers:
+      - name: cu-init
+        image: {{ .Values.images.tags.initCU }}
+        imagePullPolicy: {{ .Values.images.pullPolicy }}
+        securityContext:
+          privileged: true
+          runAsUser: 0
+        command: [ "sh",  "-c"]
+        args:
+        - >
+          iptables -I OUTPUT -p icmp --icmp-type destination-unreachable -j DROP;
+          if chroot /mnt/host-rootfs modinfo nf_conntrack_proto_sctp > /dev/null 2>&1; then \
+              chroot /mnt/host-rootfs modprobe nf_conntrack_proto_sctp; \
+          fi; \
+        volumeMounts:
+        - name: host-rootfs
+          mountPath: /mnt/host-rootfs
+      containers:
+      - name: nats
+        image: {{ .Values.images.tags.nats }}
+        imagePullPolicy: {{ .Values.images.pullPolicy }}
+        securityContext:
+          capabilities:
+            add:
+              - NET_ADMIN
+              - IPC_LOCK
+        stdin: true
+        tty: true
+        env:
+        - name: NATS_SERVICE_NAME
+          value: {{ .Values.config.nats.serviceName }}
+        command: [ "sh", "-c"]
+        args:
+        - gnatsd -DV -c /etc/gnatsd.conf;
+      {{- if .Values.resources.enabled }}
+        resources:
+{{ toYaml .Values.resources.accelleranCU | indent 10 }}
+      {{- end }}
+      - name: redis
+        image: {{ .Values.images.tags.redis }}
+        imagePullPolicy: {{ .Values.images.pullPolicy }}
+        securityContext:
+          capabilities:
+            add:
+              - NET_ADMIN
+              - IPC_LOCK
+        stdin: true
+        tty: true
+        command: ["sh", "-c"]
+        args:
+        # FIXME: Redis can't support with dns resolution, can't set hostname at here.
+        - (sleep 5; redis-cli -h localhost -p {{ .Values.config.vran.ports.redis.port }} set NATS_SERVICE_URL "nat://{{ .Values.config.nats.ip }}:{{ .Values.config.vran.ports.nats.port }}";) &
+          /usr/local/bin/redis-server
+      {{- if .Values.resources.enabled }}
+        resources:
+{{ toYaml .Values.resources.accelleranCU | indent 10 }}
+      {{- end }}
+      - name: vran-l3
+        image: {{ .Values.images.tags.vranL3 }}
+        imagePullPolicy: {{ .Values.images.pullPolicy }}
+        securityContext:
+          capabilities:
+            add:
+            - NET_ADMIN
+            - IPC_LOCK
+            - SYS_NICE
+        stdin: true
+        tty: true
+        env:
+          - name: MME_IP
+            value: {{ .Values.config.mme.ip }}
+        command: ["sh", "-c"]
+        args:
+        - sqlite3 /mnt/app/datastore.db "update cellS1apConfig set enbSctpPort={{ .Values.config.vran.ports.s1ap.port }};";
+          sqlite3 /mnt/app/datastoreRemRw.db "update FapServiceFapControlLteGateway set S1SigLinkPort={{ .Values.config.mme.s1apPort }};";
+          sqlite3 /mnt/app/datastoreRemRw.db "update FapServiceCellConfigLteEpcPlmnList set PLMNID={{ .Values.config.epc.plmnId }};";
+          sqlite3 /mnt/app/datastore.db "insert into enbSctpSocketInterface ('interfaceName') values ('{{ .Values.config.vran.socketInterface }}');";
+          sqlite3 /mnt/app/datastore.db "update logging set severity=7 where id=25;";
+{{- if .Values.config.progran.enabled }}
+          sqlite3 /mnt/app/datastore.db "update featureActivation set mmeSlicingEnabled=1;";
+          sqlite3 /mnt/app/datastore.db "update featureActivation set ranSlicingEnabled=1;";
+          sqlite3 /mnt/app/datastore.db "insert into pranServerAddress (pranServerIpAddress,pranServerPort) values ('{{ .Values.config.progran.ip }}', {{ .Values.config.progran.port }});";
+{{- end}}
+          sleep 10;
+          /l3/l3start;
+        volumeMounts:
+        - name: bbu-config
+          mountPath: /mnt/app/bootstrap.txt
+          subPath: bootstrap.txt
+      {{- if .Values.resources.enabled }}
+        resources:
+{{ toYaml .Values.resources.accelleranCU | indent 10 }}
+      {{- end }}
+      volumes:
+        - name: bbu-config
+          configMap:
+            name: accelleran-cu-config
+            items:
+              - key: bootstrap.txt
+                path: bootstrap.txt
+        - name: host-rootfs
+          hostPath:
+            path: /
diff --git a/ran/accelleran-cu/values.yaml b/ran/accelleran-cu/values.yaml
new file mode 100644
index 0000000..24d8deb
--- /dev/null
+++ b/ran/accelleran-cu/values.yaml
@@ -0,0 +1,69 @@
+# Copyright 2018-present Open Networking Foundation
+# Copyright 2018 Intel Corporation
+#
+# 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.
+
+images:
+  tags:
+    initCU: docker.io/omecproject/pod-init:1.0.0
+    nats: node1:30500/accelleran/vran-nats:20190121
+    redis: node1:30500/accelleran/vran-redis:20190121
+    vranL3: node1:30500/accelleran/vran-l3-fdd:onf-dns
+  pullPolicy: IfNotPresent
+
+nodeSelectors:
+  enabled: false
+  accelleranCU:
+    label: accelleran-cu
+    value: enabled
+
+resources:
+  enabled: true
+  accelleranCU:
+    requests:
+      cpu: 3
+      memory: 1Gi
+    limits:
+      cpu: 3
+      memory: 1Gi
+
+config:
+  vran:
+    replicas: 1
+    socketInterface: eth0
+    instId: "enb_key"
+    ports:
+      s1ap:
+        port: 36412
+        nodePort: 31412
+      nats:
+        port: 4222
+        nodePort: 4222
+      redis:
+        port: 6379
+        nodePort: 6379
+  nats:
+    serviceName: "nats-cluster"
+    ip: 10.90.0.151
+  epc:
+    plmnId: 371211
+  mme:
+    ip: 10.90.0.151
+    s1apPort: 36412
+  progran:
+    enabled: false
+    # ONOS ProgRAN IP address
+    ip: 10.90.0.151
+    port: 30410
+
+