czechlight-cfg-fs: add YANG install script

Let's move cla-sysrepo's install-yang shell script to this package.
Normally, all the packages would have installed these YANGs by
themselves, but because there's no shared state in rootfs, only
in /cfg, so that's where all these modifications belong

Change-Id: Ia5f4ade46098d66a92d940366659e44f9ea74456
diff --git a/package/czechlight-cfg-fs/Config.in b/package/czechlight-cfg-fs/Config.in
index e47d42f..40e6409 100644
--- a/package/czechlight-cfg-fs/Config.in
+++ b/package/czechlight-cfg-fs/Config.in
@@ -1,11 +1,14 @@
 config BR2_PACKAGE_CZECHLIGHT_CFG_FS
-	bool "Prepare persistent /cfg partition and /etc overlay"
+	bool "Prepare persistent /cfg partition and /etc overlay. Install required YANG models."
 	depends on BR2_INIT_SYSTEMD
+	depends on BR2_PACKAGE_NETOPEER2
 	help
 	  This is required for RAUC to work properly.  It creates a blank FS
 	  image, configures systemd to mount it, and ensures that its contents
 	  get restored to /etc at boot.  The /etc is set up as a RW overlay on
 	  top of a read-only rootfs.
+	  This takes care of installing YANG files used by czechlight system
+	  daemons into sysrepo.
 
 if BR2_PACKAGE_CZECHLIGHT_CFG_FS
 
diff --git a/package/czechlight-cfg-fs/czechlight-cfg-fs.mk b/package/czechlight-cfg-fs/czechlight-cfg-fs.mk
index bf000bd..4d79447 100644
--- a/package/czechlight-cfg-fs/czechlight-cfg-fs.mk
+++ b/package/czechlight-cfg-fs/czechlight-cfg-fs.mk
@@ -1,5 +1,5 @@
 CZECHLIGHT_CFG_FS_INSTALL_IMAGES = YES
-CZECHLIGHT_CFG_FS_DEPENDENCIES = host-e2fsprogs host-libyang netopeer2
+CZECHLIGHT_CFG_FS_DEPENDENCIES = host-e2fsprogs host-libyang netopeer2 systemd
 
 CZECHLIGHT_CFG_FS_LOCATION = $(BINARIES_DIR)/cfg.ext4
 
@@ -25,6 +25,11 @@
 		$(BR2_EXTERNAL_CZECHLIGHT_PATH)/package/czechlight-cfg-fs/init-czechlight.sh \
 		$(TARGET_DIR)/sbin/init-czechlight.sh
 	$(INSTALL) -D -m 0755 $(@D)/czechlight-random-seed $(TARGET_DIR)/sbin/czechlight-random-seed
+	$(INSTALL) -m 0755 -t $(TARGET_DIR)/usr/bin/ \
+		$(BR2_EXTERNAL_CZECHLIGHT_PATH)/package/czechlight-cfg-fs/czechlight-install-yang.sh
+	$(INSTALL) -D -m 0644 -t $(TARGET_DIR)/usr/lib/systemd/system/ \
+		$(BR2_EXTERNAL_CZECHLIGHT_PATH)/package/czechlight-cfg-fs/czechlight-install-yang.service
+	ln -sf ../czechlight-install-yang.service $(TARGET_DIR)/usr/lib/systemd/system/multi-user.target.wants/
 	mkdir -p $(TARGET_DIR)/cfg
 	$(INSTALL) -D -m 0644 \
 	    --target-directory $(TARGET_DIR)/usr/lib/systemd/system/ \
