|
|
# Kotlin
|
|
|
This section describes the steps needed to establish a development environment for TFS (TeraFlowSDN) components implemented in Kotlin. Currently, the `Gateway` component stands as the sole component developed in Kotlin.
|
|
|
|
|
|
## Install Kotlin
|
|
|
To begin, make sure that you have kotlin installed and its current version:
|
|
|
```
|
|
|
kotlin -version
|
|
|
```
|
|
|
|
|
|
If you don't have kotlin installed you will get an error like the following:
|
|
|
```
|
|
|
Command 'kotlin' not found, but can be installed with:
|
|
|
sudo snap install --classic kotlin
|
|
|
```
|
|
|
In which case you should use the following command to install the correct version:
|
|
|
```
|
|
|
sudo snap install --classic kotlin
|
|
|
```
|
|
|
|
|
|
Currently, the recommended version is 1.6.21, which uses Java Runtime Environment (JRE) version 11.
|
|
|
|
|
|
|
|
|
## Compiling and testing existing components
|
|
|
To compile a Kotlin project using Gradle, similarly to using the Maven wrapper (mvnw) for Java projects, you can use the Gradle wrapper (gradlew) within the root directory of your Kotlin component, specifically the gateway directory.
|
|
|
|
|
|
Navigate to the gateway directory within your Kotlin project. Ensure that it contains the gradlew script along with the gradle directory.
|
|
|
Then, create a directory named `proto` and move all the files with extension `.proto` in this way:
|
|
|
|
|
|
```
|
|
|
mkdir proto
|
|
|
cp ../../../proto/*.proto ./proto
|
|
|
```
|
|
|
For building the application, open a terminal or command prompt, navigate to the gateway directory, and run the following command:
|
|
|
```
|
|
|
./gradlew build
|
|
|
```
|
|
|
The following program runs the gateway application:
|
|
|
```
|
|
|
./gradlew runServer
|
|
|
```
|
|
|
|
|
|
## New Kotlin TFS component
|
|
|
|
|
|
|
|
|
|
|
|
### Sample Project
|
|
|
|
|
|
If you want to create a new TFS component written in Kotlin you could generate a Kotlin project using `gradle`. The recommended version is 7.1. Follow the following [Gradle guide](https://linuxize.com/post/how-to-install-gradle-on-ubuntu-20-04/) for its installation. For building the prokect follow [this link](https://docs.gradle.org/current/samples/sample_building_kotlin_applications.html) instead.
|
|
|
|
|
|
From inside the new project directory, run the init task using the following command in a terminal: `gradle init`.
|
|
|
|
|
|
The output will look like this:
|
|
|
|
|
|
|
|
|
```
|
|
|
$ gradle init
|
|
|
|
|
|
Select type of project to generate:
|
|
|
1: basic
|
|
|
2: application
|
|
|
3: library
|
|
|
4: Gradle plugin
|
|
|
Enter selection (default: basic) [1..4] 2
|
|
|
|
|
|
Select implementation language:
|
|
|
1: C++
|
|
|
2: Groovy
|
|
|
3: Java
|
|
|
4: Kotlin
|
|
|
5: Scala
|
|
|
6: Swift
|
|
|
Enter selection (default: Java) [1..6] 4
|
|
|
|
|
|
Select build script DSL:
|
|
|
1: Groovy
|
|
|
2: Kotlin
|
|
|
Enter selection (default: Groovy) [1..2] 1
|
|
|
|
|
|
Project name (default: demo):
|
|
|
Source package (default: demo):
|
|
|
|
|
|
|
|
|
BUILD SUCCESSFUL
|
|
|
2 actionable tasks: 2 executed
|
|
|
```
|
|
|
|
|
|
### Initial setup
|
|
|
|
|
|
The `gradle init` command generates the new project.
|
|
|
|
|
|
First, ensure the protobuf files are copied from the root directory of the TFS SDN controller. Run the following command in the directory of the new project:
|
|
|
|
|
|
```
|
|
|
mkdir proto
|
|
|
cp TFS/project/root/path/proto/*.proto ./proto/
|
|
|
```
|
|
|
|
|
|
The file `build.gradle.ktl` is fundamental as it manages dependencies. Adjust it for adding external libraries.
|
|
|
|
|
|
Next you should create the following files:
|
|
|
|
|
|
1. `new-component/.gitlab-ci.yml`
|
|
|
1. `new-component/Dockerfile`
|
|
|
|
|
|
|
|
|
We recommend leveraging the structures and configurations found in the files of existing components for inspiration.
|
|
|
|
|
|
### Docker Container
|
|
|
This project operates with Docker containers. Ensure the production of the container version for your component. To generate the container version of the project, modify the 'new-component/Dockerfile.' Execute the following command from the project's root directory:
|
|
|
```
|
|
|
docker build -t new-image -f new-component/Dockerfile ./
|
|
|
``` |