Skip to content

Commit 415e1e4

Browse files
authored
release v0.1.0
First release of the Flex Net Sim Backend API. Built with Flask, this unofficial API powers the backend of the Flex Net Sim Playground web app. Between the changes: - Added networks: Cost239, EuroCore, GermanNet, UKNet. - Implemented K routes parameter (default 3, max 6). - Added ExactFit algorithm. - Created /help endpoint for API documentation. - Updated README.md with /help endpoint and parameter details. - Renamed 'bitrate' to "fixed-grid", added 'flex-rate' option. - Updated .ignore. - Added versioning. - Added link to README.
2 parents dfa8945 + cc3acb9 commit 415e1e4

18 files changed

+53627
-2299
lines changed

.github/workflows/gke-cd.yml

Lines changed: 48 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: Deploy FNS API
1+
name: Deploy FNS API to Cloud Run (Separated Jobs)
22

33
on:
44
push:
@@ -9,25 +9,54 @@ on:
99
- master
1010

1111
jobs:
12-
deploy:
12+
build-and-push-image:
1313
name: Build and Push Docker Image
1414
runs-on: ubuntu-latest
1515
steps:
16+
- name: Checkout code
17+
uses: actions/checkout@v2
1618

17-
- name: Checkout code
18-
uses: actions/checkout@v2
19-
20-
- name: Set up Google CLoud CLI
21-
uses: google-github-actions/setup-gcloud@v0
22-
with:
23-
service_account_key: ${{ secrets.GOOGLE_APPLICATION_CREDENTIALS }}
24-
project_id: ${{ secrets.GOOGLE_PROJECT }}
25-
export_default_credentials: true
26-
27-
- name: Build and push Docker image
28-
env:
29-
GOOGLE_PROJECT: ${{ secrets.GOOGLE_PROJECT }}
30-
run: |
31-
gcloud auth configure-docker us-central1-docker.pkg.dev
32-
docker build -t us-central1-docker.pkg.dev/$GOOGLE_PROJECT/flex-net-sim-repo/fns-api:latest .
33-
docker push us-central1-docker.pkg.dev/$GOOGLE_PROJECT/flex-net-sim-repo/fns-api:latest
19+
- name: Google Cloud Auth # Authenticate gcloud CLI
20+
uses: google-github-actions/auth@v2
21+
with:
22+
credentials_json: ${{ secrets.GOOGLE_APPLICATION_CREDENTIALS }}
23+
24+
- name: Set up Google Cloud CLI # Set up gcloud CLI and Docker
25+
uses: google-github-actions/setup-gcloud@v2
26+
with:
27+
version: latest
28+
project_id: ${{ secrets.GOOGLE_PROJECT }}
29+
30+
- name: Configure Docker to push to Artifact Registry # Configure docker auth
31+
run: |
32+
gcloud auth configure-docker us-central1-docker.pkg.dev
33+
34+
- name: Build and Push Docker image to Artifact Registry # Build and push image
35+
env:
36+
GOOGLE_PROJECT: ${{ secrets.GOOGLE_PROJECT }}
37+
IMAGE_NAME: us-central1-docker.pkg.dev/${{ secrets.GOOGLE_PROJECT }}/flex-net-sim-repo/fns-api
38+
IMAGE_TAG: latest
39+
run: |
40+
docker build -t $IMAGE_NAME:$IMAGE_TAG .
41+
docker push $IMAGE_NAME:$IMAGE_TAG
42+
43+
deploy-to-cloud-run:
44+
name: Deploy to Cloud Run
45+
runs-on: ubuntu-latest
46+
needs: build-and-push-image # Ensure this job runs after build-and-push-image
47+
steps:
48+
- name: Checkout code (again, if needed for deploy steps - optional)
49+
uses: actions/checkout@v2
50+
51+
- name: Google Cloud Auth # Authenticate gcloud CLI
52+
uses: google-github-actions/auth@v2
53+
with:
54+
credentials_json: ${{ secrets.GOOGLE_APPLICATION_CREDENTIALS }}
55+
56+
- name: Deploy to Cloud Run
57+
uses: google-github-actions/deploy-cloudrun@v1
58+
with:
59+
image: us-central1-docker.pkg.dev/${{ secrets.GOOGLE_PROJECT }}/flex-net-sim-repo/fns-api:latest # Use the same image as built
60+
service: fns-api-cloud-run
61+
region: us-central1
62+
project_id: ${{ secrets.GOOGLE_PROJECT }}

