2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.protocol.util;
10 import com.google.common.base.Preconditions;
11 import com.google.common.collect.Lists;
12 import com.google.common.io.CharStreams;
14 import java.io.FileInputStream;
15 import java.io.IOException;
16 import java.io.InputStream;
17 import java.io.InputStreamReader;
18 import java.util.List;
19 import org.apache.commons.codec.DecoderException;
20 import org.apache.commons.codec.binary.Hex;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
25 * Parses PCEP messages from a text file. Messages need to follow this formatting:
27 * Received PCEP Open message. Length:28.
29 * 20 01 00 1c 01 10 00 18 20 1e 78 03 00 10 00 04 00 00 00 05 00 1a 00 04 00 00 00 b4
31 public final class PCEPHexDumpParser {
32 private static final int MINIMAL_LENGTH = 4;
33 private static final Logger LOG = LoggerFactory.getLogger(PCEPHexDumpParser.class);
34 private static final String LENGTH = "LENGTH:";
36 private PCEPHexDumpParser() {
40 public static List<byte[]> parseMessages(final File file) throws IOException {
41 Preconditions.checkArgument(file != null, "Filename cannot be null");
42 return parseMessages(new FileInputStream(file));
45 public static List<byte[]> parseMessages(final InputStream is) throws IOException {
46 Preconditions.checkNotNull(is);
47 try (InputStreamReader isr = new InputStreamReader(is)) {
48 return parseMessages(CharStreams.toString(isr));
54 private static List<byte[]> parseMessages(final String c) {
55 final String content = clearWhiteSpaceToUpper(c);
57 final List<byte[]> messages = Lists.newLinkedList();
58 int idx = content.indexOf(LENGTH, 0);
60 // next chars are final length, ending with '.'
61 final int lengthIdx = idx + LENGTH.length();
62 final int messageIdx = content.indexOf('.', lengthIdx);
63 final int length = Integer.parseInt(content.substring(lengthIdx, messageIdx));
64 final int messageEndIdx = messageIdx + (length * 2) + 1; // dot
66 // Assert that message is longer than minimum 4(header.length == 4)
67 // If length in PCEP message would be 0, loop would never end
68 Preconditions.checkArgument(length >= MINIMAL_LENGTH, "Invalid message at index " + idx + ", length atribute is lower than "
71 final String hexMessage = content.substring(messageIdx + 1, messageEndIdx); // dot
72 byte[] message = null;
74 message = Hex.decodeHex(hexMessage.toCharArray());
75 } catch (final DecoderException e) {
76 throw new IllegalArgumentException("Failed to decode message", e);
78 messages.add(message);
80 idx = content.indexOf(LENGTH, idx);
82 LOG.info("Succesfully extracted {} messages", messages.size());
86 private static String clearWhiteSpaceToUpper(final String line) {
87 return line.replaceAll("\\s", "").toUpperCase();