Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NMS-16496: Backport NMS-16184 to foundation-2023 #7401

Merged
merged 3 commits into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion container/features/src/main/resources/features.xml
Original file line number Diff line number Diff line change
Expand Up @@ -674,8 +674,8 @@
</feature>
<feature name="opennms-provisioning-api" version="${project.version}" description="OpenNMS :: Provisioning :: API">
<feature>opennms-model</feature>
<feature version="${netty4Version}">netty</feature>
<bundle dependency="true">mvn:org.apache.mina/mina-core/${minaVersion}</bundle>
<bundle dependency="true">mvn:io.netty/netty/${netty3Version}</bundle>
<bundle dependency="true">mvn:org.opennms.core/org.opennms.core.lib/${project.version}</bundle>
<feature>opentracing-api</feature>
<feature>commons-lang</feature>
Expand Down
4 changes: 2 additions & 2 deletions dependencies/netty/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
<!-- Netty -->
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty</artifactId>
<version>${netty3Version}</version>
<artifactId>netty-all</artifactId>
<version>${netty4Version}</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -41,22 +41,8 @@
import java.util.Date;
import java.util.List;

import org.jboss.netty.bootstrap.ServerBootstrap;
import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.ChannelFactory;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.channel.ChannelPipeline;
import org.jboss.netty.channel.ChannelPipelineFactory;
import org.jboss.netty.channel.Channels;
import org.jboss.netty.channel.ExceptionEvent;
import org.jboss.netty.channel.MessageEvent;
import org.jboss.netty.channel.SimpleChannelHandler;
import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;
import org.jboss.netty.handler.codec.protobuf.ProtobufDecoder;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.junit.runner.RunWith;
Expand All @@ -77,8 +63,16 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;

import com.google.protobuf.InvalidProtocolBufferException;
import org.awaitility.core.ConditionTimeoutException;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.protobuf.ProtobufDecoder;

