use of org.openremote.model.syslog.SyslogCategory.PROTOCOL in project openremote by openremote.
the class SNMPProtocol method doStart.
@Override
protected void doStart(Container container) throws Exception {
String snmpBindHost = agent.getBindHost().orElseThrow(() -> {
String msg = "No SNMP bind host provided for protocol: " + this;
LOG.info(msg);
return new IllegalArgumentException(msg);
});
Integer snmpBindPort = agent.getBindPort().orElse(162);
SNMPAgent.SNMPVersion snmpVersion = agent.getSNMPVersion().orElse(SNMPAgent.SNMPVersion.V2c);
String snmpUri = String.format("snmp:%s:%d?protocol=udp&type=TRAP&snmpVersion=%d", snmpBindHost, snmpBindPort, snmpVersion.getVersion());
messageBrokerContext.addRoutes(new RouteBuilder() {
@Override
public void configure() {
from(snmpUri).routeId(getProtocolName() + getAgent().getId()).process(exchange -> {
SnmpMessage msg = exchange.getIn(SnmpMessage.class);
LOG.fine(String.format("Message received: %s", msg));
PDU pdu = msg.getSnmpMessage();
AttributeRef wildCardAttributeRef;
if ((wildCardAttributeRef = oidMap.get("*")) != null) {
ObjectNode wildCardValue = ValueUtil.createJsonObject();
pdu.getVariableBindings().forEach(variableBinding -> {
wildCardValue.put(variableBinding.getOid().format(), variableBinding.toValueString());
});
updateLinkedAttribute(new AttributeState(wildCardAttributeRef, wildCardValue));
}
pdu.getVariableBindings().forEach(variableBinding -> {
AttributeRef attributeRef = oidMap.get(variableBinding.getOid().format());
if (attributeRef != null) {
updateLinkedAttribute(new AttributeState(attributeRef, variableBinding.toValueString()));
}
});
});
}
});
setConnectionStatus(ConnectionStatus.CONNECTED);
}
use of org.openremote.model.syslog.SyslogCategory.PROTOCOL in project openremote by openremote.
the class AbstractProtocol method start.
@Override
public void start(Container container) throws Exception {
timerService = container.getService(TimerService.class);
executorService = container.getExecutorService();
assetService = container.getService(ProtocolAssetService.class);
predictedAssetService = container.getService(ProtocolPredictedAssetService.class);
messageBrokerContext = container.getService(MessageBrokerService.class).getContext();
withLock(getProtocolName() + "::start", () -> {
try {
messageBrokerContext.addRoutes(new RouteBuilder() {
@Override
public void configure() throws Exception {
from(ACTUATOR_TOPIC).routeId("Actuator-" + getProtocolName() + getAgent().getId()).process(exchange -> {
Protocol<?> protocolInstance = exchange.getIn().getHeader(ACTUATOR_TOPIC_TARGET_PROTOCOL, Protocol.class);
if (protocolInstance != AbstractProtocol.this) {
return;
}
AttributeEvent event = exchange.getIn().getBody(AttributeEvent.class);
Attribute<?> linkedAttribute = getLinkedAttributes().get(event.getAttributeRef());
if (linkedAttribute == null) {
LOG.info("Attempt to write to attribute that is not actually linked to this protocol '" + AbstractProtocol.this + "': " + linkedAttribute);
return;
}
processLinkedAttributeWrite(event);
});
}
});
doStart(container);
} catch (Exception ex) {
throw new RuntimeException(ex);
}
});
this.producerTemplate = container.getService(MessageBrokerService.class).getProducerTemplate();
}
use of org.openremote.model.syslog.SyslogCategory.PROTOCOL in project openremote by openremote.
the class AbstractIOClientProtocol method getGenericStringEncodersAndDecoders.
/**
* Supplies a set of encoders/decoders that convert from/to {@link String} to/from {@link ByteBuf} based on the generic protocol {@link Attribute}s
*/
public static Supplier<ChannelHandler[]> getGenericStringEncodersAndDecoders(AbstractNettyIOClient<String, ?> client, IOAgent<?, ?, ?> agent) {
boolean hexMode = agent.getMessageConvertHex().orElse(false);
boolean binaryMode = agent.getMessageConvertBinary().orElse(false);
Charset charset = agent.getMessageCharset().map(Charset::forName).orElse(CharsetUtil.UTF_8);
int maxLength = agent.getMessageMaxLength().orElse(Integer.MAX_VALUE);
String[] delimiters = agent.getMessageDelimiters().orElse(new String[0]);
boolean stripDelimiter = agent.getMessageStripDelimiter().orElse(false);
return () -> {
List<ChannelHandler> encodersDecoders = new ArrayList<>();
if (hexMode || binaryMode) {
encodersDecoders.add(new AbstractNettyIOClient.MessageToByteEncoder<>(String.class, client, (msg, out) -> {
byte[] bytes = hexMode ? ProtocolUtil.bytesFromHexString(msg) : ProtocolUtil.bytesFromBinaryString(msg);
out.writeBytes(bytes);
}));
if (delimiters.length > 0) {
ByteBuf[] byteDelimiters = Arrays.stream(delimiters).map(delim -> Unpooled.wrappedBuffer(hexMode ? ProtocolUtil.bytesFromHexString(delim) : ProtocolUtil.bytesFromBinaryString(delim))).toArray(ByteBuf[]::new);
encodersDecoders.add(new DelimiterBasedFrameDecoder(maxLength, stripDelimiter, byteDelimiters));
} else {
encodersDecoders.add(new FixedLengthFrameDecoder(maxLength));
}
// Incoming messages will be bytes
encodersDecoders.add(new AbstractNettyIOClient.ByteToMessageDecoder<>(client, (byteBuf, messages) -> {
byte[] bytes = new byte[byteBuf.readableBytes()];
byteBuf.readBytes(bytes);
String msg = hexMode ? ProtocolUtil.bytesToHexString(bytes) : ProtocolUtil.bytesToBinaryString(bytes);
messages.add(msg);
}));
} else {
encodersDecoders.add(new StringEncoder(charset));
if (agent.getMessageMaxLength().isPresent()) {
encodersDecoders.add(new FixedLengthFrameDecoder(maxLength));
} else {
ByteBuf[] byteDelimiters;
if (delimiters.length > 0) {
byteDelimiters = Arrays.stream(delimiters).map(delim -> Unpooled.wrappedBuffer(delim.getBytes(charset))).toArray(ByteBuf[]::new);
} else {
byteDelimiters = Delimiters.lineDelimiter();
}
encodersDecoders.add(new DelimiterBasedFrameDecoder(maxLength, stripDelimiter, byteDelimiters));
}
encodersDecoders.add(new StringDecoder(charset));
encodersDecoders.add(new AbstractNettyIOClient.MessageToMessageDecoder<>(String.class, client));
}
return encodersDecoders.toArray(new ChannelHandler[0]);
};
}
use of org.openremote.model.syslog.SyslogCategory.PROTOCOL in project openremote by openremote.
the class AbstractHTTPServerProtocol method deploy.
protected void deploy(DeploymentInfo deploymentInfo) {
LOG.info("Deploying JAX-RS deployment for protocol instance : " + this);
DeploymentManager manager = Servlets.defaultContainer().addDeployment(deploymentInfo);
manager.deploy();
HttpHandler httpHandler;
// Get realm from owning agent asset
String agentRealm = agent.getRealm();
if (TextUtil.isNullOrEmpty(agentRealm)) {
throw new IllegalStateException("Cannot determine the realm that this agent belongs to");
}
try {
httpHandler = manager.start();
// Wrap the handler to inject the realm
HttpHandler handlerWrapper = exchange -> {
exchange.getRequestHeaders().put(HttpString.tryFromString(Constants.REALM_PARAM_NAME), agentRealm);
httpHandler.handleRequest(exchange);
};
WebService.RequestHandler requestHandler = pathStartsWithHandler(deploymentInfo.getDeploymentName(), deploymentInfo.getContextPath(), handlerWrapper);
LOG.info("Registering HTTP Server Protocol request handler '" + this.getClass().getSimpleName() + "' for request path: " + deploymentInfo.getContextPath());
// Add the handler before the greedy deployment handler
webService.getRequestHandlers().add(0, requestHandler);
deployment = new DeploymentInstance(deploymentInfo, requestHandler);
} catch (ServletException e) {
LOG.severe("Failed to deploy deployment: " + deploymentInfo.getDeploymentName());
}
}
Aggregations