/
SyncTaskHelper.java
235 lines (219 loc) · 13.1 KB
/
SyncTaskHelper.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
/*
* Copyright (c) 2010-2019 Evolveum and contributors
*
* This work is dual-licensed under the Apache License 2.0
* and European Union Public License. See LICENSE file for details.
*/
package com.evolveum.midpoint.model.impl.sync;
import com.evolveum.midpoint.common.refinery.RefinedResourceSchema;
import com.evolveum.midpoint.common.refinery.RefinedResourceSchemaImpl;
import com.evolveum.midpoint.model.impl.util.ModelImplUtils;
import com.evolveum.midpoint.prism.PrismContext;
import com.evolveum.midpoint.provisioning.api.ProvisioningService;
import com.evolveum.midpoint.repo.api.PreconditionViolationException;
import com.evolveum.midpoint.schema.ResourceShadowDiscriminator;
import com.evolveum.midpoint.schema.processor.ObjectClassComplexTypeDefinition;
import com.evolveum.midpoint.schema.result.OperationResult;
import com.evolveum.midpoint.schema.util.ExceptionUtil;
import com.evolveum.midpoint.task.api.Task;
import com.evolveum.midpoint.task.api.TaskRunResult;
import com.evolveum.midpoint.util.exception.*;
import com.evolveum.midpoint.util.logging.Trace;
import com.evolveum.midpoint.xml.ns._public.common.common_3.CriticalityType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.LayerType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ResourceType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.TaskPartitionDefinitionType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
/**
* Auxiliary methods for synchronization tasks (Live Sync, Async Update, and maybe others).
*/
@Component
public class SyncTaskHelper {
@Autowired private ProvisioningService provisioningService;
@Autowired private PrismContext prismContext;
@SuppressWarnings("FieldCanBeLocal")
public static class TargetInfo {
final ResourceShadowDiscriminator coords;
public final ResourceType resource;
private final RefinedResourceSchema refinedResourceSchema;
private final ObjectClassComplexTypeDefinition objectClassDefinition;
private TargetInfo(ResourceShadowDiscriminator coords, ResourceType resource,
RefinedResourceSchema refinedResourceSchema, ObjectClassComplexTypeDefinition objectClassDefinition) {
this.coords = coords;
this.resource = resource;
this.refinedResourceSchema = refinedResourceSchema;
this.objectClassDefinition = objectClassDefinition;
}
}
TargetInfo getTargetInfo(Trace LOGGER, Task task, OperationResult opResult, TaskRunResult runResult, String ctx) {
String resourceOid = getResourceOid(LOGGER, task, opResult, runResult, ctx);
if (resourceOid == null) {
return null;
}
ResourceType resource = getResource(LOGGER, resourceOid, task, opResult, runResult, ctx);
if (resource == null) {
return null;
}
RefinedResourceSchema refinedSchema = getRefinedResourceSchema(LOGGER, resource, opResult, runResult, ctx);
if (refinedSchema == null) {
return null;
}
ObjectClassComplexTypeDefinition objectClass;
try {
objectClass = ModelImplUtils.determineObjectClass(refinedSchema, task);
} catch (SchemaException e) {
LOGGER.error("{}: schema error: {}", ctx, e.getMessage());
opResult.recordFatalError(e);
runResult.setRunResultStatus(TaskRunResult.TaskRunResultStatus.PERMANENT_ERROR);
return null;
}
if (objectClass == null) {
LOGGER.debug("{}: Processing all object classes", ctx);
}
return new TargetInfo(
new ResourceShadowDiscriminator(resourceOid, objectClass == null ? null : objectClass.getTypeName()),
resource, refinedSchema, objectClass);
}
public String getResourceOid(Trace logger, Task task, OperationResult opResult, TaskRunResult runResult, String ctx) {
String resourceOid = task.getObjectOid();
if (resourceOid == null) {
logger.error("{}: No resource OID specified in the task", ctx);
opResult.recordFatalError("No resource OID specified in the task");
runResult.setRunResultStatus(TaskRunResult.TaskRunResultStatus.PERMANENT_ERROR);
}
return resourceOid;
}
private ResourceType getResource(Trace logger, String resourceOid, Task task, OperationResult opResult, TaskRunResult runResult, String ctx) {
try {
return provisioningService.getObject(ResourceType.class, resourceOid, null, task, opResult).asObjectable();
} catch (ObjectNotFoundException ex) {
logger.error("{}: Resource {} not found: {}", ctx, resourceOid, ex.getMessage(), ex);
// This is bad. The resource does not exist. Permanent problem.
opResult.recordFatalError("Resource not found " + resourceOid, ex);
runResult.setRunResultStatus(TaskRunResult.TaskRunResultStatus.PERMANENT_ERROR);
return null;
} catch (SchemaException ex) {
logger.error("{}: Error dealing with schema: {}", ctx, ex.getMessage(), ex);
// Not sure about this. But most likely it is a misconfigured resource or connector
// It may be worth to retry. Error is fatal, but may not be permanent.
opResult.recordFatalError("Error dealing with schema: " + ex.getMessage(), ex);
runResult.setRunResultStatus(TaskRunResult.TaskRunResultStatus.TEMPORARY_ERROR);
return null;
} catch (RuntimeException ex) {
logger.error("{}: Internal Error: {}", ctx, ex.getMessage(), ex);
// Can be anything ... but we can't recover from that.
// It is most likely a programming error. Does not make much sense to retry.
opResult.recordFatalError("Internal Error: " + ex.getMessage(), ex);
runResult.setRunResultStatus(TaskRunResult.TaskRunResultStatus.PERMANENT_ERROR);
return null;
} catch (CommunicationException ex) {
logger.error("{}: Error getting resource {}: {}", ctx, resourceOid, ex.getMessage(), ex);
opResult.recordFatalError("Error getting resource " + resourceOid+": "+ex.getMessage(), ex);
runResult.setRunResultStatus(TaskRunResult.TaskRunResultStatus.TEMPORARY_ERROR);
return null;
} catch (ConfigurationException | SecurityViolationException | ExpressionEvaluationException ex) {
logger.error("{}: Error getting resource {}: {}", ctx, resourceOid, ex.getMessage(), ex);
opResult.recordFatalError("Error getting resource " + resourceOid+": "+ex.getMessage(), ex);
runResult.setRunResultStatus(TaskRunResult.TaskRunResultStatus.PERMANENT_ERROR);
return null;
}
}
private RefinedResourceSchema getRefinedResourceSchema(Trace LOGGER, ResourceType resource, OperationResult opResult,
TaskRunResult runResult, String ctx) {
RefinedResourceSchema refinedSchema;
try {
refinedSchema = RefinedResourceSchemaImpl.getRefinedSchema(resource, LayerType.MODEL, prismContext);
} catch (SchemaException e) {
LOGGER.error("{}: Schema error during processing account definition: {}", ctx, e.getMessage());
opResult.recordFatalError("Schema error during processing account definition: "+e.getMessage(), e);
runResult.setRunResultStatus(TaskRunResult.TaskRunResultStatus.PERMANENT_ERROR);
return null;
}
if (refinedSchema == null) {
opResult.recordFatalError("No refined schema defined. Probably some configuration problem.");
runResult.setRunResultStatus(TaskRunResult.TaskRunResultStatus.PERMANENT_ERROR);
LOGGER.error("{}: No refined schema defined. Probably some configuration problem.", ctx);
}
return refinedSchema;
}
void processException(Trace LOGGER, Throwable t, OperationResult opResult, TaskRunResult runResult,
TaskPartitionDefinitionType partition, String ctx) {
if (t instanceof ObjectNotFoundException) {
String oid = ((ObjectNotFoundException) t).getOid();
LOGGER.error("{}: A required object does not exist, OID: {}", ctx, oid, t);
// This is bad. The resource or task or something like that does not exist. Permanent problem.
opResult.recordFatalError("A required object does not exist, OID: " + oid, t);
setRunResultStatus(t, partition, CriticalityType.FATAL, runResult);
} else if (t instanceof MaintenanceException) {
opResult.recordHandledError("Maintenance exception: "+t.getMessage(), t);
runResult.setRunResultStatus(TaskRunResult.TaskRunResultStatus.TEMPORARY_ERROR); // Resource is in the maintenance, do not suspend the task
} else if (t instanceof CommunicationException) {
LOGGER.error("{}: Communication error: {}", ctx, t.getMessage(), t);
// Error, but not critical. Just try later.
opResult.recordPartialError("Communication error: "+t.getMessage(), t);
setRunResultStatus(t, partition, CriticalityType.PARTIAL, runResult);
} else if (t instanceof SchemaException) {
LOGGER.error("{}: Error dealing with schema: {}", ctx, t.getMessage(), t);
// Not sure about this. But most likely it is a misconfigured resource or connector
// It may be worth to retry. Error is fatal, but may not be permanent.
opResult.recordFatalError("Error dealing with schema: "+t.getMessage(), t);
setRunResultStatus(t, partition, CriticalityType.PARTIAL, runResult);
} else if (t instanceof PolicyViolationException) {
LOGGER.error("{}: Policy violation: {}", ctx, t.getMessage(), t);
// Not sure about this. But most likely it is a misconfigured resource or connector
// It may be worth to retry. Error is fatal, but may not be permanent.
opResult.recordFatalError("Policy violation: "+t.getMessage(), t);
setRunResultStatus(t, partition, CriticalityType.PARTIAL, runResult);
} else if (t instanceof PreconditionViolationException) {
LOGGER.error("{}: Precondition violation: {}", ctx, t.getMessage(), t);
// Not sure about this.
// It may be worth to retry. Error is fatal, but may not be permanent.
opResult.recordFatalError("Internal error: "+t.getMessage(), t);
setRunResultStatus(t, partition, CriticalityType.PARTIAL, runResult);
} else if (t instanceof ConfigurationException) {
LOGGER.error("{}: Configuration error: {}", ctx, t.getMessage(), t);
// Not sure about this. But most likely it is a misconfigured resource or connector
// It may be worth to retry. Error is fatal, but may not be permanent.
opResult.recordFatalError("Configuration error: "+t.getMessage(), t);
setRunResultStatus(t, partition, CriticalityType.PARTIAL, runResult);
} else if (t instanceof SecurityViolationException) {
LOGGER.error("{}: Security violation: {}", ctx, t.getMessage(), t);
opResult.recordFatalError("Security violation: "+t.getMessage(), t);
setRunResultStatus(t, partition, CriticalityType.FATAL, runResult);
} else if (t instanceof ExpressionEvaluationException) {
LOGGER.error("{}: Expression error: {}", ctx, t.getMessage(), t);
opResult.recordFatalError("Expression error: "+t.getMessage(), t);
setRunResultStatus(t, partition, CriticalityType.FATAL, runResult);
} else if (t instanceof SystemException) {
LOGGER.error("{}: Unspecified error: {}", ctx, t.getMessage(), t);
opResult.recordFatalError("Unspecified error: "+t.getMessage(), t);
setRunResultStatus(t, partition, CriticalityType.FATAL, runResult);
} else {
LOGGER.error("{}: Internal Error: {}", ctx, t.getMessage(), t);
// Can be anything ... but we can't recover from that.
// It is most likely a programming error. Does not make much sense to retry.
opResult.recordFatalError("Internal Error: "+t.getMessage(), t);
setRunResultStatus(t, partition, CriticalityType.FATAL, runResult);
}
}
private void setRunResultStatus(Throwable ex, TaskPartitionDefinitionType partition, CriticalityType defaultCriticality, TaskRunResult runResult) {
CriticalityType criticality;
if (partition == null) {
criticality = defaultCriticality;
} else {
criticality = ExceptionUtil.getCriticality(partition.getErrorCriticality(), ex, defaultCriticality);
}
switch (criticality) {
case PARTIAL:
//noinspection DuplicateBranchesInSwitch
runResult.setRunResultStatus(TaskRunResult.TaskRunResultStatus.TEMPORARY_ERROR);
break;
case FATAL:
runResult.setRunResultStatus(TaskRunResult.TaskRunResultStatus.PERMANENT_ERROR);
break;
default:
runResult.setRunResultStatus(TaskRunResult.TaskRunResultStatus.TEMPORARY_ERROR);
}
}
}