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