blob: e848875fa6988c74c176efc3d70793bd63a5b2f1 [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;
Dimitrios Mavrommatisf0c06322017-10-31 23:49:04 -070020import org.apache.commons.lang.exception.ExceptionUtils;
slowrdb071b22017-07-07 11:10:25 -070021import org.json.JSONException;
22import org.json.JSONObject;
23import org.onlab.packet.IpPrefix;
Dimitrios Mavrommatisf0c06322017-10-31 23:49:04 -070024import org.onosproject.artemis.ArtemisPacketProcessor;
25import org.onosproject.artemis.Monitors;
slowrdb071b22017-07-07 11:10:25 -070026import org.slf4j.Logger;
27import org.slf4j.LoggerFactory;
28
29import java.net.URISyntaxException;
30import java.util.Objects;
31
32/**
Dimitrios Mavrommatisf0c06322017-10-31 23:49:04 -070033 * Implementation of RIPE Route Collector Monitors.
slowrdb071b22017-07-07 11:10:25 -070034 */
Dimitrios Mavrommatisf0c06322017-10-31 23:49:04 -070035public class RipeMonitors implements Monitors {
36 private final Logger log = LoggerFactory.getLogger(getClass());
slowrdb071b22017-07-07 11:10:25 -070037 private String host;
38 private Socket socket;
Dimitrios Mavrommatisf0c06322017-10-31 23:49:04 -070039 private IpPrefix prefix;
40 private ArtemisPacketProcessor packetProcessor;
slowrdb071b22017-07-07 11:10:25 -070041
Dimitrios Mavrommatisf0c06322017-10-31 23:49:04 -070042 public RipeMonitors(IpPrefix prefix, String host, ArtemisPacketProcessor packetProcessor) {
43 this.prefix = prefix;
slowrdb071b22017-07-07 11:10:25 -070044 this.host = host;
Dimitrios Mavrommatisf0c06322017-10-31 23:49:04 -070045 this.packetProcessor = packetProcessor;
slowrdb071b22017-07-07 11:10:25 -070046 }
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 Mavrommatisf0c06322017-10-31 23:49:04 -070070 @Override
71 public IpPrefix getPrefix() {
72 return prefix;
73 }
74
75 @Override
76 public void setPrefix(IpPrefix prefix) {
77 this.prefix = prefix;
78 }
79
slowrdb071b22017-07-07 11:10:25 -070080 /**
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")) {
slowrdb071b22017-07-07 11:10:25 -070090 // 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 Mavrommatisf0c06322017-10-31 23:49:04 -0700110 packetProcessor.processMonitorPacket(message);
slowrdb071b22017-07-07 11:10:25 -0700111 }
112 } catch (JSONException e) {
Dimitrios Mavrommatisf0c06322017-10-31 23:49:04 -0700113 log.error(ExceptionUtils.getFullStackTrace(e));
slowrdb071b22017-07-07 11:10:25 -0700114 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 Mavrommatisf0c06322017-10-31 23:49:04 -0700132 log.error(ExceptionUtils.getFullStackTrace(e));
slowrdb071b22017-07-07 11:10:25 -0700133 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
slowrdb071b22017-07-07 11:10:25 -0700152 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 Mavrommatisf0c06322017-10-31 23:49:04 -0700176 if (obj instanceof RipeMonitors) {
177 final RipeMonitors that = (RipeMonitors) obj;
slowrdb071b22017-07-07 11:10:25 -0700178 return Objects.equals(this.prefix, that.prefix) &&
179 Objects.equals(this.host, that.host);
180 }
181 return false;
182 }
183
184}