Skip to content

Commit

Permalink
extract SDK > update OpcUaProtocolAdapter to remove dependency on ABCs
Browse files Browse the repository at this point in the history
  • Loading branch information
DC2-DanielKrueger committed Apr 29, 2024
1 parent 34f31d3 commit 10b0a25
Show file tree
Hide file tree
Showing 24 changed files with 443 additions and 280 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,7 @@ public AbstractProtocolAdapter(final @NotNull ProtocolAdapterInformation adapter
Preconditions.checkNotNull(metricRegistry);
this.adapterInformation = adapterInformation;
this.adapterConfig = adapterConfig;
this.protocolAdapterMetricsHelper = new ProtocolAdapterMetricsHelper(adapterInformation.getProtocolId(),
adapterConfig.getId(), metricRegistry);
this.protocolAdapterMetricsHelper = null;
this.objectMapper = new ObjectMapper();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,12 @@ public class AdapterStatusModelConversionUtils {

public static @NotNull Status getAdapterStatus(final ProtocolAdapterWrapper protocolAdapterWrapper){
Preconditions.checkNotNull(protocolAdapterWrapper);
final ProtocolAdapter protocolAdapter = protocolAdapterWrapper.getAdapter();
return new Status(
convertRuntimeStatus(protocolAdapter.getRuntimeStatus()),
convertConnectionStatus(protocolAdapter.getConnectionStatus()),
protocolAdapter.getId(), ApiConstants.ADAPTER_TYPE,
convertRuntimeStatus(protocolAdapterWrapper.getRuntimeStatus()),
convertConnectionStatus(protocolAdapterWrapper.getConnectionStatus()),
protocolAdapterWrapper.getId(), ApiConstants.ADAPTER_TYPE,
protocolAdapterWrapper.getTimeOfLastStartAttempt(), null,
protocolAdapter.getErrorMessage());
protocolAdapterWrapper.getErrorMessage());
}

public static @NotNull Status.CONNECTION_STATUS convertConnectionStatus(@NotNull final ProtocolAdapter.ConnectionStatus connectionStatus){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,10 +156,10 @@ public ProtocolAdaptersResourceImpl(
} finally {
Thread.currentThread().setContextClassLoader(contextClassLoader);
}
return new Adapter(value.getAdapter().getId(),
return new Adapter(value.getId(),
value.getAdapterInformation().getProtocolId(),
configObject,
getStatusInternal(value.getAdapter().getId()));
getStatusInternal(value.getId()));
}

@Override
Expand All @@ -181,7 +181,7 @@ public ProtocolAdaptersResourceImpl(
try {
Thread.currentThread()
.setContextClassLoader(adapterInstance.getAdapterFactory().getClass().getClassLoader());
adapterInstance.getAdapter().discoverValues(new ProtocolAdapterDiscoveryInput() {
adapterInstance.discoverValues(new ProtocolAdapterDiscoveryInput() {
@Override
public @Nullable String getRootNode() {
return rootNode;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package com.hivemq.edge.modules.adapters.impl.factories;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.hivemq.api.error.ApiException;
import com.hivemq.api.model.core.Payload;
import com.hivemq.api.model.core.PayloadImpl;
import com.hivemq.edge.model.TypeIdentifier;
import com.hivemq.edge.model.TypeIdentifierImpl;
import com.hivemq.edge.modules.adapters.data.DataPointImpl;
import com.hivemq.edge.modules.adapters.factories.AdapterFactories;
Expand All @@ -12,6 +15,7 @@
import com.hivemq.edge.modules.adapters.factories.PayloadFactory;
import com.hivemq.edge.modules.adapters.factories.TypeIdentifierFactory;
import com.hivemq.edge.modules.adapters.factories.UserPropertyFactory;
import com.hivemq.edge.modules.api.events.model.EventBuilder;
import com.hivemq.edge.modules.api.events.model.EventBuilderImpl;
import com.hivemq.edge.modules.config.impl.AdapterSubscriptionImpl;
import com.hivemq.edge.modules.config.impl.UserPropertyImpl;
Expand All @@ -32,7 +36,19 @@ public class AdapterFactoriesImpl implements AdapterFactories {

@Override
public @NotNull PayloadFactory payloadFactory() {
return PayloadImpl::new;
return new PayloadFactory() {
@Override
public @NotNull Payload create(
final Payload.@NotNull ContentType contentType,
final @NotNull String content) {
return PayloadImpl.from(contentType, content);
}

@Override
public @NotNull Payload create(final @NotNull ObjectMapper mapper, final @NotNull Object data) {
return PayloadImpl.fromObject(mapper, data);
}
};
}

@Override
Expand All @@ -47,7 +63,14 @@ public class AdapterFactoriesImpl implements AdapterFactories {

@Override
public @NotNull EventBuilderFactory eventBuilderFactory() {
return EventBuilderImpl::new;
return (final @NotNull String id, final @NotNull String protocolId) -> {

final EventBuilder eventBuilder = new EventBuilderImpl()
.withTimestamp(System.currentTimeMillis())
.withSource(TypeIdentifierImpl.create(TypeIdentifier.TYPE.ADAPTER, id))
.withAssociatedObject(TypeIdentifierImpl.create(TypeIdentifier.TYPE.ADAPTER_TYPE, protocolId));
return eventBuilder;
};
}

@Override
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,7 @@
*
* @author Simon L Johnson
*/
public class ProtocolAdapterMetricsHelper {


public class ProtocolAdapterMetricsHelperImpl implements ProtocolAdapterMetricsHelper {

private @NotNull String protocolAdapterType;
private @NotNull String protocolAdapterId;
Expand All @@ -49,9 +47,9 @@ public class ProtocolAdapterMetricsHelper {
protected Counter connectionSuccessCounter;
protected Counter connectionFailedCounter;

public ProtocolAdapterMetricsHelper(final @NotNull String protocolAdapterType,
final @NotNull String protocolAdapterId,
final @NotNull MetricRegistry metricRegistry) {
public ProtocolAdapterMetricsHelperImpl(final @NotNull String protocolAdapterType,
final @NotNull String protocolAdapterId,
final @NotNull MetricRegistry metricRegistry) {
Preconditions.checkNotNull(protocolAdapterType);
Preconditions.checkNotNull(protocolAdapterId);
Preconditions.checkNotNull(metricRegistry);
Expand All @@ -71,13 +69,15 @@ protected void initRegistry(){
/**
* Use to indicate a read from the adapter has been successfully PUBLISHed
*/
@Override
public void incrementReadPublishSuccess(){
publishSuccessCounter.inc();
}

/**
* Use to indicate a read from the adapter has failed
*/
@Override
public void incrementReadPublishFailure(){
publishFailedCounter.inc();
}
Expand All @@ -86,13 +86,15 @@ public void incrementReadPublishFailure(){
/**
* Use to indicate a connection attempt to the device has failed
*/
@Override
public void incrementConnectionFailure(){
connectionFailedCounter.inc();
}

/**
* Use to indicate a connection attempt to the device has succeeded
*/
@Override
public void incrementConnectionSuccess(){
connectionSuccessCounter.inc();
}
Expand All @@ -101,6 +103,7 @@ public void incrementConnectionSuccess(){
* Increment an arbitrary counter in the adapter instance namespace
* @param metricName - the metric name to be incremented (inside) the adapter namespace
*/
@Override
public void increment(final @NotNull String metricName){
Preconditions.checkNotNull(metricName);
metricRegistry.counter(createAdapterMetricsNamespace(metricName)).inc();
Expand All @@ -110,6 +113,7 @@ public void increment(final @NotNull String metricName){
* Will clear down all metrics in the registry created by this metrics helper.
* NB: metrics created outside the context of this helper will not be touched.
*/
@Override
public void clearAll(){
Preconditions.checkNotNull(metricRegistry);
synchronized (mutex){
Expand Down
Loading

0 comments on commit 10b0a25

Please sign in to comment.