Work item 2: Add cloud functionality

parent 09206d7f
......@@ -11,7 +11,7 @@
# Constants
PROJECT_NAME="otgc"
VERSION="2.13.0"
VERSION="3.0.0"
program=$0
......
......@@ -6,7 +6,7 @@
<groupId>otgc</groupId>
<artifactId>otgc</artifactId>
<version>2.13.0</version>
<version>3.0.0</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
......
......@@ -33,6 +33,8 @@ import javafx.scene.control.ButtonType;
import javafx.scene.web.WebView;
import javafx.stage.Stage;
import org.apache.log4j.Logger;
import org.iotivity.OCCloud;
import org.iotivity.OCCloudContext;
import org.iotivity.OCMain;
import org.openconnectivity.otgc.data.persistence.DatabaseManager;
import org.openconnectivity.otgc.data.repository.SettingRepository;
......@@ -185,6 +187,11 @@ public class App extends MvvmfxGuiceApplication {
notificationCenter.subscribe(NotificationKey.CANCEL_ALL_OBSERVERS,
(key, payload) -> {
OCCloudContext ctx = OCCloud.getContext(0);
if (ctx != null) {
OCCloud.managerStop(ctx);
}
OCMain.mainShutdown();
DatabaseManager.closeEntityManager();
......
......@@ -315,9 +315,9 @@ public class IotivityRepository {
public Single<String> getNonSecureEndpoint(Device device) {
return Single.create(emitter -> {
String endpoint = device.getIpv6Host();
String endpoint = device.getIpv6Host() != null ? device.getIpv6Host() : device.getIpv6TcpHost();
if (endpoint == null) {
endpoint = device.getIpv4Host();
endpoint = device.getIpv4Host() != null ? device.getIpv4Host() : device.getIpv4TcpHost();
}
emitter.onSuccess(endpoint);
});
......@@ -325,9 +325,9 @@ public class IotivityRepository {
public Single<String> getSecureEndpoint(Device device) {
return Single.create(emitter -> {
String endpoint = device.getIpv6SecureHost();
String endpoint = device.getIpv6SecureHost() != null ? device.getIpv6SecureHost() : device.getIpv6TcpSecureHost();
if (endpoint == null) {
endpoint = device.getIpv4SecureHost();
endpoint = device.getIpv4SecureHost() != null ? device.getIpv4SecureHost() : device.getIpv4TcpSecureHost();
}
emitter.onSuccess(endpoint);
});
......@@ -747,6 +747,12 @@ public class IotivityRepository {
public void close() {
LOG.debug("Calling OCMain.mainShutdown()");
OCCloudContext ctx = OCCloud.getContext(0);
if (ctx != null) {
OCCloud.managerStop(ctx);
}
OCMain.mainShutdown();
OCObt.shutdown();
......
......@@ -38,9 +38,13 @@ public class Device implements Comparable<Device> {
private String deviceId;
private transient OcDeviceInfo deviceInfo;
private String ipv6SecureHost;
private String ipv6TcpSecureHost;
private String ipv6Host;
private String ipv6TcpHost;
private String ipv4SecureHost;
private String ipv4TcpSecureHost;
private String ipv4Host;
private String ipv4TcpHost;
private int permits;
public Device() {}
......@@ -55,14 +59,26 @@ public class Device implements Comparable<Device> {
while(endpoints != null) {
String endpointStr = OCEndpointUtil.toString(endpoints);
if (endpointStr.startsWith("coaps://") && endpointStr.contains(".")) {
ipv4SecureHost = endpointStr;
} else if (endpointStr.startsWith("coaps://")) {
ipv6SecureHost = endpointStr;
} else if (endpointStr.startsWith("coap://") && endpointStr.contains(".")) {
ipv4Host = endpointStr;
} else if (endpointStr.startsWith("coap://")){
ipv6Host = endpointStr;
if (endpointStr.contains(".")) {
if (endpointStr.startsWith("coaps://")) {
ipv4SecureHost = endpointStr;
} else if (endpointStr.startsWith("coaps+tcp://")) {
ipv4TcpSecureHost = endpointStr;
} else if (endpointStr.startsWith("coap://")) {
ipv4Host = endpointStr;
} else if (endpointStr.startsWith("coap+tcp://")) {
ipv4TcpHost = endpointStr;
}
} else {
if (endpointStr.startsWith("coaps://")) {
ipv6SecureHost = endpointStr;
} else if (endpointStr.startsWith("coaps+tcp://")) {
ipv6TcpSecureHost = endpointStr;
} else if (endpointStr.startsWith("coap://")) {
ipv6Host = endpointStr;
} else if (endpointStr.startsWith("coap+tcp://")) {
ipv6TcpHost = endpointStr;
}
}
endpoints = endpoints.getNext();
......@@ -77,14 +93,26 @@ public class Device implements Comparable<Device> {
this.permits = permits;
for (String endpoint : endpoints) {
if (endpoint.startsWith("coaps://") && endpoint.contains(".")) {
ipv4SecureHost = endpoint;
} else if (endpoint.startsWith("coaps://")) {
ipv6SecureHost = endpoint;
} else if (endpoint.startsWith("coap://") && endpoint.contains(".")) {
ipv4Host = endpoint;
} else if (endpoint.startsWith("coap://")) {
ipv6Host = endpoint;
if (endpoint.contains(".")) {
if (endpoint.startsWith("coaps://")) {
ipv4SecureHost = endpoint;
} else if (endpoint.startsWith("coaps+tcp://")) {
ipv4TcpSecureHost = endpoint;
} else if (endpoint.startsWith("coap://")) {
ipv4Host = endpoint;
} else if (endpoint.startsWith("coap+tcp://")) {
ipv4TcpHost = endpoint;
}
} else {
if (endpoint.startsWith("coaps://")) {
ipv6SecureHost = endpoint;
} else if (endpoint.startsWith("coaps+tcp://")) {
ipv6TcpSecureHost = endpoint;
} else if (endpoint.startsWith("coap://")) {
ipv6Host = endpoint;
} else if (endpoint.startsWith("coap+tcp://")) {
ipv6TcpHost = endpoint;
}
}
}
}
......@@ -129,6 +157,14 @@ public class Device implements Comparable<Device> {
this.ipv6Host = host;
}
public String getIpv6TcpHost() {
return this.ipv6TcpHost;
}
public void setIpv6TcpHost(String host) {
this.ipv6TcpHost = host;
}
public String getIpv6SecureHost() {
return this.ipv6SecureHost;
}
......@@ -137,6 +173,14 @@ public class Device implements Comparable<Device> {
this.ipv6SecureHost = host;
}
public String getIpv6TcpSecureHost() {
return this.ipv6TcpSecureHost;
}
public void setIpv6TcpSecureHost(String host) {
this.ipv6TcpSecureHost = host;
}
public String getIpv4Host() {
return this.ipv4Host;
}
......@@ -145,6 +189,14 @@ public class Device implements Comparable<Device> {
this.ipv4Host = host;
}
public String getIpv4TcpHost() {
return this.ipv4TcpHost;
}
public void setIpv4TcpHost(String host) {
this.ipv4TcpHost = host;
}
public String getIpv4SecureHost() {
return this.ipv4SecureHost;
}
......@@ -153,27 +205,55 @@ public class Device implements Comparable<Device> {
this.ipv4SecureHost = host;
}
public String getIpv4TcpSecureHost() {
return this.ipv4SecureHost;
}
public void setIpv4TcpSecureHost(String host) {
this.ipv4SecureHost = host;
}
public boolean equalsHosts(Device device) {
if (this.getIpv6Host() != null && device.getIpv6Host() != null
&& !this.getIpv6Host().equals(device.getIpv6Host())) {
return false;
}
if (this.getIpv6TcpHost() != null && device.getIpv6TcpHost() != null
&& !this.getIpv6TcpHost().equals(device.getIpv6TcpHost())) {
return false;
}
if (this.getIpv6SecureHost() != null && device.getIpv6SecureHost() != null
&& !this.getIpv6SecureHost().equals(device.getIpv6SecureHost())) {
return false;
}
if (this.getIpv6TcpSecureHost() != null && device.getIpv6TcpSecureHost() != null
&& !this.getIpv6TcpSecureHost().equals(device.getIpv6TcpSecureHost())) {
return false;
}
if (this.getIpv4Host() != null && device.getIpv4Host() != null
&& !this.getIpv4Host().equals(device.getIpv4Host())) {
return false;
}
if (this.getIpv4TcpHost() != null && device.getIpv4TcpHost() != null
&& !this.getIpv4TcpHost().equals(device.getIpv4TcpHost())) {
return false;
}
if (this.getIpv4SecureHost() != null && device.getIpv4SecureHost() != null
&& !this.getIpv4SecureHost().equals(device.getIpv4SecureHost())) {
return false;
}
if (this.getIpv4TcpSecureHost() != null && device.getIpv4TcpSecureHost() != null
&& !this.getIpv4TcpSecureHost().equals(device.getIpv4TcpSecureHost())) {
return false;
}
return true;
}
......
......@@ -23,5 +23,6 @@ public enum DeviceType {
UNOWNED,
OWNED_BY_SELF,
OWNED_BY_OTHER,
OWNED_BY_OTHER_WITH_PERMITS
OWNED_BY_OTHER_WITH_PERMITS,
CLOUD
}
package org.openconnectivity.otgc.domain.usecase.cloud;
import io.reactivex.Single;
import org.openconnectivity.otgc.data.repository.CloudRepository;
import javax.inject.Inject;
public class CloudDeregisterUseCase {
private final CloudRepository cloudRepository;
@Inject
public CloudDeregisterUseCase(CloudRepository cloudRepository) {
this.cloudRepository = cloudRepository;
}
public Single<Integer> execute() {
return cloudRepository.deregister()
.andThen(cloudRepository.retrieveState());
}
}
package org.openconnectivity.otgc.domain.usecase.cloud;
import io.reactivex.Observable;
import org.openconnectivity.otgc.data.repository.CloudRepository;
import org.openconnectivity.otgc.domain.model.devicelist.Device;
import javax.inject.Inject;
public class CloudDiscoverDevicesUseCase {
private final CloudRepository cloudRepository;
@Inject
public CloudDiscoverDevicesUseCase(CloudRepository cloudRepository) {
this.cloudRepository = cloudRepository;
}
public Observable<Device> execute() {
return cloudRepository.discoverDevices();
}
}
package org.openconnectivity.otgc.domain.usecase.cloud;
import io.reactivex.Completable;
import io.reactivex.Single;
import org.iotivity.OCResourcePropertiesMask;
import org.openconnectivity.otgc.data.repository.CloudRepository;
import org.openconnectivity.otgc.domain.model.client.SerializableResource;
import org.openconnectivity.otgc.domain.model.devicelist.Device;
import org.openconnectivity.otgc.domain.model.resource.virtual.res.OcResource;
import javax.inject.Inject;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.TimeUnit;
public class CloudDiscoverResourcesUseCase {
private final CloudRepository cloudRepository;
@Inject
public CloudDiscoverResourcesUseCase(CloudRepository cloudRepository) {
this.cloudRepository = cloudRepository;
}
public Single<List<SerializableResource>> execute(Device device) {
return cloudRepository.discoverVerticalResources(device.getDeviceId())
.map(ocResources -> {
List<SerializableResource> serializableResources = new ArrayList<>();
for (OcResource resource : ocResources) {
SerializableResource serializableResource = new SerializableResource();
serializableResource.setUri(resource.getHref());
serializableResource.setResourceTypes(resource.getResourceTypes());
serializableResource.setResourceInterfaces(resource.getInterfaces());
serializableResource.setObservable(false);
serializableResources.add(serializableResource);
}
Collections.sort(serializableResources,
(r1, r2) -> r1.getUri().compareTo(r2.getUri()));
return serializableResources;
});
}
}
package org.openconnectivity.otgc.domain.usecase.cloud;
import io.reactivex.Single;
import org.openconnectivity.otgc.data.repository.CloudRepository;
import org.openconnectivity.otgc.domain.model.client.SerializableResource;
import org.openconnectivity.otgc.domain.model.devicelist.Device;
import javax.inject.Inject;
public class CloudGetResourceUseCase {
private final CloudRepository cloudRepository;
@Inject
public CloudGetResourceUseCase(CloudRepository cloudRepository) {
this.cloudRepository = cloudRepository;
}
public Single<SerializableResource> execute(Device device, SerializableResource resource) {
return cloudRepository.retrieveEndpoint()
.flatMap(endpoint -> cloudRepository.get(endpoint, resource.getUri(), device.getDeviceId()))
.map(ocRepresentation -> {
resource.setProperties(ocRepresentation);
return resource;
});
}
}
package org.openconnectivity.otgc.domain.usecase.cloud;
import io.reactivex.Single;
import org.openconnectivity.otgc.data.repository.CloudRepository;
import javax.inject.Inject;
public class CloudLoginUseCase {
private final CloudRepository cloudRepository;
@Inject
public CloudLoginUseCase(CloudRepository cloudRepository) {
this.cloudRepository = cloudRepository;
}
public Single<Integer> execute() {
return cloudRepository.login()
.andThen(cloudRepository.retrieveState());
}
}
package org.openconnectivity.otgc.domain.usecase.cloud;
import io.reactivex.Single;
import org.openconnectivity.otgc.data.repository.CloudRepository;
import javax.inject.Inject;
public class CloudLogoutUseCase {
private final CloudRepository cloudRepository;
@Inject
public CloudLogoutUseCase(CloudRepository cloudRepository) {
this.cloudRepository = cloudRepository;
}
public Single<Integer> execute() {
return cloudRepository.logout()
.andThen(cloudRepository.retrieveState());
}
}
package org.openconnectivity.otgc.domain.usecase.cloud;
import io.reactivex.Completable;
import org.iotivity.OCRepresentation;
import org.openconnectivity.otgc.data.repository.CloudRepository;
import org.openconnectivity.otgc.domain.model.client.SerializableResource;
import org.openconnectivity.otgc.domain.model.devicelist.Device;
import javax.inject.Inject;
import java.util.Map;
public class CloudPostResourceUseCase {
private final CloudRepository cloudRepository;
@Inject
public CloudPostResourceUseCase(CloudRepository cloudRepository) {
this.cloudRepository = cloudRepository;
}
public Completable execute(Device device, SerializableResource resource, OCRepresentation rep, Object valueArray) {
return cloudRepository.retrieveEndpoint()
.flatMapCompletable(endpoint -> cloudRepository.post(endpoint, resource.getUri(), device.getDeviceId(), rep, valueArray));
}
public Completable execute(Device device, SerializableResource resource, Map<String, Object> values) {
return cloudRepository.retrieveEndpoint()
.flatMapCompletable(endpoint -> cloudRepository.post(endpoint, resource.getUri(), device.getDeviceId(), values));
}
}
package org.openconnectivity.otgc.domain.usecase.cloud;
import io.reactivex.Single;
import org.openconnectivity.otgc.data.repository.CloudRepository;
import javax.inject.Inject;
public class CloudRefreshTokenUseCase {
private final CloudRepository cloudRepository;
@Inject
public CloudRefreshTokenUseCase(CloudRepository cloudRepository) {
this.cloudRepository = cloudRepository;
}
public Single<Integer> execute() {
return cloudRepository.refreshToken()
.andThen(cloudRepository.retrieveState());
}
}
package org.openconnectivity.otgc.domain.usecase.cloud;
import io.reactivex.Single;
import org.openconnectivity.otgc.data.repository.CloudRepository;
import javax.inject.Inject;
public class CloudRegisterUseCase {
private final CloudRepository cloudRepository;
@Inject
public CloudRegisterUseCase(CloudRepository cloudRepository) {
this.cloudRepository = cloudRepository;
}
public Single<Integer> execute() {
return cloudRepository.register()
.andThen(cloudRepository.retrieveState());
}
}
package org.openconnectivity.otgc.domain.usecase.cloud;
import io.reactivex.Single;
import org.openconnectivity.otgc.data.repository.CloudRepository;
import org.openconnectivity.otgc.domain.model.devicelist.Device;
import org.openconnectivity.otgc.domain.model.resource.virtual.d.OcDeviceInfo;
import org.openconnectivity.otgc.utils.constant.OcfResourceUri;
import javax.inject.Inject;
public class CloudRetrieveDeviceInfoUseCase {
private final CloudRepository cloudRepository;
@Inject
public CloudRetrieveDeviceInfoUseCase(CloudRepository cloudRepository) {
this.cloudRepository = cloudRepository;
}
public Single<OcDeviceInfo> execute(Device device) {
return cloudRepository.retrieveUri(device.getDeviceId(), OcfResourceUri.DEVICE_INFO_URI)
.flatMap(uri -> cloudRepository.retrieveEndpoint()
.flatMap(endpoint -> cloudRepository.retrieveDeviceInfo(endpoint, uri)));
}
}
package org.openconnectivity.otgc.domain.usecase.cloud;
import io.reactivex.Single;
import org.openconnectivity.otgc.data.repository.CloudRepository;
import org.openconnectivity.otgc.domain.model.devicelist.Device;
import org.openconnectivity.otgc.domain.model.devicelist.DeviceRole;
import javax.inject.Inject;
public class CloudRetrieveDeviceRoleUseCase {
private final CloudRepository cloudRepository;
@Inject
public CloudRetrieveDeviceRoleUseCase(CloudRepository cloudRepository) {
this.cloudRepository = cloudRepository;
}
public Single<DeviceRole> execute(Device device) {
return cloudRepository.getResources(device.getDeviceId())
.map(ocRes -> {
DeviceRole deviceRole = DeviceRole.CLIENT;
for (String resource : ocRes) {
if (!resource.endsWith("/oic/cloud/s")
&& !resource.endsWith("/oic/d")
&& !resource.endsWith("/oic/p")) {
deviceRole = DeviceRole.SERVER;
}
if (deviceRole.equals(DeviceRole.SERVER))
break;
}
return deviceRole;
});
}
}
package org.openconnectivity.otgc.domain.usecase.cloud;
import io.reactivex.Single;
import org.openconnectivity.otgc.data.repository.CloudRepository;
import org.openconnectivity.otgc.domain.model.devicelist.Device;
import org.openconnectivity.otgc.domain.model.resource.virtual.p.OcPlatformInfo;
import org.openconnectivity.otgc.utils.constant.OcfResourceUri;
import javax.inject.Inject;
public class CloudRetrievePlatformInfoUseCase {
private final CloudRepository cloudRepository;
@Inject
public CloudRetrievePlatformInfoUseCase(CloudRepository cloudRepository) {
this.cloudRepository = cloudRepository;
}
public Single<OcPlatformInfo> execute(Device device) {
return cloudRepository.retrieveUri(device