slowr | db071b2 | 2017-07-07 11:10:25 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
slowr | db071b2 | 2017-07-07 11:10:25 -0700 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package org.onosproject.artemis.impl; |
| 17 | |
| 18 | import org.json.JSONObject; |
| 19 | |
| 20 | import java.io.File; |
| 21 | import java.io.IOException; |
| 22 | import java.io.RandomAccessFile; |
| 23 | import java.util.ArrayList; |
| 24 | import java.util.concurrent.atomic.AtomicReference; |
| 25 | |
| 26 | /** |
| 27 | * Helper class that handles BGP Update messages. |
| 28 | */ |
| 29 | public final class DataHandler { |
| 30 | private static final File DATA_FILE = new File("data.json"); |
| 31 | private static final File HIJACKS_FILE = new File("hijack.json"); |
| 32 | |
| 33 | private final AtomicReference<ArrayList<JSONObject>> data = new AtomicReference<ArrayList<JSONObject>>(); |
| 34 | |
| 35 | private static DataHandler instance = new DataHandler(); |
| 36 | |
| 37 | private DataHandler() { |
| 38 | data.set(new ArrayList<>()); |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Singleton for data handler class. |
| 43 | * |
| 44 | * @return instance of class |
| 45 | */ |
| 46 | public static synchronized DataHandler getInstance() { |
| 47 | if (instance == null) { |
| 48 | instance = new DataHandler(); |
| 49 | } |
| 50 | return instance; |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Atomic append a BGP update message to a list. |
| 55 | * |
| 56 | * @param obj BGP update message |
| 57 | */ |
| 58 | public synchronized void appendData(JSONObject obj) { |
| 59 | data.get().add(obj); |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Atomic read and clear a list of BGP updates. |
| 64 | * |
| 65 | * @return list of messages that received in 'threshold' period |
| 66 | */ |
| 67 | synchronized ArrayList<JSONObject> getData() { |
| 68 | ArrayList<JSONObject> tmp = (ArrayList<JSONObject>) data.get().clone(); |
| 69 | data.get().clear(); |
| 70 | return tmp; |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * A serializer to write incoming BGP updates and hijack attempts to json files. |
| 75 | */ |
| 76 | public static class Serializer { |
| 77 | private static RandomAccessFile fwData, fwHijack; |
| 78 | private static long lengthData, lengthHijack; |
| 79 | |
| 80 | static { |
| 81 | try { |
| 82 | if (DATA_FILE.exists()) { |
| 83 | fwData = new RandomAccessFile(DATA_FILE, "rw"); |
| 84 | } else { |
| 85 | fwData = new RandomAccessFile(DATA_FILE, "rw"); |
| 86 | fwData.writeBytes("[\n]"); |
| 87 | } |
| 88 | lengthData = fwData.length() - 1; |
| 89 | |
| 90 | if (HIJACKS_FILE.exists()) { |
| 91 | fwHijack = new RandomAccessFile(HIJACKS_FILE, "rw"); |
| 92 | } else { |
| 93 | fwHijack = new RandomAccessFile(HIJACKS_FILE, "rw"); |
| 94 | fwHijack.writeBytes("[\n]"); |
| 95 | } |
| 96 | lengthHijack = fwHijack.length() - 1; |
| 97 | } catch (IOException e) { |
| 98 | e.printStackTrace(); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Writes BGP update to json file. |
| 104 | * |
| 105 | * @param data BGP update |
| 106 | */ |
| 107 | public static synchronized void writeData(Object data) { |
| 108 | try { |
| 109 | String entry = data.toString() + ",\n]"; |
| 110 | fwData.seek(lengthData); |
| 111 | fwData.writeBytes(entry); |
| 112 | lengthData += entry.length() - 1; |
| 113 | } catch (IOException e) { |
| 114 | e.printStackTrace(); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Writes detected BGP hijack to json file. |
| 120 | * |
| 121 | * @param data BGP update of hijack |
| 122 | */ |
| 123 | static synchronized void writeHijack(Object data) { |
| 124 | try { |
| 125 | String entry = data.toString() + ",\n]"; |
| 126 | fwHijack.seek(lengthHijack); |
| 127 | fwHijack.writeBytes(entry); |
| 128 | lengthHijack += entry.length() - 1; |
| 129 | } catch (IOException e) { |
| 130 | e.printStackTrace(); |
| 131 | } |
| 132 | } |
| 133 | } |
| 134 | } |