1 package org.metricshub.ipmi.core.api.async;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 import org.metricshub.ipmi.core.api.async.messages.IpmiError;
26 import org.metricshub.ipmi.core.api.async.messages.IpmiResponse;
27 import org.metricshub.ipmi.core.api.async.messages.IpmiResponseData;
28 import org.metricshub.ipmi.core.coding.PayloadCoder;
29 import org.metricshub.ipmi.core.coding.commands.PrivilegeLevel;
30 import org.metricshub.ipmi.core.coding.commands.ResponseData;
31 import org.metricshub.ipmi.core.coding.commands.session.GetChannelAuthenticationCapabilitiesResponseData;
32 import org.metricshub.ipmi.core.coding.payload.IpmiPayload;
33 import org.metricshub.ipmi.core.coding.protocol.PayloadType;
34 import org.metricshub.ipmi.core.coding.security.CipherSuite;
35 import org.metricshub.ipmi.core.common.PropertiesManager;
36 import org.metricshub.ipmi.core.connection.Connection;
37 import org.metricshub.ipmi.core.connection.ConnectionException;
38 import org.metricshub.ipmi.core.connection.ConnectionListener;
39 import org.metricshub.ipmi.core.connection.ConnectionManager;
40 import org.metricshub.ipmi.core.connection.Session;
41 import org.metricshub.ipmi.core.connection.SessionManager;
42
43 import java.io.FileNotFoundException;
44 import java.io.IOException;
45 import java.net.InetAddress;
46 import java.util.ArrayList;
47 import java.util.List;
48
49 import org.slf4j.Logger;
50 import org.slf4j.LoggerFactory;
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83 public class IpmiAsyncConnector implements ConnectionListener {
84 public static final String FAILED_TO_RECEIVE_ANSWER_CAUSE_MESSAGE = "Failed to receive answer, cause:";
85 private ConnectionManager connectionManager;
86 private SessionManager sessionManager;
87 private int retries;
88 private final List<IpmiResponseListener> responseListeners;
89 private final List<InboundMessageListener> inboundMessageListeners;
90
91 private static Logger logger = LoggerFactory.getLogger(IpmiAsyncConnector.class);
92
93
94
95
96
97
98
99
100
101
102
103
104 public IpmiAsyncConnector(int port) throws IOException {
105 responseListeners = new ArrayList<IpmiResponseListener>();
106 inboundMessageListeners = new ArrayList<InboundMessageListener>();
107 connectionManager = new ConnectionManager(port);
108 sessionManager = new SessionManager();
109 loadProperties();
110 }
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126 public IpmiAsyncConnector(int port, InetAddress address) throws IOException {
127 responseListeners = new ArrayList<IpmiResponseListener>();
128 inboundMessageListeners = new ArrayList<InboundMessageListener>();
129 connectionManager = new ConnectionManager(port, address);
130 sessionManager = new SessionManager();
131 loadProperties();
132 }
133
134
135
136
137
138
139
140
141
142
143
144
145 public IpmiAsyncConnector(int port, long pingPeriod) throws IOException {
146 responseListeners = new ArrayList<>();
147 inboundMessageListeners = new ArrayList<>();
148 connectionManager = new ConnectionManager(port, pingPeriod);
149 sessionManager = new SessionManager();
150 loadProperties();
151 }
152
153
154
155
156 private void loadProperties() {
157 retries = Integer.parseInt(PropertiesManager.getInstance().getProperty("retries"));
158 }
159
160
161
162
163
164
165
166
167
168
169
170
171 public ConnectionHandle createConnection(InetAddress address, int port)
172 throws IOException {
173 int handle = connectionManager.createConnection(address, port);
174 connectionManager.getConnection(handle).registerListener(this);
175 return new ConnectionHandle(handle, address, port);
176 }
177
178
179
180
181
182
183
184
185
186
187
188
189 public ConnectionHandle createConnection(InetAddress address, int port, CipherSuite cipherSuite, PrivilegeLevel privilegeLevel)
190 throws IOException {
191 int handle = connectionManager.createConnection(address, port, true);
192 connectionManager.getConnection(handle).registerListener(this);
193
194 ConnectionHandle connectionHandle = new ConnectionHandle(handle, address, port);
195 connectionHandle.setCipherSuite(cipherSuite);
196 connectionHandle.setPrivilegeLevel(privilegeLevel);
197
198 return connectionHandle;
199 }
200
201
202
203
204
205
206
207
208
209
210
211
212
213 public List<CipherSuite> getAvailableCipherSuites(
214 ConnectionHandle connectionHandle) throws Exception {
215 int tries = 0;
216 List<CipherSuite> result = null;
217 while (tries <= retries && result == null) {
218 try {
219 ++tries;
220 result = connectionManager
221 .getAvailableCipherSuites(connectionHandle.getHandle());
222 } catch (Exception e) {
223 logger.warn(FAILED_TO_RECEIVE_ANSWER_CAUSE_MESSAGE, e);
224 if (tries > retries) {
225 throw e;
226 }
227 }
228 }
229 return result;
230 }
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249 public GetChannelAuthenticationCapabilitiesResponseData getChannelAuthenticationCapabilities(
250 ConnectionHandle connectionHandle, CipherSuite cipherSuite,
251 PrivilegeLevel requestedPrivilegeLevel) throws Exception {
252 int tries = 0;
253 GetChannelAuthenticationCapabilitiesResponseData result = null;
254 while (tries <= retries && result == null) {
255 try {
256 ++tries;
257 result = connectionManager
258 .getChannelAuthenticationCapabilities(
259 connectionHandle.getHandle(), cipherSuite,
260 requestedPrivilegeLevel);
261 connectionHandle.setCipherSuite(cipherSuite);
262 connectionHandle.setPrivilegeLevel(requestedPrivilegeLevel);
263 } catch (Exception e) {
264 logger.warn(FAILED_TO_RECEIVE_ANSWER_CAUSE_MESSAGE, e);
265 if (tries > retries) {
266 throw e;
267 }
268 }
269 }
270 return result;
271 }
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292 public Session openSession(ConnectionHandle connectionHandle, String username,
293 String password, byte[] bmcKey) throws Exception {
294 Session session = null;
295 int tries = 0;
296 boolean succeded = false;
297
298 connectionHandle.setUser(username);
299 connectionHandle.setPassword(password);
300
301 while (tries <= retries && !succeded) {
302 try {
303 ++tries;
304 int sessionId = connectionManager.startSession(connectionHandle.getHandle(),
305 connectionHandle.getCipherSuite(),
306 connectionHandle.getPrivilegeLevel(), username,
307 password, bmcKey);
308
309 session = sessionManager.registerSession(sessionId, connectionHandle);
310
311 succeded = true;
312 } catch (Exception e) {
313 logger.warn(FAILED_TO_RECEIVE_ANSWER_CAUSE_MESSAGE, e);
314 if (tries > retries) {
315 throw e;
316 }
317 }
318 }
319
320 return session;
321 }
322
323
324
325
326
327
328
329
330
331
332
333
334 public Session getExistingSessionForCriteria(InetAddress remoteAddress, int remotePort, String user) {
335 return sessionManager.getSessionForCriteria(remoteAddress, remotePort, user);
336 }
337
338
339
340
341
342
343
344
345
346
347
348
349
350 public void closeSession(ConnectionHandle connectionHandle)
351 throws Exception {
352 if (!connectionManager.getConnection(connectionHandle.getHandle())
353 .isSessionValid()) {
354 return;
355 }
356 int tries = 0;
357 boolean succeded = false;
358 while (tries <= retries && !succeded) {
359 try {
360 ++tries;
361 connectionManager.getConnection(connectionHandle.getHandle())
362 .closeSession();
363 sessionManager.unregisterSession(connectionHandle);
364 succeded = true;
365 } catch (Exception e) {
366 logger.warn(FAILED_TO_RECEIVE_ANSWER_CAUSE_MESSAGE, e);
367 if (tries > retries) {
368 throw e;
369 }
370 }
371 }
372 return;
373 }
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395 public int sendMessage(ConnectionHandle connectionHandle,
396 PayloadCoder request, boolean isOneWay) throws Exception {
397 int tries = 0;
398 int tag = -1;
399 while (tries <= retries && tag < 0) {
400 try {
401 ++tries;
402 while (tag < 0) {
403 tag = connectionManager.getConnection(
404 connectionHandle.getHandle()).sendMessage(
405 request, isOneWay);
406 if (tag < 0) {
407 Thread.sleep(10);
408
409 }
410 }
411 logger.debug("Sending message with tag " + tag + ", try "
412 + tries);
413 } catch (IllegalArgumentException e) {
414 throw e;
415 } catch (Exception e) {
416 logger.warn("Failed to send message, cause:", e);
417 if (tries > retries) {
418 throw e;
419 }
420 }
421 }
422 return tag;
423 }
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439 public int retry(ConnectionHandle connectionHandle, int tag, PayloadType messagePayloadType) throws ConnectionException {
440 return connectionManager.getConnection(connectionHandle.getHandle()).retry(tag, messagePayloadType);
441 }
442
443
444
445
446
447
448
449 public void registerListener(IpmiResponseListener listener) {
450 synchronized (responseListeners) {
451 responseListeners.add(listener);
452 }
453 }
454
455
456
457
458
459
460
461
462 public void unregisterListener(IpmiResponseListener listener) {
463 synchronized (responseListeners) {
464 responseListeners.remove(listener);
465 }
466 }
467
468
469
470
471
472
473
474 public void registerIncomingPayloadListener(InboundMessageListener listener) {
475 synchronized (inboundMessageListeners) {
476 inboundMessageListeners.add(listener);
477 }
478 }
479
480
481
482
483
484
485
486 public void unregisterIncomingPayloadListener(InboundMessageListener listener) {
487 synchronized (inboundMessageListeners) {
488 inboundMessageListeners.remove(listener);
489 }
490 }
491
492 @Override
493 public void processResponse(ResponseData responseData, int handle, int tag, Exception exception) {
494 IpmiResponse response = null;
495 Connection connection = connectionManager.getConnection(handle);
496
497 if (responseData == null || exception != null) {
498 Exception notNullException = exception != null ? exception : new Exception("Empty response");
499
500 response = new IpmiError(notNullException, tag, new ConnectionHandle(
501 handle, connection.getRemoteMachineAddress(), connection.getRemoteMachinePort()));
502 } else {
503 response = new IpmiResponseData(responseData, tag,
504 new ConnectionHandle(handle, connection.getRemoteMachineAddress(), connection.getRemoteMachinePort()));
505
506 }
507 synchronized (responseListeners) {
508 for (IpmiResponseListener listener : responseListeners) {
509 if (listener != null) {
510 listener.notify(response);
511 }
512 }
513 }
514 }
515
516 @Override
517 public void processRequest(IpmiPayload payload) {
518 for (InboundMessageListener listener : inboundMessageListeners) {
519 if (listener.isPayloadSupported(payload)) {
520 listener.notify(payload);
521 }
522 }
523 }
524
525
526
527
528 public void closeConnection(ConnectionHandle handle) {
529 connectionManager.getConnection(handle.getHandle()).unregisterListener(
530 this);
531 connectionManager.closeConnection(handle.getHandle());
532 }
533
534
535
536
537 public void tearDown() {
538 connectionManager.close();
539 }
540
541
542
543
544
545
546
547
548 public void setTimeout(ConnectionHandle handle, int timeout) {
549 connectionManager.getConnection(handle.getHandle()).setTimeout(timeout);
550 }
551
552 }