This is an automated email from the ASF dual-hosted git repository.

pearl11594 pushed a commit to branch netris-integration-upstream
in repository https://gitbox.apache.org/repos/asf/cloudstack.git

commit 4e5a79b87ad98ee5e09cd61e49619002b5d24bbf
Author: Nicolas Vazquez <[email protected]>
AuthorDate: Thu Oct 10 11:03:42 2024 -0300

    Add dependency and Netris API client (#4)
    
    * Add dependency and first approach to Netris API client
    
    * Fix authentication and create Netris API client, in progress sites listing
    
    * Fix get sites
---
 deps/install-non-oss.sh                            |  3 +
 plugins/network-elements/netris/pom.xml            |  5 ++
 .../apache/cloudstack/service/NetrisApiClient.java | 26 ++++++
 .../cloudstack/service/NetrisApiClientImpl.java    | 96 ++++++++++++++++++++++
 .../service/NetrisApiClientImplTest.java           | 43 ++++++++++
 5 files changed, 173 insertions(+)

diff --git a/deps/install-non-oss.sh b/deps/install-non-oss.sh
index 56cfc2cba9c..e40946ce312 100755
--- a/deps/install-non-oss.sh
+++ b/deps/install-non-oss.sh
@@ -85,4 +85,7 @@ mvn install:install-file 
-Dfile=juniper-contrail-api-1.0-SNAPSHOT.jar -DgroupId=
 # From 
https://github.com/radu-todirica/tungsten-api/raw/master/net/juniper/tungsten/juniper-tungsten-api/2.0/juniper-tungsten-api-2.0.jar
 mvn install:install-file -Dfile=juniper-tungsten-api-2.0.jar 
-DgroupId=net.juniper.tungsten -DartifactId=juniper-tungsten-api -Dversion=2.0 
-Dpackaging=jar
 
+# Netris Integration
+mvn install:install-file -Dfile=netris-java-sdk-1.0.0.jar -DgroupId=io.netris 
-DartifactId=netris-java-sdk -Dversion=1.0.0 -Dpackaging=jar
+
 exit 0
diff --git a/plugins/network-elements/netris/pom.xml 
b/plugins/network-elements/netris/pom.xml
index a1ff7264a4f..881d8195a93 100644
--- a/plugins/network-elements/netris/pom.xml
+++ b/plugins/network-elements/netris/pom.xml
@@ -30,5 +30,10 @@
         <relativePath>../../pom.xml</relativePath>
     </parent>
     <dependencies>
+        <dependency>
+            <groupId>io.netris</groupId>
+            <artifactId>netris-java-sdk</artifactId>
+            <version>1.0.0</version>
+        </dependency>
     </dependencies>
 </project>
diff --git 
a/plugins/network-elements/netris/src/main/java/org/apache/cloudstack/service/NetrisApiClient.java
 
b/plugins/network-elements/netris/src/main/java/org/apache/cloudstack/service/NetrisApiClient.java
new file mode 100644
index 00000000000..ac59f3f8c3c
--- /dev/null
+++ 
b/plugins/network-elements/netris/src/main/java/org/apache/cloudstack/service/NetrisApiClient.java
@@ -0,0 +1,26 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+package org.apache.cloudstack.service;
+
+import io.netris.model.GetSiteBody;
+
+import java.util.List;
+
+public interface NetrisApiClient {
+    boolean isSessionAlive();
+    List<GetSiteBody> listSites();
+}
diff --git 
a/plugins/network-elements/netris/src/main/java/org/apache/cloudstack/service/NetrisApiClientImpl.java
 
b/plugins/network-elements/netris/src/main/java/org/apache/cloudstack/service/NetrisApiClientImpl.java
new file mode 100644
index 00000000000..1654954017e
--- /dev/null
+++ 
b/plugins/network-elements/netris/src/main/java/org/apache/cloudstack/service/NetrisApiClientImpl.java
@@ -0,0 +1,96 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+package org.apache.cloudstack.service;
+
+import com.cloud.utils.exception.CloudRuntimeException;
+import io.netris.ApiClient;
+import io.netris.ApiException;
+import io.netris.ApiResponse;
+import io.netris.api.AuthenticationApi;
+import io.netris.api.SitesApi;
+import io.netris.model.AuthSchema;
+import io.netris.model.GetSiteBody;
+import io.netris.model.SitesResponseOK;
+import io.netris.model.response.AuthResponse;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+
+import java.math.BigDecimal;
+import java.util.List;
+
+public class NetrisApiClientImpl implements NetrisApiClient {
+
+    private final Logger logger = LogManager.getLogger(getClass());
+
+    private static final ApiClient apiClient = new ApiClient();
+
+    public NetrisApiClientImpl(String endpointBaseUrl, String username, String 
password) {
+        apiClient.setBasePath(endpointBaseUrl);
+        authenticate(username, password);
+    }
+
+    private void authenticate(String username, String password) {
+        AuthSchema authSchema = createAuthSchema(username, password);
+        AuthenticationApi authenticationApi = new AuthenticationApi(apiClient);
+        try {
+            ApiResponse<AuthResponse> authResponse = 
authenticationApi.apiAuthPost(authSchema);
+            if (authResponse.getStatusCode() == 200) {
+                String cookie = 
authResponse.getHeaders().get("Set-Cookie").get(0).split(";")[0];
+                apiClient.setApiKey(cookie);
+                apiClient.addDefaultHeader("Cookie", cookie);
+            } else {
+                String msg = String.format("Authentication to the Netris 
Controller %s failed, please check the credentials provided", 
apiClient.getBasePath());
+                logger.error(msg);
+                throw new CloudRuntimeException(msg);
+            }
+        } catch (ApiException e) {
+            String msg = String.format("Error authenticating to the Netris 
Controller %s: Code %s - Message: %s", apiClient.getBasePath(), e.getCode(), 
e.getResponseBody());
+            logger.error(msg, e);
+            throw new CloudRuntimeException(msg);
+        }
+    }
+
+    private AuthSchema createAuthSchema(String username, String password) {
+        AuthSchema authSchema = new AuthSchema();
+        authSchema.setUser(username);
+        authSchema.setPassword(password);
+        authSchema.setAuthSchemeID(new BigDecimal(1));
+        return authSchema;
+    }
+
+    @Override
+    public boolean isSessionAlive() {
+        AuthenticationApi api = new AuthenticationApi(apiClient);
+        try {
+            ApiResponse<AuthResponse> response = api.apiAuthGet();
+            return response.getStatusCode() == 200;
+        } catch (ApiException e) {
+            throw new CloudRuntimeException(e);
+        }
+    }
+
+    @Override
+    public List<GetSiteBody> listSites() {
+        SitesApi api = new SitesApi(apiClient);
+        try {
+            SitesResponseOK response = api.apiSitesGet();
+            return response.getData();
+        } catch (ApiException e) {
+            throw new CloudRuntimeException(e);
+        }
+    }
+}
diff --git 
a/plugins/network-elements/netris/src/test/java/org/apache/cloudstack/service/NetrisApiClientImplTest.java
 
b/plugins/network-elements/netris/src/test/java/org/apache/cloudstack/service/NetrisApiClientImplTest.java
new file mode 100644
index 00000000000..b8324dad4b0
--- /dev/null
+++ 
b/plugins/network-elements/netris/src/test/java/org/apache/cloudstack/service/NetrisApiClientImplTest.java
@@ -0,0 +1,43 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+package org.apache.cloudstack.service;
+
+import io.netris.model.GetSiteBody;
+import org.junit.Assert;
+import org.junit.Test;
+
+import java.util.List;
+
+public class NetrisApiClientImplTest {
+
+    private static final String endpointUrl = 
"https://shapeblue-ctl.netris.dev";;
+    private static final String username = "netris";
+    private static final String password = "qHHa$CZ2oJv*@!7mwoSR";
+
+    private NetrisApiClientImpl client = new NetrisApiClientImpl(endpointUrl, 
username, password);
+
+    @Test
+    public void testNetrisAuthStatus() {
+        Assert.assertTrue(client.isSessionAlive());
+    }
+
+    @Test
+    public void testListSites() {
+        List<GetSiteBody> sites = client.listSites();
+        Assert.assertTrue(sites.size() > 0);
+    }
+}

Reply via email to