-
Notifications
You must be signed in to change notification settings - Fork 188
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MID-8842 ninja, upgrade objects action impl started, work in progress
- Loading branch information
Showing
9 changed files
with
197 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 79 additions & 22 deletions
101
.../ninja/src/main/java/com/evolveum/midpoint/ninja/action/upgrade/UpgradeObjectsAction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,103 @@ | ||
package com.evolveum.midpoint.ninja.action.upgrade; | ||
|
||
import java.lang.reflect.Modifier; | ||
import java.util.Set; | ||
import java.io.File; | ||
import java.io.FileReader; | ||
import java.io.IOException; | ||
import java.util.*; | ||
import java.util.concurrent.BlockingQueue; | ||
import java.util.stream.Collectors; | ||
|
||
import com.evolveum.midpoint.ninja.Main; | ||
import org.apache.commons.csv.CSVFormat; | ||
import org.apache.commons.csv.CSVParser; | ||
import org.apache.commons.csv.CSVRecord; | ||
import org.apache.commons.lang3.StringUtils; | ||
|
||
import com.evolveum.midpoint.ninja.action.AbstractRepositorySearchAction; | ||
import com.evolveum.midpoint.ninja.action.verify.VerificationReporter; | ||
import com.evolveum.midpoint.ninja.util.OperationStatus; | ||
import com.evolveum.midpoint.util.ClassPathUtil; | ||
import com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectType; | ||
|
||
public abstract class UpgradeObjectsAction extends AbstractRepositorySearchAction<UpgradeObjectsOptions> { | ||
public class UpgradeObjectsAction extends AbstractRepositorySearchAction<UpgradeObjectsOptions> { | ||
|
||
private Set<UUID> skipUpgradeForUUIDs; | ||
|
||
@Override | ||
public Void execute() throws Exception { | ||
// final VerifyResult verifyResult = context.getResult(VerifyResult.class); | ||
// | ||
// final File output = verifyResult.getOutput(); | ||
skipUpgradeForUUIDs = loadVerificationFile(); | ||
|
||
// todo load CSV, only OIDs + state (whether to update) | ||
// go through all oids that need to be updated | ||
// if csv not available go through all | ||
log.info("Upgrade will skip {} objects", skipUpgradeForUUIDs.size()); | ||
|
||
Set<Class<?>> classes = ClassPathUtil.listClasses(Main.class.getPackageName()); | ||
Set<Class<?>> processors = classes.stream() | ||
.filter(UpgradeObjectProcessor.class::isAssignableFrom) | ||
.filter(c -> !Modifier.isAbstract(c.getModifiers())) | ||
.collect(Collectors.toUnmodifiableSet()); | ||
|
||
context.out.println("Found " + processors.size() + " upgrade rules"); | ||
|
||
return null; | ||
return super.execute(); | ||
// | ||
// | ||
//// final VerifyResult verifyResult = context.getResult(VerifyResult.class); | ||
//// | ||
//// final File output = verifyResult.getOutput(); | ||
// | ||
// // todo load CSV, only OIDs + state (whether to update) | ||
// // go through all oids that need to be updated | ||
// // if csv not available go through all | ||
// | ||
// Set<Class<?>> classes = ClassPathUtil.listClasses(Main.class.getPackageName()); | ||
// Set<Class<?>> processors = classes.stream() | ||
// .filter(UpgradeObjectProcessor.class::isAssignableFrom) | ||
// .filter(c -> !Modifier.isAbstract(c.getModifiers())) | ||
// .collect(Collectors.toUnmodifiableSet()); | ||
// | ||
// context.out.println("Found " + processors.size() + " upgrade rules"); | ||
// | ||
// return null; | ||
} | ||
|
||
@Override | ||
protected String getOperationShortName() { | ||
return "upgrade"; | ||
} | ||
|
||
private Set<UUID> loadVerificationFile() throws IOException { | ||
File verification = options.getVerification(); | ||
if (verification == null || !verification.exists() || !verification.isFile()) { | ||
return Collections.emptySet(); | ||
} | ||
|
||
log.info("Loading verification file"); | ||
|
||
Set<UUID> set = new HashSet<>(); | ||
|
||
CSVFormat format = VerificationReporter.CSV_FORMAT; | ||
try (CSVParser parser = format.parse(new FileReader(verification, context.getCharset()))) { | ||
Iterator<CSVRecord> iterator = parser.iterator(); | ||
while (iterator.hasNext()) { | ||
CSVRecord record = iterator.next(); | ||
if (record.getRecordNumber() == 1 || isRecordEmpty(record)) { | ||
// csv header or empty record | ||
continue; | ||
} | ||
|
||
if (VerificationReporter.skipUpgradeForRecord(record)) { | ||
UUID uuid = VerificationReporter.getUuidFromRecord(record); | ||
if (uuid != null) { | ||
set.add(uuid); | ||
} | ||
} | ||
} | ||
} | ||
|
||
return Collections.unmodifiableSet(set); | ||
} | ||
|
||
private boolean isRecordEmpty(CSVRecord record) { | ||
for (int i = 0; i < record.size(); i++) { | ||
String value = record.get(i); | ||
if (StringUtils.isNotBlank(value)) { | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
|
||
@Override | ||
protected Runnable createConsumer(BlockingQueue<ObjectType> queue, OperationStatus operation) { | ||
return new UpgradeObjectsConsumerWorker(context, options, queue, operation); | ||
return new UpgradeObjectsConsumerWorker(skipUpgradeForUUIDs, context, options, queue, operation); | ||
} | ||
} |
71 changes: 68 additions & 3 deletions
71
...rc/main/java/com/evolveum/midpoint/ninja/action/upgrade/UpgradeObjectsConsumerWorker.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,88 @@ | ||
package com.evolveum.midpoint.ninja.action.upgrade; | ||
|
||
import java.util.Set; | ||
import java.util.UUID; | ||
import java.util.concurrent.BlockingQueue; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import com.evolveum.midpoint.ninja.action.worker.BaseWorker; | ||
import com.evolveum.midpoint.ninja.impl.Log; | ||
import com.evolveum.midpoint.ninja.impl.NinjaContext; | ||
import com.evolveum.midpoint.ninja.util.OperationStatus; | ||
import com.evolveum.midpoint.prism.PrismObject; | ||
import com.evolveum.midpoint.prism.delta.ObjectDelta; | ||
import com.evolveum.midpoint.repo.api.RepoModifyOptions; | ||
import com.evolveum.midpoint.repo.api.RepositoryService; | ||
import com.evolveum.midpoint.schema.result.OperationResult; | ||
import com.evolveum.midpoint.util.exception.ObjectAlreadyExistsException; | ||
import com.evolveum.midpoint.util.exception.ObjectNotFoundException; | ||
import com.evolveum.midpoint.util.exception.SchemaException; | ||
import com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectType; | ||
|
||
public class UpgradeObjectsConsumerWorker extends BaseWorker<UpgradeObjectsOptions, ObjectType> { | ||
public class UpgradeObjectsConsumerWorker<T extends ObjectType> extends BaseWorker<UpgradeObjectsOptions, T> { | ||
|
||
private final Set<UUID> skipUpgradeForUUIDs; | ||
|
||
private final Log log; | ||
|
||
public UpgradeObjectsConsumerWorker( | ||
NinjaContext context, UpgradeObjectsOptions options, | ||
BlockingQueue<ObjectType> queue, OperationStatus operation) { | ||
Set<UUID> skipUpgradeForUUIDs, NinjaContext context, UpgradeObjectsOptions options, | ||
BlockingQueue<T> queue, OperationStatus operation) { | ||
|
||
super(context, options, queue, operation); | ||
|
||
this.skipUpgradeForUUIDs = skipUpgradeForUUIDs; | ||
this.log = context.getLog(); | ||
} | ||
|
||
@Override | ||
public void run() { | ||
RepositoryService repository = context.getRepository(); | ||
// todo implement | ||
|
||
try { | ||
while (!shouldConsumerStop()) { | ||
T object = null; | ||
try { | ||
object = queue.poll(CONSUMER_POLL_TIMEOUT, TimeUnit.SECONDS); | ||
if (object == null) { | ||
continue; | ||
} | ||
|
||
processObject(repository, object); | ||
|
||
operation.incrementTotal(); | ||
} catch (Exception ex) { | ||
log.error("Couldn't store object {}, reason: {}", ex, object, ex.getMessage()); | ||
operation.incrementError(); | ||
} | ||
} | ||
} catch (Exception ex) { | ||
log.error(ex.getMessage(), ex); | ||
} finally { | ||
markDone(); | ||
|
||
if (isWorkersDone()) { | ||
operation.finish(); | ||
} | ||
} | ||
} | ||
|
||
private void processObject(RepositoryService repository, T object) | ||
throws SchemaException, ObjectNotFoundException, ObjectAlreadyExistsException { | ||
|
||
PrismObject<? extends ObjectType> prismObject = object.asPrismObject(); | ||
|
||
if (skipUpgradeForUUIDs.contains(object.getOid())) { | ||
log.info("Skipping object {} with oid {}", prismObject.getBusinessDisplayName(), object.getOid()); | ||
return; | ||
} | ||
|
||
ObjectDelta<T> delta = new UpgradeObjectsHelper().upgradeObject(prismObject); | ||
|
||
if (delta != null && !delta.isEmpty()) { | ||
OperationResult result = new OperationResult("aaaa"); // todo fix operation result name and handling! | ||
repository.modifyObject(object.getClass(), object.getOid(), delta.getModifications(), result); | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
.../ninja/src/main/java/com/evolveum/midpoint/ninja/action/upgrade/UpgradeObjectsHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.evolveum.midpoint.ninja.action.upgrade; | ||
|
||
import com.evolveum.midpoint.prism.PrismObject; | ||
import com.evolveum.midpoint.prism.delta.ObjectDelta; | ||
|
||
public class UpgradeObjectsHelper { | ||
|
||
public ObjectDelta upgradeObject(PrismObject<?> object) { | ||
return null; | ||
} | ||
} |
3 changes: 0 additions & 3 deletions
3
...main/java/com/evolveum/midpoint/ninja/action/upgrade/handler/RemovedElementProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters