Skip to content

Commit

Permalink
Rework all applications to use ArgumentParser
Browse files Browse the repository at this point in the history
We already used argument parser in toaster-multiple-devices, and
using different logic in other applications does not make sense.
Standardize all applications to use ArgumentParser to improve future
development and add -help function.

This change is also crucial in order to add persistence.

JIRA: LIGHTY-353
Signed-off-by: tobias.pobocik <[email protected]>
  • Loading branch information
Tobianas committed Jan 30, 2025
1 parent d16ced3 commit eb72267
Show file tree
Hide file tree
Showing 10 changed files with 285 additions and 100 deletions.
95 changes: 95 additions & 0 deletions .github/workflows/publish-simulator.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
name: Publish netconf-simulator
on:
workflow_dispatch:
inputs:
version:
description: Desired version of published docker image & helm charts, e.g. "XX.YY.ZZ"
required: true
image-tag-latest:
description: Should be this docker labeled with tag latest? Enter `true` if the tag `latest` should be added for image.
default: "true"
required: true
publish-access-key:
description: The branch, tag or SHA to checkout. (if "default" the selected branch will be used)
default: default
required: true

jobs:
publish-docker-helm:
runs-on: ubuntu-latest
defaults:
run:
shell: bash
env:
IMAGE_NAME: "lighty-network-topology-device"
PUBLISH_ACCESS_KEY: ${{ secrets.MM_PKG_WRITE }}
name: "Publish netconf-simulator docker image. Checkout-ref: ${{ github.event.inputs.publish-access-key }}"
steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Set up JDK 21
uses: actions/setup-java@v3
with:
java-version: 21
distribution: 'temurin'

- name: Build lighty-netconf-simulator
shell: bash
run: |
echo "Building lighty-netconf-simulator..."
mvn install -DskipTests
- name: Run docker:build...
shell: bash
run: |
echo "Image name set to:" ${{ env.IMAGE_NAME }}
DOCKER_IMAGE_NAME=${{env.IMAGE_NAME}}
DOCKER_IMAGE_NAME_TAG=$(echo $DOCKER_IMAGE_NAME:${{ inputs.version }})
DOCKER_IMAGE_NAME_GHCR=$(echo ghcr.io/pantheontech/${{ env.IMAGE_NAME }})
DOCKER_IMAGE_NAME_GHCR_TAG=$(echo $DOCKER_IMAGE_NAME_GHCR:${{ inputs.version }})
echo "cd examples/devices/lighty-network-topology-device"
cd examples/devices/lighty-network-topology-device
mvn docker:build
echo "Docker image tag:" $DOCKER_IMAGE_NAME_GHCR $DOCKER_IMAGE_NAME_GHCR_TAG
docker tag $DOCKER_IMAGE_NAME $DOCKER_IMAGE_NAME_TAG
if [ "${{ inputs.image-tag-latest }}" = 'true' ]; then
docker tag $DOCKER_IMAGE_NAME $DOCKER_IMAGE_NAME:latest
fi
docker images | grep $DOCKER_IMAGE_NAME
- name: List docker images
shell: bash
run: |
docker images
- name: Docker log in (ghcr.io)
shell: bash
run: |
echo ${{ inputs.publish-access-key}} | docker login --username ${{ github.actor }} --password-stdin ghcr.io
- name: Publish docker image (ghcr.io)
shell: bash
run: |
docker push $DOCKER_IMAGE_NAME_GHCR_TAG
if [ "${{ inputs.image-tag-latest }}" = 'true' ]; then
docker push $DOCKER_IMAGE_NAME_GHCR:latest
fi
- name: Check if docker image is pullable (ghcr.io)
shell: bash
run: |
docker rmi $DOCKER_IMAGE_NAME_GHCR_TAG
docker pull $DOCKER_IMAGE_NAME_GHCR_TAG
- name: Install yq (yaml processor)
shell: bash
run: |
sudo snap install yq
- name: Set image.name, image.version in values.yaml of helm chart
shell: bash
run: |
yq eval '.image.name="ghcr.io/pantheontech/$'"IMAGE_NAME"'" | .image.version="'${{ inputs.version }}'"' "${{ inputs.app-helm-values-path }}" -i
- name: Print values.yaml
shell: bash
run: |
cat -A ${{ inputs.app-helm-values-path }}
45 changes: 45 additions & 0 deletions examples/devices/lighty-network-topology-device/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@
<properties>
<application.main.class>io.lighty.netconf.device.topology.Main</application.main.class>
<application.attach.zip>true</application.attach.zip>
<image.name>lighty-network-topology-device</image.name>
<lighty.app.name>lighty-network-topology-device-${project.version}</lighty.app.name>
<lighty.app.zip>${lighty.app.name}-bin.zip</lighty.app.zip>
<lighty.app.jar>${lighty.app.name}.jar</lighty.app.jar>
</properties>

