
io.fixprotocol.orchestra.model.quickfix.QuickfixValidator Maven / Gradle / Ivy
The newest version!
/*
* Copyright 2017-2020 FIX Protocol Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*
*/
package io.fixprotocol.orchestra.model.quickfix;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.function.BiPredicate;
import io.fixprotocol._2020.orchestra.repository.CodeSetType;
import io.fixprotocol._2020.orchestra.repository.CodeType;
import io.fixprotocol._2020.orchestra.repository.ComponentRefType;
import io.fixprotocol._2020.orchestra.repository.ComponentType;
import io.fixprotocol._2020.orchestra.repository.Datatype;
import io.fixprotocol._2020.orchestra.repository.FieldRefType;
import io.fixprotocol._2020.orchestra.repository.FieldRuleType;
import io.fixprotocol._2020.orchestra.repository.GroupRefType;
import io.fixprotocol._2020.orchestra.repository.GroupType;
import io.fixprotocol._2020.orchestra.repository.MessageType;
import io.fixprotocol._2020.orchestra.repository.PresenceT;
import io.fixprotocol.orchestra.dsl.antlr.Evaluator;
import io.fixprotocol.orchestra.dsl.antlr.ScoreException;
import io.fixprotocol.orchestra.dsl.antlr.SemanticErrorListener;
import io.fixprotocol.orchestra.message.CodeSetScope;
import io.fixprotocol.orchestra.message.TestException;
import io.fixprotocol.orchestra.message.Validator;
import io.fixprotocol.orchestra.model.FixValue;
import io.fixprotocol.orchestra.model.PathStep;
import io.fixprotocol.orchestra.model.Scope;
import io.fixprotocol.orchestra.model.SymbolResolver;
import quickfix.FieldMap;
import quickfix.FieldNotFound;
import quickfix.Group;
import quickfix.Message;
/**
* Validates a FIX message against an Orchestra file
*
* Features:
*
* - Checks field presence including conditionally required field rule
* - Checks code membership in a codeSet
*
* Only validates the message body, not session level header and trailer. This implementation is a
* demonstration of capabilities. There is no claim to high performance.
*
* @author Don Mendelson
*
*/
public class QuickfixValidator implements Validator {
private static class ErrorListener implements SemanticErrorListener {
private final BlockingQueue messages = new LinkedBlockingQueue<>();
@Override
public void onError(String msg) {
try {
messages.put(msg);
} catch (final InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
void getErrors(Collection toReceive) {
messages.drainTo(toReceive);
}
boolean hasError() {
return !messages.isEmpty();
}
}
private final ErrorListener errorListener = new ErrorListener();
private final Evaluator evaluator;
private final BiPredicate predicateEvaluator =
new BiPredicate() {
@Override
public boolean test(String expression, TestException testException) {
FixValue> fixValue;
try {
fixValue = evaluator.evaluate(expression);
final ArrayList toReceive = new ArrayList<>();
errorListener.getErrors(toReceive);
toReceive.forEach(testException::addDetail);
if (fixValue.getValue() == Boolean.TRUE) {
return true;
}
} catch (final ScoreException e) {
testException.addDetail(e.getMessage());
}
return false;
}
};
private final RepositoryAccessor repositoryAdapter;
private final SymbolResolver symbolResolver;
public QuickfixValidator(RepositoryAccessor repositoryAdapter, SymbolResolver symbolResolver) {
this.repositoryAdapter = repositoryAdapter;
this.symbolResolver = symbolResolver;
evaluator = new Evaluator(symbolResolver, errorListener);
}
@Override
public void validate(Message message, MessageType messageType) throws TestException {
final TestException testException =
new TestException("Invalid message type " + messageType.getName());
try (final MessageScope messageScope =
new MessageScope(message, messageType, repositoryAdapter, symbolResolver, evaluator)) {
symbolResolver.nest(new PathStep("in."), messageScope);
try (Scope local = (Scope) symbolResolver.resolve(SymbolResolver.LOCAL_ROOT)) {
local.nest(new PathStep(messageType.getName()), messageScope);
final List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy