Samples :: FIX Dictionary Validator

FIX Dictionary validator

Description

This application provides ability to validate log file against particular FIX dictionary. Please follow instructions in the configuration file to configure the application.

Directory Contents

Item Description
conf/sample/DictionaryValidator.properties sample configuration
conf/logback.xml Logback logging facility configuration
conf/sample/Dialect.xml FIX dialects
conf/sample/logfile.txt test data

Usage

  • Run the sample:
    • win: DictionaryValidator.bat
    • linux: DictionaryValidator.sh
  • Clean everything:
    • win: clean.bat
    • linux: clean.sh

Source Code

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
import biz.onixs.fix.dictionary.Version;
import biz.onixs.fix.engine.Engine;
import biz.onixs.fix.parser.Message;
import biz.onixs.fix.parser.MessageValidationFlags;
import biz.onixs.util.settings.PropertyBasedSettings;
import biz.onixs.util.settings.Settings;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
  
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Stream;
  
public class DictionaryValidator {
    public static final String VALIDATE_VERSION_KEY = "ValidateVersion";
    public static final String LOG_FILE_TO_VALIDATE_KEY = "LogFileToValidate";
    public static final String VALIDATE_FLAGS_KEY = "ValidateFlags";
  
    private static final String SETTINGS_RESOURCE = "sample/DictionaryValidator.properties";
    private static final Logger LOG = LoggerFactory.getLogger(DictionaryValidator.class);
    private static Settings settings = null;
  
    public static class CannotParseLogFileException extends Exception {
        public CannotParseLogFileException(String message) {
            super(message);
        }
  
        public CannotParseLogFileException(){}
    }
  
    public static void main(final String[] args) {
        LOG.info("Loading settings from: {}", SETTINGS_RESOURCE);
        settings = new PropertyBasedSettings(SETTINGS_RESOURCE);
  
        try {
            final Engine engine = Engine.init(settings);
  
            final String versionName = settings.isDefined(VALIDATE_VERSION_KEY)
                    ? settings.getString(VALIDATE_VERSION_KEY)
                    : null;
  
            if (versionName == null) {
                LOG.info("Validate messages using suitable version");
            } else {
                LOG.info("Validate against '" + versionName  + "' version");
            }
  
            Version version = versionName == null
                                        ? null
                                        : Version.getById(versionName);
  
            if (versionName != null && version == Version.UNKNOWN) {
                throw new IllegalArgumentException("Version '" + versionName + "' is unknown");
            }
  
            int validationFlags = getValidationFlags(settings.getString(VALIDATE_FLAGS_KEY, ""));
  
            int numberOfProcessedMessages = parseLogFile(settings.getString(LOG_FILE_TO_VALIDATE_KEY), version, validationFlags);
  
            System.out.println("Validated " + numberOfProcessedMessages + " message(s)");
  
        } catch(Exception e) {
            LOG.error(e.getMessage());
        }
    }
  
    private static int parseLogFile(String logFileToValidate, Version version, int validationFlags) throws Exception {
        int lineCounter = 0;
        Path path = Paths.get(logFileToValidate);
        LOG.info("Validating file '" + path.toFile().getAbsolutePath() + "'");
        try(Stream<String> stream = Files.lines(path)) {
            for(String line : (Iterable<String>) stream::iterator) {
                ++lineCounter;
  
                if (0 == line.length()) {
                    continue;
                }
  
                String fixMessage = getFixMessage(logFileToValidate, line, lineCounter);
  
                try
                {
                    Message message = null;
  
                    if (null == version)
                    {
                        message = new Message(fixMessage);
                    }
                    else
                    {
                        message = new Message(version, fixMessage);
                    }
  
                    message.validate(validationFlags);
                }
                catch (Exception e)
                {
                    LOG.error(line);
                    throw e;
                }
            }
        }
        return lineCounter;
    }
  
    private static int getValidationFlags(String s) throws Exception {
        LOG.info("Use validation flags: " + s);
  
        if ("".equals(s)) {
            return 0;
        }
  
        Field[] fields = MessageValidationFlags.class.getDeclaredFields();
        Map<String, Integer> fieldsMap = new HashMap<>();
  
        for(Field field: fields) {
            if (Modifier.isStatic(field.getModifiers())) {
                fieldsMap.put(field.getName(), (Integer)field.get(MessageValidationFlags.class));
            }
        }
  
        String names[] = s.split("\\|");
  
        int result = 0;
        for(String flagName : names) {
            if (fieldsMap.containsKey(flagName)) {
                result |= fieldsMap.get(flagName);
            } else {
                throw new IllegalArgumentException("Unknown flag name: '" + flagName + "'");
            }
        }
  
        LOG.info("Validation flags numeric value: " + result);
        return result;
    }
  
    private static String getFixMessage(String logFileToValidate, String line, int lineCounter) throws CannotParseLogFileException {
        int startOfFixMsgIndex = line.indexOf("8=FIX");
  
        if (-1 == startOfFixMsgIndex) {
            throw new CannotParseLogFileException("Cannot find the start of the FIX message (8=FIX) in line # "
                    + lineCounter
                    + " of " + logFileToValidate);
        }
  
        String CheckSumTagStr = "10";
        char DelimChar = '\u0001';
        char TagValueSepChar = '=';
        int CheckSumValueLength = 6;
  
        int startOfCheckSumTag = line.indexOf(DelimChar+CheckSumTagStr+TagValueSepChar, startOfFixMsgIndex);
        if (-1 == startOfCheckSumTag) {
            throw new CannotParseLogFileException("Cannot find the checksum of the FIX message (10=***) in line # "
                    + lineCounter
                    + " of " + logFileToValidate);
        }
  
        String fixMessage = line.substring(startOfFixMsgIndex, startOfCheckSumTag + CheckSumValueLength + 2);
  
        return fixMessage;
    }
}