Skip to content

Commit

Permalink
feat(plc4j): Refactored the way transports are configured via the add…
Browse files Browse the repository at this point in the history
…itional parameters of the connection string.

* feat(plc4j): Rewrote the configuration for all java drivers to generally support overwriting (WIP)

* chore(plc4j): Removed the getTimeoutManager from the Configuration interface.

* chore(plc4j): Removed the non-existent "skip" config option from the plc4x-maven-plugin config.

* feat(plc4j): Added support for hierarchical configurations in the ConfigurationFactory

* feat(plc4j): Added the general support for configuring transports via connection-string parameters.

* fis(plc4j): Actually ensured the config reaches the TcpTransport

* refactor(plc4j): Changed the way the transports are configured, by detaching them from the protocol configuration.

* chore: Merged in changes from develop

* fix(plc4j/cbus): use correct c-bus port from constant

---------

Co-authored-by: Sebastian Rühl <sruehl@apache.org>
  • Loading branch information
chrisdutz and sruehl committed Oct 30, 2023
1 parent 792049f commit 554c083
Show file tree
Hide file tree
Showing 94 changed files with 2,053 additions and 428 deletions.
Expand Up @@ -20,11 +20,14 @@

import io.netty.buffer.ByteBuf;
import org.apache.plc4x.java.abeth.configuration.AbEthConfiguration;
import org.apache.plc4x.java.abeth.configuration.AbEthTcpTransportConfiguration;
import org.apache.plc4x.java.abeth.tag.AbEthTag;
import org.apache.plc4x.java.abeth.tag.AbEthTagHandler;
import org.apache.plc4x.java.abeth.protocol.AbEthProtocolLogic;
import org.apache.plc4x.java.abeth.readwrite.CIPEncapsulationPacket;
import org.apache.plc4x.java.api.model.PlcTag;
import org.apache.plc4x.java.spi.transport.TransportConfiguration;
import org.apache.plc4x.java.spi.transport.TransportConfigurationTypeProvider;
import org.apache.plc4x.java.spi.values.PlcValueHandler;
import org.apache.plc4x.java.spi.configuration.Configuration;
import org.apache.plc4x.java.spi.connection.GeneratedDriverBase;
Expand All @@ -33,7 +36,7 @@

import java.util.function.ToIntFunction;

