-
Notifications
You must be signed in to change notification settings - Fork 188
/
OpType.java
296 lines (266 loc) · 14.7 KB
/
OpType.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
/*
* Copyright (c) 2020 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.schema.traces;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Function;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import com.evolveum.midpoint.util.annotation.Experimental;
import com.evolveum.midpoint.xml.ns._public.common.common_3.*;
import org.apache.commons.lang3.StringUtils;
import com.evolveum.midpoint.prism.PrismObject;
import com.evolveum.midpoint.prism.path.ItemPath;
import com.evolveum.midpoint.prism.polystring.PolyString;
import com.evolveum.prism.xml.ns._public.types_3.ItemPathType;
import static com.evolveum.midpoint.schema.traces.TraceUtil.*;
@Experimental
public enum OpType {
CLOCKWORK_RUN(OperationKindType.CLOCKWORK_EXECUTION, "Clockwork run", "com.evolveum.midpoint.model.impl.lens.Clockwork.run"),
CLOCKWORK_CLICK(OperationKindType.CLOCKWORK_CLICK, "Clockwork click", "com.evolveum.midpoint.model.impl.lens.Clockwork.click"),
PROJECTOR_PROJECT(OperationKindType.PROJECTOR_EXECUTION, "Projector project", "com.evolveum.midpoint.model.impl.lens.projector.Projector.project"),
PROJECTOR_COMPONENT(OperationKindType.OTHER,"Projector component",
"com.evolveum.midpoint.model.impl.lens.projector.Projector.*"),
CLOCKWORK_METHOD(OperationKindType.OTHER,"Clockwork method",
"com.evolveum.midpoint.model.impl.lens.Clockwork.*"),
MAPPING_EVALUATION(OperationKindType.MAPPING_EVALUATION,"Mapping evaluation",
"com.evolveum.midpoint.model.common.mapping.MappingImpl.evaluate"),
MAPPING_PREPARATION(OperationKindType.OTHER,"Mapping preparation",
"com.evolveum.midpoint.model.common.mapping.MappingImpl.prepare"),
MAPPING_EVALUATION_PREPARED (OperationKindType.OTHER,"Prepared mapping evaluation",
"com.evolveum.midpoint.model.common.mapping.MappingImpl.evaluatePrepared"),
SCRIPT_EXECUTION (OperationKindType.SCRIPT_EVALUATION, "Script evaluation",
"xxxx"),
CHANGE_EXECUTION (OperationKindType.OTHER,"Change execution",
"com.evolveum.midpoint.model.impl.lens.ChangeExecutor.execute"),
FOCUS_CHANGE_EXECUTION (OperationKindType.FOCUS_CHANGE_EXECUTION,"Focus change execution",
"com.evolveum.midpoint.model.impl.lens.ChangeExecutor.execute.focus.*"),
PROJECTION_CHANGE_EXECUTION (OperationKindType.PROJECTION_CHANGE_EXECUTION,"Projection change execution",
"com.evolveum.midpoint.model.impl.lens.ChangeExecutor.execute.projection.*"),
CHANGE_EXECUTION_DELTA (OperationKindType.OTHER,"Change execution - delta",
"com.evolveum.midpoint.model.impl.lens.ChangeExecutor.executeDelta"),
CHANGE_EXECUTION_OTHER (OperationKindType.OTHER,"Change execution - other",
"com.evolveum.midpoint.model.impl.lens.ChangeExecutor.*"),
FOCUS_LOAD (OperationKindType.FOCUS_LOAD,"Focus load",
result -> isLoadedFromRepository(result),
"com.evolveum.midpoint.model.impl.lens.projector.ContextLoader.determineFocusContext"),
FOCUS_LOAD_CHECK (OperationKindType.FOCUS_LOAD_CHECK,"Focus load check",
"com.evolveum.midpoint.model.impl.lens.projector.ContextLoader.determineFocusContext"),
SHADOW_LOAD (OperationKindType.OTHER,"Shadow load",
"com.evolveum.midpoint.model.impl.lens.projector.ContextLoader.loadProjection"),
FULL_PROJECTION_LOAD (OperationKindType.FULL_PROJECTION_LOAD, "Full projection load",
"com.evolveum.midpoint.model.impl.lens.projector.ContextLoader.loadFullShadow"),
MODEL_OTHER (OperationKindType.OTHER,"Model - other",
"com.evolveum.midpoint.model.*"),
PROVISIONING_API (OperationKindType.PROVISIONING,"Provisioning (API)", "com.evolveum.midpoint.provisioning.api.*"),
PROVISIONING_INTERNAL (OperationKindType.OTHER,"Provisioning (internal)", "com.evolveum.midpoint.provisioning.impl.*"),
REPOSITORY (OperationKindType.REPOSITORY, "Repository", "com.evolveum.midpoint.repo.api.RepositoryService.*"),
REPOSITORY_CACHE (OperationKindType.REPOSITORY_CACHE, "Repository cache", "com.evolveum.midpoint.repo.cache.RepositoryCache.*"),
CONNECTOR (OperationKindType.CONNECTOR, "Connector", "com.evolveum.midpoint.provisioning.ucf.api.ConnectorInstance.*"),
OTHER (OperationKindType.OTHER, "Other",
"*");
private static final String LOADED_FROM_REPOSITORY = "Loaded from repository"; // TODO
private final OperationKindType kind;
private final String label;
private final Function<OperationResultType, Boolean> predicate;
@SuppressWarnings("unused")
private final List<String> patterns;
private final List<Pattern> compiledPatterns;
OpType(OperationKindType kind, String label, Function<OperationResultType, Boolean> predicate, String... patterns) {
this.kind = kind;
this.label = label;
this.predicate = predicate;
this.patterns = Arrays.asList(patterns);
this.compiledPatterns = new ArrayList<>();
for (String pattern : patterns) {
String regex = toRegex(pattern);
compiledPatterns.add(Pattern.compile(regex));
}
}
private static boolean isLoadedFromRepository(OperationResultType result) {
return LOADED_FROM_REPOSITORY.equals(OpNode.getResultComment(result));
}
OpType(OperationKindType kind, String label, String... patterns) {
this(kind, label, null, patterns);
}
public String getFormattedName(OpNode node) {
OperationResultType opResult = node.getResult();
String operation = opResult.getOperation();
String last = getLast(operation);
String qualifiers = String.join("; ", opResult.getQualifier());
String commaQualifiers = qualifiers.isEmpty() ? "" : " - " + qualifiers;
if ("com.evolveum.midpoint.model.impl.lens.projector.ConsolidationProcessor.consolidateItem".equals(operation)) {
return "Consolidating " + getParameter(opResult, "itemPath");
} else if ("com.evolveum.midpoint.model.common.expression.evaluator.AbstractValueTransformationExpressionEvaluator.processValuesCombination".equals(operation)) {
return "Processing value combination";
} else if ("com.evolveum.midpoint.model.common.expression.evaluator.AbstractValueTransformationExpressionEvaluator.evaluateScriptExpression".equals(operation) ||
"com.evolveum.midpoint.model.common.expression.evaluator.AbstractValueTransformationExpressionEvaluator.evaluateExpression".equals(operation)) {
return "Evaluate: " + getContext(opResult, "context");
} else if ("com.evolveum.midpoint.model.common.expression.script.ScriptExpression.evaluate".equals(operation)) {
return "Script: " + getContext(opResult, "context");
} else if ("com.evolveum.midpoint.model.impl.lens.AssignmentEvaluator.evaluateFromSegment".equals(operation)) {
String srcName = getContext(opResult, "segmentSourceName");
String tgtName = getContext(opResult, "segmentTargetName");
return "Segment: " + (srcName != null ? srcName + " " : "") + "->" + (tgtName != null ? " " + tgtName : "");
} else if ("com.evolveum.midpoint.model.impl.lens.AssignmentEvaluator.evaluate".equals(operation)) {
String tgtName = getContext(opResult, "assignmentTargetName");
return "AssignmentEvaluator.evaluate" + (tgtName != null ? " (-> " + tgtName + ")" : "");
} else if ("com.evolveum.midpoint.model.impl.lens.projector.focus.AssignmentTripleEvaluator.evaluateAssignment".equals(operation)) {
String tgtName = getContext(opResult, "assignmentTargetName");
return "AssignmentTripleEvaluator.evaluateAssignment" + (tgtName != null ? " (-> " + tgtName + ")" : "");
} else if ("com.evolveum.midpoint.model.impl.lens.projector.policy.PolicyRuleProcessor.evaluateRule".equals(operation)) {
String triggeredString = getReturn(opResult, "triggered");
int enabledActions = TraceUtil.getReturnsAsStringList(opResult, "enabledActions").size();
String triggeredSuffix = "true".equals(triggeredString) ? " # (" + enabledActions + ")" : "";
return "Evaluate rule: " + getParameter(opResult, "policyRule") + triggeredSuffix;
}
switch (this) {
case CLOCKWORK_RUN: return "Clockwork run";
case CLOCKWORK_CLICK: return "Clockwork click";
case PROJECTOR_PROJECT: return "Projector";
case PROJECTOR_COMPONENT:
if ("projection".equals(last)) {
return "Projector projection: " + getParameter(opResult, "resourceName");
} else {
return "Projector " + last;
}
case CLOCKWORK_METHOD: return "Clockwork " + last;
case MAPPING_EVALUATION: return "Mapping: " + getMappingInfo(opResult);
case MAPPING_PREPARATION: return "Mapping preparation";
case MAPPING_EVALUATION_PREPARED: return "Prepared mapping evaluation";
// TODO script
case CHANGE_EXECUTION: return "Change execution";
case FOCUS_CHANGE_EXECUTION: return "Change execution for focus (" + last + ")";
case PROJECTION_CHANGE_EXECUTION: return "Change execution for focus (" + last + ")";
case CHANGE_EXECUTION_DELTA: return "Delta execution";
case CHANGE_EXECUTION_OTHER: return "Change execution - " + last;
case REPOSITORY: return "Repository " + last + commaQualifiers;
case REPOSITORY_CACHE:
return getRepoCacheOpDescription(node, opResult, last, commaQualifiers);
case PROVISIONING_API: return "Provisioning " + last + commaQualifiers;
case PROVISIONING_INTERNAL: return getLastTwo(operation) + commaQualifiers;
case FOCUS_LOAD: return "Focus load";
case FOCUS_LOAD_CHECK: return "Focus load check (" + node.getResultComment() + ")";
case SHADOW_LOAD: return "Shadow load";
case MODEL_OTHER:
case OTHER:
return getLastTwo(operation) + commaQualifiers;
}
return opResult.getOperation() + (qualifiers.isEmpty() ? "" : " (" + qualifiers + ")");
}
private String getRepoCacheOpDescription(OpNode node, OperationResultType opResult, String last, String commaQualifiers) {
String postfix = "";
if ("getObject".equals(last)) {
postfix = " - " + getContext(opResult, "objectName");
} else if ("searchObjects".equals(last) || "searchObjectsIterative".equals(last)) {
String objectsFound = getReturn(opResult, "objectsFound");
if (objectsFound != null) {
postfix = " - " + objectsFound + " obj(s)";
}
RepositorySearchObjectsTraceType trace = getTrace(opResult, RepositorySearchObjectsTraceType.class);
if (trace != null && trace.getObjectRef().size() == 1) {
String name = getName(trace.getObjectRef().get(0));
if (name != null) {
postfix += " - " + name;
}
}
} else if ("addObject".equals(last)) {
RepositoryAddTraceType add = getTrace(opResult, RepositoryAddTraceType.class);
String name = add != null ? getName(add.getObjectRef()) : null;
if (name != null) {
postfix = " - " + name;
}
} else if ("modifyObject".equals(last)) {
RepositoryModifyTraceType trace = getTrace(opResult, RepositoryModifyTraceType.class);
if (trace != null) {
postfix += " - " + trace.getModification().size() + " mod(s)";
PrismObject<?> object = node.getTraceInfo().findObject(trace.getOid());
if (object != null) {
postfix += " - " + PolyString.getOrig(object.getName());
}
}
}
return "Cache " + last + commaQualifiers + postfix;
}
private String getName(ObjectReferenceType ref) {
if (ref != null && ref.getObject() != null) {
return PolyString.getOrig(ref.getObject().getName());
} else {
return null;
}
}
private String getMappingInfo(OperationResultType result) {
MappingEvaluationTraceType trace = getTrace(result, MappingEvaluationTraceType.class);
String context = getContext(result, "context");
if (trace != null) {
AbstractMappingType mapping = trace.getMapping();
if (mapping != null) {
if (mapping.getName() != null) {
return mapping.getName();
} else {
String sources = mapping.getSource().stream()
.map(source -> stringifyPath(source.getPath()))
.collect(Collectors.joining(", "));
if (mapping.getTarget() != null && mapping.getTarget().getPath() != null) {
String sourcesPlusSpace = sources.isEmpty() ? "" : sources + " ";
return sourcesPlusSpace + "-> " + stringifyPath(mapping.getTarget().getPath());
} else {
return context + (sources.isEmpty() ? "" : " <- " + sources);
}
}
}
}
return context;
}
private String stringifyPath(ItemPathType pathBean) {
if (pathBean != null) {
ItemPath path = pathBean.getItemPath();
return path.stripVariableSegment().toString();
} else {
return "(no path)";
}
}
private String getLastTwo(String operation) {
int i = StringUtils.lastOrdinalIndexOf(operation, ".", 2);
if (i < 0) {
return operation;
} else {
return operation.substring(i+1);
}
}
private String getLast(String operation) {
return StringUtils.substringAfterLast(operation, ".");
}
private String toRegex(String pattern) {
return pattern.replace(".", "\\.").replace("*", ".*");
}
public static OpType determine(OperationResultType operation) {
for (OpType type : OpType.values()) {
if (type.matches(operation)) {
return type;
}
}
return null;
}
private boolean matches(OperationResultType operation) {
for (Pattern pattern : compiledPatterns) {
if (pattern.matcher(operation.getOperation()).matches()) {
if (predicate == null || predicate.apply(operation)) {
return true;
}
}
}
return false;
}
public String getLabel() {
return label;
}
public OperationKindType getKind() {
return kind;
}
}