Skip to content

Commit aceeeb2

Browse files
authored
Merge pull request #323 from Cumulocity-IoT/develop
Fixing requests hanging during microservice startup
2 parents 119685f + 56aa09a commit aceeeb2

29 files changed

+157
-100
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,5 +83,5 @@ The mappings with inputs and substitutions are explained in the [sample document
8383

8484
---
8585

86-
These tools are provided as-is and without warranty or support. They do not constitute part of the Cumulocity product suite. Users are free to use, fork and modify them, subject to the license agreement. While Cumulocity GmbH welcomes contributions, we cannot guarantee to include every contribution in the master project.
86+
These tools are provided as-is and without warranty or support. They do not constitute part of the Cumulocity GmbH product suite. Users are free to use, fork and modify them, subject to the license agreement. While Cumulocity GmbH welcomes contributions, we cannot guarantee to include every contribution in the master project.
8787

dynamic-mapping-extension/src/main/java/dynamic/mapping/processor/extension/external/ProcessorExtensionCustomAlarm.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ private void prepareAndSubstituteInPayload(ProcessingContext<byte[]> context, Do
212212
String pathTarget, SubstituteValue substitute, C8YAgent c8yAgent) {
213213
Mapping mapping = context.getMapping();
214214
String tenant = context.getTenant();
215-
if ((Mapping.IDENTITY + ".externalId").equals(pathTarget)) {
215+
if ((Mapping.TOKEN_IDENTITY + ".externalId").equals(pathTarget)) {
216216
ID identity = new ID(mapping.externalIdType, substitute.value.toString());
217217
SubstituteValue sourceId = new SubstituteValue(substitute.value,
218218
TYPE.TEXTUAL, RepairStrategy.CREATE_IF_MISSING, false);
@@ -230,7 +230,7 @@ private void prepareAndSubstituteInPayload(ProcessingContext<byte[]> context, Do
230230
context.setSourceId(sourceId.value.toString());
231231
substitute.repairStrategy = RepairStrategy.CREATE_IF_MISSING;
232232
}
233-
} else if ((Mapping.IDENTITY + ".c8ySourceId").equals(pathTarget)) {
233+
} else if ((Mapping.TOKEN_IDENTITY + ".c8ySourceId").equals(pathTarget)) {
234234
SubstituteValue sourceId = new SubstituteValue(substitute.value,
235235
TYPE.TEXTUAL, RepairStrategy.CREATE_IF_MISSING, false);
236236
// in this case the device needs to exists beforehand

dynamic-mapping-service/src/main/configuration/cumulocity.json

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,25 @@
3838
"ROLE_USER_MANAGEMENT_CREATE",
3939
"ROLE_USER_MANAGEMENT_ADMIN"
4040
],
41-
4241
"roles":[
4342
"ROLE_MAPPING_ADMIN",
4443
"ROLE_MAPPING_CREATE",
4544
"ROLE_MAPPING_HTTP_CONNECTOR_CREATE"
46-
]
45+
],
46+
"livenessProbe": {
47+
"httpGet": {
48+
"path": "/health"
49+
},
50+
"initialDelaySeconds": 90,
51+
"periodSeconds": 10
52+
},
53+
"readinessProbe": {
54+
"httpGet": {
55+
"path": "/health",
56+
"port": 80
57+
58+
},
59+
"initialDelaySeconds": 90,
60+
"periodSeconds": 10
61+
}
4762
}

dynamic-mapping-service/src/main/java/dynamic/mapping/controller/OperationController.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import java.util.concurrent.TimeUnit;
2929

3030
import jakarta.validation.Valid;
31-
import dynamic.mapping.configuration.CodeTemplate;
3231
import dynamic.mapping.configuration.ConnectorConfiguration;
3332
import dynamic.mapping.configuration.ConnectorConfigurationComponent;
3433
import dynamic.mapping.configuration.ServiceConfiguration;
@@ -127,8 +126,9 @@ public ResponseEntity<?> runOperation(@Valid @RequestBody ServiceOperation opera
127126
case REFRESH_STATUS_MAPPING:
128127
return handleRefreshStatusMapping(tenant);
129128
case RESET_STATUS_MAPPING:
130-
case RESET_DEPLOYMENT_MAP:
131129
return handleResetStatusMapping(tenant);
130+
case RESET_DEPLOYMENT_MAP:
131+
return handleResetDeploymentMap(tenant);
132132
case RELOAD_EXTENSIONS:
133133
return handleReloadExtensions(tenant);
134134
case ACTIVATE_MAPPING:
@@ -222,6 +222,11 @@ private ResponseEntity<?> handleReloadMappings(String tenant) throws ConnectorRe
222222
return ResponseEntity.status(HttpStatus.CREATED).build();
223223
}
224224

225+
private ResponseEntity<?> handleResetDeploymentMap(String tenant) throws Exception {
226+
mappingComponent.initializeDeploymentMap(tenant, true);
227+
return ResponseEntity.status(HttpStatus.CREATED).build();
228+
}
229+
225230
private ResponseEntity<?> handleSnoopReset(String tenant, Map<String, String> parameters) throws Exception {
226231
String id = parameters.get("id");
227232
mappingComponent.resetSnoop(tenant, id);

dynamic-mapping-service/src/main/java/dynamic/mapping/core/MappingComponent.java

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -191,11 +191,22 @@ public void sendMappingStatus(String tenant) {
191191
if (statusMapping.values().size() > 0 && mappingServiceRepresentation != null && initialized) {
192192
log.debug("Tenant {} - Sending monitoring: {}", tenant, statusMapping.values().size());
193193
Map<String, Object> service = new HashMap<String, Object>();
194-
MappingStatus[] ms = statusMapping.values().toArray(new MappingStatus[0]);
195-
// add current name of mappings to the status messages
194+
// Convert statusMapping values to a list for filtering
195+
List<MappingStatus> msList = new ArrayList<>(statusMapping.values());
196+
197+
// Filter the list to keep only desired items
198+
msList.removeIf(status -> !"UNSPECIFIED".equals(status.id) &&
199+
!cacheMappingInbound.get(tenant).containsKey(status.id) &&
200+
!cacheMappingOutbound.get(tenant).containsKey(status.id));
201+
202+
// Convert filtered list to array
203+
MappingStatus[] ms = msList.toArray(new MappingStatus[0]);
204+
205+
// Set names according to the rules
196206
for (int index = 0; index < ms.length; index++) {
197-
ms[index].name = "UNSPECIFIED".equals(ms[index].id) ? "Unspecified" : "Mapping deleted";
198-
if (cacheMappingInbound.get(tenant).containsKey(ms[index].id)) {
207+
if ("UNSPECIFIED".equals(ms[index].id)) {
208+
ms[index].name = "Unspecified";
209+
} else if (cacheMappingInbound.get(tenant).containsKey(ms[index].id)) {
199210
ms[index].name = cacheMappingInbound.get(tenant).get(ms[index].id).name;
200211
} else if (cacheMappingOutbound.get(tenant).containsKey(ms[index].id)) {
201212
ms[index].name = cacheMappingOutbound.get(tenant).get(ms[index].id).name;

dynamic-mapping-service/src/main/java/dynamic/mapping/model/Mapping.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,12 @@
4747
@ToString(exclude = { "sourceTemplate", "targetTemplate", "snoopedTemplates", "code" })
4848
public class Mapping implements Serializable {
4949

50-
public static final String IDENTITY = "_IDENTITY_";
50+
public static final String TOKEN_IDENTITY = "_IDENTITY_";
5151
public static final String TOKEN_TOPIC_LEVEL = "_TOPIC_LEVEL_";
5252
public static final String TOKEN_CONTEXT_DATA = "_CONTEXT_DATA_";
5353
public static final String CONTEXT_DATA_KEY_NAME = "key";
5454
public static final String CONTEXT_DATA_METHOD_NAME = "method";
55-
public static final String TIME = "time";
55+
public static final String KEY_TIME = "time";
5656

5757
public static int SNOOP_TEMPLATES_MAX = 10;
5858
public static final String SPLIT_TOPIC_REGEXP = "((?<=/)|(?=/))";
@@ -168,9 +168,9 @@ public boolean equals(Object m) {
168168
@JsonIgnore
169169
public String getGenericDeviceIdentifier() {
170170
if (useExternalId && !("").equals(externalIdType)) {
171-
return (Mapping.IDENTITY + ".externalId");
171+
return (Mapping.TOKEN_IDENTITY + ".externalId");
172172
} else {
173-
return (Mapping.IDENTITY + ".c8ySourceId");
173+
return (Mapping.TOKEN_IDENTITY + ".c8ySourceId");
174174
}
175175
}
176176

@@ -179,15 +179,15 @@ public Boolean definesDeviceIdentifier(
179179
Substitution sub) {
180180
if (Direction.INBOUND.equals(direction)) {
181181
if (useExternalId && !("").equals(externalIdType)) {
182-
return (Mapping.IDENTITY + ".externalId").equals(sub.pathTarget);
182+
return (Mapping.TOKEN_IDENTITY + ".externalId").equals(sub.pathTarget);
183183
} else {
184-
return (Mapping.IDENTITY + ".c8ySourceId").equals(sub.pathTarget);
184+
return (Mapping.TOKEN_IDENTITY + ".c8ySourceId").equals(sub.pathTarget);
185185
}
186186
} else {
187187
if (useExternalId && !("").equals(externalIdType)) {
188-
return (Mapping.IDENTITY + ".externalId").equals(sub.pathSource);
188+
return (Mapping.TOKEN_IDENTITY + ".externalId").equals(sub.pathSource);
189189
} else {
190-
return (Mapping.IDENTITY + ".c8ySourceId").equals(sub.pathSource);
190+
return (Mapping.TOKEN_IDENTITY + ".c8ySourceId").equals(sub.pathSource);
191191
}
192192
}
193193
}

dynamic-mapping-service/src/main/java/dynamic/mapping/processor/inbound/BaseProcessorInbound.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ private void prepareAndSubstituteInPayload(ProcessingContext<T> context, Documen
291291
String pathTarget, SubstituteValue substitute) {
292292
Mapping mapping = context.getMapping();
293293
String tenant = context.getTenant();
294-
if ((Mapping.IDENTITY + ".externalId").equals(pathTarget)) {
294+
if ((Mapping.TOKEN_IDENTITY + ".externalId").equals(pathTarget)) {
295295
ID identity = new ID(mapping.externalIdType, substitute.value.toString());
296296
SubstituteValue sourceId = new SubstituteValue(substitute.value,
297297
TYPE.TEXTUAL, RepairStrategy.CREATE_IF_MISSING, false);
@@ -309,7 +309,7 @@ private void prepareAndSubstituteInPayload(ProcessingContext<T> context, Documen
309309
context.setSourceId(sourceId.value.toString());
310310
substitute.repairStrategy = RepairStrategy.CREATE_IF_MISSING;
311311
}
312-
} else if ((Mapping.IDENTITY + ".c8ySourceId").equals(pathTarget)) {
312+
} else if ((Mapping.TOKEN_IDENTITY + ".c8ySourceId").equals(pathTarget)) {
313313
SubstituteValue sourceId = new SubstituteValue(substitute.value,
314314
TYPE.TEXTUAL, RepairStrategy.CREATE_IF_MISSING, false);
315315
// in this case the device needs to exists beforehand

dynamic-mapping-service/src/main/java/dynamic/mapping/processor/inbound/CodeBasedProcessorInbound.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ public void extractFromSource(ProcessingContext<Object> context)
165165
}
166166
processingCache.put(key, processingCacheEntry);
167167

168-
if (key.equals(Mapping.TIME)) {
168+
if (key.equals(Mapping.KEY_TIME)) {
169169
substitutionTimeExists = true;
170170
}
171171
}
@@ -180,12 +180,12 @@ public void extractFromSource(ProcessingContext<Object> context)
180180
// no substitution for the time property exists, then use the system time
181181
if (!substitutionTimeExists && mapping.targetAPI != API.INVENTORY && mapping.targetAPI != API.OPERATION) {
182182
List<SubstituteValue> processingCacheEntry = processingCache.getOrDefault(
183-
Mapping.TIME,
183+
Mapping.KEY_TIME,
184184
new ArrayList<>());
185185
processingCacheEntry.add(
186186
new SubstituteValue(new DateTime().toString(),
187187
TYPE.TEXTUAL, RepairStrategy.CREATE_IF_MISSING, false));
188-
processingCache.put(Mapping.TIME, processingCacheEntry);
188+
processingCache.put(Mapping.KEY_TIME, processingCacheEntry);
189189
}
190190
}
191191

dynamic-mapping-service/src/main/java/dynamic/mapping/processor/inbound/JSONProcessorInbound.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,20 +119,20 @@ public void extractFromSource(ProcessingContext<Object> context)
119119
substitution.pathTarget);
120120
}
121121

122-
if (substitution.pathTarget.equals(Mapping.TIME)) {
122+
if (substitution.pathTarget.equals(Mapping.KEY_TIME)) {
123123
substitutionTimeExists = true;
124124
}
125125
}
126126

127127
// no substitution for the time property exists, then use the system time
128128
if (!substitutionTimeExists && mapping.targetAPI != API.INVENTORY && mapping.targetAPI != API.OPERATION) {
129129
List<SubstituteValue> processingCacheEntry = processingCache.getOrDefault(
130-
Mapping.TIME,
130+
Mapping.KEY_TIME,
131131
new ArrayList<>());
132132
processingCacheEntry.add(
133133
new SubstituteValue(new DateTime().toString(),
134134
TYPE.TEXTUAL, RepairStrategy.CREATE_IF_MISSING, false));
135-
processingCache.put(Mapping.TIME, processingCacheEntry);
135+
processingCache.put(Mapping.KEY_TIME, processingCacheEntry);
136136
}
137137
}
138138

dynamic-mapping-service/src/main/java/dynamic/mapping/processor/model/SubstitutionContext.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
public class SubstitutionContext {
1111
private final String payload;
1212
private final String genericDeviceIdentifier;
13-
public final String IDENTITY_EXTERNAL = Mapping.IDENTITY + ".externalId";
14-
public final String IDENTITY_C8Y = Mapping.IDENTITY + ".c8ySourceId";
13+
public final String IDENTITY_EXTERNAL = Mapping.TOKEN_IDENTITY + ".externalId";
14+
public final String IDENTITY_C8Y = Mapping.TOKEN_IDENTITY + ".c8ySourceId";
1515

1616
public SubstitutionContext(String genericDeviceIdentifier, String payload) {
1717
this.payload = payload;
@@ -30,11 +30,11 @@ public String getExternalIdentifier() {
3030

3131
try {
3232
// Check if payload and the IDENTITY map exist
33-
if (json == null || json.get(Mapping.IDENTITY) == null || ! (json instanceof Map) ) {
33+
if (json == null || json.get(Mapping.TOKEN_IDENTITY) == null || ! (json instanceof Map) ) {
3434
return null;
3535
}
3636

37-
Map identityMap = (Map)json.get(Mapping.IDENTITY);
37+
Map identityMap = (Map)json.get(Mapping.TOKEN_IDENTITY);
3838
return (String) identityMap.get("externalId");
3939
} catch (Exception e) {
4040
// Optionally log the exception
@@ -51,11 +51,11 @@ public String getC8YIdentifier() {
5151

5252
try {
5353
// Check if payload and the IDENTITY map exist
54-
if (json == null || json.get(Mapping.IDENTITY) == null || ! (json instanceof Map) ) {
54+
if (json == null || json.get(Mapping.TOKEN_IDENTITY) == null || ! (json instanceof Map) ) {
5555
return null;
5656
}
5757

58-
Map identityMap = (Map) json.get(Mapping.IDENTITY);
58+
Map identityMap = (Map) json.get(Mapping.TOKEN_IDENTITY);
5959
return (String) identityMap.get("c8ySourceId");
6060
} catch (Exception e) {
6161
// Optionally log the exception

0 commit comments

Comments
 (0)