blob: 5fb9be4b71b3f7d5f22aa120e2c9386ab3f9f2b6 [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 ExaBGP Route Collector Monitors.
slowrdb071b22017-07-07 11:10:25 -070033 */
Dimitrios Mavrommatisf0c06322017-10-31 23:49:04 -070034public class ExaBgpMonitors 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 ExaBgpMonitors(IpPrefix prefix, String host, ArtemisPacketProcessor packetProcessor) {
slowrdb071b22017-07-07 11:10:25 -070042 this.host = host;
Dimitrios Mavrommatisf0c06322017-10-31 23:49:04 -070043 this.prefix = prefix;
44 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 JSONObject parameters = new JSONObject();
53 parameters.put("prefix", this.prefix);
54
55 socket.emit("exa_subscribe", parameters);
56 } catch (JSONException e) {
57 e.printStackTrace();
58 }
59 }
60
Dimitrios Mavrommatisf0c06322017-10-31 23:49:04 -070061 /**
62 * ExaBGP message received on the socket.io.
63 *
64 * @param args exabgp message
65 */
slowrdb071b22017-07-07 11:10:25 -070066 private void onExaMessage(Object[] args) {
67 JSONObject message = (JSONObject) args[0];
68
69 try {
70 if (message.getString("type").equals("A")) {
slowrdb071b22017-07-07 11:10:25 -070071 // Example of BGP Update message:
72 // {
73 // "path":[65001],
Dimitrios Mavrommatisf0c06322017-10-31 23:49:04 -070074 // "peer":"1.1.1.s1",
slowrdb071b22017-07-07 11:10:25 -070075 // "prefix":"12.0.0.0/8",
76 // "host":"exabgp", <-- Can put IP here
77 // "type":"A",
78 // "timestamp":1488120484
79 // }
80
81 // We want to keep only prefix and path in memory.
82 message.remove("peer");
83 message.remove("host");
84 message.remove("type");
85 message.remove("timestamp");
86
87 // Append synchronized message to message list in memory.
Dimitrios Mavrommatisf0c06322017-10-31 23:49:04 -070088 packetProcessor.processMonitorPacket(message);
slowrdb071b22017-07-07 11:10:25 -070089 }
90 } catch (JSONException e) {
91 e.printStackTrace();
92 }
93 }
94
95 @Override
Dimitrios Mavrommatisf0c06322017-10-31 23:49:04 -070096 public IpPrefix getPrefix() {
97 return prefix;
98 }
99
100 @Override
101 public void setPrefix(IpPrefix prefix) {
102 this.prefix = prefix;
103 }
104
105 @Override
slowrdb071b22017-07-07 11:10:25 -0700106 public void startMonitor() {
107 if (!isRunning()) {
108 log.info("Starting EXA monitor for " + prefix + " / " + host);
109 try {
110 this.socket = IO.socket("http://" + this.host + "/onos");
111 this.socket.on(Socket.EVENT_CONNECT, args -> onConnect());
112 this.socket.on(Socket.EVENT_PING, args -> socket.emit("pong"));
113 this.socket.on("exa_message", this::onExaMessage);
114 } catch (URISyntaxException e) {
115 e.printStackTrace();
116 }
117 this.socket.connect();
118 }
119 }
120
121 @Override
122 public void stopMonitor() {
123 if (isRunning()) {
124 log.info("Stopping EXA monitor for " + prefix + " / " + host);
125 this.socket.off();
126 this.socket.disconnect();
127 this.socket.close();
128 this.socket = null;
129 }
130 }
131
132 @Override
slowrdb071b22017-07-07 11:10:25 -0700133 public boolean isRunning() {
134 return this.socket != null;
135 }
136
137 @Override
138 public String getHost() {
139 return host;
140 }
141
142 @Override
143 public void setHost(String host) {
144 this.host = host;
145 }
146
147 @Override
148 public int hashCode() {
149 return Objects.hash(prefix, host);
150 }
151
152 @Override
153 public boolean equals(Object obj) {
154 if (this == obj) {
155 return true;
156 }
Dimitrios Mavrommatisf0c06322017-10-31 23:49:04 -0700157 if (obj instanceof ExaBgpMonitors) {
158 final ExaBgpMonitors that = (ExaBgpMonitors) obj;
slowrdb071b22017-07-07 11:10:25 -0700159 return Objects.equals(this.prefix, that.prefix) &&
160 Objects.equals(this.host, that.host);
161 }
162 return false;
163 }
164
165}