Skip to content

Commit d4cf95d

Browse files
committed
Merge remote-tracking branch 'origin/release/v1.3.0'
2 parents 32baccf + 35ab5e4 commit d4cf95d

File tree

180 files changed

+1397
-1272
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

180 files changed

+1397
-1272
lines changed

activationdemo/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
<modelVersion>4.0.0</modelVersion>
2020
<parent>
2121
<groupId>org.kaaproject.kaa</groupId>
22-
<version>1.2.1</version>
22+
<version>1.3.0</version>
2323
<artifactId>examples</artifactId>
2424
</parent>
2525
<groupId>org.kaaproject.kaa.examples</groupId>

activationdemo/resources/projects.xml

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,23 +26,43 @@
2626
<description>Endpoint activation application that demonstrates Kaa’s server profiling, grouping, and configuration management capabilities</description>
2727
<details>
2828
<![CDATA[
29-
This Endpoint activation app demonstrates the use of server-side endpoint profile feature. It allows to activate/deactivate device based on the endpoint ID using Kaa REST API.
29+
This Endpoint activation app demonstrates the use of server-side endpoint profile and device management features. It allows to activate/deactivate device based on the endpoint ID and work with endpoint credentials using Kaa REST API.
3030
3131
<h2>Installation</h2>
32-
Download the jar file to your desktop by clicking the "Binary" button on the left. Make sure that you have Java runtime installed. Application can run in two modes: client and admin. The mode is specified by command line arguments.<br/>
32+
Download the jar file to your desktop by clicking the "Binary" button on the left. Make sure that you have Java runtime installed. Application can run in three modes: client, admin and developer. The mode is specified by command line arguments.<br/>
3333
To run in the "admin" mode, use the following command in the console:
3434
<pre>
35-
$ java -jar ActivationDemo.jar admin ipAddress
35+
$ java -jar ActivationDemo.jar admin [ipAddress] [port]
36+
Example:
37+
$ java -jar ActivationDemo.jar admin localhost 8080
3638
</pre>
3739
Where ipAddress is the address of the Kaa node, where the administrative REST API is deployed.<br/><br/>
3840
To run in the "client" mode, use the following command in the console:
3941
<pre>
4042
$ java -jar ActivationDemo.jar client
4143
</pre>
44+
To run in the "developer" mode, use the following command in the console:
45+
<pre>
46+
$ java -jar ActivationDemo.jar developer [ipAddress] [port]
47+
</pre>
4248
<h2>Playing around</h2>
43-
Make sure that the Kaa Sandbox is up and running. When using the "Activation Demo" app in "admin" mode you will be able
44-
to activate/deactivate any endoint associated with Activation app. Endpoint activation and deactivation are performed by updating server profile using REST API.<br/><br/>
45-
"Activation Demo" app in "client" mode creates and registers one Kaa endpoint (per application instance). In this mode app displays the current state of the endpoint (active or inactive). Once the activation/deactivation is performed via the "admin" application, your endpoint will be served with the new configuration data according to the server-side endpoint profile change.<br/><br/>
49+
Make sure that the Kaa Sandbox is up and running.
50+
"Activation Demo" consists of the next modes:
51+
52+
1) In "admin" mode you will be able to do credentials provisioning (Device Management feature), revocation and registration. Before using "developer" or "client" mode, you need to create and register endpoint credentials via Kaa REST API.
53+
Run app with the next command:
54+
<pre>
55+
$ java -jar ActivationDemo.jar admin [kaa_hostname] 8080
56+
</pre>
57+
Then choose the first menu item by typing "1" and pressing "Enter".
58+
Exit from application by typing "3" and pressing "Enter".
59+
Now credentials was created and registered in Kaa. Corresponding "key.private" and "key.public" files are created.
60+
Starting from now, you can use other modes.
61+
2) In "developer" mode you will be able
62+
to activate/deactivate any endpoint associated with Activation app. Endpoint activation and deactivation are performed by updating server profile using REST API.<br/><br/>
63+
"Activation Demo" app in "client" mode creates and registers one Kaa endpoint (per application instance). In this mode app displays the current state of the endpoint (active or inactive). Once the activation/deactivation is performed via the "developer" application, your endpoint will be served with the new configuration data according to the server-side endpoint profile change.<br/>
64+
65+
<br/>
4666
4767
To understand better how this app works, you should navigate to the Kaa server administrative interface by clicking the "Administrative console" link at the top of your Sandbox window. Log in as a tenant developer (devuser / devuser123 by default), and go to the "Activation demo" application. Select "Schemas"->"Server-side EP profile". The app uses one schema version, which contains one value: "active" (boolean). The Kaa server uses this value to filter endpoints by their profile contents. Server-side endpoint profiles can be updated either via Admin Console, or via the REST API (which is done by the "Activation app" in the "admin" mode)<br/><br/>
4868
@@ -52,7 +72,7 @@ Finally, it's time to see what determines the configuration data. Go to the "End
5272
5373
The endpoints are put into either "Active device group" or "Inactive device group" depending on the "active" variable value in the server profile. See the corresponding profile filters in the group details window. Each endpoint group defines its own configuration (active or inactive) - and when the server-side profile for some EP is changed, the EP is moved from one group to another and this results in updated configuration being pushed by the server to the corresponding EP.<br/><br/>
5474
55-
Please note that all features of the <a href="https://docs.kaaproject.org/display/KAA/Administration+UI+guide">Administrative UI</a> are also supported via the <a href="https://docs.kaaproject.org/display/KAA/Admin+REST+API">REST API</a>.
75+
Please note that all features of the <a href="http://docs.kaaproject.org/display/KAA/Administration+UI+guide">Administrative UI</a> are also supported via the <a href="http://docs.kaaproject.org/display/KAA/Admin+REST+API">REST API</a>.
5676
5777
<h2>What's next?</h2>
5878

