From dc8f31ca36c3037bc59f74ea01e46ab466c4b161 Mon Sep 17 00:00:00 2001
From: lukken <lukken@astron.nl>
Date: Wed, 30 Mar 2022 11:17:36 +0000
Subject: [PATCH] L2SS-680: Add empty PDU device

---
 .gitlab-ci.yml                                | 12 ++++
 CDB/LOFAR_ConfigDb.json                       |  7 +++
 docker-compose/device-pdu.yml                 | 43 ++++++++++++++
 .../startup/01-devices.py                     |  1 +
 sbin/run_integration_test.sh                  |  2 +
 .../docs/source/devices/pdu.rst               |  7 +++
 tangostationcontrol/docs/source/index.rst     |  1 +
 tangostationcontrol/setup.cfg                 |  1 +
 .../tangostationcontrol/devices/README.md     |  5 +-
 .../tangostationcontrol/devices/boot.py       |  1 +
 .../tangostationcontrol/devices/pdu.py        | 57 +++++++++++++++++++
 .../default/devices/test_device_pdu.py        | 17 ++++++
 12 files changed, 152 insertions(+), 2 deletions(-)
 create mode 100644 docker-compose/device-pdu.yml
 create mode 100644 tangostationcontrol/docs/source/devices/pdu.rst
 create mode 100644 tangostationcontrol/tangostationcontrol/devices/pdu.py
 create mode 100644 tangostationcontrol/tangostationcontrol/integration_test/default/devices/test_device_pdu.py

diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index b832d175e..e9acfe73b 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -103,6 +103,7 @@ docker_build_image_all:
     - bash $CI_PROJECT_DIR/sbin/tag_and_push_docker_image.sh device-boot latest
     - bash $CI_PROJECT_DIR/sbin/tag_and_push_docker_image.sh device-docker latest
     - bash $CI_PROJECT_DIR/sbin/tag_and_push_docker_image.sh device-observation_control latest
+    - bash $CI_PROJECT_DIR/sbin/tag_and_push_docker_image.sh device-pdu latest
     - bash $CI_PROJECT_DIR/sbin/tag_and_push_docker_image.sh device-recv latest
     - bash $CI_PROJECT_DIR/sbin/tag_and_push_docker_image.sh device-sdp latest
     - bash $CI_PROJECT_DIR/sbin/tag_and_push_docker_image.sh device-sst latest
@@ -253,6 +254,17 @@ docker_build_image_device_apspu:
   script:
 #    Do not remove 'bash' or statement will be ignored by primitive docker shell
     - bash $CI_PROJECT_DIR/sbin/tag_and_push_docker_image.sh device-apspu $tag
