blob: 52025c9b94794c43df19a7b8dc5d0b2e18beec7f [file] [log] [blame]
slowrdb071b22017-07-07 11:10:25 -07001/*
Dimitrios Mavrommatisf0c06322017-10-31 23:49:04 -07002 * Copyright 2017-present Open Networking Foundation
slowrdb071b22017-07-07 11:10:25 -07003 *
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 */
16package org.onosproject.artemis.impl.monitors;
17
18import io.socket.client.IO;
19import io.socket.client.Socket;
20import org.json.JSONException;
21import org.json.JSONObject;
22import org.onlab.packet.IpPrefix;
Dimitrios Mavrommatisf0c06322017-10-31 23:49:04 -070023import org.onosproject.artemis.ArtemisPacketProcessor;
24import org.onosproject.artemis.Monitors;
slowrdb071b22017-07-07 11:10:25 -070025import org.slf4j.Logger;
26import org.slf4j.LoggerFactory;
27
28import java.net.URISyntaxException;
29import java.util.Objects;
30
31/**
Dimitrios Mavrommatisf0c06322017-10-31 23:49:04 -070032 * Implementation of RIPE Route Collector Monitors.
slowrdb071b22017-07-07 11:10:25 -070033 */
Dimitrios Mavrommatisf0c06322017-10-31 23:49:04 -070034public class RipeMonitors implements Monitors {
35 private final Logger log = LoggerFactory.getLogger(getClass());
slowrdb071b22017-07-07 11:10:25 -070036 private String host;
37 private Socket socket;
Dimitrios Mavrommatisf0c06322017-10-31 23:49:04 -070038 private IpPrefix prefix;
39 private ArtemisPacketProcessor packetProcessor;
slowrdb071b22017-07-07 11:10:25 -070040
Dimitrios Mavrommatisf0c06322017-10-31 23:49:04 -070041 public RipeMonitors(IpPrefix prefix, String host, ArtemisPacketProcessor packetProcessor) {
42 this.prefix = prefix;
slowrdb071b22017-07-07 11:10:25 -070043 this.host = host;
Dimitrios Mavrommatisf0c06322017-10-31 23:49:04 -070044 this.packetProcessor = packetProcessor;
slowrdb071b22017-07-07 11:10:25 -070045 }
46
47 /**
48 * socket.io onConnect event handler.
49 */
50 private void onConnect() {
51 try {
52 socket.emit("ping");
53
54 JSONObject parameters = new JSONObject();
55 parameters.put("origin", (Object) null);
56 parameters.put("type", (Object) null);
57 parameters.put("moreSpecific", true);
58 parameters.put("lessSpecific", false);
59 parameters.put("peer", (Object) null);
60 parameters.put("host", this.host);
61 parameters.put("prefix", this.prefix);
62
63 socket.emit("ris_subscribe", parameters);
64 } catch (JSONException e) {
Ray Milkeyba547f92018-02-01 15:22:31 -080065 log.warn("onConnect()", e);
slowrdb071b22017-07-07 11:10:25 -070066 }
67 }
68
Dimitrios Mavrommatisf0c06322017-10-31 23:49:04 -070069 @Override
70 public IpPrefix getPrefix() {
71 return prefix;
72 }
73
74 @Override
75 public void setPrefix(IpPrefix prefix) {
76 this.prefix = prefix;
77 }
78
slowrdb071b22017-07-07 11:10:25 -070079 /**
80 * socket.io onRisMessage event handler.
81 * This event is custom made that triggers when it receives an BGP update/withdraw for our prefix.
82 *
83 * @param args RIS message
84 */
85 private void onRisMessage(Object[] args) {
86 try {
87 JSONObject message = (JSONObject) args[0];
88 if (message.getString("type").equals("A")) {
slowrdb071b22017-07-07 11:10:25 -070089 // Example of BGP Update message:
90 // {
91 // "timestamp":1488044022.97,
92 // "prefix":"101.1.46.0/24",
93 // "host":"rrc21",
94 // "next_hop":"37.49.236.246",
95 // "peer":"37.49.236.246",
96 // "path":[2613,25091,9318,9524],
97 // "type":"A"
98 // }
99
100 // We want to keep only prefix and path in memory.
101 message.remove("community");
102 message.remove("timestamp");
103 message.remove("next_hop");
104 message.remove("peer");
105 message.remove("type");
106 message.remove("host");
107
108 // Append synchronized message to message list in memory.
Dimitrios Mavrommatisf0c06322017-10-31 23:49:04 -0700109 packetProcessor.processMonitorPacket(message);
slowrdb071b22017-07-07 11:10:25 -0700110 }
111 } catch (JSONException e) {
Ray Milkeyba547f92018-02-01 15:22:31 -0800112 log.error("onRisMessage()", e);
slowrdb071b22017-07-07 11:10:25 -0700113 }
114 socket.emit("ping");
115 }
116
117 @Override
118 public void startMonitor() {
119 if (!isRunning()) {
120 log.info("Starting RIPE monitor for " + prefix + " / " + host);
121 IO.Options opts = new IO.Options();
122 opts.path = "/stream/socket.io/";
123
124 try {
125 this.socket = IO.socket("http://stream-dev.ris.ripe.net/", opts);
126 this.socket.on(Socket.EVENT_CONNECT, args -> onConnect());
127 this.socket.on(Socket.EVENT_PONG, args -> socket.emit("ping"));
128 this.socket.on("ris_message", this::onRisMessage);
129 } catch (URISyntaxException e) {
Ray Milkeyba547f92018-02-01 15:22:31 -0800130 log.error("startMonitor()", e);
slowrdb071b22017-07-07 11:10:25 -0700131 }
132
133 this.socket.connect();
134 }
135 }
136
137 @Override
138 public void stopMonitor() {
139 if (isRunning()) {
140 log.info("Stopping RIPE monitor for " + prefix + " / " + host);
141 this.socket.off();
142 this.socket.disconnect();
143 this.socket.close();
144 this.socket = null;
145 }
146 }
147
148 @Override
slowrdb071b22017-07-07 11:10:25 -0700149 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 }
Dimitrios Mavrommatisf0c06322017-10-31 23:49:04 -0700173 if (obj instanceof RipeMonitors) {
174 final RipeMonitors that = (RipeMonitors) obj;
slowrdb071b22017-07-07 11:10:25 -0700175 return Objects.equals(this.prefix, that.prefix) &&
176 Objects.equals(this.host, that.host);
177 }
178 return false;
179 }
180
181}