slowr | db071b2 | 2017-07-07 11:10:25 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-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; |
| 20 | import org.json.JSONException; |
| 21 | import org.json.JSONObject; |
| 22 | import org.onlab.packet.IpPrefix; |
| 23 | import org.onosproject.artemis.impl.ArtemisManager; |
| 24 | import org.onosproject.artemis.impl.DataHandler; |
| 25 | import org.slf4j.Logger; |
| 26 | import org.slf4j.LoggerFactory; |
| 27 | |
| 28 | import java.net.URISyntaxException; |
| 29 | import java.util.Objects; |
| 30 | |
| 31 | /** |
| 32 | * Implementation of ExaBGP Route Collector Monitor. |
| 33 | */ |
| 34 | public class ExaBgpMonitor extends Monitor { |
| 35 | private String host; |
| 36 | private Socket socket; |
| 37 | |
| 38 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 39 | |
| 40 | public ExaBgpMonitor(IpPrefix prefix, String host) { |
| 41 | super(prefix); |
| 42 | this.host = host; |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * socket.io onConnect event handler. |
| 47 | */ |
| 48 | private void onConnect() { |
| 49 | try { |
| 50 | JSONObject parameters = new JSONObject(); |
| 51 | parameters.put("prefix", this.prefix); |
| 52 | |
| 53 | socket.emit("exa_subscribe", parameters); |
| 54 | } catch (JSONException e) { |
| 55 | e.printStackTrace(); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | private void onExaMessage(Object[] args) { |
| 60 | JSONObject message = (JSONObject) args[0]; |
| 61 | |
| 62 | try { |
| 63 | if (message.getString("type").equals("A")) { |
| 64 | // Write BGP message to a json database |
| 65 | DataHandler.Serializer.writeData(args[0]); |
| 66 | |
| 67 | if (ArtemisManager.logging) { |
| 68 | log.info(message.toString()); |
| 69 | } |
| 70 | |
| 71 | // Example of BGP Update message: |
| 72 | // { |
| 73 | // "path":[65001], |
| 74 | // "peer":"1.1.1.1", |
| 75 | // "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. |
| 88 | DataHandler.getInstance().appendData(message); |
| 89 | } |
| 90 | } catch (JSONException e) { |
| 91 | e.printStackTrace(); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | @Override |
| 96 | public void startMonitor() { |
| 97 | if (!isRunning()) { |
| 98 | log.info("Starting EXA monitor for " + prefix + " / " + host); |
| 99 | try { |
| 100 | this.socket = IO.socket("http://" + this.host + "/onos"); |
| 101 | this.socket.on(Socket.EVENT_CONNECT, args -> onConnect()); |
| 102 | this.socket.on(Socket.EVENT_PING, args -> socket.emit("pong")); |
| 103 | this.socket.on("exa_message", this::onExaMessage); |
| 104 | } catch (URISyntaxException e) { |
| 105 | e.printStackTrace(); |
| 106 | } |
| 107 | this.socket.connect(); |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | @Override |
| 112 | public void stopMonitor() { |
| 113 | if (isRunning()) { |
| 114 | log.info("Stopping EXA monitor for " + prefix + " / " + host); |
| 115 | this.socket.off(); |
| 116 | this.socket.disconnect(); |
| 117 | this.socket.close(); |
| 118 | this.socket = null; |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | @Override |
| 123 | public Types getType() { |
| 124 | return Types.EXABGP; |
| 125 | } |
| 126 | |
| 127 | @Override |
| 128 | public boolean isRunning() { |
| 129 | return this.socket != null; |
| 130 | } |
| 131 | |
| 132 | @Override |
| 133 | public String getHost() { |
| 134 | return host; |
| 135 | } |
| 136 | |
| 137 | @Override |
| 138 | public void setHost(String host) { |
| 139 | this.host = host; |
| 140 | } |
| 141 | |
| 142 | @Override |
| 143 | public int hashCode() { |
| 144 | return Objects.hash(prefix, host); |
| 145 | } |
| 146 | |
| 147 | @Override |
| 148 | public boolean equals(Object obj) { |
| 149 | if (this == obj) { |
| 150 | return true; |
| 151 | } |
| 152 | if (obj instanceof ExaBgpMonitor) { |
| 153 | final ExaBgpMonitor that = (ExaBgpMonitor) obj; |
| 154 | return Objects.equals(this.prefix, that.prefix) && |
| 155 | Objects.equals(this.host, that.host); |
| 156 | } |
| 157 | return false; |
| 158 | } |
| 159 | |
| 160 | } |