activationdemo/source/java/src/main/java/org/kaaproject/kaa/demo/activation/ActivationDemo.java

Lines changed: 79 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,20 @@
1616

1717
package org.kaaproject.kaa.demo.activation;
1818

19-
import org.kaaproject.kaa.client.DesktopKaaPlatformContext;
20-
import org.kaaproject.kaa.client.Kaa;
21-
import org.kaaproject.kaa.client.KaaClient;
22-
import org.kaaproject.kaa.client.SimpleKaaClientStateListener;
19+
import org.kaaproject.kaa.client.*;
2320
import org.kaaproject.kaa.client.channel.IPTransportInfo;
2421
import org.kaaproject.kaa.client.configuration.base.ConfigurationListener;
2522
import org.kaaproject.kaa.client.configuration.base.SimpleConfigurationStorage;
23+
import org.kaaproject.kaa.client.profile.ProfileContainer;
2624
import org.kaaproject.kaa.common.TransportType;
2725
import org.kaaproject.kaa.common.dto.EndpointGroupDto;
2826
import org.kaaproject.kaa.common.dto.EndpointProfileDto;
2927
import org.kaaproject.kaa.demo.activation.model.DeviceState;
30-
import org.slf4j.Logger;
31-
import org.slf4j.LoggerFactory;
3228
import org.kaaproject.kaa.demo.activation.utils.AdminClientManager;
3329
import org.kaaproject.kaa.demo.activation.utils.Utils;
30+
import org.kaaproject.kaa.schema.system.EmptyData;
31+
import org.slf4j.Logger;
32+
import org.slf4j.LoggerFactory;
3433

3534
import java.util.Base64;
3635
import java.util.HashMap;
@@ -54,29 +53,42 @@ public static void main(String[] args) throws InterruptedException {
5453
LOG.info("Possible options:");
5554
LOG.info(" java -jar ActivationDemo.jar client");
5655
LOG.info(" java -jar ActivationDemo.jar admin host [port]");
56+
LOG.info(" java -jar ActivationDemo.jar developer host [port]");
5757
return;
5858
}
5959

