Skip to content

Package: ConfigurationResponsePayload$PayloadToDisplayStringMode

ConfigurationResponsePayload$PayloadToDisplayStringMode

nameinstructionbranchcomplexitylinemethod
static {...}
M: 44 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2016, 2022 Eurotech and/or its affiliates and others
3: *
4: * This program and the accompanying materials are made
5: * available under the terms of the Eclipse Public License 2.0
6: * which is available at https://www.eclipse.org/legal/epl-2.0/
7: *
8: * SPDX-License-Identifier: EPL-2.0
9: *
10: * Contributors:
11: * Eurotech - initial API and implementation
12: *******************************************************************************/
13: package org.eclipse.kapua.service.device.management.configuration.message.internal;
14:
15: import org.eclipse.kapua.commons.util.xml.XmlUtil;
16: import org.eclipse.kapua.locator.KapuaLocator;
17: import org.eclipse.kapua.message.KapuaPayload;
18: import org.eclipse.kapua.service.device.management.commons.message.response.KapuaResponsePayloadImpl;
19: import org.eclipse.kapua.service.device.management.commons.setting.DeviceManagementSetting;
20: import org.eclipse.kapua.service.device.management.commons.setting.DeviceManagementSettingKey;
21: import org.eclipse.kapua.service.device.management.configuration.DeviceComponentConfiguration;
22: import org.eclipse.kapua.service.device.management.configuration.DeviceConfiguration;
23: import org.eclipse.kapua.service.device.management.configuration.DeviceConfigurationFactory;
24: import org.eclipse.kapua.service.device.management.configuration.internal.settings.DeviceConfigurationManagementSettings;
25: import org.eclipse.kapua.service.device.management.configuration.internal.settings.DeviceConfigurationManagementSettingsKeys;
26: import org.eclipse.kapua.service.device.management.message.response.KapuaResponsePayload;
27: import org.slf4j.Logger;
28: import org.slf4j.LoggerFactory;
29:
30: import javax.validation.constraints.NotNull;
31: import java.util.stream.Collectors;
32:
33: /**
34: * {@link DeviceConfiguration} {@link KapuaResponsePayload} implementation.
35: *
36: * @since 1.0.0
37: */
38: public class ConfigurationResponsePayload extends KapuaResponsePayloadImpl implements KapuaResponsePayload {
39:
40: private static final Logger LOG = LoggerFactory.getLogger(ConfigurationResponsePayload.class);
41:
42: private static final String PAYLOAD_TO_DISPLAY_STRING_MODE = DeviceConfigurationManagementSettings.getInstance().getString(DeviceConfigurationManagementSettingsKeys.PAYLOAD_TO_DISPLAY_STRING_MODE, "NONE");
43: private static final String CHAR_ENCODING = DeviceManagementSetting.getInstance().getString(DeviceManagementSettingKey.CHAR_ENCODING);
44:
45: private static final DeviceConfigurationFactory DEVICE_CONFIGURATION_FACTORY = KapuaLocator.getInstance().getFactory(DeviceConfigurationFactory.class);
46:
47: /**
48: * Gets the {@link DeviceConfiguration}from the {@link #getBody()}.
49: *
50: * @return The {@link DeviceConfiguration}from the {@link #getBody()}.
51: * @throws Exception if reading {@link #getBody()} errors.
52: * @since 1.5.0
53: */
54: public DeviceConfiguration getDeviceConfigurations() throws Exception {
55: if (!hasBody()) {
56: return DEVICE_CONFIGURATION_FACTORY.newConfigurationInstance();
57: }
58:
59: String bodyString = new String(getBody(), CHAR_ENCODING);
60: return XmlUtil.unmarshal(bodyString, DeviceConfiguration.class);
61: }
62:
63: /**
64: * Sets the {@link DeviceConfiguration} in the {@link #getBody()}.
65: *
66: * @param deviceConfiguration The {@link DeviceConfiguration}.
67: * @throws Exception if writing errors.
68: * @since 1.5.0
69: */
70: public void setDeviceConfigurations(@NotNull DeviceConfiguration deviceConfiguration) throws Exception {
71: String bodyString = XmlUtil.marshal(deviceConfiguration);
72: setBody(bodyString.getBytes(CHAR_ENCODING));
73: }
74:
75: @Override
76: public String toDisplayString() {
77: try {
78: PayloadToDisplayStringMode toDisplayStringMode;
79: try {
80: toDisplayStringMode = PayloadToDisplayStringMode.valueOf(PAYLOAD_TO_DISPLAY_STRING_MODE);
81: } catch (IllegalArgumentException iae) {
82: LOG.warn("Invalid device.management.configuration.payload.toDisplayString.mode setting value {}. Please fix the configuration value. Allowed values are: NONE, DEFAULT, NUMBER_OF_COMPONENTS, LIST_OF_COMPONENTS. Defaulting to DEFAULT", PAYLOAD_TO_DISPLAY_STRING_MODE);
83: toDisplayStringMode = PayloadToDisplayStringMode.DEFAULT;
84: }
85:
86: switch (toDisplayStringMode) {
87: case NONE:
88: return "";
89: case DEFAULT:
90: return super.toDisplayString();
91: case NUMBER_OF_COMPONENTS:
92: return "Read " + getDeviceConfigurations().getComponentConfigurations().size() + " configuration components: " + getDeviceConfigurations().getComponentConfigurations().stream().map(DeviceComponentConfiguration::getId).sorted(String::compareTo).collect(Collectors.joining(", "));
93: case LIST_OF_COMPONENTS:
94: return "Read configuration components: " + getDeviceConfigurations().getComponentConfigurations().stream().map(DeviceComponentConfiguration::getId).sorted(String::compareTo).collect(Collectors.joining(", "));
95: }
96: } catch (Exception e) {
97: LOG.warn("Error while invoking ConfigurationResponsePayload.toDisplayString(). Defaulting to KapuaResponsePayload.toDisplayString(). Error: {}", e.getMessage());
98: }
99:
100: return super.toDisplayString();
101: }
102:
103: /**
104: * Defines the way the {@link #toDisplayString()} will behave.
105: *
106: * @since 2.0.0
107: */
108: private enum PayloadToDisplayStringMode {
109:
110: /**
111: * Displays nothing.
112: *
113: * @since 2.0.0
114: */
115: NONE,
116:
117: /**
118: * Displays what {@link KapuaPayload#toDisplayString()} displays (only metrics currently).
119: *
120: * @since 2.0.0
121: */
122: DEFAULT,
123:
124: /**
125: * Displays the number of {@link DeviceComponentConfiguration} in the {@link DeviceConfiguration}.
126: *
127: * @since 2.0.0
128: */
129: NUMBER_OF_COMPONENTS,
130:
131: /**
132: * Displays the list of {@link DeviceComponentConfiguration} in the {@link DeviceConfiguration}.
133: *
134: * @since 2.0.0
135: */
136: LIST_OF_COMPONENTS,
137: }
138: }
139:
140: