Skip to content

Commit

Permalink
Added UriFormat definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
czfdcn committed Sep 8, 2023
1 parent 92eb9c9 commit e48f772
Show file tree
Hide file tree
Showing 8 changed files with 186 additions and 12 deletions.
40 changes: 40 additions & 0 deletions src/main/java/org/eclipse/uprotocol/uri/datamodel/LongUri.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright (c) 2023 General Motors GTO LLC
*
* 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.eclipse.uprotocol.uri.datamodel;

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

/**
* Long Format URI
*/
public class LongUri extends UriFormat<String> {

public LongUri(UUri uri) {
super (UriFactory.buildUProtocolUri(uri), uri);
}

@Override
public boolean isEmpty() {
return uProtocolUri.isEmpty();
}

}
39 changes: 39 additions & 0 deletions src/main/java/org/eclipse/uprotocol/uri/datamodel/MicroUri.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright (c) 2023 General Motors GTO LLC
*
* 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.eclipse.uprotocol.uri.datamodel;

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

/**
* Micro Format uProtocol URI.
*/
public class MicroUri extends UriFormat<byte[]>{

public MicroUri(UUri uri) {
super(UriFactory.buildUProtocolMicroUri(uri), uri);
}

@Override
public boolean isEmpty() {
return uProtocolUri.length == 0;
}

}
38 changes: 38 additions & 0 deletions src/main/java/org/eclipse/uprotocol/uri/datamodel/ShortUri.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright (c) 2023 General Motors GTO LLC
*
* 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.eclipse.uprotocol.uri.datamodel;

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

public class ShortUri extends UriFormat<String> {

public ShortUri(UUri uri) {
super(UriFactory.buildUProtocolShortUri(uri), uri);
}

@Override
public boolean isEmpty() {
return uProtocolUri.isEmpty();
}

}

