-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathUpdateSettingsRequestHandler.java
112 lines (98 loc) · 5.18 KB
/
UpdateSettingsRequestHandler.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
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/
package org.opensearch.sdk.handlers;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Consumer;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.opensearch.Version;
import org.opensearch.common.settings.Setting;
import org.opensearch.common.settings.WriteableSetting;
import org.opensearch.core.common.unit.ByteSizeValue;
import org.opensearch.common.unit.TimeValue;
import org.opensearch.extensions.AcknowledgedResponse;
import org.opensearch.extensions.UpdateSettingsRequest;
/**
* Handles requests to update settings
*/
public class UpdateSettingsRequestHandler {
private static final Logger logger = LogManager.getLogger(UpdateSettingsRequestHandler.class);
private Map<Setting<?>, Consumer<?>> settingUpdateConsumers;
/**
* Instantiates a new Update Setting Request Handler
*/
public UpdateSettingsRequestHandler() {
this.settingUpdateConsumers = new HashMap<>();
}
/**
* Registers the component {@link Setting} and the corresponding consumer to the settingsUpdateConsumer map.
* This map is used only when handling {@link UpdateSettingsRequest}
*
* @param settingUpdateConsumers The settings and their corresponding update consumers to register
*/
public void registerSettingUpdateConsumer(Map<Setting<?>, Consumer<?>> settingUpdateConsumers) {
this.settingUpdateConsumers.putAll(settingUpdateConsumers);
}
/**
* Handles a request to update a setting from OpenSearch. Extensions must register their setting keys and consumers within the settingUpdateConsumer map
*
* @param updateSettingsRequest The request to handle.
* @return A response acknowledging the request.
*/
@SuppressWarnings("unchecked")
public AcknowledgedResponse handleUpdateSettingsRequest(UpdateSettingsRequest updateSettingsRequest) {
logger.info("Registering UpdateSettingsRequest received from OpenSearch");
boolean settingUpdateStatus = true;
WriteableSetting.SettingType settingType = updateSettingsRequest.getSettingType();
Setting<?> componentSetting = updateSettingsRequest.getComponentSetting();
Object data = updateSettingsRequest.getData();
// Setting updater in OpenSearch performs setting change validation, only need to cast the consumer to the corresponding type and
// invoke the consumer
try {
switch (settingType) {
case Boolean:
Consumer<Boolean> boolConsumer = (Consumer<Boolean>) this.settingUpdateConsumers.get(componentSetting);
boolConsumer.accept(Boolean.parseBoolean(data.toString()));
case Integer:
Consumer<Integer> intConsumer = (Consumer<Integer>) this.settingUpdateConsumers.get(componentSetting);
intConsumer.accept(Integer.parseInt(data.toString()));
case Long:
Consumer<Long> longConsumer = (Consumer<Long>) this.settingUpdateConsumers.get(componentSetting);
longConsumer.accept(Long.parseLong(data.toString()));
case Float:
Consumer<Float> floatConsumer = (Consumer<Float>) this.settingUpdateConsumers.get(componentSetting);
floatConsumer.accept(Float.parseFloat(data.toString()));
case Double:
Consumer<Double> doubleConsumer = (Consumer<Double>) this.settingUpdateConsumers.get(componentSetting);
doubleConsumer.accept(Double.parseDouble(data.toString()));
case String:
Consumer<String> stringConsumer = (Consumer<String>) this.settingUpdateConsumers.get(componentSetting);
stringConsumer.accept(data.toString());
case TimeValue:
Consumer<TimeValue> timeValueConsumer = (Consumer<TimeValue>) this.settingUpdateConsumers.get(componentSetting);
timeValueConsumer.accept(TimeValue.parseTimeValue(data.toString(), componentSetting.getKey()));
case ByteSizeValue:
Consumer<ByteSizeValue> byteSizeValueConsumer = (Consumer<ByteSizeValue>) this.settingUpdateConsumers.get(
componentSetting
);
byteSizeValueConsumer.accept(ByteSizeValue.parseBytesSizeValue(data.toString(), componentSetting.getKey()));
case Version:
Consumer<Version> versionConsumer = (Consumer<Version>) this.settingUpdateConsumers.get(componentSetting);
versionConsumer.accept((Version) data);
default:
throw new UnsupportedOperationException("Setting Update Consumer type does not exist and is not handled here");
}
} catch (Exception e) {
logger.info(e.getMessage());
settingUpdateStatus = false;
}
return new AcknowledgedResponse(settingUpdateStatus);
}
}