.github/workflows/google.yml

Lines changed: 0 additions & 118 deletions
This file was deleted.

.gitignore

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,8 @@
11
__pycache__
2-
simulation.out
2+
simulation.out
3+
4+
# Exclude local IDE settings
5+
.vscode/
6+
7+
# macOS
8+
**/.DS_Store

README.md

Lines changed: 104 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
# Flex Net Sim Backend API
22

3-
This is a Flask-based backend API that runs the FlexNetSim C++ library. It is an unofficial API that powers the backend for the web app deployed at [www.in-progress.com](www.in-progress.com).
3+
Flask-based backend API for integrating the FlexNetSim C++ library, powering the web application.
44

55
## Prerequisites
66

77
* Python 3.9 or higher
88
* g++ (GNU C++ Compiler)
99
* Docker (for containerization)
10-
* Google Cloud SDK (for deployment to GKE) -> In progress.
11-
* A Google Cloud Project with Google Kubernetes Engine (GKE) and Google Container Registry (GCR) enabled -> In progress.
10+
* Google Cloud SDK (for deployment to Cloud Run)
11+
* A Google Cloud Project with Cloud Run API enabled.
1212

1313
## Getting Started (Local Development)
1414

1515
1. **Clone the repository:**
1616
```bash
17-
git clone [repository-url]
17+
git clone [https://github.com/MirkoZETA/FlexNetSim-API.git](https://github.com/MirkoZETA/FlexNetSim-API.git)
1818
cd flask-simulation-backend
1919
```
2020

@@ -36,14 +36,34 @@ This is a Flask-based backend API that runs the FlexNetSim C++ library. It is an
3636
```
3737
The backend will be accessible at `http://127.0.0.1:5000`.
3838

39-
5. **Send simulation requests using `curl` or a frontend application:**
40-
Example `curl` request with minimal parameters (defaults applied):
41-
```bash
42-
curl -X POST -H "Content-Type: application/json" -d '{"algorithm": "FirstFit", "networkType": 1, "bitrate": "bitrate"}' [http://127.0.0.1:5000/run_simulation](http://127.0.0.1:5000/run_simulation)
43-
```
39+
## API Endpoints
4440

