-
Notifications
You must be signed in to change notification settings - Fork 11
/
GRpcManipulations.java
530 lines (483 loc) · 26.6 KB
/
GRpcManipulations.java
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
/*
* This Source Code Form is subject to the terms of the MIT License.
* Copyright (c) 2023 Draegerwerk AG & Co. KGaA.
*
* SPDX-License-Identifier: MIT
*/
package com.draeger.medical.sdccc.manipulation;
import com.draeger.medical.sdccc.configuration.TestSuiteConfig;
import com.draeger.medical.sdccc.messages.guice.ManipulationInfoFactory;
import com.draeger.medical.sdccc.util.Constants;
import com.draeger.medical.t2iapi.BasicRequests;
import com.draeger.medical.t2iapi.BasicResponses;
import com.draeger.medical.t2iapi.ResponseTypes;
import com.draeger.medical.t2iapi.activation_state.ActivationStateRequests;
import com.draeger.medical.t2iapi.activation_state.ActivationStateServiceGrpc;
import com.draeger.medical.t2iapi.activation_state.ActivationStateTypes;
import com.draeger.medical.t2iapi.alert.AlertRequests;
import com.draeger.medical.t2iapi.alert.AlertServiceGrpc;
import com.draeger.medical.t2iapi.context.ContextRequests;
import com.draeger.medical.t2iapi.context.ContextServiceGrpc;
import com.draeger.medical.t2iapi.context.ContextTypes;
import com.draeger.medical.t2iapi.device.DeviceRequests;
import com.draeger.medical.t2iapi.device.DeviceResponses;
import com.draeger.medical.t2iapi.device.DeviceServiceGrpc;
import com.draeger.medical.t2iapi.device.DeviceTypes;
import com.draeger.medical.t2iapi.metric.MetricRequests;
import com.draeger.medical.t2iapi.metric.MetricServiceGrpc;
import com.draeger.medical.t2iapi.metric.MetricTypes;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import com.google.inject.name.Named;
import com.google.protobuf.Empty;
import com.google.protobuf.StringValue;
import io.grpc.Channel;
import io.grpc.ManagedChannelBuilder;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;
import javax.xml.namespace.QName;
import org.apache.commons.lang3.tuple.ImmutablePair;
import org.apache.commons.lang3.tuple.Pair;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.somda.sdc.biceps.model.participant.AbstractDescriptor;
import org.somda.sdc.biceps.model.participant.AlertActivation;
import org.somda.sdc.biceps.model.participant.AlertSignalManifestation;
import org.somda.sdc.biceps.model.participant.ComponentActivation;
import org.somda.sdc.biceps.model.participant.ContextAssociation;
import org.somda.sdc.biceps.model.participant.LocationDetail;
import org.somda.sdc.biceps.model.participant.MdsDescriptor;
import org.somda.sdc.biceps.model.participant.MeasurementValidity;
import org.somda.sdc.biceps.model.participant.MetricCategory;
/**
* Device manipulations calling on a gRPC backend.
*/
@Singleton
public class GRpcManipulations implements Manipulations {
private static final Logger LOG = LogManager.getLogger(GRpcManipulations.class);
private static final Map<QName, DeviceTypes.ReportType> REPORT_TYPE_MAP = Map.of(
Constants.MSG_EPISODIC_ALERT_REPORT,
DeviceTypes.ReportType.REPORT_TYPE_EPISODIC_ALERT_REPORT,
Constants.MSG_EPISODIC_COMPONENT_REPORT,
DeviceTypes.ReportType.REPORT_TYPE_EPISODIC_COMPONENT_REPORT,
Constants.MSG_EPISODIC_CONTEXT_REPORT,
DeviceTypes.ReportType.REPORT_TYPE_EPISODIC_CONTEXT_REPORT,
Constants.MSG_EPISODIC_METRIC_REPORT,
DeviceTypes.ReportType.REPORT_TYPE_EPISODIC_METRIC_REPORT,
Constants.MSG_EPISODIC_OPERATIONAL_STATE_REPORT,
DeviceTypes.ReportType.REPORT_TYPE_EPISODIC_OPERATIONAL_STATE_REPORT,
Constants.MSG_DESCRIPTION_MODIFICATION_REPORT,
DeviceTypes.ReportType.REPORT_TYPE_DESCRIPTION_MODIFICATION_REPORT,
Constants.MSG_OPERATION_INVOKED_REPORT,
DeviceTypes.ReportType.REPORT_TYPE_OPERATION_INVOKED_REPORT,
Constants.MSG_SYSTEM_ERROR_REPORT,
DeviceTypes.ReportType.REPORT_TYPE_SYSTEM_ERROR_REPORT,
Constants.MSG_OBSERVED_VALUE_STREAM,
DeviceTypes.ReportType.REPORT_TYPE_OBSERVED_VALUE_STREAM,
Constants.MSG_WAVEFORM_STREAM,
DeviceTypes.ReportType.REPORT_TYPE_WAVEFORM_STREAM);
private final ActivationStateServiceGrpc.ActivationStateServiceBlockingStub activationStateStub;
private final AlertServiceGrpc.AlertServiceBlockingStub alertStub;
private final ContextServiceGrpc.ContextServiceBlockingStub contextStub;
private final DeviceServiceGrpc.DeviceServiceBlockingStub deviceStub;
private final MetricServiceGrpc.MetricServiceBlockingStub metricStub;
private final Manipulations fallback;
private final ManipulationInfoFactory manipulationInfoFactory;
private final StackWalker walker = StackWalker.getInstance(StackWalker.Option.RETAIN_CLASS_REFERENCE);
/**
* Creates an instance of gRPC-based manipulations.
*
* @param serverAddress to connect to
* @param fallbackManipulations fallback manipulations should the server fail
* @param manipulationInfoFactory factory to create manipulation info
*/
@Inject
public GRpcManipulations(
@Named(TestSuiteConfig.GRPC_SERVER_ADDRESS) final String serverAddress,
final FallbackManipulations fallbackManipulations,
final ManipulationInfoFactory manipulationInfoFactory) {
this.fallback = fallbackManipulations;
this.manipulationInfoFactory = manipulationInfoFactory;
final Channel channel = ManagedChannelBuilder.forTarget(serverAddress)
// Channels are secure by default (via SSL/TLS), which we don't really need
.usePlaintext()
.build();
activationStateStub = ActivationStateServiceGrpc.newBlockingStub(channel);
alertStub = AlertServiceGrpc.newBlockingStub(channel);
contextStub = ContextServiceGrpc.newBlockingStub(channel);
deviceStub = DeviceServiceGrpc.newBlockingStub(channel);
metricStub = MetricServiceGrpc.newBlockingStub(channel);
}
@Override
public ResponseTypes.Result setLocationDetail(final LocationDetail locationDetail) {
final var protoLocation = locationDetailToProto(locationDetail);
final var message = ContextRequests.SetLocationDetailRequest.newBuilder()
.setLocation(protoLocation)
.build();
return performCallWrapper(
v -> contextStub.setLocationDetail(message),
v -> fallback.setLocationDetail(locationDetail),
BasicResponses.BasicResponse::getResult,
BasicResponses.BasicResponse::getResult,
List.of(new ImmutablePair<>(
Constants.MANIPULATION_PARAMETER_LOCATION_DETAIL,
String.format(
"poC=%s, " + "room=%s, " + "bed=%s, " + "facility=%s, " + "building=%s, " + "floor=%s",
locationDetail.getPoC(),
locationDetail.getRoom(),
locationDetail.getBed(),
locationDetail.getFacility(),
locationDetail.getBuilding(),
locationDetail.getFloor()))));
}
@Override
public List<String> getRemovableDescriptorsOfClass() {
return getRemovableDescriptorsOfClass(AbstractDescriptor.class);
}
@Override
public List<String> getRemovableDescriptorsOfClass(final Class<? extends AbstractDescriptor> descriptorClass) {
final DeviceRequests.GetRemovableDescriptorsOfClassRequest request =
DeviceRequests.GetRemovableDescriptorsOfClassRequest.newBuilder()
.setDescriptorClass(toApiDescriptorClass(descriptorClass))
.build();
return performCallWrapper(
v -> deviceStub.getRemovableDescriptorsOfClass(request),
v -> fallback.getRemovableDescriptorsOfClass(descriptorClass),
res -> res.getStatus().getResult(),
DeviceResponses.GetRemovableDescriptorsResponse::getHandleList,
Collections.emptyList());
}
@Override
public ResponseTypes.Result removeDescriptor(final String handle) {
final var request =
BasicRequests.BasicHandleRequest.newBuilder().setHandle(handle).build();
return performCallWrapper(
v -> deviceStub.removeDescriptor(request),
v -> fallback.removeDescriptor(handle),
BasicResponses.BasicResponse::getResult,
BasicResponses.BasicResponse::getResult,
List.of(new ImmutablePair<>(Constants.MANIPULATION_PARAMETER_HANDLE, handle)));
}
@Override
public ResponseTypes.Result insertDescriptor(final String handle) {
final var request =
BasicRequests.BasicHandleRequest.newBuilder().setHandle(handle).build();
return performCallWrapper(
v -> deviceStub.insertDescriptor(request),
v -> fallback.insertDescriptor(handle),
BasicResponses.BasicResponse::getResult,
BasicResponses.BasicResponse::getResult,
List.of(new ImmutablePair<>(Constants.MANIPULATION_PARAMETER_HANDLE, handle)));
}
@Override
public ResponseTypes.Result sendHello() {
return performCallWrapper(
v -> deviceStub.sendHello(Empty.getDefaultInstance()),
v -> fallback.sendHello(),
BasicResponses.BasicResponse::getResult,
BasicResponses.BasicResponse::getResult,
Collections.emptyList());
}
@Override
public Optional<String> createContextStateWithAssociation(
final String descriptorHandle, final ContextAssociation association) {
final var request = ContextRequests.CreateContextStateWithAssociationRequest.newBuilder()
.setDescriptorHandle(descriptorHandle)
.setContextAssociation(toApiContextType(association))
.build();
return performCallWrapper(
v -> contextStub.createContextStateWithAssociation(request),
v -> fallback.createContextStateWithAssociation(descriptorHandle, association),
response -> response.getStatus().getResult(),
msg -> {
if (msg.getContextStateHandle().isBlank()) {
return Optional.empty();
}
return Optional.of(msg.getContextStateHandle());
},
List.of(
new ImmutablePair<>(Constants.MANIPULATION_PARAMETER_HANDLE, descriptorHandle),
new ImmutablePair<>(
Constants.MANIPULATION_PARAMETER_CONTEXT_ASSOCIATION, association.value())));
}
@Override
public ResponseTypes.Result setAlertActivation(final String handle, final AlertActivation activationState) {
final var message = ActivationStateRequests.SetAlertActivationRequest.newBuilder()
.setHandle(handle)
.setActivation(toApiActivationStateType(activationState))
.build();
return performCallWrapper(
v -> activationStateStub.setAlertActivation(message),
v -> fallback.setAlertActivation(handle, activationState),
BasicResponses.BasicResponse::getResult,
BasicResponses.BasicResponse::getResult,
List.of(
new ImmutablePair<>(Constants.MANIPULATION_PARAMETER_HANDLE, handle),
new ImmutablePair<>(
Constants.MANIPULATION_PARAMETER_ALERT_ACTIVATION, activationState.value())));
}
@Override
public ResponseTypes.Result setAlertConditionPresence(final String handle, final boolean presence) {
final var message = AlertRequests.SetAlertConditionPresenceRequest.newBuilder()
.setHandle(handle)
.setPresence(presence)
.build();
return performCallWrapper(
v -> alertStub.setAlertConditionPresence(message),
v -> fallback.setAlertConditionPresence(handle, presence),
BasicResponses.BasicResponse::getResult,
BasicResponses.BasicResponse::getResult,
List.of(
new ImmutablePair<>(Constants.MANIPULATION_PARAMETER_HANDLE, handle),
new ImmutablePair<>(Constants.MANIPULATION_PARAMETER_PRESENCE, String.format("%s", presence))));
}
@Override
public ResponseTypes.Result setSystemSignalActivation(
final String handle, final AlertSignalManifestation manifestation, final AlertActivation activation) {
final var message = ActivationStateRequests.SetSystemSignalActivationRequest.newBuilder()
.setHandle(handle)
.setManifestation(toApiManifestationType(manifestation))
.setActivation(toApiActivationStateType(activation))
.build();
return performCallWrapper(
v -> activationStateStub.setSystemSignalActivation(message),
v -> fallback.setSystemSignalActivation(handle, manifestation, activation),
BasicResponses.BasicResponse::getResult,
BasicResponses.BasicResponse::getResult,
List.of(
new ImmutablePair<>(Constants.MANIPULATION_PARAMETER_HANDLE, handle),
new ImmutablePair<>(
Constants.MANIPULATION_PARAMETER_ALERT_SIGNAL_ACTIVATION, manifestation.value()),
new ImmutablePair<>(Constants.MANIPULATION_PARAMETER_ALERT_ACTIVATION, activation.value())));
}
@Override
public ResponseTypes.Result setComponentActivation(final String handle, final ComponentActivation activationState) {
final var message = ActivationStateRequests.SetComponentActivationRequest.newBuilder()
.setHandle(handle)
.setActivation(toApiComponentActivationStateType(activationState))
.build();
return performCallWrapper(
v -> activationStateStub.setComponentActivation(message),
v -> fallback.setComponentActivation(handle, activationState),
BasicResponses.BasicResponse::getResult,
BasicResponses.BasicResponse::getResult,
List.of(
new ImmutablePair<>(Constants.MANIPULATION_PARAMETER_HANDLE, handle),
new ImmutablePair<>(
Constants.MANIPULATION_PARAMETER_COMPONENT_ACTIVATION, activationState.toString())));
}
@Override
public ResponseTypes.Result setMetricQualityValidity(final String handle, final MeasurementValidity validity) {
final var message = MetricRequests.SetMetricQualityValidityRequest.newBuilder()
.setHandle(handle)
.setValidity(toApiMeasurementValidityType(validity))
.build();
return performCallWrapper(
v -> metricStub.setMetricQualityValidity(message),
v -> fallback.setMetricQualityValidity(handle, validity),
BasicResponses.BasicResponse::getResult,
BasicResponses.BasicResponse::getResult,
List.of(
new ImmutablePair<>(Constants.MANIPULATION_PARAMETER_HANDLE, handle),
new ImmutablePair<>(Constants.MANIPULATION_PARAMETER_MEASUREMENT_VALIDITY, validity.value())));
}
@Override
public ResponseTypes.Result setMetricStatus(
final String handle, final MetricCategory category, final ComponentActivation activation) {
final var metricStatus = getMetricStatus(activation);
if (metricStatus.isEmpty()) return ResponseTypes.Result.RESULT_FAIL;
final var message = MetricRequests.SetMetricStatusRequest.newBuilder()
.setHandle(handle)
.setStatus(metricStatus.orElseThrow())
.build();
return performCallWrapper(
v -> metricStub.setMetricStatus(message),
v -> fallback.setMetricStatus(handle, category, activation),
BasicResponses.BasicResponse::getResult,
BasicResponses.BasicResponse::getResult,
List.of(
new ImmutablePair<>(Constants.MANIPULATION_PARAMETER_HANDLE, handle),
new ImmutablePair<>(Constants.MANIPULATION_PARAMETER_METRIC_CATEGORY, category.value()),
new ImmutablePair<>(
Constants.MANIPULATION_PARAMETER_COMPONENT_ACTIVATION, activation.toString())));
}
@Override
public ResponseTypes.Result triggerDescriptorUpdate(final String handle) {
final var message =
BasicRequests.BasicHandleRequest.newBuilder().setHandle(handle).build();
return performCallWrapper(
v -> deviceStub.triggerDescriptorUpdate(message),
v -> fallback.triggerDescriptorUpdate(handle),
BasicResponses.BasicResponse::getResult,
BasicResponses.BasicResponse::getResult,
List.of(new ImmutablePair<>(Constants.MANIPULATION_PARAMETER_QNAME, handle)));
}
@Override
public ResponseTypes.Result triggerReport(final QName report) {
final var reportType = REPORT_TYPE_MAP.get(report);
if (reportType == null) return ResponseTypes.Result.RESULT_FAIL;
final var message = DeviceRequests.TriggerReportRequest.newBuilder()
.setReport(reportType)
.build();
return performCallWrapper(
v -> deviceStub.triggerReport(message),
v -> fallback.triggerReport(report),
BasicResponses.BasicResponse::getResult,
BasicResponses.BasicResponse::getResult,
List.of(new ImmutablePair<>(Constants.MANIPULATION_PARAMETER_QNAME, report.toString())));
}
private Optional<MetricTypes.MetricStatus> getMetricStatus(final ComponentActivation activation) {
return switch (activation) {
case ON -> Optional.of(MetricTypes.MetricStatus.METRIC_STATUS_PERFORMED_OR_APPLIED);
case NOT_RDY -> Optional.of(MetricTypes.MetricStatus.METRIC_STATUS_CURRENTLY_INITIALIZING);
case STND_BY -> Optional.of(
MetricTypes.MetricStatus.METRIC_STATUS_INITIALIZED_BUT_NOT_PERFORMING_OR_APPLYING);
case SHTDN -> Optional.of(MetricTypes.MetricStatus.METRIC_STATUS_CURRENTLY_DE_INITIALIZING);
case OFF -> Optional.of(
MetricTypes.MetricStatus.METRIC_STATUS_DE_INITIALIZED_AND_NOT_PERFORMING_OR_APPLYING);
case FAIL -> Optional.of(MetricTypes.MetricStatus.METRIC_STATUS_FAILED);
};
}
/**
* Performs a gRPC call. Adds manipulation information to the database.
*
* @param func to call for gRPC call
* @param fallbackFunc to call in case of gRPC failures
* @param statusExtractor to extract status from gRPC call
* @param responseExtractor to extract response value from gRPC call
* @param <GRES> gRPC response type
* @param <RES> response type
* @param parameter for the manipulation, can be empty
* @return response
*/
public <GRES, RES> RES performCallWrapper(
final Function<Void, GRES> func,
final Function<Void, RES> fallbackFunc,
final Function<GRES, ResponseTypes.Result> statusExtractor,
final Function<GRES, RES> responseExtractor,
final List<Pair<String, String>> parameter) {
final var startTime = System.nanoTime();
final var result = performCall(func, fallbackFunc, statusExtractor, responseExtractor);
final var endTime = System.nanoTime();
final var methodName = walker.walk(
s -> s.map(StackWalker.StackFrame::getMethodName).skip(1).findFirst());
final var manipulation = manipulationInfoFactory.create(
startTime, endTime, getResultForPersisting(result), methodName.orElseThrow(), parameter);
manipulation.addToStorage();
return result;
}
private <GRES, RES> RES performCall(
final Function<Void, GRES> func,
final Function<Void, RES> fallbackFunc,
final Function<GRES, ResponseTypes.Result> statusExtractor,
final Function<GRES, RES> responseExtractor) {
final GRES response;
try {
response = func.apply(null);
LOG.debug("API Response was {}", response);
} catch (final io.grpc.StatusRuntimeException e) {
LOG.debug("grpc call was unavailable", e);
LOG.warn("Automated manipulation not available");
return fallbackFunc.apply(null);
}
return switch (statusExtractor.apply(response)) {
case RESULT_NOT_IMPLEMENTED -> {
LOG.warn("Server has not implemented method");
yield fallbackFunc.apply(null);
}
case RESULT_SUCCESS, RESULT_NOT_SUPPORTED, RESULT_FAIL -> responseExtractor.apply(response);
default -> {
LOG.warn("Server has not sent a valid result, going to fallback");
yield fallbackFunc.apply(null);
}
};
}
private <RES> ResponseTypes.Result getResultForPersisting(final RES result) {
ResponseTypes.Result response = ResponseTypes.Result.UNRECOGNIZED;
if (result instanceof final List resultList) {
if (resultList.isEmpty()) {
response = ResponseTypes.Result.RESULT_FAIL;
} else {
response = ResponseTypes.Result.RESULT_SUCCESS;
}
} else if (result instanceof final Optional<?> resultOptional) {
if (resultOptional.isEmpty()) {
response = ResponseTypes.Result.RESULT_FAIL;
} else {
response = ResponseTypes.Result.RESULT_SUCCESS;
}
} else if (result instanceof ResponseTypes.Result castedResult) {
response = castedResult;
}
return response;
}
ContextTypes.ContextAssociation toApiContextType(final ContextAssociation contextAssociation) {
return switch (contextAssociation) {
case NO -> ContextTypes.ContextAssociation.CONTEXT_ASSOCIATION_NOT_ASSOCIATED;
case DIS -> ContextTypes.ContextAssociation.CONTEXT_ASSOCIATION_DISASSOCIATED;
case PRE -> ContextTypes.ContextAssociation.CONTEXT_ASSOCIATION_PRE_ASSOCIATED;
case ASSOC -> ContextTypes.ContextAssociation.CONTEXT_ASSOCIATION_ASSOCIATED;
};
}
ActivationStateTypes.AlertSignalManifestation toApiManifestationType(final AlertSignalManifestation manifestation) {
return switch (manifestation) {
case VIS -> ActivationStateTypes.AlertSignalManifestation.ALERT_SIGNAL_MANIFESTATION_VIS;
case AUD -> ActivationStateTypes.AlertSignalManifestation.ALERT_SIGNAL_MANIFESTATION_AUD;
case TAN -> ActivationStateTypes.AlertSignalManifestation.ALERT_SIGNAL_MANIFESTATION_TAN;
case OTH -> ActivationStateTypes.AlertSignalManifestation.ALERT_SIGNAL_MANIFESTATION_OTH;
};
}
ActivationStateTypes.AlertActivation toApiActivationStateType(final AlertActivation alertActivation) {
return switch (alertActivation) {
case ON -> ActivationStateTypes.AlertActivation.ALERT_ACTIVATION_ON;
case OFF -> ActivationStateTypes.AlertActivation.ALERT_ACTIVATION_OFF;
case PSD -> ActivationStateTypes.AlertActivation.ALERT_ACTIVATION_PSD;
};
}
ActivationStateTypes.ComponentActivation toApiComponentActivationStateType(final ComponentActivation activation) {
return switch (activation) {
case ON -> ActivationStateTypes.ComponentActivation.COMPONENT_ACTIVATION_ON;
case NOT_RDY -> ActivationStateTypes.ComponentActivation.COMPONENT_ACTIVATION_NOT_READY;
case STND_BY -> ActivationStateTypes.ComponentActivation.COMPONENT_ACTIVATION_STANDBY;
case SHTDN -> ActivationStateTypes.ComponentActivation.COMPONENT_ACTIVATION_SHUTDOWN;
case OFF -> ActivationStateTypes.ComponentActivation.COMPONENT_ACTIVATION_OFF;
case FAIL -> ActivationStateTypes.ComponentActivation.COMPONENT_ACTIVATION_FAILURE;
};
}
MetricTypes.MeasurementValidity toApiMeasurementValidityType(final MeasurementValidity validity) {
return switch (validity) {
case VLD -> MetricTypes.MeasurementValidity.MEASUREMENT_VALIDITY_VALID;
case VLDATED -> MetricTypes.MeasurementValidity.MEASUREMENT_VALIDITY_VALIDATED_DATA;
case ONG -> MetricTypes.MeasurementValidity.MEASUREMENT_VALIDITY_MEASUREMENT_ONGOING;
case QST -> MetricTypes.MeasurementValidity.MEASUREMENT_VALIDITY_QUESTIONABLE;
case CALIB -> MetricTypes.MeasurementValidity.MEASUREMENT_VALIDITY_CALIBRATION_ONGOING;
case INV -> MetricTypes.MeasurementValidity.MEASUREMENT_VALIDITY_INVALID;
case OFLW -> MetricTypes.MeasurementValidity.MEASUREMENT_VALIDITY_OVERFLOW;
case UFLW -> MetricTypes.MeasurementValidity.MEASUREMENT_VALIDITY_UNDERFLOW;
case NA -> MetricTypes.MeasurementValidity.MEASUREMENT_VALIDITY_NA;
};
}
private DeviceTypes.DescriptorClass toApiDescriptorClass(final Class<? extends AbstractDescriptor> descriptorType) {
if (MdsDescriptor.class.equals(descriptorType)) {
return DeviceTypes.DescriptorClass.DESCRIPTOR_CLASS_MDS;
} else {
return DeviceTypes.DescriptorClass.DESCRIPTOR_CLASS_ABSTRACT;
}
}
private static ContextTypes.LocationDetail locationDetailToProto(final LocationDetail locationDetail) {
final var builder = ContextTypes.LocationDetail.newBuilder();
if (locationDetail.getPoC() != null) builder.setPoc(buildStringValue(locationDetail.getPoC()));
if (locationDetail.getRoom() != null) builder.setRoom(buildStringValue(locationDetail.getRoom()));
if (locationDetail.getBed() != null) builder.setBed(buildStringValue(locationDetail.getBed()));
if (locationDetail.getFacility() != null) builder.setFacility(buildStringValue(locationDetail.getFacility()));
if (locationDetail.getBuilding() != null) builder.setBuilding(buildStringValue(locationDetail.getBuilding()));
if (locationDetail.getFloor() != null) builder.setFloor(buildStringValue(locationDetail.getFloor()));
return builder.build();
}
private static StringValue buildStringValue(final String value) {
return StringValue.newBuilder().setValue(value).build();
}
}