#/*
# * Licensed to the OpenAirInterface (OAI) Software Alliance under one or more
# * contributor license agreements.  See the NOTICE file distributed with
# * this work for additional information regarding copyright ownership.
# * The OpenAirInterface Software Alliance licenses this file to You under
# * the OAI Public License, Version 1.1  (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.openairinterface.org/?page_id=698
# *
# * 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.
# *-------------------------------------------------------------------------------
# * For more information about the OpenAirInterface (OAI) Software Alliance:
# *      contact@openairinterface.org
# */
#---------------------------------------------------------------------
#
# Dockerfile for the Open-Air-Interface BUILD service
#   Valid for Ubuntu18.04
#
#---------------------------------------------------------------------

FROM ran-build:latest AS gnb-build

RUN rm -Rf /oai-ran
WORKDIR /oai-ran
COPY . .

#run build_oai to build the target image
RUN /bin/sh oaienv && \ 
    cd cmake_targets && \
    mkdir -p log && \
    ./build_oai --gNB --ninja -w USRP --verbose-ci

RUN python3 ./docker/scripts/generateTemplate.py ./docker/scripts/gnb_parameters.yaml

#start from scratch for target executable
FROM ubuntu:bionic as oai-gnb
ENV DEBIAN_FRONTEND=noninteractive
ENV TZ=Europe

RUN apt-get update && \
    DEBIAN_FRONTEND=noninteractive apt-get upgrade --yes && \
    DEBIAN_FRONTEND=noninteractive apt-get install --yes \
        software-properties-common \
        procps \
        libsctp1 \
        tzdata \
        libnettle6 \
        libblas3 \
        libatlas3-base \
        libconfig9 \
        openssl \
        net-tools \
        iproute2 \
        iputils-ping \
        python \
        python3 \
        python3-six \
        python3-requests \
        libusb-1.0-0 \
        libyaml-0-2 && \
    rm -rf /var/lib/apt/lists/*

WORKDIR /opt/oai-gnb/bin
COPY --from=gnb-build /oai-ran/targets/bin/nr-softmodem.Rel15 .
COPY --from=gnb-build /oai-ran/docker/scripts/gnb_entrypoint.sh entrypoint.sh

WORKDIR /usr/local/lib/
COPY --from=gnb-build /oai-ran/targets/bin/liboai_eth_transpro.so.Rel15 .
COPY --from=gnb-build /oai-ran/targets/bin/libtcp_bridge_oai.so.Rel15 .
COPY --from=gnb-build /oai-ran/targets/bin/librfsimulator.so.Rel15 .
COPY --from=gnb-build /oai-ran/targets/bin/liboai_usrpdevif.so.Rel15 .
COPY --from=gnb-build /oai-ran/targets/bin/libcoding.so .
COPY --from=gnb-build /oai-ran/targets/bin/libparams_libconfig.so .
COPY --from=gnb-build /oai-ran/cmake_targets/ran_build/build/libdfts.so .
COPY --from=gnb-build /oai-ran/cmake_targets/ran_build/build/libldpc.so .
COPY --from=gnb-build /oai-ran/cmake_targets/ran_build/build/libldpc_optim.so .
COPY --from=gnb-build /oai-ran/cmake_targets/ran_build/build/libldpc_optim8seg.so .
COPY --from=gnb-build /oai-ran/cmake_targets/ran_build/build/libldpc_orig.so .

RUN /bin/bash -c "ln -s /usr/local/lib/liboai_eth_transpro.so.Rel15 /usr/local/lib/liboai_transpro.so"
RUN /bin/bash -c "ln -s /usr/local/lib/liboai_usrpdevif.so.Rel15 /usr/local/lib/liboai_device.so"
RUN /bin/bash -c "ln -s /usr/local/lib/librfsimulator.so.Rel15 /usr/local/lib/librfsimulator.so"
COPY --from=gnb-build /usr/local/lib/libprotobuf-c.so.1 .

# Now we are copying from builder-image the UHD files.
COPY --from=gnb-build /usr/local/bin/uhd_find_devices /usr/local/bin
COPY --from=gnb-build /usr/local/lib/libuhd.so.3.15.0 /usr/local/lib
COPY --from=gnb-build /usr/local/lib/uhd/utils/uhd_images_downloader.py /opt/oai-gnb/bin
WORKDIR /usr/lib/x86_64-linux-gnu
COPY --from=gnb-build /usr/lib/x86_64-linux-gnu/libboost_date_time.so.1.65.1 .
COPY --from=gnb-build /usr/lib/x86_64-linux-gnu/libboost_filesystem.so.1.65.1 .
COPY --from=gnb-build /usr/lib/x86_64-linux-gnu/libboost_regex.so.1.65.1 .
COPY --from=gnb-build /usr/lib/x86_64-linux-gnu/libboost_serialization.so.1.65.1 .
COPY --from=gnb-build /usr/lib/x86_64-linux-gnu/libboost_thread.so.1.65.1 .
COPY --from=gnb-build /usr/lib/x86_64-linux-gnu/libboost_system.so.1.65.1 .
COPY --from=gnb-build /usr/lib/x86_64-linux-gnu/libboost_program_options.so.1.65.1 .

RUN ldconfig

# Copy the relevant configuration files for gNB
WORKDIR /opt/oai-gnb/etc
COPY --from=gnb-build /oai-ran/docker/etc .

WORKDIR /opt/oai-gnb
#EXPOSE 2152/udp  # S1U, GTP/UDP
#EXPOSE 22100/tcp # ?
#EXPOSE 36412/udp # S1C, SCTP/UDP
#EXPOSE 36422/udp # X2C, SCTP/UDP
#EXPOSE 50000/udp # IF5 / ORI (control)
#EXPOSE 50001/udp # IF5 / ECPRI (data)

ENTRYPOINT ["/opt/oai-gnb/bin/entrypoint.sh"]
CMD ["/opt/oai-gnb/bin/nr-softmodem.Rel15", "-O", "/opt/oai-gnb/etc/gnb.conf"]