Skip to content

Commit

Permalink
Reverting the UriFormat change
Browse files Browse the repository at this point in the history
Also adding more business logic to UAuthority, UEntity, and UResource.
  • Loading branch information
czfdcn committed Sep 11, 2023
1 parent 7fdbe96 commit a968959
Show file tree
Hide file tree
Showing 11 changed files with 84 additions and 241 deletions.
40 changes: 0 additions & 40 deletions src/main/java/org/eclipse/uprotocol/uri/datamodel/LongUri.java

This file was deleted.

39 changes: 0 additions & 39 deletions src/main/java/org/eclipse/uprotocol/uri/datamodel/MicroUri.java

This file was deleted.

38 changes: 0 additions & 38 deletions src/main/java/org/eclipse/uprotocol/uri/datamodel/ShortUri.java

This file was deleted.

71 changes: 40 additions & 31 deletions src/main/java/org/eclipse/uprotocol/uri/datamodel/UAuthority.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
* An Authority represents the deployment location of a specific Software Entity in the Ultiverse.
*/
public class UAuthority {
private final static UAuthority EMPTY = new UAuthority(null, null, false);
private final static UAuthority EMPTY = new UAuthority(null, null, null, false);

/**
* A device is a logical independent representation of a service bus in different execution environments.<br>
Expand All @@ -54,8 +54,6 @@ public class UAuthority {
*/
private final boolean markedRemote;

// TODO add user information - what is this used for? make sure it is part of the domain.


/**
* The device IP address.
Expand All @@ -65,36 +63,14 @@ public class UAuthority {

/**
* Constructor.
*
* @param device The device an software entity is deployed on, such as the VCU, CCU or Cloud (PaaS).
* @param domain The domain an software entity is deployed on, such as vehicle or backoffice.
* @param address The device IP address.
* @param markedRemote Indicates if this UAuthority was implicitly marked as remote. Used for validation.
*/
private UAuthority(String device, String domain, boolean markedRemote) {
private UAuthority(String device, String domain, InetAddress address, boolean markedRemote) {
this.device = device == null ? null : device.toLowerCase();
this.domain = domain == null ? null : domain.toLowerCase();
try {
if ((device != null) && InetAddressValidator.getInstance().isValid(device)) {
this.address = InetAddress.getByName(device);
} else {
this.address = null;
}
} catch (Exception e) {
throw new IllegalArgumentException("Invalid device name: " + device, e);
}

this.markedRemote = markedRemote;
}

/**
* using IP Address.
*
* @param address The device IP address.
* @param markedRemote Indicates if this UAuthority was implicitly marked as remote. Used for validation.
*/
private UAuthority(InetAddress address, boolean markedRemote) {
this.device = address != null ? address.getHostAddress() : null;
this.domain = null;
this.address = address;
this.markedRemote = markedRemote;
}
Expand All @@ -119,16 +95,49 @@ public static UAuthority local() {
* @return returns a remote authority that contains the device and the domain.
*/
public static UAuthority remote(String device, String domain) {
return new UAuthority(device, domain, true);
return remote(device, domain, null);
}

/**
* Static factory method for creating a remote authority.<br>
* @param address The device an software entity is deployed on
* @return returns a remote authority that contains the device and the domain.
*/
public static UAuthority remote(InetAddress device) {
return new UAuthority(device, true);
public static UAuthority remote(InetAddress address) {
return remote(null, null, address);
}


/**
* Static factory method for creating a remote authority using device, domain, and address.<br>
* @param device The device name
* @param domain The domain name
* @param address the IP address for the device
* @return returns a remote authority that contains the device and the domain.
*/
public static UAuthority remote(String device, String domain, InetAddress address) {
InetAddress addr = address;
// Try and set the address based on device name if the name is the string
// representation of an ip address
if ( (device != null) && (addr == null) ) {
try {
if ((device != null) && InetAddressValidator.getInstance().isValid(device)) {
addr = InetAddress.getByName(device);
} else {
addr = null;
}
} catch (Exception e) {
throw new IllegalArgumentException("Invalid device name: " + device, e);
}
}

// The IP address is populated but not the device name so set the device name
// to be the string representation of the IP address
if ((addr !=null) && (device == null)) {
device = addr.getHostAddress();
}

return new UAuthority(device, domain, addr, true);
}

/**
Expand All @@ -143,7 +152,7 @@ public static UAuthority empty() {
* @return returns true if this authority is remote, meaning it contains a device or a domain.
*/
public boolean isRemote() {
return address().isPresent() || domain().isPresent() || device().isPresent();
return domain().isPresent() || device().isPresent();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public static UEntity fromName(String name) {
* @return Returns a UEntity with id but unknown name.
*/
public static UEntity fromId(String version, Short id) {
return new UEntity("unknown", version, id);
return new UEntity(String.valueOf(id), version, id);
}


Expand Down
32 changes: 32 additions & 0 deletions src/main/java/org/eclipse/uprotocol/uri/datamodel/UResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,38 @@ public static UResource forRpc(String commandName, Short id) {
return new UResource("rpc", commandName, null, id);
}

/**
* Static factory method for creating a UResource using a string that contains either the id or
* a name + instance + message.
* @param resourceString String that contains the UResource information.
* @return Returns a UResource object
*/
public static UResource fromString(String resourceString) {
Objects.requireNonNull(resourceString, " Resource must have a command name.");
String[] parts = resourceString.split("#");
String nameAndInstance = parts[0];

// Try and fetch the resource ID if there is one (short form)
Short maybeId = null;
try {
maybeId = Short.parseShort(nameAndInstance);
} catch (NumberFormatException e) {
maybeId = null;

}

if (maybeId != null) {
return UResource.fromId(maybeId);
}

String[] nameAndInstanceParts = nameAndInstance.split("\\.");
String resourceName = nameAndInstanceParts[0];
String resourceInstance = nameAndInstanceParts.length > 1 ? nameAndInstanceParts[1] : null;
String resourceMessage = parts.length > 1 ? parts[1] : null;
return new UResource(resourceName, resourceInstance, resourceMessage);
}


/**
* @return Returns true if this resource specifies an RPC method call.
*/
Expand Down
19 changes: 1 addition & 18 deletions src/main/java/org/eclipse/uprotocol/uri/datamodel/UUri.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@

package org.eclipse.uprotocol.uri.datamodel;

import org.eclipse.uprotocol.uri.factory.UriFactory;

import java.util.Objects;
import java.util.Optional;

Expand All @@ -45,8 +43,6 @@ public class UUri {
protected final UEntity uEntity;
protected final UResource uResource;

private transient String uProtocolUri;

// Transport specific ID
private transient Integer id;

Expand Down Expand Up @@ -91,7 +87,7 @@ public boolean isEmpty() {
}

/**
* @return Returns the Authority represents the deployment location of a specific Software Entity in the Ultiverse.
* @return Returns the Authority represents the deployment location of a specific Software Entity.
*/
public UAuthority uAuthority() {
return uAuthority;
Expand All @@ -111,19 +107,6 @@ public UResource uResource() {
return this.uResource;
}

/**
* Support for a lazy generation of the uProtocol Uri string that is used in CloudEvent routing for
* sources and sinks.
* The function used to generate the string is the buildUProtocolUri method in the {@link UriFactory}.
* @return Returns the String that can be used as Source and Sink values of CloudEvents. The value is cached and only calculated on the first call.
*/
public String uProtocolUri() {
if (this.uProtocolUri == null) {
uProtocolUri = UriFactory.buildUProtocolUri(uAuthority(), uEntity(), uResource());
}
return uProtocolUri;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
Expand Down
46 changes: 0 additions & 46 deletions src/main/java/org/eclipse/uprotocol/uri/datamodel/UriFormat.java

This file was deleted.

Loading

0 comments on commit a968959

Please sign in to comment.