45-
Example `curl` request with all parameters specified
46-
```bash
41+
### `/run_simulation` (POST)
42+
43+
This endpoint runs a FlexNetSim simulation based on the parameters provided in the JSON request body.
44+
45+
**Request Body Parameters:**
46+
47+
| Parameter | Type | Description | Allowed Values | Default Value | Constraints |
48+
| :---------------- | :--------------- | :----------------------------------------------------------------------------- | :---------------------------- | :------------ | :-------------------- |
49+
| `algorithm` | `string` | Routing and spectrum assignment algorithm to use. | `FirstFit`, `ExactFit` | `FirstFit` | |
50+
| `networkType` | `integer` | Type of optical network. | `1` | `1` | Only `1` (EON) available |
51+
| `goal_connections`| `integer` | Target number of connection requests for the simulation. | | `100000` | Must be integer > 0 |
52+
| `confidence` | `number (float)` | Confidence level for the simulation results. | | `0.05` | Must be > 0 |
53+
| `lambda_param` | `number (float)` | Arrival rate (lambda) of connection requests. | | `1.0` | Must be > 0 |
54+
| `mu` | `number (float)` | Service rate (mu) of connection requests. | | `10.0` | Must be > 0 |
55+
| `network` | `string` | Network topology to simulate. | `NSFNet`, `Cost239`, `EuroCore`, `GermanNet`, `UKNet` | `NSFNet` | |
56+
| `bitrate` | `string` | Type of bitrate allocation. | `fixed-rate`, `flex-rate` | `bitrate` | |
57+
| `K` | `integer` | Number of paths to compute. | | `3` | |
58+
59+
**Example `curl` request with minimal parameters (defaults applied):**
60+
61+
```bash
62+
curl -X POST -H "Content-Type: application/json" -d '{"algorithm": "FirstFit", "networkType": 1, "bitrate": "bitrate"}' http://127.0.0.1:5000/run_simulation
63+
```
64+
65+
**Example `curl`request with all parameters specified:**
66+
```bash
4767
curl -X POST -H "Content-Type: application/json" \
4868
-d '{
4969
"algorithm": "FirstFit",
@@ -56,7 +76,27 @@ This is a Flask-based backend API that runs the FlexNetSim C++ library. It is an
5676
"bitrate": "bitrate" -> (filename in the ./bitrates folder)
5777
}' \
5878
http://127.0.0.1:5000/run_simulation
79+
```
80+
81+
**Response**:
82+
- 200 OK: Simulation executed successfully. The response body will be a JSON object with the following structure:
83+
```JSON
84+
{
85+
"output": "string", // Simulation output results
86+
"error": "string" // Empty string if no errors
87+
}
5988
```
89+
- 400 Bad Request: Indicates an error in the request, such as missing or invalid parameters. The response body will be a JSON object with an `"error"` field describing the issue.
90+
- 500 Internal Server Error: Indicates a server-side error, either during compilation or simulation execution. The response body will be a JSON object with `"error"` and "details" fields providing more information about the error.
91+
92+
### `/help` (GET)
93+
94+
This endpoint provides detailed information about the `/run_simulation` endpoint, including the expected request structure, parameters, allowed values, and response formats.
95+
96+
**Request**:
97+
```bash
98+
curl http://127.0.0.1:5000/help
99+
```
60100
61101
## Dockerization
62102
@@ -72,4 +112,56 @@ docker run -p 8080:8080 fns-api
72112
To stop:
73113
```bash
74114
docker stop fns-api
75-
```
115+
```
116+
117+
## GCloud Deployment Configuration
118+
119+
As a prerequisite is mandatory to apply the following steps to the GCloud project for the docker image build and upload to artifacts, and also service account creation and IAM policy binding:
120+
121+
[GCloud Configuration Video Tutorial](https://www.youtube.com/watch?v=KQUKDiBz3IA)
122+
123+
This video will guide you through the necessary configurations in the Google Cloud Console to prepare your project for Cloud Run deployments using GitHub Actions.
124+
125+
**Key Reminders from the Video & for Successful Deployment:**
126+
127+
* **Keep Track of Docker Image Name, Project ID:** Note these down during the video configuration, as you will need them in subsequent steps and for your GitHub Actions workflow.
128+
* **Service Account Email:** Ensure you create a Service Account as shown in the video and securely download and store the JSON key file. You'll also need to note the Service Account's email address.
129+
130+
**Post-Configuration Steps (using `gcloud` and `cloud-run`):**
131+
132+
1. Activate necessary apis:
133+
134+
* `gcloud services enable run.googleapis.com`
135+
136+
2. Create cloud run service:
137+
* Navigate to the Cloud Run section in your Google Cloud Console.
138+
* Create a **Service**.
139+
* Select *Use an inline editor to create a function*.
140+
* Set a name, in this case *fns-api-cloud-run* will be used.
141+
* Note down the Endpoint URL, because it will the defaul url for the API.
142+
* Select the authetification preferences.
143+
* Create.
144+
145+
3. Update access of service accounts to cloud run resources:
146+
147+
```bash
148+
gcloud projects add-iam-policy-binding "<YOUR-GOOGLE-PROJECT-ID>" --member="serviceAccount:<SERVICE_ACCOUNT_EMAIL>" --role="roles/run.admin"
149+
```
150+
151+
and
152+
153+
```bash
154+
gcloud iam service-accounts add-iam-policy-binding "<YOUR_PROJECT_NUMBER>-compute@developer.gserviceaccount.com" --member="serviceAccount:<SERVICE_ACCOUNT_EMAIL>" --role="roles/iam.serviceAccountActor"
155+
```
156+
157+
4. **Test the Deployed API (using `curl`):**
158+
159+
Use the `curl` command with the `ENDPOINT-URL` you obtained from the previous steps to test your deployed API. Replace `YOUR-ENDPOINT-URL` with the actual `ENDPOINT-URL`.
160+
161+
```bash
162+
curl -X POST -H "Content-Type: application/json" -d '{"algorithm": "FirstFit", "networkType": 1, "bitrate": "bitrate"}' <YOUR-ENDPOINT-URL>/run_simulation
163+
```
164+
165+
Remember that depending on the authetification preferences you might need to authetificate to send request to the Endpoint just created.
166+
167+
**Remember**: These GCloud configurations, along with the repository's `gke-cd.yml` GitHub Actions workflow and correctly configured GitHub secrets, are essential for successful automated deployment of your FlexNetSim-API application to Google Cloud Run.

0 commit comments

Comments
 (0)