Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions DEVELOPMENT.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# APIS Development Environment Setup

## Prerequisites
* Docker & Docker Compose
* Git

## Setup Steps

1. **Clone Dependencies**
Ensure all sub-repositories are cloned into the root structure:
* apis-bom
* apis-common
* apis-main
* apis-web
* apis-emulator

2. **Start Sandbox Environment**
```bash
docker-compose up --build
```

3. **Services**
* **Dashboard (Web)**: Endpoints available at `localhost:43900` and `localhost:9999`.
* **Grid Master (Main)**: Running autonomously.
* **Hardware (Emulator)**: Simulating 4 battery units.

## Testing APIs
See `validate_env.py` for automated testing.

* **Energy Balance Check**: `GET http://localhost:43900/get/log`
* **Power Allocation**: `POST http://localhost:9999/deal`

## Troubleshooting
* **Build Fails**: Ensure `apis-bom` and `apis-common` are using version 3.0.0 in `pom.xml`.
* **Ports in Use**: Stop native services if running (`make stop`).
* **Hazelcast Errors**: Docker Compose uses `hostname -i` to discover peers. Ensure firewall allows port 5701.
49 changes: 49 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Stage 1: Build All Java Components
FROM maven:3.9-eclipse-temurin-11 AS builder
WORKDIR /build

# Copy and install BOM
COPY apis-bom /build/apis-bom
# APPLY PATCH: Fix dependency versions
COPY patches/apis-bom-pom.xml /build/apis-bom/pom.xml
WORKDIR /build/apis-bom
RUN mvn install -DskipTests

# Copy and install Common
COPY apis-common /build/apis-common
# APPLY PATCH: Fix dependency versions
COPY patches/apis-common-pom.xml /build/apis-common/pom.xml
WORKDIR /build/apis-common
RUN mvn install -DskipTests

# Build apis-main
COPY apis-main /build/apis-main
WORKDIR /build/apis-main
RUN mvn package -DskipTests

# Build apis-web
COPY apis-web /build/apis-web
WORKDIR /build/apis-web
RUN mvn package -DskipTests

# Stage 2: APIS Main Runtime
FROM eclipse-temurin:11-jre AS apis-main
WORKDIR /app
COPY --from=builder /build/apis-main/target/apis-main-*-fat.jar /app/apis-main.jar
# Default config files will be mounted via Volume or use defaults
COPY apis-main/exe/logging.properties /app/
COPY apis-main/exe/config.json /app/
COPY apis-main/exe/cluster.xml /app/
ENV CLUSTER_HOST_IP=127.0.0.1
# Entrypoint to dynamically set cluster host IP
CMD ["sh", "-c", "java -Djava.net.preferIPv4Stack=true -Djava.util.logging.config.file=./logging.properties -Dvertx.hazelcast.config=./cluster.xml -jar apis-main.jar -conf ./config.json -cluster -cluster-host $(hostname -i)"]

# Stage 3: APIS Web Runtime
FROM eclipse-temurin:11-jre AS apis-web
WORKDIR /app
COPY --from=builder /build/apis-web/target/apis-web-*-fat.jar /app/apis-web.jar
COPY apis-web/exe/logging.properties /app/
COPY apis-web/exe/config.json /app/
COPY apis-web/exe/cluster.xml /app/
ENV CLUSTER_HOST_IP=127.0.0.1
CMD ["sh", "-c", "java -Djava.net.preferIPv4Stack=true -Djava.util.logging.config.file=./logging.properties -jar apis-web.jar -conf ./config.json -cp ./ -cluster -cluster-host $(hostname -i)"]
40 changes: 40 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
version: '3.8'

services:
mongodb:
image: mongo:4.4
container_name: apis-mongo
ports:
- "27017:27017"

apis-main:
build:
context: .
dockerfile: Dockerfile
target: apis-main
container_name: apis-main
depends_on:
- mongodb
ports:
- "5701:5701" # Hazelcast