<dependencies>
Expand Down Expand Up @@ -56,4 +60,45 @@
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>io.fabric8</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>0.45.1</version>
<configuration>
<images>
<image>
<name>${image.name}</name>
<build>
<contextDir>${project.basedir}</contextDir>
<dockerFile>src/main/docker/Dockerfile</dockerFile>
<assembly>
<inline>
<fileSets>
<fileSet>
<directory>${project.build.directory}</directory>
<includes>
<include>*.jar</include>
</includes>
<outputDirectory>/</outputDirectory>
</fileSet>
<fileSet>
<directory>${project.build.directory}</directory>
<includes>
<include>*.zip</include>
</includes>
<outputDirectory>/</outputDirectory>
</fileSet>
</fileSets>
</inline>
</assembly>
<cleanup>try</cleanup>
</build>
</image>
</images>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
FROM eclipse-temurin:21-jre-alpine

WORKDIR /lighty-netconf-simulator

COPY maven/${lighty.app.zip} ./

RUN unzip ${lighty.app.zip} && \
rm ${lighty.app.zip}

WORKDIR /lighty-netconf-simulator/${lighty.app.name}

ENTRYPOINT ["java", "-jar", "${lighty.app.jar}"]
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,12 @@
import io.lighty.netconf.device.topology.processors.NetworkTopologyServiceRemoveNodeProcessor;
import io.lighty.netconf.device.topology.processors.NetworkTopologyServiceRemoveTopologyProcessor;
import io.lighty.netconf.device.topology.rpcs.NetworkTopologyServiceImpl;
import io.lighty.netconf.device.utils.ArgumentParser;
import io.lighty.netconf.device.utils.ModelUtils;
import java.io.File;
import java.util.List;
import java.util.Set;
import net.sourceforge.argparse4j.inf.Namespace;
import org.opendaylight.mdsal.binding.api.DataBroker;
import org.opendaylight.yangtools.binding.meta.YangModuleInfo;
import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
Expand All @@ -39,13 +42,18 @@ public final class Main {

public static void main(String[] args) {
Main app = new Main();
app.start(args, true, true, true);
app.start(args, true);
}

@SuppressFBWarnings({"SLF4J_SIGN_ONLY_FORMAT", "OBL_UNSATISFIED_OBLIGATION"})
public void start(String[] args, boolean registerShutdownHook, final boolean initDatastore,
final boolean saveDatastore) {
int port = getPortFromArgs(args);
public void start(String[] args, boolean registerShutdownHook) {
final ArgumentParser argumentParser = new ArgumentParser();
final Namespace parseArguments = argumentParser.parseArguments(args);

//parameters are stored as string list
final List<?> portList = parseArguments.get("port");
final int port = Integer.parseInt(String.valueOf(portList.getFirst()));

LOG.info("Lighty-Network-Topology device started {}", port);
LOG.info(" _______ __ ________ .__");
LOG.info(" \\ \\ _____/ |\\______ \\ _______ _|__| ____ ____");
Expand All @@ -67,18 +75,20 @@ public void start(String[] args, boolean registerShutdownHook, final boolean ini
//2. Initialize DataStores
File operationalFile = null;
File configFile = null;
final String configDir = System.getProperty("config.dir",
"./examples/devices/lighty-network-topology-device/src/main/resources");
if (initDatastore) {
if (argumentParser.isInitDatastore()) {
final List initDatastoreList = parseArguments.get("init_datastore");
final String configDir = initDatastoreList.getFirst().toString();
LOG.info("Using initial datastore from: {}", configDir);
operationalFile = new File(
configDir, "initial-network-topo-operational-datastore.xml");
configDir, "/initial-network-topo-operational-datastore.xml");
configFile = new File(
configDir, "initial-network-topo-config-datastore.xml");
configDir, "/initial-network-topo-config-datastore.xml");
}
if (saveDatastore) {
operationalFile = new File(configDir, "initial-network-topo-operational-datastore.xml");
configFile = new File(configDir, "initial-network-topo-config-datastore.xml");
if (argumentParser.isSaveDatastore()) {
final List outputDatastoreList = parseArguments.get("init_datastore");
final String outDir = outputDatastoreList.getFirst().toString();
operationalFile = new File(outDir, "/initial-network-topo-operational-datastore.xml");
configFile = new File(outDir, "/initial-network-topo-config-datastore.xml");
}

//3. Initialize RPCs
Expand Down Expand Up @@ -169,13 +179,4 @@ public void execute() {
}
}

@SuppressWarnings("checkstyle:IllegalCatch")
private static int getPortFromArgs(String[] args) {
try {
return Integer.parseInt(args[0]);
} catch (Exception e) {
return 17830;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public class DeviceTest {
@BeforeAll
public static void setUpClass() {
deviceSimulator = new Main();
deviceSimulator.start(new String[]{DEVICE_SIMULATOR_PORT + ""}, false, false, false);
deviceSimulator.start(new String[]{"-p" + DEVICE_SIMULATOR_PORT}, false);

dispatcher = new NetconfClientFactoryImpl(new DefaultNetconfTimer());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@
import io.lighty.netconf.device.toaster.processors.ToasterServiceMakeToastProcessor;
import io.lighty.netconf.device.toaster.processors.ToasterServiceRestockToasterProcessor;
import io.lighty.netconf.device.toaster.rpcs.ToasterServiceImpl;
import io.lighty.netconf.device.utils.ArgumentParser;
import io.lighty.netconf.device.utils.ModelUtils;
import java.io.File;
import java.util.List;
import java.util.Set;
import net.sourceforge.argparse4j.inf.Namespace;
import org.opendaylight.yangtools.binding.meta.YangModuleInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -29,17 +32,22 @@ public class Main {

public static void main(String[] args) {
Main app = new Main();
app.start(args, true, true, true);
app.start(args, true);
}

public void start(String[] args) {
start(args, false, true, true);
start(args, false);
}

@SuppressFBWarnings({"SLF4J_SIGN_ONLY_FORMAT", "OBL_UNSATISFIED_OBLIGATION"})
public void start(String[] args, boolean registerShutdownHook, final boolean initDatastore,
final boolean saveDatastore) {
int port = getPortFromArgs(args);
public void start(String[] args, boolean registerShutdownHook) {
final ArgumentParser argumentParser = new ArgumentParser();
final Namespace parseArguments = argumentParser.parseArguments(args);

//parameters are stored as string list
final List<?> portList = parseArguments.get("port");
final int port = Integer.parseInt(String.valueOf(portList.getFirst()));

LOG.info("Lighty-Toaster device started at port {}", port);
LOG.info("___________ __ ________ .__");
LOG.info("\\__ ___/___ _______/ |_______\\______ \\ _______ _|__| ____ ____");
Expand Down Expand Up @@ -81,14 +89,14 @@ public void start(String[] args, boolean registerShutdownHook, final boolean ini
File configFile = null;
final String configDir = System.getProperty("config.dir",
"./examples/devices/lighty-toaster-device/src/main/resources");
if (initDatastore) {
if (argumentParser.isInitDatastore()) {
LOG.info("Using initial datastore from: {}", configDir);
operationalFile = new File(
configDir, "initial-toaster-operational-datastore.xml");
configFile = new File(
configDir, "initial-toaster-config-datastore.xml");
}
if (saveDatastore) {
if (argumentParser.isSaveDatastore()) {
operationalFile = new File(configDir, "initial-toaster-operational-datastore.xml");
configFile = new File(configDir, "initial-toaster-config-datastore.xml");
}
Expand Down Expand Up @@ -144,14 +152,4 @@ public void execute() {
}
}
}

@SuppressWarnings("checkstyle:IllegalCatch")
private static int getPortFromArgs(String[] args) {
try {
return Integer.parseInt(args[0]);
} catch (Exception e) {
return 17830;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public class ToasterDeviceTest {
@BeforeAll
public static void setUpClass() {
deviceSimulator = new Main();
deviceSimulator.start(new String[]{DEVICE_SIMULATOR_PORT + ""}, false, false, false);
deviceSimulator.start(new String[]{"-p" + DEVICE_SIMULATOR_PORT}, false);
dispatcher = new NetconfClientFactoryImpl(new DefaultNetconfTimer());
}

Expand Down
Loading

0 comments on commit eb72267

Please sign in to comment.