-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClCompAppMain.java
More file actions
354 lines (312 loc) · 14.7 KB
/
ClCompAppMain.java
File metadata and controls
354 lines (312 loc) · 14.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
import com.sun.jna.*;
import com.sun.jna.ptr.*;
import saAmf.SaAmfLibrary;
import saAmf.SaAmfLibrary.*;
//import saAmf.SaAmfLibrary.SaAmfHAStateT.*;
import saAmf.SaAmfCallbacksT;
import saAmf.SaAmfCallbacksT.*;
import saAmf.SaAmfCSIDescriptorT;
import saAis.SaAisLibrary;
import saAis.SaAisLibrary.*;
import clEoLib.ClEoLibrary;
import clEoLib.ClEoLibrary.*;
import clUtils.*;
import clUtils.ClUtilsLibrary;
import libc.LibC;
import libc.LibC.*;
import libc.Errno;
import libc.Errno.*;
import java.lang.ProcessHandle;
public class ClCompAppMain {
private static interface CompCfgLibrary extends Library {
public static final String JNA_LIBRARY_NAME = "clCompCfg";
public static final CompCfgLibrary INSTANCE = Native.load(JNA_LIBRARY_NAME, CompCfgLibrary.class);
};
private static long mypid = 0;
private static saAis.SaAisLibrary.SaNameT.ByReference appName = new saAis.SaAisLibrary.SaNameT.ByReference();
private static short svcId = 10;
private static CompCfgLibrary compCfgLib = CompCfgLibrary.INSTANCE;
private static NativeLibrary mwNativeLib = clUtils.ClUtilsLibrary.MW_NATIVE_LIB;
private static clUtils.ClUtilsLibrary utilsLib = clUtils.ClUtilsLibrary.UTILS_INSTANCE;
private static NativeLibrary iocLib = NativeLibrary.getInstance("ClIoc");
private static void clprintf(int severity, String fmtString, Object...varArgs) {
StackTraceElement e = Thread.currentThread().getStackTrace()[2];
Pointer symPtr = mwNativeLib.getGlobalVariableAddress("CL_LOG_HANDLE_APP");
long handleApp = symPtr.getLong(0);
utilsLib.clLogMsgWrite( handleApp,
severity,
svcId,
"---",
"---",
e.getFileName(),
e.getLineNumber(),
fmtString,
varArgs);
}
private static saAmf.SaAmfLibrary saAmfLib = SaAmfLibrary.INSTANCE;
private static clEoLib.ClEoLibrary eoLib = clEoLib.ClEoLibrary.INSTANCE;
private static LongByReference amfHandle = new LongByReference();
private static saAmf.SaAmfCallbacksT.ByReference callbacks = new saAmf.SaAmfCallbacksT.ByReference();
private static boolean unblockNow = false;
private static saAmf.SaAmfLibrary.SaAmfCSISetCallbackT csiSetCb = (invocation, compName, haState, csiDescriptor) -> {
/*
* Take appropriate action based on state
*/
try {
csiDescriptor.read();
compName.read();
csiDescriptor.csiName.read();
String msg = String.format("Component [%s] : PID [%d]. CSI Set Received\n", Native.toString(compName.value), mypid);
clprintf(clUtils.ClUtilsLibrary.ClLogSeverityT.CL_LOG_SEV_INFO, msg);
csiDescriptor.csiStateDescriptor.setType(
(haState == saAmf.SaAmfLibrary.SaAmfHaStateT.SA_AMF_HA_ACTIVE)
? saAmf.SaAmfCSIActiveDescriptorT.class
: saAmf.SaAmfCSIStandbyDescriptorT.class
);
csiDescriptor.csiStateDescriptor.read();
clCompAppAMFPrintCSI(csiDescriptor, haState);
switch ( haState ) {
case saAmf.SaAmfLibrary.SaAmfHaStateT.SA_AMF_HA_ACTIVE:
{
/*
* AMF has requested application to take the active HA state
* for the CSI.
*/
saAmfLib.saAmfResponse(amfHandle.getValue(), invocation, saAis.SaAisLibrary.SaAisErrorT.SA_AIS_OK);
break;
}
case saAmf.SaAmfLibrary.SaAmfHaStateT.SA_AMF_HA_STANDBY:
{
/*
* AMF has requested application to take the standby HA state
* for this CSI.
*/
saAmfLib.saAmfResponse(amfHandle.getValue(), invocation, saAis.SaAisLibrary.SaAisErrorT.SA_AIS_OK);
break;
}
case saAmf.SaAmfLibrary.SaAmfHaStateT.SA_AMF_HA_QUIESCED:
{
/*
* AMF has requested application to quiesce the CSI currently
* assigned the active or quiescing HA state. The application
* must stop work associated with the CSI immediately.
*/
saAmfLib.saAmfResponse(amfHandle.getValue(), invocation, saAis.SaAisLibrary.SaAisErrorT.SA_AIS_OK);
break;
}
case saAmf.SaAmfLibrary.SaAmfHaStateT.SA_AMF_HA_QUIESCING:
{
/*
* AMF has requested application to quiesce the CSI currently
* assigned the active HA state. The application must stop work
* associated with the CSI gracefully and not accept any new
* workloads while the work is being terminated.
*/
saAmfLib.saAmfCSIQuiescingComplete(amfHandle.getValue(), invocation, saAis.SaAisLibrary.SaAisErrorT.SA_AIS_OK);
break;
}
default:
{
assert false : "Invalid haState";
break;
}
}
}catch (Throwable t) {
t.printStackTrace(); // don't let exceptions escape native callback
}
return;
};
private static saAmf.SaAmfLibrary.SaAmfCSIRemoveCallbackT csiRemoveCb = (invocation, compName, csiName, csiFlags) -> {
try {
compName.read();
csiName.read();
clprintf(clUtils.ClUtilsLibrary.ClLogSeverityT.CL_LOG_SEV_INFO, String.format("Component [%s] : PID [%d]. CSI Remove Received\n", Native.toString(compName.value), mypid));
clprintf (clUtils.ClUtilsLibrary.ClLogSeverityT.CL_LOG_SEV_INFO, String.format(" CSI : %s\n", Native.toString(csiName.value)));
clprintf (clUtils.ClUtilsLibrary.ClLogSeverityT.CL_LOG_SEV_INFO, String.format(" CSI Flags : 0x%d\n", csiFlags));
/*
* Add application specific logic for removing the work for this CSI.
*/
saAmfLib.saAmfResponse(amfHandle.getValue(), invocation, saAis.SaAisLibrary.SaAisErrorT.SA_AIS_OK);
}catch (Throwable t) {
t.printStackTrace(); // avoid letting exceptions escape native callback
}
return;
};
private static saAmf.SaAmfLibrary.SaAmfComponentTerminateCallbackT termCb = (invocation, compName) -> {
try {
int rc = saAis.SaAisLibrary.SaAisErrorT.SA_AIS_OK;
clprintf(clUtils.ClUtilsLibrary.ClLogSeverityT.CL_LOG_SEV_INFO, String.format("Component [%s] : PID [%d]. Terminating\n", Native.toString(compName.value), mypid));
/*
* Unregister with AMF and respond to AMF saying whether the
* termination was successful or not.
*/
compName.read();
rc = saAmfLib.saAmfComponentUnregister(amfHandle.getValue(), compName.getPointer(), null);
if (rc == saAis.SaAisLibrary.SaAisErrorT.SA_AIS_OK) {
saAmfLib.saAmfResponse(amfHandle.getValue(), invocation, saAis.SaAisLibrary.SaAisErrorT.SA_AIS_OK);
clprintf(clUtils.ClUtilsLibrary.ClLogSeverityT.CL_LOG_SEV_INFO, String.format("Component [%s] : PID [%d]. Terminated\n", Native.toString(compName.value), mypid));
unblockNow = true;
}
else {
clprintf(clUtils.ClUtilsLibrary.ClLogSeverityT.CL_LOG_SEV_ERROR, String.format("Component [%s] : PID [%d]. Termination error [0x%x]\n", Native.toString(compName.value), mypid, rc));
}
}catch (Throwable t) {
t.printStackTrace();
}
return;
};
private static void errorexit(int rc, int status) {
clprintf(clUtils.ClUtilsLibrary.ClLogSeverityT.CL_LOG_SEV_ERROR, String.format("Component [%s] : PID [%d]. Initialization error [0x%x]\n", Native.toString(appName.value), mypid, rc));
System.exit(status);
}
public static void main(String argv[]) {
mypid = ProcessHandle.current().pid();
saAis.SaAisLibrary.SaVersionT.ByReference version = new saAis.SaAisLibrary.SaVersionT.ByReference();
version.releaseCode = 'B';
version.majorVersion = 01;
version.minorVersion = 01;
version.write();
callbacks.saAmfHealthcheckCallback = null;
callbacks.saAmfComponentTerminateCallback = termCb;
callbacks.saAmfCSISetCallback = csiSetCb;
callbacks.saAmfCSIRemoveCallback = csiRemoveCb;
callbacks.saAmfProtectionGroupTrackCallback = null;
callbacks.saAmfProxiedComponentInstantiateCallback = null;
callbacks.saAmfProxiedComponentCleanupCallback = null;
callbacks.write(); // sync struct
int rc = saAmfLib.saAmfInitialize(amfHandle, callbacks, version);
if (rc != saAis.SaAisLibrary.SaAisErrorT.SA_AIS_OK) {
errorexit(rc,-1);
}
FDSet readfds = new FDSet();
readfds.FD_ZERO();
LongByReference dispatch_fd = new LongByReference();
rc = saAmfLib.saAmfSelectionObjectGet(amfHandle.getValue(), dispatch_fd);
if (rc != saAis.SaAisLibrary.SaAisErrorT.SA_AIS_OK) {
errorexit(rc,-1);
}
readfds.FD_SET(dispatch_fd.getValue());
/*
* Do the application specific initialization here.
*/
rc = saAmfLib.saAmfComponentNameGet(amfHandle.getValue(), appName);
if (rc != saAis.SaAisLibrary.SaAisErrorT.SA_AIS_OK) {
errorexit(rc,-1);
}
appName.read();
rc = saAmfLib.saAmfComponentRegister(amfHandle.getValue(), appName.getPointer(), null);
if (rc != saAis.SaAisLibrary.SaAisErrorT.SA_AIS_OK) {
errorexit(rc,-1);
}
/*
* Print out standard information for this component.
*/
IntByReference iocPort = new IntByReference();
eoLib.clEoMyEoIocPortGet(iocPort);
clprintf(clUtils.ClUtilsLibrary.ClLogSeverityT.CL_LOG_SEV_INFO, String.format("Component [%s] : PID [%d]. Initializing\n", Native.toString(appName.value), mypid));
Function iocLocalAddrGetFunc = iocLib.getFunction("clIocLocalAddressGet");
int myIocAddr = iocLocalAddrGetFunc.invokeInt(new Object[]{});
clprintf(clUtils.ClUtilsLibrary.ClLogSeverityT.CL_LOG_SEV_INFO, String.format(" IOC Address : 0x%x\n", myIocAddr));
clprintf(clUtils.ClUtilsLibrary.ClLogSeverityT.CL_LOG_SEV_INFO, String.format(" IOC Port : 0x%x\n",iocPort.getValue()));
/*
* Block on AMF dispatch file descriptor for callbacks
*/
do {
int err = libc.LibC.INSTANCE.select(dispatch_fd.getValue()+1, readfds, null, null, null);
if (err < 0) {
if (libc.Errno.EINTR == Native.getLastError()) {
continue;
}
break;
}
saAmfLib.saAmfDispatch(amfHandle.getValue(), saAis.SaAisLibrary.SaDispatchFlagsT.SA_DISPATCH_ALL);
}while(!unblockNow);
/*
* Do the application specific finalization here.
*/
rc = saAmfLib.saAmfFinalize(amfHandle.getValue());
if (rc != saAis.SaAisLibrary.SaAisErrorT.SA_AIS_OK) {
errorexit(rc,-1);
}
}
private static String haStateToString(int haState) {
switch (haState) {
case saAmf.SaAmfLibrary.SaAmfHaStateT.SA_AMF_HA_ACTIVE:
return "Active";
case saAmf.SaAmfLibrary.SaAmfHaStateT. SA_AMF_HA_STANDBY:
return "Standby";
case saAmf.SaAmfLibrary.SaAmfHaStateT.SA_AMF_HA_QUIESCED:
return "Quiesced";
case saAmf.SaAmfLibrary.SaAmfHaStateT.SA_AMF_HA_QUIESCING:
return "Quiescing";
default:
return "Unknown";
}
}
private static String csiFlagsToString(int csiFlags) {
if ((csiFlags & saAmf.SaAmfLibrary.SA_AMF_CSI_ADD_ONE) != 0)
return "Add One";
if ((csiFlags & saAmf.SaAmfLibrary.SA_AMF_CSI_TARGET_ONE) != 0)
return "Target One";
if ((csiFlags & saAmf.SaAmfLibrary.SA_AMF_CSI_TARGET_ALL) != 0)
return "Target All";
return "Unknown";
}
/******************************************************************************
* Utility functions
*****************************************************************************/
/*
* clCompAppAMFPrintCSI
* --------------------
* Print information received in a CSI set request.
*/
private static void clCompAppAMFPrintCSI(saAmf.SaAmfCSIDescriptorT csiDescriptor,
int haState) {
clprintf (clUtils.ClUtilsLibrary.ClLogSeverityT.CL_LOG_SEV_INFO,
"CSI Flags : [%s]",
csiFlagsToString(csiDescriptor.csiFlags));
if (saAmf.SaAmfLibrary.SA_AMF_CSI_TARGET_ALL != csiDescriptor.csiFlags) {
clprintf (clUtils.ClUtilsLibrary.ClLogSeverityT.CL_LOG_SEV_INFO, String.format("CSI Name : [%s]",
Native.toString(csiDescriptor.csiName.value)));
}
if (saAmf.SaAmfLibrary.SA_AMF_CSI_ADD_ONE == csiDescriptor.csiFlags) {
clprintf (clUtils.ClUtilsLibrary.ClLogSeverityT.CL_LOG_SEV_INFO, "Name value pairs :");
int count = csiDescriptor.csiAttr.number;
Pointer ptr = csiDescriptor.csiAttr.attr;
if (ptr != null && count > 0) {
saAmf.SaAmfCSIAttributeT[] attrs = (saAmf.SaAmfCSIAttributeT[]) new saAmf.SaAmfCSIAttributeT(ptr).toArray(count);
for (int i = 0; i < count; i++) {
attrs[i].read();
clprintf (clUtils.ClUtilsLibrary.ClLogSeverityT.CL_LOG_SEV_INFO, String.format("Name :[%s]",attrs[i].getName()));
clprintf (clUtils.ClUtilsLibrary.ClLogSeverityT.CL_LOG_SEV_INFO, String.format("Value : [%s]", attrs[i].getValue()));
}
}
}
clprintf (clUtils.ClUtilsLibrary.ClLogSeverityT.CL_LOG_SEV_INFO, String.format("HA state : [%s]",
haStateToString(haState)));
if (saAmf.SaAmfLibrary.SaAmfHaStateT.SA_AMF_HA_ACTIVE == haState) {
clprintf (clUtils.ClUtilsLibrary.ClLogSeverityT.CL_LOG_SEV_INFO, "Active Descriptor :");
saAmf.SaAmfCSIActiveDescriptorT act = csiDescriptor.csiStateDescriptor.activeDescriptor;
act.read();
clprintf (clUtils.ClUtilsLibrary.ClLogSeverityT.CL_LOG_SEV_INFO,
String.format("Transition Descriptor : [%d]",
act.transitionDescriptor));
clprintf (clUtils.ClUtilsLibrary.ClLogSeverityT.CL_LOG_SEV_INFO,
String.format("Active Component : [%s]",
Native.toString(act.activeCompName.value)));
}
else if (saAmf.SaAmfLibrary.SaAmfHaStateT.SA_AMF_HA_STANDBY == haState) {
clprintf (clUtils.ClUtilsLibrary.ClLogSeverityT.CL_LOG_SEV_INFO, "Standby Descriptor :");
clprintf (clUtils.ClUtilsLibrary.ClLogSeverityT.CL_LOG_SEV_INFO,
String.format("Standby Rank : [%d]",
csiDescriptor.csiStateDescriptor.
standbyDescriptor.standbyRank));
clprintf (clUtils.ClUtilsLibrary.ClLogSeverityT.CL_LOG_SEV_INFO, String.format("Active Component : [%s]",
Native.toString(csiDescriptor.csiStateDescriptor.
standbyDescriptor.activeCompName.value)));
}
}
/*
* Insert any other utility functions here.
*/
}