2 * Copyright (c) 2016 Pantheon Technologies s.r.o. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.openflowjava.protocol.impl.clients;
10 import java.net.InetAddress;
11 import java.util.concurrent.Callable;
13 import com.google.common.base.Preconditions;
14 import com.google.common.util.concurrent.SettableFuture;
15 import io.netty.bootstrap.Bootstrap;
16 import io.netty.channel.EventLoopGroup;
17 import io.netty.channel.socket.nio.NioSocketChannel;
18 import org.slf4j.LoggerFactory;
22 * Callable client class, inspired by SimpleClient class
23 * Simulating device/switch connected to controller
24 * @author Jozef Bacigal
27 public class CallableClient implements Callable<Boolean>, OFClient {
29 private static final org.slf4j.Logger LOG = LoggerFactory.getLogger(CallableClient.class);
31 private int port = 6653;
32 private boolean securedClient = false;
33 private InetAddress ipAddress = null;
34 private String name = "Empty name";
36 private final EventLoopGroup workerGroup;
37 private SettableFuture<Boolean> isOnlineFuture;
38 private SettableFuture<Boolean> scenarioDone;
39 private ScenarioHandler scenarioHandler = null;
40 private Bootstrap bootstrap = null;
42 public CallableClient(
44 final boolean securedClient,
45 final InetAddress ipAddress,
47 final ScenarioHandler scenarioHandler,
48 final Bootstrap bootstrap,
49 final EventLoopGroup eventExecutors) {
51 Preconditions.checkNotNull(ipAddress, "IP address cannot be null");
52 Preconditions.checkNotNull(scenarioHandler, "Scenario handler cannot be null");
54 this.securedClient = securedClient;
55 this.ipAddress = ipAddress;
56 this.workerGroup = eventExecutors;
57 this.bootstrap = bootstrap;
59 this.scenarioHandler = scenarioHandler;
63 public SettableFuture<Boolean> getIsOnlineFuture() {
64 return isOnlineFuture;
68 public SettableFuture<Boolean> getScenarioDone() {
73 public void setScenarioHandler(final ScenarioHandler scenario) {
74 this.scenarioHandler = scenario;
78 public void setSecuredClient(final boolean securedClient) {
79 this.securedClient = securedClient;
84 public Boolean call() throws Exception {
85 Preconditions.checkNotNull(bootstrap);
86 Preconditions.checkNotNull(workerGroup);
87 LOG.info("Switch {} trying connect to controller", this.name);
88 SimpleClientInitializer clientInitializer = new SimpleClientInitializer(isOnlineFuture, securedClient);
89 clientInitializer.setScenario(scenarioHandler);
91 bootstrap.group(workerGroup)
92 .channel(NioSocketChannel.class)
93 .handler(clientInitializer);
95 bootstrap.connect(ipAddress, port).sync();
96 synchronized (scenarioHandler) {
97 LOG.debug("WAITING FOR SCENARIO");
98 while (!scenarioHandler.isScenarioFinished()) {
99 scenarioHandler.wait();
102 } catch (Exception ex) {
103 LOG.error(ex.getMessage(), ex);
106 if (scenarioHandler.isFinishedOK()) {
107 LOG.info("Device {} finished scenario OK", this.name);
109 LOG.error("Device {} finished scenario with error", this.name);
111 return scenarioHandler.isFinishedOK();
117 throw new UnsupportedOperationException();