Skip to content

Commit c5bf82a

Browse files
authored
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
1 parent 9b97a2a commit c5bf82a

File tree

5 files changed

+173
-0
lines changed

5 files changed

+173
-0
lines changed

deps/install-non-oss.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,7 @@ mvn install:install-file -Dfile=juniper-contrail-api-1.0-SNAPSHOT.jar -DgroupId=
8585
# From https://github.com/radu-todirica/tungsten-api/raw/master/net/juniper/tungsten/juniper-tungsten-api/2.0/juniper-tungsten-api-2.0.jar
8686
mvn install:install-file -Dfile=juniper-tungsten-api-2.0.jar -DgroupId=net.juniper.tungsten -DartifactId=juniper-tungsten-api -Dversion=2.0 -Dpackaging=jar
8787

88+
# Netris Integration
89+
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
90+
8891
exit 0

plugins/network-elements/netris/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,10 @@
3030
<relativePath>../../pom.xml</relativePath>
3131
</parent>
3232
<dependencies>
33+
<dependency>
34+
<groupId>io.netris</groupId>
35+
<artifactId>netris-java-sdk</artifactId>
36+
<version>1.0.0</version>
37+
</dependency>
3338
</dependencies>
3439
</project>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
package org.apache.cloudstack.service;
18+
19+
import io.netris.model.GetSiteBody;
20+
21+
import java.util.List;
22+
23+
public interface NetrisApiClient {
24+
boolean isSessionAlive();
25+
List<GetSiteBody> listSites();
26+
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
package org.apache.cloudstack.service;
18+
19+
import com.cloud.utils.exception.CloudRuntimeException;
20+
import io.netris.ApiClient;
21+
import io.netris.ApiException;
22+
import io.netris.ApiResponse;
23+
import io.netris.api.AuthenticationApi;
24+
import io.netris.api.SitesApi;
25+
import io.netris.model.AuthSchema;
26+
import io.netris.model.GetSiteBody;
27+
import io.netris.model.SitesResponseOK;
28+
import io.netris.model.response.AuthResponse;
29+
import org.apache.logging.log4j.LogManager;
30+
import org.apache.logging.log4j.Logger;
31+
32+
import java.math.BigDecimal;
33+
import java.util.List;
34+
35+
public class NetrisApiClientImpl implements NetrisApiClient {
36+
37+
private final Logger logger = LogManager.getLogger(getClass());
38+
39+
private static final ApiClient apiClient = new ApiClient();
40+
41+
public NetrisApiClientImpl(String endpointBaseUrl, String username, String password) {
42+
apiClient.setBasePath(endpointBaseUrl);
43+
authenticate(username, password);
44+
}
45+
46+
private void authenticate(String username, String password) {
47+
AuthSchema authSchema = createAuthSchema(username, password);
48+
AuthenticationApi authenticationApi = new AuthenticationApi(apiClient);
49+
try {
50+
ApiResponse<AuthResponse> authResponse = authenticationApi.apiAuthPost(authSchema);
51+
if (authResponse.getStatusCode() == 200) {
52+
String cookie = authResponse.getHeaders().get("Set-Cookie").get(0).split(";")[0];
53+
apiClient.setApiKey(cookie);
54+
apiClient.addDefaultHeader("Cookie", cookie);
55+
} else {
56+
String msg = String.format("Authentication to the Netris Controller %s failed, please check the credentials provided", apiClient.getBasePath());
57+
logger.error(msg);
58+
throw new CloudRuntimeException(msg);
59+
}
60+
} catch (ApiException e) {
61+
String msg = String.format("Error authenticating to the Netris Controller %s: Code %s - Message: %s", apiClient.getBasePath(), e.getCode(), e.getResponseBody());
62+
logger.error(msg, e);
63+
throw new CloudRuntimeException(msg);
64+
}
65+
}
66+
67+
private AuthSchema createAuthSchema(String username, String password) {
68+
AuthSchema authSchema = new AuthSchema();
69+
authSchema.setUser(username);
70+
authSchema.setPassword(password);
71+
authSchema.setAuthSchemeID(new BigDecimal(1));
72+
return authSchema;
73+
}
74+
75+
@Override
76+
public boolean isSessionAlive() {
77+
AuthenticationApi api = new AuthenticationApi(apiClient);
78+
try {
79+
ApiResponse<AuthResponse> response = api.apiAuthGet();
80+
return response.getStatusCode() == 200;
81+
} catch (ApiException e) {
82+
throw new CloudRuntimeException(e);
83+
}
84+
}
85+
86+
@Override
87+
public List<GetSiteBody> listSites() {
88+
SitesApi api = new SitesApi(apiClient);
89+
try {
90+
SitesResponseOK response = api.apiSitesGet();
91+
return response.getData();
92+
} catch (ApiException e) {
93+
throw new CloudRuntimeException(e);
94+
}
95+
}
96+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
package org.apache.cloudstack.service;
18+
19+
import io.netris.model.GetSiteBody;
20+
import org.junit.Assert;
21+
import org.junit.Test;
22+
23+
import java.util.List;
24+
25+
public class NetrisApiClientImplTest {
26+
27+
private static final String endpointUrl = "https://shapeblue-ctl.netris.dev";
28+
private static final String username = "netris";
29+
private static final String password = "qHHa$CZ2oJv*@!7mwoSR";
30+
31+
private NetrisApiClientImpl client = new NetrisApiClientImpl(endpointUrl, username, password);
32+
33+
@Test
34+
public void testNetrisAuthStatus() {
35+
Assert.assertTrue(client.isSessionAlive());
36+
}
37+
38+
@Test
39+
public void testListSites() {
40+
List<GetSiteBody> sites = client.listSites();
41+
Assert.assertTrue(sites.size() > 0);
42+
}
43+
}

0 commit comments

Comments
 (0)