Skip to content

Commit

Permalink
feat: PduObjectBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
towi committed Sep 30, 2024
1 parent ba96aed commit ab3cac8
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 5 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.sipgate.li</groupId>
<artifactId>lib</artifactId>
<version>0.2.0-SNAPSHOT</version>
<version>0.2.1-SNAPSHOT</version>
<name>lib</name>
<description>X1/X2/X3 library for Java based on ETSI TS 103 221</description>

Expand Down
10 changes: 6 additions & 4 deletions src/main/java/com/sipgate/li/lib/x2x3/PduObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ public record PduObject(
byte[] payload
) {
public PduObject {
if (majorVersion != 0) {
throw new PduValidationException("Must be 0", "majorVersion");
if (majorVersion != MAJOR_VERSION) {
throw new PduValidationException("Must be " + MAJOR_VERSION, "majorVersion");
}
if (minorVersion != 5) {
throw new PduValidationException("Must be 5", "minorVersion");
if (minorVersion != MINOR_VERSION) {
throw new PduValidationException("Must be " + MINOR_VERSION, "minorVersion");
}
if (pduType == PduType.X2_PDU && !payloadFormat.x2allowed) {
throw new PduValidationException("Must be X2 compatible", "payloadFormat");
Expand All @@ -39,6 +39,8 @@ public record PduObject(
}
}

public static final int MINOR_VERSION = 5;
public static final int MAJOR_VERSION = 0;
public static final int MANDATORY_HEADER_LENGTH = 40;
public static final int CORRELATION_ID_LENGTH = 8;
}
101 changes: 101 additions & 0 deletions src/main/java/com/sipgate/li/lib/x2x3/PduObjectBuilder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package com.sipgate.li.lib.x2x3;

import java.util.UUID;

public class PduObjectBuilder {

private Short majorVersion;
private Short minorVersion;
private PduType pduType;
private Long headerLength;
private Long payloadLength;
private PayloadFormat payloadFormat;
private PayloadDirection payloadDirection;
private UUID xid;
private byte[] correlationID;
private byte[] conditionalAttributeFields;
private byte[] payload;

public PduObjectBuilder setMajorVersion(final Short majorVersion) {
this.majorVersion = majorVersion;
return this;
}

public PduObjectBuilder setMinorVersion(final Short minorVersion) {
this.minorVersion = minorVersion;
return this;
}

public PduObjectBuilder setPduType(final PduType pduType) {
this.pduType = pduType;
return this;
}

public PduObjectBuilder setHeaderLength(final Long headerLength) {
this.headerLength = headerLength;
return this;
}

public PduObjectBuilder setPayloadLength(final Long payloadLength) {
this.payloadLength = payloadLength;
return this;
}

public PduObjectBuilder setPayloadFormat(final PayloadFormat payloadFormat) {
this.payloadFormat = payloadFormat;
return this;
}

public PduObjectBuilder setPayloadDirection(final PayloadDirection payloadDirection) {
this.payloadDirection = payloadDirection;
return this;
}

public PduObjectBuilder setXid(final UUID xid) {
this.xid = xid;
return this;
}

public PduObjectBuilder setCorrelationID(final byte[] correlationID) {
this.correlationID = correlationID;
return this;
}

public PduObjectBuilder setConditionalAttributeFields(final byte[] conditionalAttributeFields) {
this.conditionalAttributeFields = conditionalAttributeFields;
return this;
}

public PduObjectBuilder setPayload(final byte[] payload) {
this.payload = payload;
return this;
}

public PduObject build() {
final byte[] conditionalAttributeFields = this.conditionalAttributeFields != null
? this.conditionalAttributeFields
: new byte[0];
final byte[] payload = this.payload != null ? this.payload : new byte[0];
return new PduObject(
this.majorVersion != null ? this.majorVersion : PduObject.MAJOR_VERSION,
this.minorVersion != null ? this.minorVersion : PduObject.MINOR_VERSION,
this.pduType != null ? this.pduType : PduType.X2_PDU,
this.headerLength != null
? this.headerLength
: PduObject.MANDATORY_HEADER_LENGTH + conditionalAttributeFields.length,
this.payloadLength != null ? this.payloadLength : payload.length,
this.payloadFormat != null ? this.payloadFormat : PayloadFormat.SIP,
this.payloadDirection != null ? this.payloadDirection : PayloadDirection.SENT_FROM_TARGET,
this.xid != null ? this.xid : UUID.randomUUID(),
this.correlationID != null ? this.correlationID : new byte[PduObject.CORRELATION_ID_LENGTH],
conditionalAttributeFields,
payload
);
}

public PduObject buildSip() {
this.pduType = PduType.X2_PDU;
this.payloadFormat = PayloadFormat.SIP;
return build();
}
}

0 comments on commit ab3cac8

Please sign in to comment.