Use String(byte[], Charset)
[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 com.google.common.base.Preconditions;
11 import com.google.common.util.concurrent.SettableFuture;
12 import io.netty.bootstrap.Bootstrap;
13 import io.netty.channel.EventLoopGroup;
14 import io.netty.channel.socket.nio.NioSocketChannel;
15 import java.net.InetAddress;
16 import java.util.concurrent.Callable;
17 import org.slf4j.LoggerFactory;
18
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
50         Preconditions.checkNotNull(ipAddress, "IP address cannot be null");
51         Preconditions.checkNotNull(scenarioHandler, "Scenario handler cannot be null");
52         this.port = port;
53         this.securedClient = securedClient;
54         this.ipAddress = ipAddress;
55         this.workerGroup = eventExecutors;
56         this.bootstrap = bootstrap;
57         this.name = name;
58         this.scenarioHandler = scenarioHandler;
59     }
60
61     @Override
62     public SettableFuture<Boolean> getIsOnlineFuture() {
63         return isOnlineFuture;
64     }
65
66     @Override
67     public SettableFuture<Boolean> getScenarioDone() {
68         return scenarioDone;
69     }
70
71     @Override
72     public void setScenarioHandler(final ScenarioHandler scenario) {
73         this.scenarioHandler = scenario;
74     }
75
76     @Override
77     public void setSecuredClient(final boolean securedClient) {
78         this.securedClient = securedClient;
79     }
80
81
82     @Override
83     @SuppressWarnings("checkstyle:IllegalCatch")
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);
90         try {
91             bootstrap.group(workerGroup)
92                     .channel(NioSocketChannel.class)
93                     .handler(clientInitializer);
94
95             bootstrap.connect(ipAddress, port).sync();
96             synchronized (scenarioHandler) {
97                 LOG.debug("WAITING FOR SCENARIO");
98                 while (!scenarioHandler.isScenarioFinished()) {
99                     scenarioHandler.wait();
100                 }
101             }
102         } catch (RuntimeException ex) {
103             LOG.error("Error", ex);
104             return false;
105         }
106         if (scenarioHandler.isFinishedOK()) {
107             LOG.info("Device {} finished scenario OK", this.name);
108         } else {
109             LOG.error("Device {} finished scenario with error", this.name);
110         }
111         return scenarioHandler.isFinishedOK();
112
113     }
114
115     @Override
116     public void run() {
117         throw new UnsupportedOperationException();
118     }
119 }