apis-web:
build:
context: .
dockerfile: Dockerfile
target: apis-web
container_name: apis-web
depends_on:
- apis-main
ports:
- "43830:43830" # Budo Emulator
- "43900:43900" # Emulator Emulator
- "9999:9999" # API Server (Deals/Errors)

apis-emulator:
build:
context: .
dockerfile: emulator.Dockerfile
container_name: apis-emulator
command: python3 startEmul.py 4
network_mode: service:apis-main # Run in same net namespace to simulate local Hardware connection if using multicast, simplifed for now
8 changes: 8 additions & 0 deletions emulator.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
FROM python:3.9-slim
WORKDIR /app
COPY apis-emulator/requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY apis-emulator/ .
# Fix line endings if cloned on Windows
RUN apt-get update && apt-get install -y dos2unix && dos2unix *.py *.sh
CMD ["python3", "startEmul.py", "4"]
58 changes: 58 additions & 0 deletions patches/apis-bom-pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<vertx.version>3.7.1</vertx.version>
<junit.version>4.12</junit.version>
<commons-io.version>2.4</commons-io.version>
</properties>

<groupId>jp.co.sony.csl.dcoes.apis</groupId>
<artifactId>apis-bom</artifactId>
<version>3.0.0</version>
<packaging>pom</packaging>

<name>APIS BOM</name>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>jp.co.sony.csl.dcoes.apis</groupId>
<artifactId>apis-common</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-core</artifactId>
<version>${vertx.version}</version>
</dependency>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-mongo-client</artifactId>
<version>${vertx.version}</version>
</dependency>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-hazelcast</artifactId>
<version>${vertx.version}</version>
</dependency>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-unit</artifactId>
<version>${vertx.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>${commons-io.version}</version>
</dependency>
</dependencies>
</dependencyManagement>

</project>
59 changes: 59 additions & 0 deletions patches/apis-common-pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<vertx.version>3.7.1</vertx.version>
</properties>

<groupId>jp.co.sony.csl.dcoes.apis</groupId>
<artifactId>apis-common</artifactId>
<version>3.0.0</version>

<name>APIS COMMON</name>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>jp.co.sony.csl.dcoes.apis</groupId>
<artifactId>apis-bom</artifactId>
<version>${project.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-core</artifactId>
</dependency>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-unit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.11.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
55 changes: 55 additions & 0 deletions validate_env.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import requests
import json
import time

print("Waiting for services to start...")
time.sleep(10) # Simple wait, or use retry logic

# 1. Energy Balance Check (Get Hardware Logs)
try:
print("\n--- Testing Energy Balance Check API ---")
url = "http://localhost:43900/get/log"
print(f"GET {url}")
response = requests.get(url, timeout=5)
print(f"Status: {response.status_code}")
if response.status_code == 200:
data = response.json()
print("Success! Received hardware telemetry.")
# Check first key (usually 'apis' or specific unit ID)
print(f"Keys: {list(data.keys())[:3]}...")
else:
print("Failed to get logs.")
except Exception as e:
print(f"Error: {e}")

# 2. Power Allocation (Deal Generation)
try:
print("\n--- Testing Power Allocation API ---")
url = "http://localhost:9999/deal"

# Mock OpenPAYGO Deal Payload (Example shape based on code)
payload = {
"dealId": "test-deal-001",
"requestUnitId": "unit1",
"acceptUnitId": "unit2",
"amountWh": 100,
"type": "charge"
}

# The API expects form-data with a 'json' field containing the stringified JSON
files = {
'json': (None, json.dumps(payload))
}

print(f"POST {url} with payload: {payload}")
response = requests.post(url, files=files, timeout=5)
print(f"Status: {response.status_code}")
print(f"Response: {response.text}")

if response.status_code == 200:
print("Success! Deal request submitted.")
else:
print("Deal submission failed.")

except Exception as e:
print(f"Error: {e}")