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