blob: cfcea12178db23d8d5df46caf636ce87018c25bf [file] [log] [blame]
ADARA Networks1fb1eb12016-09-01 12:04:07 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
ADARA Networks1fb1eb12016-09-01 12:04:07 -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.rabbitmq.util;
17
ADARA Networks1fb1eb12016-09-01 12:04:07 -070018import java.io.InputStream;
19import java.io.UnsupportedEncodingException;
ADARA Networks1fb1eb12016-09-01 12:04:07 -070020import java.net.URL;
21import java.net.URLEncoder;
22import java.util.Date;
23import java.util.HashMap;
24import java.util.Map;
25import java.util.Properties;
26import org.apache.commons.lang.exception.ExceptionUtils;
27
28import org.onlab.packet.EthType;
29import org.onosproject.net.Link;
30import org.onosproject.net.device.DeviceEvent;
31import org.onosproject.net.link.LinkEvent;
32import org.onosproject.net.packet.PacketContext;
33import org.onosproject.net.topology.Topology;
34import org.onosproject.net.topology.TopologyEvent;
35import org.onosproject.net.packet.InboundPacket;
36import org.osgi.service.component.ComponentContext;
37import com.google.gson.JsonObject;
38import org.slf4j.Logger;
39import org.slf4j.LoggerFactory;
40
41import static org.onosproject.rabbitmq.api.MQConstants.*;
42
43/**
44 * MQ utility class for constructing server url, packet message, device message,
45 * topology message and link message.
46 */
47public final class MQUtil {
48
49 private static final String COLON = ":";
50 private static final String AT = "@";
51 private static final String CDFS = "://";
52 private static final String FS = "/";
53 private static final String UTF8 = "UTF-8";
54 private static final Logger log = LoggerFactory.getLogger(MQUtil.class);
55
56 private MQUtil() {
57 }
58
59 /**
60 * Returns the MQ server url.
61 *
62 * @param proto mq server protocol
63 * @param userName mq server username
64 * @param password mq server password
65 * @param ipAddr server ip address
66 * @param port server port
67 * @param vhost server vhost
68 * @return server url
69 */
70 public static String getMqUrl(String proto, String userName,
71 String password, String ipAddr, String port,
72 String vhost) {
73 StringBuilder urlBuilder = new StringBuilder();
74 try {
75 urlBuilder.append(proto).append(CDFS).append(userName).append(COLON)
76 .append(password).append(AT)
77 .append(ipAddr).append(COLON).append(port).append(FS)
78 .append(URLEncoder.encode(vhost, UTF8));
79 } catch (UnsupportedEncodingException e) {
80 log.error(ExceptionUtils.getFullStackTrace(e));
81 }
82 return urlBuilder.toString().replaceAll("\\s+", "");
83 }
84
85 /**
86 * Initializes and returns publisher channel configuration.
87 *
88 * @param exchange the configured mq exchange name
89 * @param routingKey the configured mq routing key
90 * @param queueName the configured mq queue name
91 * @return the server url
92 */
93 public static Map<String, String> rfProducerChannelConf(String exchange,
94 String routingKey, String queueName) {
95 Map<String, String> channelConf = new HashMap<>();
96 channelConf.put(EXCHANGE_NAME_PROPERTY, exchange);
97 channelConf.put(ROUTING_KEY_PROPERTY, routingKey);
98 channelConf.put(QUEUE_NAME_PROPERTY, queueName);
99 return channelConf;
100 }
101
102 /**
103 * Returns a JSON representation of the given device event.
104 *
105 * @param event the device event
106 * @return the device event json message
107 */
108 public static JsonObject json(DeviceEvent event) {
109 JsonObject jo = new JsonObject();
110 jo.addProperty(SWITCH_ID, event.subject().id().toString());
111 jo.addProperty(INFRA_DEVICE_NAME, event.subject().type().name());
112 jo.addProperty(EVENT_TYPE, DEVICE_EVENT);
113 if (event.port() != null) {
114 jo.addProperty(PORT_NUMBER, event.port().number().toLong());
115 jo.addProperty(PORT_ENABLED, event.port().isEnabled());
116 jo.addProperty(PORT_SPEED, event.port().portSpeed());
117 jo.addProperty(SUB_EVENT_TYPE,
118 event.type().name() != null ? event.type().name() : null);
119 } else {
120 jo.addProperty(SUB_EVENT_TYPE,
121 event.type().name() != null ? event.type().name() : null);
122 }
123 jo.addProperty(HW_VERSION, event.subject().hwVersion());
124 jo.addProperty(MFR, event.subject().manufacturer());
125 jo.addProperty(SERIAL, event.subject().serialNumber());
126 jo.addProperty(SW_VERSION, event.subject().swVersion());
127 jo.addProperty(CHASIS_ID, event.subject().chassisId().id());
128 jo.addProperty(OCC_TIME, new Date(event.time()).toString());
129 return jo;
130 }
131
132 /**
133 * Returns a JSON representation of the given packet context.
134 *
135 * @param context the packet context
136 * @return the inbound packetjson message
137 */
138 public static JsonObject json(PacketContext context) {
139 JsonObject jo = new JsonObject();
140 InboundPacket pkt = context.inPacket();
141 // parse connection host
142 jo.addProperty(SWITCH_ID, pkt.receivedFrom().deviceId().toString());
143 jo.addProperty(IN_PORT, pkt.receivedFrom().port().name());
144 jo.addProperty(LOGICAL, pkt.receivedFrom().port().isLogical());
Ray Milkeyc108a6b2017-08-23 15:23:50 -0700145 jo.addProperty(RECEIVED, new Date(context.time()).toString());
ADARA Networks1fb1eb12016-09-01 12:04:07 -0700146 jo.addProperty(MSG_TYPE, PKT_TYPE);
147 // parse ethernet
148 jo.addProperty(SUB_MSG_TYPE,
149 EthType.EtherType.lookup(pkt.parsed().getEtherType()).name());
150 jo.addProperty(ETH_TYPE, pkt.parsed().getEtherType());
151 jo.addProperty(SRC_MAC_ADDR, pkt.parsed().getSourceMAC().toString());
152 jo.addProperty(DEST_MAC_ADDR, pkt.parsed().getDestinationMAC().toString());
153 jo.addProperty(VLAN_ID, pkt.parsed().getVlanID());
154 jo.addProperty(B_CAST, pkt.parsed().isBroadcast());
155 jo.addProperty(M_CAST, pkt.parsed().isMulticast());
156 jo.addProperty(PAD, pkt.parsed().isPad());
157 jo.addProperty(PRIORITY_CODE, pkt.parsed().getPriorityCode());
158 // parse bytebuffer
159 jo.addProperty(DATA_LEN, pkt.unparsed().array().length);
160 jo.addProperty(PAYLOAD, pkt.unparsed().asCharBuffer().toString());
161 return jo;
162 }
163
164 /**
165 * Returns a JSON representation of the given topology event.
166 *
167 * @param event the topology event
168 * @return the topology event json message
169 */
170 public static JsonObject json(TopologyEvent event) {
171 Topology topology = event.subject();
172 JsonObject jo = new JsonObject();
173 jo.addProperty(TOPO_TYPE, TopologyEvent.Type.TOPOLOGY_CHANGED.name());
174 jo.addProperty(CLUSTER_COUNT, topology.clusterCount());
175 jo.addProperty(COMPUTE_COST, topology.computeCost());
176 jo.addProperty(CREATE_TIME, new Date(topology.creationTime()).toString());
177 jo.addProperty(DEVICE_COUNT, topology.deviceCount());
178 jo.addProperty(LINK_COUNT, topology.linkCount());
179 jo.addProperty(AVAILABLE, new Date(topology.time()).toString());
180 return jo;
181 }
182
183 /**
184 * Returns a JSON representation of the given link event.
185 *
186 * @param event the link event
187 * @return the link event json message
188 */
189 public static JsonObject json(LinkEvent event) {
190 Link link = event.subject();
191 JsonObject jo = new JsonObject();
192 jo.addProperty(EVENT_TYPE, event.type().name());
193 jo.addProperty(DEST, link.dst().deviceId().toString());
194 jo.addProperty(SRC, link.src().deviceId().toString());
195 jo.addProperty(EXPECTED, link.isExpected());
196 jo.addProperty(STATE, link.state().name());
197 jo.addProperty(LINK_TYPE, link.type().name());
198 return jo;
199 }
200
201 /**
202 * Handles load mq property file from resources and returns Properties.
203 *
204 * @param context the component context
205 * @return the mq server properties
206 * @throws RuntimeException if property file not found.
207 */
208 public static Properties getProp(ComponentContext context) {
Ray Milkey780bb9b2017-12-18 16:14:52 -0800209 InputStream is;
ADARA Networks1fb1eb12016-09-01 12:04:07 -0700210 URL configUrl;
211 try {
212 configUrl = context.getBundleContext().getBundle()
213 .getResource(MQ_PROP_NAME);
Ray Milkey780bb9b2017-12-18 16:14:52 -0800214 is = configUrl.openStream();
ADARA Networks1fb1eb12016-09-01 12:04:07 -0700215 } catch (Exception ex) {
216 // This will be used only during junit test case since bundle
217 // context will be available during runtime only.
Ray Milkey780bb9b2017-12-18 16:14:52 -0800218 // FIXME - this should be configured with component config when running as a test
219 is = MQUtil.class.getClassLoader().getResourceAsStream(MQ_PROP_NAME);
ADARA Networks1fb1eb12016-09-01 12:04:07 -0700220 }
221
222 Properties properties;
223 try {
ADARA Networks1fb1eb12016-09-01 12:04:07 -0700224 properties = new Properties();
225 properties.load(is);
226 } catch (Exception e) {
227 log.error(ExceptionUtils.getFullStackTrace(e));
Ray Milkey2b4958a2018-02-06 18:59:06 -0800228 throw new IllegalStateException(e);
ADARA Networks1fb1eb12016-09-01 12:04:07 -0700229 }
230 return properties;
231 }
232}