fc70a14f354c18f110f5211d2d88f01f2bbe8fb8
[openflowplugin.git] / samples / simple-client / src / main / java / org / opendaylight / openflowjava / protocol / impl / clients / CallableClient.java
1 /*
2  * Copyright (c) 2016 Pantheon Technologies s.r.o. and others. All rights reserved.
3  *
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
7  */
8 package org.opendaylight.openflowjava.protocol.impl.clients;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.util.concurrent.SettableFuture;
13 import io.netty.bootstrap.Bootstrap;
14 import io.netty.channel.EventLoopGroup;
15 import io.netty.channel.socket.nio.NioSocketChannel;
16 import java.net.InetAddress;
17 import java.util.concurrent.Callable;
18 import org.slf4j.LoggerFactory;
19
20 /**
21  * Callable client class, inspired by SimpleClient class.
22  * Simulating device/switch connected to controller.
23  *
24  * @author Jozef Bacigal
25  */
26 public class CallableClient implements Callable<Boolean>, OFClient {
27
28     private static final org.slf4j.Logger LOG = LoggerFactory.getLogger(CallableClient.class);
29
30     private int port = 6653;
31     private boolean securedClient = false;
32     private InetAddress ipAddress = null;
33     private String name = "Empty name";
34
35     private final EventLoopGroup workerGroup;
36     private SettableFuture<Boolean> isOnlineFuture;
37     private SettableFuture<Boolean> scenarioDone;
38     private ScenarioHandler scenarioHandler = null;
39     private Bootstrap bootstrap = null;
40
41     public CallableClient(
42             final int port,
43             final boolean securedClient,
44             final InetAddress ipAddress,
45             final String name,
46             final ScenarioHandler scenarioHandler,
47             final Bootstrap bootstrap,
48             final EventLoopGroup eventExecutors) {
49         this.port = port;
50         this.securedClient = securedClient;
51         this.ipAddress = requireNonNull(ipAddress, "IP address cannot be null");
52         this.workerGroup = eventExecutors;
53         this.bootstrap = bootstrap;
54         this.name = name;
55         this.scenarioHandler = requireNonNull(scenarioHandler, "Scenario handler cannot be null");
56     }
57
58     @Override
59     public SettableFuture<Boolean> getIsOnlineFuture() {
60         return isOnlineFuture;
61     }
62
63     @Override
64     public SettableFuture<Boolean> getScenarioDone() {
65         return scenarioDone;
66     }
67
68     @Override
69     public void setScenarioHandler(final ScenarioHandler scenario) {
70         this.scenarioHandler = scenario;
71     }
72
73     @Override
74     public void setSecuredClient(final boolean securedClient) {
75         this.securedClient = securedClient;
76     }
77
78
79     @Override
80     @SuppressWarnings("checkstyle:IllegalCatch")
81     public Boolean call() throws Exception {
82         requireNonNull(bootstrap);
83         requireNonNull(workerGroup);
84         LOG.info("Switch {} trying connect to controller", this.name);
85         SimpleClientInitializer clientInitializer = new SimpleClientInitializer(isOnlineFuture, securedClient);
86         clientInitializer.setScenario(scenarioHandler);
87         try {
88             bootstrap.group(workerGroup)
89                     .channel(NioSocketChannel.class)
90                     .handler(clientInitializer);
91
92             bootstrap.connect(ipAddress, port).sync();
93             synchronized (scenarioHandler) {
94                 LOG.debug("WAITING FOR SCENARIO");
95                 while (!scenarioHandler.isScenarioFinished()) {
96                     scenarioHandler.wait();
97                 }
98             }
99         } catch (RuntimeException ex) {
100             LOG.error("Error", ex);
101             return false;
102         }
103         if (scenarioHandler.isFinishedOK()) {
104             LOG.info("Device {} finished scenario OK", this.name);
105         } else {
106             LOG.error("Device {} finished scenario with error", this.name);
107         }
108         return scenarioHandler.isFinishedOK();
109
110     }
111
112     @Override
113     public void run() {
114         throw new UnsupportedOperationException();
115     }
116 }