6060
String mode = args[0];
6161
switch (mode) {
62-
case "admin":
63-
if (args.length < 2) {
64-
}
65-
if (args.length == 2) {
66-
AdminClientManager.init(args[1]);
67-
} else if (args.length == 3) {
68-
AdminClientManager.init(args[1], Integer.valueOf(args[2]));
69-
} else {
70-
LOG.info("ip/host is not specified or address is invalid");
71-
return;
72-
}
73-
useAdminClient();
74-
break;
75-
case "client":
76-
useKaaClient();
77-
break;
78-
default:
79-
LOG.info("Invalid parameters. Please specify 'client' or 'admin'");
62+
case "developer":
63+
if (args.length < 2) {
64+
}
65+
if (args.length == 2) {
66+
AdminClientManager.init(args[1]);
67+
} else if (args.length == 3) {
68+
AdminClientManager.init(args[1], Integer.valueOf(args[2]));
69+
} else {
70+
LOG.info("ip/host is not specified or address is invalid");
71+
return;
72+
}
73+
useDeveloperClient();
74+
break;
75+
case "admin":
76+
if (args.length < 2) {
77+
}
78+
if (args.length == 2) {
79+
AdminClientManager.init(args[1], AdminClientManager.UserType.TENANT_ADMIN);
80+
} else if (args.length == 3) {
81+
AdminClientManager.init(args[1], Integer.valueOf(args[2]), AdminClientManager.UserType.TENANT_ADMIN);
82+
} else {
83+
LOG.info("ip/host is not specified or address is invalid");
84+
return;
85+
}
86+
useAdminClient();
87+
case "client":
88+
useKaaClient();
89+
break;
90+
default:
91+
LOG.info("Invalid parameters. Please specify 'client' or 'admin' or 'developer'");
8092

8193
}
8294
}
@@ -100,7 +112,12 @@ public void onStarted() {
100112
LOG.info("Device state: " + (config.getActive() ? "active" : "inactive"));
101113
}
102114
});
103-
115+
kaaClient.setProfileContainer(new ProfileContainer() {
116+
@Override
117+
public EmptyData getProfile() {
118+
return new EmptyData();
119+
}
120+
});
104121
/*
105122
* Persist configuration in a local storage to avoid downloading it each
106123
* time the Kaa client is started.
@@ -112,6 +129,7 @@ public void onStarted() {
112129
* it is updated.
113130
*/
114131
kaaClient.addConfigurationListener(new ConfigurationListener() {
132+
@Override
115133
public void onConfigurationUpdate(DeviceType deviceType) {
116134
LOG.info("Configuration was updated. New device state: " + (deviceType.getActive() ? "active" : "inactive"));
117135
}
@@ -130,14 +148,35 @@ public void onConfigurationUpdate(DeviceType deviceType) {
130148
kaaClient.stop();
131149
}
132150

133-
private static void useAdminClient() {
151+
private static void useAdminClient(){
152+
LOG.info("Choose action by entering corresponding number:");
153+
while (true){
154+
LOG.info("\n1. Generate and provision endpoint credentials.\n2. Revoke endpoint " +
155+
"credentials\n3. Exit");
156+
switch(Utils.getUserInput()){
157+
case "1":
158+
generateAndProvisionKeys();
159+
break;
160+
case "2":
161+
revokeCredentials();
162+
break;
163+
default:
164+
System.exit(0);
165+
}
166+
}
167+
}
168+
169+
private static void useDeveloperClient() {
170+
134171
Map<String, EndpointProfileDto> endpointProfiles = retrieveEndpointProfiles();
135172
if (endpointProfiles.isEmpty()) {
136173
LOG.info("There is no endpoints registered!");
137174
return;
138175
}
139176
printAllEndpointProfiles(endpointProfiles);
140177

178+
179+
141180
for (;;) {
142181
LOG.info("Specify endpoint profile id# you want to activate/deactivate or print 'exit' to exit");
143182
String userInput = Utils.getUserInput();
@@ -156,6 +195,20 @@ private static void useAdminClient() {
156195
}
157196
}
158197

198+
private static void generateAndProvisionKeys(){
199+
AdminClientManager clientManager = AdminClientManager.instance();
200+
LOG.info("Going to generate and provision credentials");
201+
clientManager.provideCredentials(APPLICATION_NAME, clientManager.generateKeyPair().getPublic().getEncoded());
202+
}
203+
204+
private static void revokeCredentials(){
205+
AdminClientManager clientManager = AdminClientManager.instance();
206+
LOG.info("Enter ID of credentials that needs to be revoked:");
207+
String credentialsId = Utils.getUserInput();
208+
LOG.info("Going to revoke credentials");
209+
clientManager.revokeCredentials(APPLICATION_NAME, credentialsId);
210+
}
211+
159212
/**
160213
* Retrieve all endpoint profiles associated with activation application
161214
*

activationdemo/source/java/src/main/java/org/kaaproject/kaa/demo/activation/utils/AdminClientManager.java

Lines changed: 63 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,26 +18,38 @@
1818

1919
import org.kaaproject.kaa.common.dto.*;
2020
import org.kaaproject.kaa.common.dto.admin.AuthResultDto;
21+
import org.kaaproject.kaa.common.dto.credentials.CredentialsDto;
22+
import org.kaaproject.kaa.common.endpoint.security.KeyUtil;
23+
import org.kaaproject.kaa.demo.activation.model.DeviceState;
2124
import org.kaaproject.kaa.server.common.admin.AdminClient;
2225
import org.slf4j.Logger;
2326
import org.slf4j.LoggerFactory;
2427

25-
import java.util.*;
28+
import java.security.KeyPair;
29+
import java.util.LinkedHashMap;
30+
import java.util.List;
31+
import java.util.Map;
2632

2733
public class AdminClientManager {
2834

2935
private static final Logger LOG = LoggerFactory.getLogger(AdminClientManager.class);
3036

3137
private static final String TENANT_DEV_USERNAME = "devuser";
3238
private static final String TENANT_DEV_PASSWORD = "devuser123";
39+
40+
private static final String TENANT_ADMIN_USERNAME = "admin";
41+
private static final String TENANT_ADMIN_PASSWORD = "admin123";
42+
3343
private static final String DEFAULT_LIMIT = "20";
3444
private static final String DEFAULT_OFFSET = "0";
3545
private static final int KAA_PORT = 8080;
3646

3747
private AdminClient adminClient;
3848
private static AdminClientManager instance;
49+
private UserType userType;
3950

40-
private AdminClientManager(String host, int port) {
51+
private AdminClientManager(String host, int port, UserType userType) {
52+
this.userType = userType;
4153
adminClient = new AdminClient(host, port);
4254
}
4355

@@ -46,7 +58,15 @@ public static void init(String host) {
4658
}
4759

4860
public static void init(String host, int port) {
49-
instance = new AdminClientManager(host, port);
61+
instance = new AdminClientManager(host, port, UserType.TENANT_DEVELOPER);
62+
}
63+
64+
public static void init(String host, UserType userType){
65+
init(host, KAA_PORT, userType);
66+
}
67+
68+
public static void init(String host, int port, UserType userType){
69+
instance = new AdminClientManager(host, port, userType);
5070
}
5171

5272
public static AdminClientManager instance() {
@@ -56,6 +76,30 @@ public static AdminClientManager instance() {
5676
return instance;
5777
}
5878

79+
/**
80+
* Generate public and private key pair
81+
*
82+
* @return Generated KeyPair
83+
*/
84+
public KeyPair generateKeyPair(){
85+
return KeyUtil.generateKeyPair("./key.private", "./key.public");
86+
}
87+
88+
public void provideCredentials(String applicationName, byte[] publicKey){
89+
CredentialsDto credentialsDto = adminClient.provisionCredentials(getApplicationByName(applicationName).getApplicationToken(),
90+
publicKey);
91+
adminClient.provisionRegistration(getApplicationByName(applicationName).getApplicationToken(), credentialsDto
92+
.getId(), 1, DeviceState.toJsonString(true));
93+
LOG.debug("APP TOKEN: {}", getApplicationByName(applicationName).getApplicationToken());
94+
LOG.info("Credentials with ID={} are now in status: {}", credentialsDto.getId(), credentialsDto.getStatus());
95+
}
96+
97+
public void revokeCredentials(String applicationName, String credentialsId){
98+
LOG.debug("APP TOKEN: {}", getApplicationByName(applicationName).getApplicationToken());
99+
adminClient.revokeCredentials(getApplicationByName(applicationName).getApplicationToken(), credentialsId);
100+
LOG.info("Credentials revoked.");
101+
}
102+
59103
/**
60104
* Do authorization check
61105
*
@@ -76,7 +120,11 @@ public boolean checkAuth() {
76120
*/
77121
public void checkAuthorizationAndLogin() {
78122
if (!checkAuth()) {
79-
adminClient.login(TENANT_DEV_USERNAME, TENANT_DEV_PASSWORD);
123+
if(UserType.TENANT_ADMIN.equals(userType)){
124+
adminClient.login(TENANT_ADMIN_USERNAME, TENANT_ADMIN_PASSWORD);
125+
}else {
126+
adminClient.login(TENANT_DEV_USERNAME, TENANT_DEV_PASSWORD);
127+
}
80128
}
81129
}
82130

@@ -127,18 +175,18 @@ public ApplicationDto getApplicationByName(String applicationName) {
127175
}
128176

129177
/**
130-
* Get all endpoint groups associated with given application Id
178+
* Get all endpoint groups associated with given application Token
131179
*
132-
* @param applicationId
133-
* the application Id
180+
* @param applicationToken
181+
* the application Token
134182
* @return list of endpoint groups
135183
*/
136-
public List<EndpointGroupDto> getEndpointGroups(String applicationId) {
184+
public List<EndpointGroupDto> getEndpointGroups(String applicationToken) {
137185
checkAuthorizationAndLogin();
138186

139187
List<EndpointGroupDto> endpointGroups = null;
140188
try {
141-
endpointGroups = adminClient.getEndpointGroups(applicationId);
189+
endpointGroups = adminClient.getEndpointGroupsByAppToken(applicationToken);
142190
} catch (Exception e) {
143191
LOG.error("Exception has occurred: " + e.getMessage());
144192
}
@@ -179,7 +227,7 @@ public List<EndpointGroupDto> getEndpointGroupsByApplicationName(String applicat
179227
return null;
180228
}
181229

182-
return getEndpointGroups(applicationDto.getId());
230+
return getEndpointGroups(applicationDto.getApplicationToken());
183231
}
184232

185233
/**
@@ -204,4 +252,9 @@ public Map<String, EndpointProfileDto> getEndpointProfiles(List<EndpointGroupDto
204252
return endpointProfiles;
205253
}
206254

255+
public enum UserType {
256+
TENANT_ADMIN,
257+
TENANT_DEVELOPER
258+
}
259+
207260
}

0 commit comments

Comments
 (0)