@RunWith(OpenNMSJUnit4ClassRunner.class)
@ContextConfiguration(locations={
Expand All @@ -99,18 +93,22 @@ public class TcpOutputStrategyTest {
public static void setUpClass() {
// Setup a quick Netty TCP server that decodes the protobuf messages
// and appends these to a list when received
ChannelFactory factory = new NioServerSocketChannelFactory();
ServerBootstrap bootstrap = new ServerBootstrap(factory);
bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
public ChannelPipeline getPipeline() {
return Channels.pipeline(
new ProtobufDecoder(PerformanceDataReadings.getDefaultInstance()),
new PerfDataServerHandler());
}
});
bootstrap.setOption("reuseAddress", true);
Channel channel = bootstrap.bind(new InetSocketAddress(0));
InetSocketAddress addr = (InetSocketAddress)channel.getLocalAddress();
EventLoopGroup bossLoopGroup = new NioEventLoopGroup();
EventLoopGroup workerLoopGroup = new NioEventLoopGroup();

ServerBootstrap bootstrap = new ServerBootstrap();
bootstrap.group(bossLoopGroup, workerLoopGroup)
.childOption(ChannelOption.SO_REUSEADDR, true)
.channel(NioServerSocketChannel.class)
.childHandler(new ChannelInitializer<>() {
@Override
protected void initChannel(Channel channel) throws Exception {
channel.pipeline().addLast(new ProtobufDecoder(PerformanceDataReadings.getDefaultInstance())).addLast(new PerfDataServerHandler());
}
});

Channel channel = bootstrap.bind(new InetSocketAddress(0)).syncUninterruptibly().channel();
InetSocketAddress addr = (InetSocketAddress)channel.localAddress();

// Point the TCP exporter to our server
System.setProperty("org.opennms.rrd.tcp.host", addr.getHostString());
Expand All @@ -121,10 +119,11 @@ public ChannelPipeline getPipeline() {
System.setProperty("rrd.base.dir", tempFolder.getRoot().getAbsolutePath());
}

public static class PerfDataServerHandler extends SimpleChannelHandler {
public static class PerfDataServerHandler extends ChannelInboundHandlerAdapter {

@Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) {
allReadings.add((PerformanceDataReadings) e.getMessage());
public void channelRead(ChannelHandlerContext ctx, Object obj) {
allReadings.add((PerformanceDataReadings) obj);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@

package org.opennms.netmgt.provision.detector.simple;

import org.jboss.netty.channel.ChannelPipeline;
import org.jboss.netty.handler.codec.frame.DelimiterBasedFrameDecoder;
import org.jboss.netty.handler.codec.frame.Delimiters;
import org.jboss.netty.handler.codec.string.StringDecoder;
import org.jboss.netty.handler.codec.string.StringEncoder;
import org.jboss.netty.util.CharsetUtil;
import io.netty.channel.ChannelPipeline;
import io.netty.handler.codec.Delimiters;
import io.netty.handler.codec.DelimiterBasedFrameDecoder;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import io.netty.util.CharsetUtil;
import org.opennms.netmgt.provision.detector.simple.request.LineOrientedRequest;
import org.opennms.netmgt.provision.detector.simple.response.LineOrientedResponse;
import org.opennms.netmgt.provision.detector.simple.support.LineOrientedRequestEncoder;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,13 @@

package org.opennms.netmgt.provision.detector.simple;

import org.jboss.netty.channel.ChannelPipeline;
import org.jboss.netty.handler.codec.frame.DelimiterBasedFrameDecoder;
import org.jboss.netty.handler.codec.frame.Delimiters;
import org.jboss.netty.handler.codec.string.StringDecoder;
import org.jboss.netty.handler.codec.string.StringEncoder;
import org.jboss.netty.util.CharsetUtil;
import io.netty.channel.ChannelPipeline;
import io.netty.handler.codec.Delimiters;
import io.netty.handler.codec.DelimiterBasedFrameDecoder;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import io.netty.util.CharsetUtil;

import org.opennms.netmgt.provision.detector.simple.request.LineOrientedRequest;
import org.opennms.netmgt.provision.detector.simple.response.MultilineOrientedResponse;
import org.opennms.netmgt.provision.detector.simple.support.LineOrientedRequestEncoder;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,25 +28,28 @@

package org.opennms.netmgt.provision.detector.simple.support;

import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.handler.codec.oneone.OneToOneEncoder;
import java.util.List;

import io.netty.channel.ChannelHandlerContext;
import org.opennms.netmgt.provision.detector.simple.request.LineOrientedRequest;

import io.netty.handler.codec.MessageToMessageDecoder;
import io.netty.handler.codec.MessageToMessageEncoder;

/**
* <p>LineOrientedRequestEncoder class.</p>
*
* @author Seth
*/
public class LineOrientedRequestEncoder extends OneToOneEncoder {
public class LineOrientedRequestEncoder extends MessageToMessageEncoder<Object> {

/**
* This method encodes {@link LineOrientedRequest} objects into {@link String} instances
* that contain the byte representation of the request.
*/
@Override
public Object encode(final ChannelHandlerContext ctx, final Channel channel, final Object msg) throws Exception {
public void encode(final ChannelHandlerContext ctx, final Object msg, final List<Object> messages) throws Exception {
LineOrientedRequest request = (LineOrientedRequest)msg;
return request.getRequest();
messages.add(request.getRequest());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,24 +28,26 @@

package org.opennms.netmgt.provision.detector.simple.support;

import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.handler.codec.oneone.OneToOneDecoder;
import java.util.List;

import io.netty.channel.ChannelHandlerContext;
import org.opennms.netmgt.provision.detector.simple.response.LineOrientedResponse;

import io.netty.handler.codec.MessageToMessageDecoder;

/**
* <p>LineOrientedResponseDecoder class.</p>
*
* @author Seth
*/
public class LineOrientedResponseDecoder extends OneToOneDecoder {
public class LineOrientedResponseDecoder extends MessageToMessageDecoder<Object> {

/**
* This method decodes {@link String} objects into {@link LineOrientedResponse} instances
* that contain the byte representation of the response.
*/
@Override
public Object decode(final ChannelHandlerContext ctx, final Channel channel, final Object msg) throws Exception {
return new LineOrientedResponse((String)msg);
public void decode(final ChannelHandlerContext ctx, final Object msg, final List<Object> messages) throws Exception {
messages.add(new LineOrientedResponse((String)msg));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,28 +28,25 @@

package org.opennms.netmgt.provision.detector.simple.support;

import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.handler.codec.oneone.OneToOneDecoder;
import java.util.List;

import io.netty.channel.ChannelHandlerContext;

import io.netty.handler.codec.MessageToMessageDecoder;
import org.opennms.netmgt.provision.detector.simple.response.MultilineOrientedResponse;

/**
* <p>MultilineOrientedResponseDecoder class.</p>
*
* @author Seth
*/
public class MultilineOrientedResponseDecoder extends OneToOneDecoder {
public class MultilineOrientedResponseDecoder extends MessageToMessageDecoder<Object> {

public static final String DEFAULT_MULTILINE_INDICATOR = "-";

private final String m_multilineIndicator;
private MultilineOrientedResponse m_response;

/**
* <p>Constructor for MultilineOrientedResponseDecoder.</p>
*
* @param multilineIndicator a {@link java.lang.String} object.
*/
public MultilineOrientedResponseDecoder() {
this(DEFAULT_MULTILINE_INDICATOR);
}
Expand All @@ -68,10 +65,10 @@ public MultilineOrientedResponseDecoder(final String multilineIndicator) {
* that contain each line of the string response.
*/
@Override
protected Object decode(final ChannelHandlerContext ctx, final Channel channel, final Object msg) throws Exception {
protected void decode(final ChannelHandlerContext ctx, final Object msg, final List<Object> messages) throws Exception {
// We can only decode strings
if (!(msg instanceof String)) {
return msg;
messages.add(msg);
}
// Construct a new response if there isn't one yet
if (m_response == null) {
Expand All @@ -82,11 +79,10 @@ protected Object decode(final ChannelHandlerContext ctx, final Channel channel,
if(checkIndicator(response)) {
// Do nothing; if the multi-line indicator is present then
// continue to accumulate lines into the m_response instance
return null;
} else {
MultilineOrientedResponse retval = m_response;
m_response = null;
return retval;
messages.add(retval);
}
}

Expand Down
Loading
Loading