public class AbEthDriver extends GeneratedDriverBase<CIPEncapsulationPacket> {
public class AbEthDriver extends GeneratedDriverBase<CIPEncapsulationPacket> implements TransportConfigurationTypeProvider {

public static final int AB_ETH_PORT = 2222;

Expand Down Expand Up @@ -104,4 +107,13 @@ public PlcTag prepareTag(String tagAddress) {
return AbEthTag.of(tagAddress);
}

@Override
public Class<? extends TransportConfiguration> getTransportConfigurationType(String transportCode) {
switch (transportCode) {
case "tcp":
return AbEthTcpTransportConfiguration.class;
}
return null;
}

}
Expand Up @@ -18,12 +18,10 @@
*/
package org.apache.plc4x.java.abeth.configuration;

import org.apache.plc4x.java.abeth.AbEthDriver;
import org.apache.plc4x.java.spi.configuration.Configuration;
import org.apache.plc4x.java.spi.configuration.annotations.ConfigurationParameter;
import org.apache.plc4x.java.transport.tcp.TcpTransportConfiguration;

public class AbEthConfiguration implements Configuration, TcpTransportConfiguration {
public class AbEthConfiguration implements Configuration {

@ConfigurationParameter
private int station;
Expand All @@ -36,9 +34,4 @@ public void setStation(int station) {
this.station = station;
}

@Override
public int getDefaultPort() {
return AbEthDriver.AB_ETH_PORT;
}

}
@@ -0,0 +1,32 @@
/*
* 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.apache.plc4x.java.abeth.configuration;

import org.apache.plc4x.java.abeth.AbEthDriver;
import org.apache.plc4x.java.transport.tcp.DefaultTcpTransportConfiguration;

public class AbEthTcpTransportConfiguration extends DefaultTcpTransportConfiguration {

@Override
public int getDefaultPort() {
return AbEthDriver.AB_ETH_PORT;
}

}
5 changes: 0 additions & 5 deletions plc4j/drivers/ads/pom.xml
Expand Up @@ -98,11 +98,6 @@
<artifactId>plc4j-transport-tcp</artifactId>
<version>0.12.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.apache.plc4x</groupId>
<artifactId>plc4j-transport-serial</artifactId>
<version>0.12.0-SNAPSHOT</version>
</dependency>

<dependency>
<groupId>io.netty</groupId>
Expand Down
Expand Up @@ -20,13 +20,16 @@

import io.netty.buffer.ByteBuf;
import org.apache.plc4x.java.ads.configuration.AdsConfiguration;
import org.apache.plc4x.java.ads.configuration.AdsTcpTransportConfiguration;
import org.apache.plc4x.java.ads.discovery.AdsPlcDiscoverer;
import org.apache.plc4x.java.ads.tag.AdsTagHandler;
import org.apache.plc4x.java.ads.protocol.AdsProtocolLogic;
import org.apache.plc4x.java.ads.readwrite.AmsTCPPacket;
import org.apache.plc4x.java.api.messages.PlcDiscoveryRequest;
import org.apache.plc4x.java.api.metadata.PlcDriverMetadata;
import org.apache.plc4x.java.spi.messages.DefaultPlcDiscoveryRequest;
import org.apache.plc4x.java.spi.transport.TransportConfiguration;
import org.apache.plc4x.java.spi.transport.TransportConfigurationTypeProvider;
import org.apache.plc4x.java.spi.values.PlcValueHandler;
import org.apache.plc4x.java.spi.configuration.Configuration;
import org.apache.plc4x.java.spi.connection.GeneratedDriverBase;
Expand All @@ -41,7 +44,7 @@
* - TCP
* - Serial
*/
public class AdsPlcDriver extends GeneratedDriverBase<AmsTCPPacket> {
public class AdsPlcDriver extends GeneratedDriverBase<AmsTCPPacket> implements TransportConfigurationTypeProvider {

@Override
public String getProtocolCode() {
Expand Down Expand Up @@ -137,4 +140,13 @@ public int applyAsInt(ByteBuf byteBuf) {
}
}

@Override
public Class<? extends TransportConfiguration> getTransportConfigurationType(String transportCode) {
switch (transportCode) {
case "tcp":
return AdsTcpTransportConfiguration.class;
}
return null;
}

}
Expand Up @@ -19,22 +19,17 @@
package org.apache.plc4x.java.ads.configuration;

import org.apache.commons.lang3.ArrayUtils;
import org.apache.plc4x.java.ads.readwrite.AdsConstants;
import org.apache.plc4x.java.ads.readwrite.AmsNetId;
import org.apache.plc4x.java.spi.configuration.Configuration;
import org.apache.plc4x.java.spi.configuration.ConfigurationParameterConverter;
import org.apache.plc4x.java.spi.configuration.annotations.ConfigurationParameter;
import org.apache.plc4x.java.spi.configuration.annotations.ParameterConverter;
import org.apache.plc4x.java.spi.configuration.annotations.Required;
import org.apache.plc4x.java.spi.configuration.annotations.*;
import org.apache.plc4x.java.spi.configuration.annotations.defaults.BooleanDefaultValue;
import org.apache.plc4x.java.spi.configuration.annotations.defaults.IntDefaultValue;
import org.apache.plc4x.java.transport.serial.SerialTransportConfiguration;
import org.apache.plc4x.java.transport.tcp.TcpTransportConfiguration;

import java.util.regex.Pattern;
import java.util.stream.Stream;

public class AdsConfiguration implements Configuration, TcpTransportConfiguration, SerialTransportConfiguration {
public class AdsConfiguration implements Configuration {

public static final Pattern AMS_NET_ID_PATTERN =
Pattern.compile("\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}");
Expand Down Expand Up @@ -125,16 +120,6 @@ public void setLoadSymbolAndDataTypeTables(boolean loadSymbolAndDataTypeTables)
this.loadSymbolAndDataTypeTables = loadSymbolAndDataTypeTables;
}

@Override
public int getDefaultPort() {
return AdsConstants.ADSTCPDEFAULTPORT;
}

@Override
public int getBaudRate() {
return 57600;
}

public static class AmsNetIdConverter implements ConfigurationParameterConverter<AmsNetId> {

@Override
Expand Down
@@ -0,0 +1,32 @@
/*
* 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.apache.plc4x.java.ads.configuration;

import org.apache.plc4x.java.ads.readwrite.AdsConstants;
import org.apache.plc4x.java.transport.tcp.DefaultTcpTransportConfiguration;

public class AdsTcpTransportConfiguration extends DefaultTcpTransportConfiguration {

@Override
public int getDefaultPort() {
return AdsConstants.ADSTCPDEFAULTPORT;
}

}
Expand Up @@ -20,19 +20,24 @@

import io.netty.buffer.ByteBuf;
import org.apache.plc4x.java.bacnetip.configuration.BacNetIpConfiguration;
import org.apache.plc4x.java.bacnetip.configuration.BacNetPcapReplayTransportConfiguration;
import org.apache.plc4x.java.bacnetip.configuration.BacNetRawSocketTransportConfiguration;
import org.apache.plc4x.java.bacnetip.configuration.BacNetUdpTransportConfiguration;
import org.apache.plc4x.java.bacnetip.tag.BacNetIpTagHandler;
import org.apache.plc4x.java.bacnetip.protocol.BacNetIpProtocolLogic;
import org.apache.plc4x.java.bacnetip.readwrite.BVLC;
import org.apache.plc4x.java.spi.configuration.Configuration;
import org.apache.plc4x.java.spi.connection.GeneratedDriverBase;
import org.apache.plc4x.java.spi.connection.ProtocolStackConfigurer;
import org.apache.plc4x.java.spi.connection.SingleProtocolStackConfigurer;
import org.apache.plc4x.java.spi.transport.TransportConfiguration;
import org.apache.plc4x.java.spi.transport.TransportConfigurationTypeProvider;
import org.apache.plc4x.java.spi.values.PlcValueHandler;

import java.util.function.Consumer;
import java.util.function.ToIntFunction;

public class BacNetIpDriver extends GeneratedDriverBase<BVLC> {
public class BacNetIpDriver extends GeneratedDriverBase<BVLC> implements TransportConfigurationTypeProvider {

@Override
public String getProtocolCode() {
Expand Down Expand Up @@ -110,4 +115,16 @@ public void accept(ByteBuf byteBuf) {
}
}

@Override
public Class<? extends TransportConfiguration> getTransportConfigurationType(String transportCode) {
switch (transportCode) {
case "udp":
return BacNetUdpTransportConfiguration.class;
case "raw":
return BacNetRawSocketTransportConfiguration.class;
case "pcap":
return BacNetPcapReplayTransportConfiguration.class;
}
return null;
}
}
Expand Up @@ -18,18 +18,10 @@
*/
package org.apache.plc4x.java.bacnetip.configuration;

import org.apache.plc4x.java.bacnetip.readwrite.BacnetConstants;
import org.apache.plc4x.java.spi.configuration.Configuration;
import org.apache.plc4x.java.spi.configuration.annotations.ConfigurationParameter;
import org.apache.plc4x.java.spi.configuration.annotations.defaults.DoubleDefaultValue;
import org.apache.plc4x.java.spi.configuration.annotations.defaults.StringDefaultValue;
import org.apache.plc4x.java.transport.pcapreplay.PcapReplayTransportConfiguration;
import org.apache.plc4x.java.transport.rawsocket.RawSocketTransportConfiguration;
import org.apache.plc4x.java.transport.udp.UdpTransportConfiguration;
import org.apache.plc4x.java.utils.pcap.netty.handlers.PacketHandler;
import org.pcap4j.packet.Dot1qVlanTagPacket;

public class BacNetIpConfiguration implements Configuration, UdpTransportConfiguration, RawSocketTransportConfiguration, PcapReplayTransportConfiguration {
public class BacNetIpConfiguration implements Configuration {

// Path to a single EDE file.
@ConfigurationParameter("ede-file-path")
Expand All @@ -39,19 +31,6 @@ public class BacNetIpConfiguration implements Configuration, UdpTransportConfigu
@ConfigurationParameter("ede-directory-path")
private String edeDirectoryPath;

// The speed in which the pcap file is replayed:
// - 1.0 being the original speed
// - 0 being as fast as possible (no delays between the packets)
// - 0.5 being double speed
// - 2.0 being half speed
@ConfigurationParameter("pcap-replay-speed")
@DoubleDefaultValue(1.0F)
private double pcapReplaySpeed;

@ConfigurationParameter("filter")
@StringDefaultValue("")
private String filter = "";

public String getEdeFilePath() {
return edeFilePath;
}
Expand All @@ -68,61 +47,4 @@ public void setEdeDirectoryPath(String edeDirectoryPath) {
this.edeDirectoryPath = edeDirectoryPath;
}

public void setPcapReplaySpeed(double pcapReplaySpeed) {
this.pcapReplaySpeed = pcapReplaySpeed;
}

@Override
public float getReplaySpeedFactor() {
return (float) pcapReplaySpeed;
}

@Override
public boolean getSupportVlans() {
return true;
}

@Override
public int getDefaultPort() {
return BacnetConstants.BACNETUDPDEFAULTPORT;
}

@Override
public Integer getProtocolId() {
return null;
}

@Override
public String getFilter() {
return filter;
}

public void setFilter(String filter) {
this.filter = filter;
}

/**
* Packet handler to use when running in PCAP mode.
* In this case all packets are Ethernet frames and we need to first get the
* IP packet and then the UDP packet and then the raw data from that.
*
* @return payload of the packet.
*/
@Override
public PacketHandler getPcapPacketHandler() {
return packet -> {
// If it's a VLan packet, we need to go one level deeper.
if (packet.getPayload() instanceof Dot1qVlanTagPacket) {
return packet.getPayload().getPayload().getPayload().getPayload().getRawData();
}
// This is a normal udp packet.
else {
if ((packet.getPayload() != null) && (packet.getPayload().getPayload() != null) && (packet.getPayload().getPayload().getPayload() != null)) {
return packet.getPayload().getPayload().getPayload().getRawData();
}
}
return null;
};
}

}

0 comments on commit 554c083

Please sign in to comment.