diff --git a/package/czechlight-cfg-fs/czechlight-install-yang.service b/package/czechlight-cfg-fs/czechlight-install-yang.service
new file mode 100644
index 0000000..4588f71
--- /dev/null
+++ b/package/czechlight-cfg-fs/czechlight-install-yang.service
@@ -0,0 +1,14 @@
+[Unit]
+Description=Install CzechLight YANG models
+After=netopeer2-install-yang.service cfg.mount
+Requires=netopeer2-install-yang.service cfg.mount
+Before=netopeer2.service cfg-restore-sysrepo.service
+
+[Service]
+Type=oneshot
+RemainAfterExit=yes
+ExecStart=/bin/bash /usr/bin/czechlight-install-yang.sh
+Group=sysrepo
+
+[Install]
+WantedBy=multi-user.target
diff --git a/package/czechlight-cfg-fs/czechlight-install-yang.sh b/package/czechlight-cfg-fs/czechlight-install-yang.sh
new file mode 100755
index 0000000..d8a11ed
--- /dev/null
+++ b/package/czechlight-cfg-fs/czechlight-install-yang.sh
@@ -0,0 +1,97 @@
+#!/bin/bash
+
+set -ex
+
+YANG_ROADM=0
+YANG_COHERENT=0
+YANG_INLINE=0
+YANG_CALIBRATION=0
+
+CLA_YANG=/usr/share/cla-sysrepo/yang
+VELIA_YANG=/usr/share/velia/yang
+
+for ARG in $(cat /proc/cmdline); do
+    case "${ARG}" in
+        czechlight=*)
+            CZECHLIGHT="${ARG:11}"
+            ;;
+    esac
+done
+
+INITIAL_DATA=${CZECHLIGHT%%-g2}
+
+case "${CZECHLIGHT}" in
+    sdn-roadm-line*)
+        YANG_ROADM=1
+        WITH_FEATURE=hw-line-9
+        ;;
+    sdn-roadm-add-drop*)
+        YANG_ROADM=1
+        WITH_FEATURE=hw-add-drop-20
+        ;;
+    sdn-roadm-hires-add-drop*)
+        YANG_ROADM=1
+        WITH_FEATURE="hw-add-drop-20 pre-wss-ocm"
+        INITIAL_DATA=sdn-roadm-add-drop
+        ;;
+    sdn-roadm-coherent-a-d*)
+        YANG_COHERENT=1
+        ;;
+    sdn-inline*)
+        YANG_INLINE=1
+        ;;
+    calibration-box)
+        YANG_CALIBRATION=1
+        ;;
+esac
+
+sysrepoctl --search-dirs ${CLA_YANG} --install ${CLA_YANG}/iana-hardware@2018-03-13.yang
+sysrepoctl --search-dirs ${CLA_YANG} --install ${CLA_YANG}/ietf-hardware@2018-03-13.yang
+sysrepoctl --change ietf-hardware --permissions 0660 --enable-feature hardware-sensor
+
+if [[ ${YANG_ROADM} == 1 ]]; then
+    FEATURE_ARGS=""
+    if [[ ${WITH_FEATURE} ]]; then
+        for FEATURE in ${WITH_FEATURE}; do
+            FEATURE_ARGS="${FEATURE_ARGS} --enable-feature ${FEATURE}"
+        done
+    fi
+    sysrepoctl --search-dirs ${CLA_YANG} --install ${CLA_YANG}/czechlight-roadm-device@2021-03-05.yang ${FEATURE_ARGS} --permissions 0660
+    sysrepocfg --datastore=startup --format=json --module=czechlight-roadm-device --import="${CLA_YANG}/${INITIAL_DATA}.json"
+fi
+
+if [[ ${YANG_COHERENT} == 1 ]]; then
+    sysrepoctl --search-dirs ${CLA_YANG} --install ${CLA_YANG}/czechlight-coherent-add-drop@2021-03-05.yang --permissions 0660
+    sysrepocfg --datastore=startup --format=json --module=czechlight-coherent-add-drop --new-data="${CLA_YANG}/${INITIAL_DATA}.json"
+    sysrepoctl --change czechlight-coherent-add-drop --permissions 0660
+fi
+
+if [[ ${YANG_INLINE} == 1 ]]; then
+    sysrepoctl --search-dirs ${CLA_YANG} --install ${CLA_YANG}/czechlight-inline-amp@2021-03-05.yang --permissions 0660
+    sysrepocfg --datastore=startup --format=json --module=czechlight-inline-amp --import="${CLA_YANG}/${INITIAL_DATA}.json"
+fi
+
+if [[ ${YANG_CALIBRATION} == 1 ]]; then
+    sysrepoctl --search-dirs ${CLA_YANG} --install ${CLA_YANG}/czechlight-calibration-device@2019-06-25.yang --permissions 0660
+    sysrepocfg --datastore=startup --format=json --module=czechlight-calibration-device --import="${CLA_YANG}/${INITIAL_DATA}.json"
+fi
+
+sysrepoctl --search-dirs ${VELIA_YANG} --install ${VELIA_YANG}/ietf-system@2014-08-06.yang --permissions 0660
+
+sysrepoctl --search-dirs ${VELIA_YANG} --install ${VELIA_YANG}/czechlight-lldp@2020-11-04.yang --permissions 0660
+
+sysrepoctl --search-dirs ${VELIA_YANG} --install ${VELIA_YANG}/czechlight-system@2021-01-13.yang --permissions 0660
+
+sysrepoctl --search-dirs ${VELIA_YANG} --install ${VELIA_YANG}/iana-if-type@2017-01-19.yang
+sysrepoctl --search-dirs ${VELIA_YANG} --install ${VELIA_YANG}/ietf-interfaces@2018-02-20.yang --permissions 0660
+sysrepoctl --search-dirs ${VELIA_YANG} --install ${VELIA_YANG}/ietf-ip@2018-02-22.yang --permissions 0660
+sysrepoctl --search-dirs ${VELIA_YANG} --install ${VELIA_YANG}/ietf-routing@2018-03-13.yang --permissions 0660
+sysrepoctl --search-dirs ${VELIA_YANG} --install ${VELIA_YANG}/ietf-ipv4-unicast-routing@2018-03-13.yang --permissions 0660
+sysrepoctl --search-dirs ${VELIA_YANG} --install ${VELIA_YANG}/ietf-ipv6-unicast-routing@2018-03-13.yang --permissions 0660
+sysrepoctl --search-dirs ${VELIA_YANG} --install ${VELIA_YANG}/czechlight-network@2021-02-22.yang --permissions 0660
+
+sysrepoctl --search-dirs ${VELIA_YANG} --install ${VELIA_YANG}/czechlight-firewall@2021-01-25.yang --permissions 0600
+sysrepoctl --change ietf-access-control-list --enable-feature match-on-eth --enable-feature eth --enable-feature match-on-ipv4 --enable-feature ipv4 --enable-feature match-on-ipv6 --enable-feature ipv6 --enable-feature mixed-eth-ipv4-ipv6
+
+# If not do not copy here from startup -> running, running might be stale.
+sysrepocfg -C startup