*/
package org.opendaylight.protocol.pcep.testtool;
+import io.netty.channel.socket.SocketChannel;
import io.netty.util.HashedWheelTimer;
+import io.netty.util.concurrent.DefaultPromise;
+import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.GlobalEventExecutor;
+import io.netty.util.concurrent.Promise;
import java.net.InetSocketAddress;
-import java.util.List;
-import org.opendaylight.protocol.framework.DispatcherImpl;
+import org.opendaylight.protocol.framework.AbstractDispatcher;
import org.opendaylight.protocol.framework.NeverReconnectStrategy;
-import org.opendaylight.protocol.pcep.PCEPMessage;
-import org.opendaylight.protocol.pcep.PCEPSession;
+import org.opendaylight.protocol.framework.ProtocolHandlerFactory;
+import org.opendaylight.protocol.framework.ProtocolSession;
+import org.opendaylight.protocol.framework.ReconnectStrategy;
+import org.opendaylight.protocol.framework.SessionListener;
+import org.opendaylight.protocol.framework.SessionListenerFactory;
+import org.opendaylight.protocol.framework.SessionNegotiatorFactory;
import org.opendaylight.protocol.pcep.PCEPSessionListener;
-import org.opendaylight.protocol.pcep.PCEPTerminationReason;
-import org.opendaylight.protocol.pcep.PCEPTlv;
import org.opendaylight.protocol.pcep.impl.DefaultPCEPSessionNegotiatorFactory;
-import org.opendaylight.protocol.pcep.impl.PCEPDispatcherImpl;
-import org.opendaylight.protocol.pcep.object.PCEPOpenObject;
-import org.opendaylight.protocol.pcep.tlv.NodeIdentifierTlv;
+import org.opendaylight.protocol.pcep.impl.PCEPHandlerFactory;
+import org.opendaylight.protocol.pcep.impl.PCEPSessionImpl;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Message;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.message.open.message.OpenBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.TlvsBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.tlvs.PredundancyGroupIdBuilder;
-import com.google.common.collect.Lists;
+import com.google.common.base.Preconditions;
-public class PCCMock {
+public class PCCMock<M, S extends ProtocolSession<M>, L extends SessionListener<M, ?, ?>> extends AbstractDispatcher<S, L> {
- public static void main(final String[] args) throws Exception {
- final List<PCEPTlv> tlvs = Lists.newArrayList();
- tlvs.add(new NodeIdentifierTlv(new byte[] { (byte) 127, (byte) 2, (byte) 3, (byte) 7 }));
-
- final DispatcherImpl di = new DispatcherImpl();
- final PCEPDispatcherImpl d = new PCEPDispatcherImpl(di,
- new DefaultPCEPSessionNegotiatorFactory(new HashedWheelTimer(), new PCEPOpenObject(30, 120, 0, tlvs), 0));
-
- try {
- d.createClient(new InetSocketAddress("127.0.0.3", 12345),
- new PCEPSessionListener() {
-
- @Override
- public void onMessage(final PCEPSession session, final PCEPMessage message) {
- // TODO Auto-generated method stub
+ private final SessionNegotiatorFactory<M, S, L> negotiatorFactory;
+ private final ProtocolHandlerFactory<?> factory;
- }
-
- @Override
- public void onSessionUp(final PCEPSession session) {
- // TODO Auto-generated method stub
+ public PCCMock(final SessionNegotiatorFactory<M, S, L> negotiatorFactory, final ProtocolHandlerFactory<?> factory,
+ final DefaultPromise<PCEPSessionImpl> defaultPromise) {
+ this.negotiatorFactory = Preconditions.checkNotNull(negotiatorFactory);
+ this.factory = Preconditions.checkNotNull(factory);
+ }
- }
+ public Future<S> createClient(final InetSocketAddress address, final ReconnectStrategy strategy,
+ final SessionListenerFactory<L> listenerFactory) {
+ return super.createClient(address, strategy, new PipelineInitializer<S>() {
+ @Override
+ public void initializeChannel(final SocketChannel ch, final Promise<S> promise) {
+ ch.pipeline().addLast(PCCMock.this.factory.getDecoders());
+ ch.pipeline().addLast("negotiator", PCCMock.this.negotiatorFactory.getSessionNegotiator(listenerFactory, ch, promise));
+ ch.pipeline().addLast(PCCMock.this.factory.getEncoders());
+ }
+ });
+ }
- @Override
- public void onSessionDown(final PCEPSession session, final Exception e) {
- // TODO Auto-generated method stub
+ public static void main(final String[] args) throws Exception {
+ final TlvsBuilder builder = new TlvsBuilder();
+ builder.setPredundancyGroupId(new PredundancyGroupIdBuilder().setIdentifier(new byte[] { (byte) 127, (byte) 2, (byte) 3, (byte) 7 }).build());
- }
+ final SessionNegotiatorFactory<Message, PCEPSessionImpl, PCEPSessionListener> snf = new DefaultPCEPSessionNegotiatorFactory(new HashedWheelTimer(), new OpenBuilder().setKeepalive(
+ (short) 30).setDeadTimer((short) 120).setSessionId((short) 0).setTlvs(builder.build()).build(), 0);
- @Override
- public void onSessionTerminated(final PCEPSession session,
- final PCEPTerminationReason cause) {
- // TODO Auto-generated method stub
+ final PCCMock<Message, PCEPSessionImpl, PCEPSessionListener> pcc = new PCCMock<>(snf, new PCEPHandlerFactory(), new DefaultPromise<PCEPSessionImpl>(GlobalEventExecutor.INSTANCE));
- }
- }, new NeverReconnectStrategy(GlobalEventExecutor.INSTANCE, 2000)).get();
+ pcc.createClient(new InetSocketAddress("127.0.0.3", 12345), new NeverReconnectStrategy(GlobalEventExecutor.INSTANCE, 2000),
+ new SessionListenerFactory<PCEPSessionListener>() {
- // Thread.sleep(5000);
- // final List<CompositeRequestObject> cro = new ArrayList<CompositeRequestObject>();
- // cro.add(new CompositeRequestObject(new PCEPRequestParameterObject(false, true, true, true, true, (short)
- // 4, 123, false, false),
- // new PCEPEndPointsObject<IPv4Address>(new IPv4Address(InetAddress.getByName("10.0.0.3")), new
- // IPv4Address(InetAddress.getByName("10.0.0.5")))));
- // for (int i = 0; i < 3; i++) {
- // Thread.sleep(1000);
- // session.sendMessage(new PCEPRequestMessage(cro));
- // }
- // Thread.sleep(5000);
- // Thread.sleep(1000);
- } finally {
- // di.stop();
- }
+ @Override
+ public PCEPSessionListener getSessionListener() {
+ return new SimpleSessionListener();
+ }
+ }).get();
}
}