Skip to content

Commit

Permalink
Clean up notification processing a bit
Browse files Browse the repository at this point in the history
Minor refactorings, cleanups and fixes were done.

First parts of a test of messaging-based resource with attribute
caching is passing.
  • Loading branch information
mederly committed Feb 6, 2019
1 parent 9fc92ca commit 86fe098
Show file tree
Hide file tree
Showing 18 changed files with 639 additions and 215 deletions.
Expand Up @@ -375,14 +375,14 @@ public static <O extends ObjectType> void reduceSearchResult(List<PrismObject<O>
if (results == null || results.isEmpty()) {
return;
}
Map<String,PrismObject<O>> map = new HashMap<>();
Set<String> oidsSeen = new HashSet<>();
Iterator<PrismObject<O>> iterator = results.iterator();
while (iterator.hasNext()) {
PrismObject<O> prismObject = iterator.next();
if (map.containsKey(prismObject.getOid())) {
if (oidsSeen.contains(prismObject.getOid())) {
iterator.remove();
} else {
map.put(prismObject.getOid(), prismObject);
oidsSeen.add(prismObject.getOid());
}
}
}
Expand Down
Expand Up @@ -502,11 +502,19 @@ public static boolean isDead(ShadowType shadow) {
Boolean dead = shadow.isDead();
return dead != null && dead;
}


public static boolean isDead(PrismObject<ShadowType> shadow) {
return isDead(shadow.asObjectable());
}

public static boolean isExists(ShadowType shadow) {
Boolean exists = shadow.isExists();
return exists == null || exists;
}

public static boolean isExists(PrismObject<ShadowType> shadow) {
return isExists(shadow.asObjectable());
}

public static boolean matches(ShadowType shadowType, String resourceOid, ShadowKindType kind, String intent) {
if (shadowType == null) {
Expand Down
Expand Up @@ -15,8 +15,8 @@
*/
package com.evolveum.midpoint.model.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

import javax.xml.namespace.QName;
Expand Down Expand Up @@ -128,46 +128,42 @@ public void notifyChange(ResourceObjectShadowChangeDescriptionType changeDescrip

PrismObject<ShadowType> oldShadow;
LOGGER.trace("resolving old object");
if (!StringUtils.isEmpty(oldShadowOid)){
if (!StringUtils.isEmpty(oldShadowOid)) {
oldShadow = getObject(ShadowType.class, oldShadowOid, SelectorOptions.createCollection(GetOperationOptions.createDoNotDiscovery()), task, parentResult);
eventDescription.setOldShadow(oldShadow);
LOGGER.trace("old object resolved to: {}", oldShadow.debugDump());
} else{
LOGGER.trace("old object resolved to: {}", oldShadow.debugDumpLazily());
} else {
LOGGER.trace("Old shadow null");
}

PrismObject<ShadowType> currentShadow = null;
ShadowType currentShadowType = changeDescription.getCurrentShadow();
LOGGER.trace("resolving current shadow");
if (currentShadowType != null){
if (currentShadowType != null) {
prismContext.adopt(currentShadowType);
currentShadow = currentShadowType.asPrismObject();
LOGGER.trace("current shadow resolved to {}", currentShadow.debugDump());
LOGGER.trace("current shadow resolved to {}", currentShadow.debugDumpLazily());
}

eventDescription.setCurrentShadow(currentShadow);

ObjectDeltaType deltaType = changeDescription.getObjectDelta();
ObjectDelta delta = null;

PrismObject<ShadowType> shadowToAdd;
if (deltaType != null){
if (deltaType != null) {

delta = prismContext.deltaFactory().object().createEmptyDelta(ShadowType.class, deltaType.getOid(),
PrismObject<ShadowType> shadowToAdd;
ObjectDelta<ShadowType> delta = prismContext.deltaFactory().object().createEmptyDelta(ShadowType.class, deltaType.getOid(),
ChangeType.toChangeType(deltaType.getChangeType()));

if (delta.getChangeType() == ChangeType.ADD) {
// LOGGER.trace("determined ADD change ");
if (deltaType.getObjectToAdd() == null){
LOGGER.trace("No object to add specified. Check your delta. Add delta must contain object to add");
throw new IllegalArgumentException("No object to add specified. Check your delta. Add delta must contain object to add");
// return handleTaskResult(task);
}
Object objToAdd = deltaType.getObjectToAdd();
if (!(objToAdd instanceof ShadowType)){
LOGGER.trace("Wrong object specified in change description. Expected on the the shadow type, but got " + objToAdd.getClass().getSimpleName());
throw new IllegalArgumentException("Wrong object specified in change description. Expected on the the shadow type, but got " + objToAdd.getClass().getSimpleName());
// return handleTaskResult(task);
}
prismContext.adopt((ShadowType)objToAdd);

Expand All @@ -176,13 +172,11 @@ public void notifyChange(ResourceObjectShadowChangeDescriptionType changeDescrip
delta.setObjectToAdd(shadowToAdd);
} else {
Collection<? extends ItemDelta> modifications = DeltaConvertor.toModifications(deltaType.getItemDelta(), prismContext.getSchemaRegistry().findObjectDefinitionByCompileTimeClass(ShadowType.class));
delta.getModifications().addAll(modifications);
delta.addModifications(modifications);
}
ModelImplUtils.encrypt(Collections.singletonList(delta), protector, null, parentResult);
eventDescription.setDelta(delta);
}
Collection<ObjectDelta<? extends ObjectType>> deltas = new ArrayList<>();
deltas.add(delta);
ModelImplUtils.encrypt(deltas, protector, null, parentResult);
eventDescription.setDelta(delta);

eventDescription.setSourceChannel(changeDescription.getChannel());

Expand Down
Expand Up @@ -50,6 +50,7 @@
import com.evolveum.midpoint.prism.delta.*;
import com.evolveum.midpoint.prism.equivalence.EquivalenceStrategy;
import com.evolveum.midpoint.prism.path.*;
import com.evolveum.midpoint.provisioning.api.ChangeNotificationDispatcher;
import com.evolveum.midpoint.schema.*;
import com.evolveum.midpoint.schema.result.OperationResultStatus;
import com.evolveum.midpoint.task.api.TaskDebugUtil;
Expand Down Expand Up @@ -288,7 +289,7 @@ public abstract class AbstractModelIntegrationTest extends AbstractIntegrationTe
@Autowired protected SecurityContextManager securityContextManager;
@Autowired protected MidpointFunctions libraryMidpointFunctions;
@Autowired protected ValuePolicyProcessor valuePolicyProcessor;

@Autowired(required = false)
@Qualifier("modelObjectResolver")
protected ObjectResolver modelObjectResolver;
Expand Down Expand Up @@ -5554,7 +5555,16 @@ protected OrgAsserter<Void> assertOrgAfter(String oid) throws ObjectNotFoundExce
asserter.assertOid(oid);
return asserter;
}


protected OrgAsserter<Void> assertOrgByName(String name, String message) throws ObjectNotFoundException, SchemaException, SecurityViolationException, CommunicationException, ConfigurationException, ExpressionEvaluationException {
PrismObject<OrgType> org = findObjectByName(OrgType.class, name);
assertNotNull("No org with name '"+name+"'", org);
OrgAsserter<Void> asserter = OrgAsserter.forOrg(org, message);
initializeAsserter(asserter);
asserter.assertName(name);
return asserter;
}

protected RoleAsserter<Void> assertRole(String oid, String message) throws ObjectNotFoundException, SchemaException, SecurityViolationException, CommunicationException, ConfigurationException, ExpressionEvaluationException {
PrismObject<RoleType> role = getObject(RoleType.class, oid);
RoleAsserter<Void> asserter = assertRole(role, message);
Expand Down
Expand Up @@ -13,7 +13,7 @@ public class ResourceEventDescription implements Serializable, DebugDumpable{

private PrismObject<ShadowType> oldShadow;
private PrismObject<ShadowType> currentShadow;
private ObjectDelta delta;
private ObjectDelta<ShadowType> delta;
private String sourceChannel;
// private PrismObject<ResourceType> resource;

Expand All @@ -26,7 +26,7 @@ public PrismObject<ShadowType> getOldShadow() {
return oldShadow;
}

public ObjectDelta getDelta() {
public ObjectDelta<ShadowType> getDelta() {
return delta;
}

Expand Down
Expand Up @@ -27,6 +27,8 @@

public interface ResourceEventListener extends ProvisioningListener {

public void notifyEvent(ResourceEventDescription eventDescription, Task task, OperationResult parentResult) throws SchemaException, CommunicationException, ConfigurationException, SecurityViolationException, ObjectNotFoundException, GenericConnectorException, ObjectAlreadyExistsException, ExpressionEvaluationException;
void notifyEvent(ResourceEventDescription eventDescription, Task task, OperationResult parentResult) throws SchemaException,
CommunicationException, ConfigurationException, SecurityViolationException, ObjectNotFoundException,
GenericConnectorException, ObjectAlreadyExistsException, ExpressionEvaluationException;

}
Expand Up @@ -31,10 +31,7 @@
import com.evolveum.midpoint.util.exception.*;
import com.evolveum.midpoint.util.logging.Trace;
import com.evolveum.midpoint.util.logging.TraceManager;
import com.evolveum.midpoint.xml.ns._public.common.common_3.CapabilitiesType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ResourceType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ShadowKindType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ShadowType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.*;
import com.evolveum.midpoint.xml.ns._public.resource.capabilities_3.CapabilityType;

import org.jetbrains.annotations.NotNull;
Expand Down Expand Up @@ -338,4 +335,10 @@ public PrismContext getPrismContext() {
public ItemPath path(Object... components) {
return ItemPath.create(components);
}

public CachingStategyType getCachingStrategy()
throws ObjectNotFoundException, SchemaException, CommunicationException, ConfigurationException,
ExpressionEvaluationException {
return ProvisioningUtil.getCachingStrategy(this);
}
}
Expand Up @@ -21,6 +21,7 @@
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

import com.evolveum.midpoint.xml.ns._public.common.common_3.CachingStategyType;
import org.apache.commons.lang.Validate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
Expand All @@ -33,7 +34,6 @@
import com.evolveum.midpoint.provisioning.api.ResourceEventDescription;
import com.evolveum.midpoint.provisioning.api.ResourceEventListener;
import com.evolveum.midpoint.provisioning.ucf.api.Change;
import com.evolveum.midpoint.schema.processor.ObjectClassComplexTypeDefinition;
import com.evolveum.midpoint.schema.processor.ResourceAttribute;
import com.evolveum.midpoint.schema.result.OperationResult;
import com.evolveum.midpoint.schema.util.ShadowUtil;
Expand Down Expand Up @@ -77,44 +77,50 @@ public String getName() {
}

@Override
public void notifyEvent(ResourceEventDescription eventDescription, Task task, OperationResult parentResult) throws SchemaException, CommunicationException, ConfigurationException, SecurityViolationException, ObjectNotFoundException, GenericConnectorException, ObjectAlreadyExistsException, ExpressionEvaluationException {
public void notifyEvent(ResourceEventDescription eventDescription, Task task, OperationResult parentResult)
throws SchemaException, CommunicationException, ConfigurationException, SecurityViolationException,
ObjectNotFoundException, GenericConnectorException, ObjectAlreadyExistsException, ExpressionEvaluationException {

Validate.notNull(eventDescription, "Event description must not be null.");
Validate.notNull(task, "Task must not be null.");
Validate.notNull(parentResult, "Operation result must not be null");

LOGGER.trace("Received event notification with the description: {}", eventDescription.debugDump());
LOGGER.trace("Received event notification with the description: {}", eventDescription.debugDumpLazily());

if (eventDescription.getCurrentShadow() == null && eventDescription.getDelta() == null){
if (eventDescription.getCurrentShadow() == null && eventDescription.getDelta() == null) {
throw new IllegalStateException("Neither current shadow, nor delta specified. It is required to have at least one of them specified.");
}

applyDefinitions(eventDescription, parentResult);

PrismObject<ShadowType> shadow = null;

shadow = eventDescription.getShadow();

PrismObject<ShadowType> shadow = eventDescription.getShadow();
ProvisioningContext ctx = provisioningContextFactory.create(shadow, task, parentResult);
ctx.assertDefinition();

Collection<ResourceAttribute<?>> identifiers = ShadowUtil.getPrimaryIdentifiers(shadow);

// TODO reconsider this
if (ctx.getCachingStrategy() == CachingStategyType.PASSIVE) {
if (eventDescription.getCurrentShadow() == null && eventDescription.getOldShadow() != null && eventDescription.getDelta() != null) {
PrismObject<ShadowType> newShadow = eventDescription.getOldShadow().clone();
eventDescription.getDelta().applyTo(newShadow);
eventDescription.setCurrentShadow(newShadow);
}
}

Change change = new Change(identifiers, eventDescription.getCurrentShadow(), eventDescription.getOldShadow(), eventDescription.getDelta());
ObjectClassComplexTypeDefinition objectClassDefinition = ShadowUtil.getObjectClassDefinition(shadow);
change.setObjectClassDefinition(objectClassDefinition);
change.setObjectClassDefinition(ShadowUtil.getObjectClassDefinition(shadow));

LOGGER.trace("Start to precess change: {}", change.toString());
LOGGER.trace("Starting to process change: {}", change);
try {
shadowCache.processChange(ctx, change, null, parentResult);
shadowCache.preProcessChange(ctx, change, eventDescription.getOldShadow(), parentResult);
} catch (EncryptionException e) {
// TODO: better handling
throw new SystemException(e.getMessage(), e);
}

LOGGER.trace("Change after processing {} . Start synchronizing.", change.toString());
LOGGER.trace("Processed change {}. Starting synchronizing.", change);
shadowCache.processSynchronization(ctx, change, parentResult);

}

private void applyDefinitions(ResourceEventDescription eventDescription,
Expand Down

0 comments on commit 86fe098

Please sign in to comment.