+docker_build_image_device_pdu:
+  extends: .base_docker_images_except
+  only:
+    refs:
+      - merge_requests
+    changes:
+      - docker-compose/device-pdu.yml
+      - docker-compose/lofar-device-base/*
+  script:
+#    Do not remove 'bash' or statement will be ignored by primitive docker shell
+    - bash $CI_PROJECT_DIR/sbin/tag_and_push_docker_image.sh device-pdu $tag
 docker_build_image_device_tilebeam:
   extends: .base_docker_images_except
   only:
diff --git a/CDB/LOFAR_ConfigDb.json b/CDB/LOFAR_ConfigDb.json
index 75da3c114..2032c4de7 100644
--- a/CDB/LOFAR_ConfigDb.json
+++ b/CDB/LOFAR_ConfigDb.json
@@ -14,6 +14,13 @@
                 }
             }
         },
+        "PDU": {
+            "STAT": {
+                "PDU": {
+                    "STAT/PDU/1": {}
+                }
+            }
+        },
         "TileBeam": {
             "STAT": {
                 "TileBeam": {
diff --git a/docker-compose/device-pdu.yml b/docker-compose/device-pdu.yml
new file mode 100644
index 000000000..c000327cc
--- /dev/null
+++ b/docker-compose/device-pdu.yml
@@ -0,0 +1,43 @@
+#
+# Requires:
+#   - lofar-device-base.yml
+#
+version: '2'
+
+volumes:
+  iers-data: {}
+
+services:
+  device-pdu:
+    image: device-pdu
+    # build explicitly, as docker-compose does not understand a local image
+    # being shared among services.
+    build:
+        context: ..
+        dockerfile: docker-compose/lofar-device-base/Dockerfile
+        args:
+            SOURCE_IMAGE: ${DOCKER_REGISTRY_HOST}/${DOCKER_REGISTRY_USER}-tango-itango:${TANGO_ITANGO_VERSION}
+    container_name: ${CONTAINER_NAME_PREFIX}device-pdu
+    logging:
+      driver: "json-file"
+      options:
+        max-size: "100m"
+        max-file: "10"
+    networks:
+      - control
+    ports:
+      - "5714:5714" # unique port for this DS
+    extra_hosts:
+      - "host.docker.internal:host-gateway"
+    volumes:
+      - ..:/opt/lofar/tango:rw
+      - iers-data:/opt/IERS
+    environment:
+      - TANGO_HOST=${TANGO_HOST}
+    working_dir: /opt/lofar/tango
+    entrypoint:
+      - bin/start-ds.sh
+      # configure CORBA to _listen_ on 0:port, but tell others we're _reachable_ through ${HOSTNAME}:port, since CORBA
+      # can't know about our Docker port forwarding
+      - l2ss-pdu PDU STAT -v -ORBendPoint giop:tcp:0:5714 -ORBendPointPublish giop:tcp:${HOSTNAME}:5714
+    restart: unless-stopped
diff --git a/docker-compose/jupyter/ipython-profiles/stationcontrol-jupyter/startup/01-devices.py b/docker-compose/jupyter/ipython-profiles/stationcontrol-jupyter/startup/01-devices.py
index cf3e092e8..1434393a8 100644
--- a/docker-compose/jupyter/ipython-profiles/stationcontrol-jupyter/startup/01-devices.py
+++ b/docker-compose/jupyter/ipython-profiles/stationcontrol-jupyter/startup/01-devices.py
@@ -8,6 +8,7 @@ xst = DeviceProxy("STAT/XST/1")
 unb2 = DeviceProxy("STAT/UNB2/1")
 boot = DeviceProxy("STAT/Boot/1")
 tilebeam = DeviceProxy("STAT/TileBeam/1")
+pdu = DeviceProxy("STAT/PDU/1")
 beamlet = DeviceProxy("STAT/Beamlet/1")
 digitalbeam = DeviceProxy("STAT/DigitalBeam/1")
 docker = DeviceProxy("STAT/Docker/1")
diff --git a/sbin/run_integration_test.sh b/sbin/run_integration_test.sh
index aec2d1d40..b0f0afb21 100755
--- a/sbin/run_integration_test.sh
+++ b/sbin/run_integration_test.sh
@@ -26,9 +26,11 @@ SIMULATORS="sdptr-sim recv-sim unb2-sim apsct-sim apspu-sim"
 # Build only the required images, please do not build everything that makes CI
 # take really long to finish, especially grafana / jupyter / prometheus.
 # jupyter is physically large > 2.5gb and overlayfs is really slow.
+
 # shellcheck disable=SC2086
 make build $DEVICES $SIMULATORS
 make build elk integration-test
+
 make build archiver-timescale hdbppts-cm hdbppts-es
 
 # Start and stop sequence
diff --git a/tangostationcontrol/docs/source/devices/pdu.rst b/tangostationcontrol/docs/source/devices/pdu.rst
new file mode 100644
index 000000000..ca78ad3c5
--- /dev/null
+++ b/tangostationcontrol/docs/source/devices/pdu.rst
@@ -0,0 +1,7 @@
+.. _pdu:
+
+PDU
+====================
+
+The ``pdu == DeviceProxy("STAT/PDU/1")`` device controls the Power Distribution
+Unit (PDU)
diff --git a/tangostationcontrol/docs/source/index.rst b/tangostationcontrol/docs/source/index.rst
index 97f827210..c0b64b2a8 100644
--- a/tangostationcontrol/docs/source/index.rst
+++ b/tangostationcontrol/docs/source/index.rst
@@ -24,6 +24,7 @@ Even without having access to any LOFAR2.0 hardware, you can install the full st
    devices/digitalbeam
    devices/boot
    devices/docker
+   devices/pdu
    devices/recv
    devices/sdp
    devices/sst-xst
diff --git a/tangostationcontrol/setup.cfg b/tangostationcontrol/setup.cfg
index e9d1b0ada..997b7947d 100644
--- a/tangostationcontrol/setup.cfg
+++ b/tangostationcontrol/setup.cfg
@@ -35,6 +35,7 @@ where=./
 console_scripts =
     l2ss-apsct = tangostationcontrol.devices.apsct:main
     l2ss-apspu = tangostationcontrol.devices.apspu:main
+    l2ss-pdu = tangostationcontrol.devices.pdu:main
     l2ss-tilebeam = tangostationcontrol.devices.tilebeam:main
     l2ss-beamlet = tangostationcontrol.devices.sdp.beamlet:main
     l2ss-digitalbeam = tangostationcontrol.devices.sdp.digitalbeam:main
diff --git a/tangostationcontrol/tangostationcontrol/devices/README.md b/tangostationcontrol/tangostationcontrol/devices/README.md
index 08eaf1f67..19d72e35c 100644
--- a/tangostationcontrol/tangostationcontrol/devices/README.md
+++ b/tangostationcontrol/tangostationcontrol/devices/README.md
@@ -10,10 +10,11 @@ If a new device is added, it will (likely) need to be referenced in several plac
 - Adjust `docker-compose/jupyter/ipython-profiles/stationcontrol-jupyter/startup/01-devices.py` to make an alias for it available in Jupyter,
 - Adjust `tangostationcontrol/tangostationcontrol/devices/boot.py` to add the device to the station initialisation sequence,
 - Add to `docker-compose/` to create a YaML file to start the device in a docker container. NOTE: it needs a unique 57xx port assigned,
+                            current _unused_ port value: 5715
 - Adjust `tangostationcontrol/setup.cfg` to add an entry point for the device in the package installation,
 - Add to `tangostationcontrol/tangostationcontrol/integration_test/default/devices/` to add an integration test,
 - Adjust `sbin/run_integration_test.sh` to have the device started when running the integration tests,
 - Adjust `.gitlab-ci.yml` to add the device to the `docker_build_image_all` step and to create a `docker_build_image_device_XXX` step, 
-- Add to `docs/source/devices/` to mention the device in the end-user documentation.
-- Adjust `docs/source/index.rst` to include the newly created file in `docs/source/devices/`.
+- Add to `tangostationcontrol/docs/source/devices/` to mention the device in the end-user documentation.
+- Adjust `tangostationcontrol/docs/source/index.rst` to include the newly created file in `docs/source/devices/`.
 
diff --git a/tangostationcontrol/tangostationcontrol/devices/boot.py b/tangostationcontrol/tangostationcontrol/devices/boot.py
index 47dcc3741..450df1b6f 100644
--- a/tangostationcontrol/tangostationcontrol/devices/boot.py
+++ b/tangostationcontrol/tangostationcontrol/devices/boot.py
@@ -233,6 +233,7 @@ class Boot(lofar_device):
         dtype='DevVarStringArray',
         mandatory=False,
         default_value=["STAT/Docker/1", # Docker controls the device containers, so it goes before anything else
+                       "STAT/PDU/1",  # PDU boot early to detect power delivery failure as fast as possible
                        "STAT/APSPU/1",  # APS Power Units control other hardware we want to initialise
                        "STAT/APSCT/1",
                        "STAT/RECV/1",   # RCUs are input for SDP, so initialise them first
diff --git a/tangostationcontrol/tangostationcontrol/devices/pdu.py b/tangostationcontrol/tangostationcontrol/devices/pdu.py
new file mode 100644
index 000000000..88a1af8d1
--- /dev/null
+++ b/tangostationcontrol/tangostationcontrol/devices/pdu.py
@@ -0,0 +1,57 @@
+# -*- coding: utf-8 -*-
+#
+# Distributed under the terms of the APACHE license.
+# See LICENSE.txt for more info.
+
+""" PDU Device Server for LOFAR2.0
+
+"""
+
+# Additional import
+from tangostationcontrol.common.entrypoint import entry
+from tangostationcontrol.devices.lofar_device import lofar_device
+from tangostationcontrol.common.lofar_logging import device_logging_to_python, log_exceptions
+
+import logging
+
+logger = logging.getLogger()
+
+__all__ = ["PDU", "main"]
+
+
+@device_logging_to_python()
+class PDU(lofar_device):
+    # -----------------
+    # Device Properties
+    # -----------------
+
+    # ----------
+    # Attributes
+    # ----------
+
+    # --------
+    # overloaded functions
+    # --------
+
+    def init_device(self):
+        super().init_device()
+
+    @log_exceptions()
+    def configure_for_initialise(self):
+        super().configure_for_initialise()
+
+    @log_exceptions()
+    def configure_for_on(self):
+        super().configure_for_on()
+
+    @log_exceptions()
+    def configure_for_off(self):
+        super().configure_for_off()
+
+
+# ----------
+# Run server
+# ----------
+def main(**kwargs):
+    """Main function of the PDU module."""
+    return entry(PDU, **kwargs)
diff --git a/tangostationcontrol/tangostationcontrol/integration_test/default/devices/test_device_pdu.py b/tangostationcontrol/tangostationcontrol/integration_test/default/devices/test_device_pdu.py
new file mode 100644
index 000000000..8ffefd83a
--- /dev/null
+++ b/tangostationcontrol/tangostationcontrol/integration_test/default/devices/test_device_pdu.py
@@ -0,0 +1,17 @@
+# -*- coding: utf-8 -*-
+#
+# This file is part of the LOFAR 2.0 Station Software
+#
+#
+#
+# Distributed under the terms of the APACHE license.
+# See LICENSE.txt for more info.
+
+from .base import AbstractTestBases
+
+
+class TestDevicePDU(AbstractTestBases.TestDeviceBase):
+
+    def setUp(self):
+        """Intentionally recreate the device object in each test"""
+        super().setUp("STAT/PDU/1")
-- 
GitLab