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