8 changes: 4 additions & 4 deletions src/main/java/org/eclipse/uprotocol/uri/datamodel/UUri.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@
public class UUri {
private static final UUri EMPTY = new UUri(UAuthority.empty(), UEntity.empty(), UResource.empty());

private final UAuthority uAuthority;
private final UEntity uEntity;
private final UResource uResource;
protected final UAuthority uAuthority;
protected final UEntity uEntity;
protected final UResource uResource;

private transient String uProtocolUri;

Expand Down Expand Up @@ -75,7 +75,7 @@ public UUri(UAuthority uAuthority, UEntity uEntity, String uResource) {

/**
* Static factory method for creating an empty uri, to avoid working with null<br>
* @return Returns an empty altifi uri to avoid working with null.
* @return Returns an empty ultify uri to avoid working with null.
*/
public static UUri empty() {
return EMPTY;
Expand Down
46 changes: 46 additions & 0 deletions src/main/java/org/eclipse/uprotocol/uri/datamodel/UriFormat.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package org.eclipse.uprotocol.uri.datamodel;

/**
* UriFormat is the interface that represents either Long, Short, or micro formats of a uProtocol Uri.
* UriFormat is passed to the transports such that a transport can fix what format they expect to use.
*
* @param <T> The type used for the uProtocol Uri, long and short use String while micro is byte[]
*/
public abstract class UriFormat<T> {
protected final T uProtocolUri;
private final UUri uuri;

/**
* Constructor for UriFormat called by the child classes (Short, Long, Micro)
* @param uProtocolUri Format specific generated uProtocol URI
* @param uuri UUri data object
*/
protected UriFormat(T uProtocolUri, UUri uuri) {
this.uProtocolUri = uProtocolUri;
this.uuri = uuri;
}

/**
* Return the uProtocol URI in the correct expected format
* @return uProtocol URI
*/
public T uProtocolUri() {
return uProtocolUri;
}

/**
* Obtain the UUri that was used to build the Uri Format
* This method might be used if we want to convert from one format to another
* @return UUri data object
*/
public UUri uuri() {
return uuri;
}

/**
* Check if the UProtocolUri is empty. An empty Uri means that we were not able to
* build it from the UUri.
* @return true if empty, false otherwise
*/
abstract boolean isEmpty();
}
15 changes: 9 additions & 6 deletions src/main/java/org/eclipse/uprotocol/utransport/UTransport.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

import org.eclipse.uprotocol.uri.datamodel.UEntity;
import org.eclipse.uprotocol.uri.datamodel.UUri;
import org.eclipse.uprotocol.uri.datamodel.UriFormat;
import org.eclipse.uprotocol.utransport.datamodel.UAttributes;
import org.eclipse.uprotocol.utransport.datamodel.UListener;
import org.eclipse.uprotocol.utransport.datamodel.UPayload;
Expand All @@ -45,26 +46,28 @@ public interface UTransport {

/**
* Transmit UPayload to the topic using the attributes defined in UTransportAttributes.
* @param topic UUri receiver of the payload.
* @param <T>
* @param topic UriFormat of a specific type (Long, Short, or Micro) receiver of the payload.
* @param payload Actual payload.
* @param attributes Additional transport attributes.
* @return Returns an Status if managed to send to the underlying communication technology or not.
*/
UStatus send(UUri topic, UPayload payload, UAttributes attributes);
<T> UStatus send(UriFormat<T> topic, UPayload payload, UAttributes attributes);

/**
* Register a method that will be called when a message comes in on the specific topic.
* @param topic UUri of the message that arrived via the underlying transport technology.
* @param topic UriFormat of a specific type (Long, Short, or Micro) of the message that arrived via the underlying transport technology.
* @param listener The method to execute to process the date for the topic.
* @return Returns an Ack if the method is registered successfully.
*/
UStatus registerListener(UUri topic, UListener listener);
<T> UStatus registerListener(UriFormat<T> topic, UListener listener);

/**
* Unregister a method on a topic. Messages arriving on this topic will no longer be processed by this listener.
* @param topic UUri of the messages that will no longer be processed.
* @param topic UriFormat of a specific type (Long, Short, or Micro) of the messages that will no longer be processed.
* @param listener The method to execute to process the date for the topic.
* @return Returns an Ack if the method is removed successfully.
*
*/
UStatus unregisterListener(UUri topic, UListener listener);
<T> UStatus unregisterListener(UriFormat<T> topic, UListener listener);
}
Original file line number Diff line number Diff line change
Expand Up @@ -190,4 +190,12 @@ public void test_create_uAuthority_with_valid_ipv6_address() {
String expectedLocal = "UAuthority{device='2001:db8:85a3:0:0:8a2e:370:7334', domain='null', address='/2001:db8:85a3:0:0:8a2e:370:7334', markedRemote=true}";
assertEquals(expectedLocal, remote.toString());
}

@Test
@DisplayName("Test creating uAuthority with valid ipv4 address in the device name")
public void test_create_uAuthority_with_valid_ipv4_address_in_device_name() {
UAuthority remote = UAuthority.remote("192.168.1.100", null);
String expectedLocal = "UAuthority{device='192.168.1.100', domain='null', address='/192.168.1.100', markedRemote=true}";
assertEquals(expectedLocal, remote.toString());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ public void testToString() {
.build();
assertEquals(String.format("UAttributes{id=%s, type=RESPONSE, priority=LOW, ttl=1000, token='someToken', " +
"sink=Uri{uAuthority=UAuthority{device='null', domain='null', address='null', markedRemote=false}, " +
"uEntity=UEntity{name='body.access', version='latest', id='unknown'}, " +
"uResource=UResource{name='', instance='null', message='null', id='unknown'}}, " +
"uEntity=UEntity{name='body.access', version='latest', id='null'}, " +
"uResource=UResource{name='', instance='null', message='null', id='null'}}, " +
"plevel=1, commstatus=5, reqid=%s}",
id,requestId),
uAttributes.toString());
Expand Down

0 comments on commit e48f772

Please sign in to comment.