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.monitors; |
| 17 | |
| 18 | import io.socket.client.IO; |
| 19 | import io.socket.client.Socket; |
| 20 | import org.json.JSONException; |
| 21 | import org.json.JSONObject; |
| 22 | import org.onlab.packet.IpPrefix; |
| 23 | import org.onosproject.artemis.impl.ArtemisManager; |
| 24 | import org.onosproject.artemis.impl.DataHandler; |
| 25 | import org.slf4j.Logger; |
| 26 | import org.slf4j.LoggerFactory; |
| 27 | |
| 28 | import java.net.URISyntaxException; |
| 29 | import java.util.Objects; |
| 30 | |
| 31 | /** |
| 32 | * Implementation of RIPE Route Collector Monitor. |
| 33 | */ |
| 34 | public class RipeMonitor extends Monitor { |
| 35 | private String host; |
| 36 | private Socket socket; |
| 37 | |
| 38 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 39 | |
| 40 | public RipeMonitor(IpPrefix prefix, String host) { |
| 41 | super(prefix); |
| 42 | this.host = host; |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * socket.io onConnect event handler. |
| 47 | */ |
| 48 | private void onConnect() { |
| 49 | try { |
| 50 | socket.emit("ping"); |
| 51 | |
| 52 | JSONObject parameters = new JSONObject(); |
| 53 | parameters.put("origin", (Object) null); |
| 54 | parameters.put("type", (Object) null); |
| 55 | parameters.put("moreSpecific", true); |
| 56 | parameters.put("lessSpecific", false); |
| 57 | parameters.put("peer", (Object) null); |
| 58 | parameters.put("host", this.host); |
| 59 | parameters.put("prefix", this.prefix); |
| 60 | |
| 61 | socket.emit("ris_subscribe", parameters); |
| 62 | } catch (JSONException e) { |
| 63 | e.printStackTrace(); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * socket.io onRisMessage event handler. |
| 69 | * This event is custom made that triggers when it receives an BGP update/withdraw for our prefix. |
| 70 | * |
| 71 | * @param args RIS message |
| 72 | */ |
| 73 | private void onRisMessage(Object[] args) { |
| 74 | try { |
| 75 | JSONObject message = (JSONObject) args[0]; |
| 76 | if (message.getString("type").equals("A")) { |
| 77 | // Write BGP message to a json database |
| 78 | DataHandler.Serializer.writeData(args[0]); |
| 79 | |
| 80 | if (ArtemisManager.logging) { |
| 81 | log.info(message.toString()); |
| 82 | } |
| 83 | |
| 84 | // Example of BGP Update message: |
| 85 | // { |
| 86 | // "timestamp":1488044022.97, |
| 87 | // "prefix":"101.1.46.0/24", |
| 88 | // "host":"rrc21", |
| 89 | // "next_hop":"37.49.236.246", |
| 90 | // "peer":"37.49.236.246", |
| 91 | // "path":[2613,25091,9318,9524], |
| 92 | // "type":"A" |
| 93 | // } |
| 94 | |
| 95 | // We want to keep only prefix and path in memory. |
| 96 | message.remove("community"); |
| 97 | message.remove("timestamp"); |
| 98 | message.remove("next_hop"); |
| 99 | message.remove("peer"); |
| 100 | message.remove("type"); |
| 101 | message.remove("host"); |
| 102 | |
| 103 | // Append synchronized message to message list in memory. |
| 104 | DataHandler.getInstance().appendData(message); |
| 105 | } |
| 106 | } catch (JSONException e) { |
| 107 | e.printStackTrace(); |
| 108 | } |
| 109 | socket.emit("ping"); |
| 110 | } |
| 111 | |
| 112 | @Override |
| 113 | public void startMonitor() { |
| 114 | if (!isRunning()) { |
| 115 | log.info("Starting RIPE monitor for " + prefix + " / " + host); |
| 116 | IO.Options opts = new IO.Options(); |
| 117 | opts.path = "/stream/socket.io/"; |
| 118 | |
| 119 | try { |
| 120 | this.socket = IO.socket("http://stream-dev.ris.ripe.net/", opts); |
| 121 | this.socket.on(Socket.EVENT_CONNECT, args -> onConnect()); |
| 122 | this.socket.on(Socket.EVENT_PONG, args -> socket.emit("ping")); |
| 123 | this.socket.on("ris_message", this::onRisMessage); |
| 124 | } catch (URISyntaxException e) { |
| 125 | e.printStackTrace(); |
| 126 | } |
| 127 | |
| 128 | this.socket.connect(); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | @Override |
| 133 | public void stopMonitor() { |
| 134 | if (isRunning()) { |
| 135 | log.info("Stopping RIPE monitor for " + prefix + " / " + host); |
| 136 | this.socket.off(); |
| 137 | this.socket.disconnect(); |
| 138 | this.socket.close(); |
| 139 | this.socket = null; |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | @Override |
| 144 | public Types getType() { |
| 145 | return Types.RIPE; |
| 146 | } |
| 147 | |
| 148 | @Override |
| 149 | public boolean isRunning() { |
| 150 | return this.socket != null; |
| 151 | } |
| 152 | |
| 153 | @Override |
| 154 | public String getHost() { |
| 155 | return host; |
| 156 | } |
| 157 | |
| 158 | @Override |
| 159 | public void setHost(String host) { |
| 160 | this.host = host; |
| 161 | } |
| 162 | |
| 163 | @Override |
| 164 | public int hashCode() { |
| 165 | return Objects.hash(prefix, host); |
| 166 | } |
| 167 | |
| 168 | @Override |
| 169 | public boolean equals(Object obj) { |
| 170 | if (this == obj) { |
| 171 | return true; |
| 172 | } |
| 173 | if (obj instanceof RipeMonitor) { |
| 174 | final RipeMonitor that = (RipeMonitor) obj; |
| 175 | return Objects.equals(this.prefix, that.prefix) && |
| 176 | Objects.equals(this.host, that.host); |
| 177 | } |
| 178 | return false; |
| 179 | } |
| 180 | |
| 181 | } |