package org.opendaylight.controller.topology;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;
import org.apache.commons.codec.binary.Base64;
import org.opendaylight.controller.sal.reader.FlowOnNode;
import org.opendaylight.controller.statistics.northbound.AllFlowStatistics;
import org.opendaylight.controller.statistics.northbound.FlowStatistics;
public class JAXBStatisticsClient {
public static void main(String[] args) {
System.out.println("Starting Statistics JAXB client.");
String baseURL = "http://127.0.0.1:8080/one/nb/v2/statistics";
String containerName = "default";
String user = "admin";
String password = "admin";
/*****************************************************
连接数据模块,打开链接
******************************************************/
URL url;
try {
url = new java.net.URL(baseURL + "/" + containerName + "/flowstats");
String authString = user + ":" + password;
byte[] authEncBytes = Base64.encodeBase64(authString.getBytes());
//String.getBytes 是返回一个字节数组 Base64.encodeBase64是对这个数组重新编码
String authStringEnc = new String(authEncBytes);
URLConnection connection = url.openConnection();
connection.setRequestProperty("Authorization", "Basic "
+ authStringEnc);
connection.setRequestProperty("Content-Type", "application/xml");
connection.setRequestProperty("Accept", "application/xml");
//设置头信息
connection.connect();
/*****************************************************
创建JAXB context来解组(unmarshalled)数据来导入到allflowstatics object中
******************************************************/
JAXBContext context = JAXBContext.newInstance(AllFlowStatistics.class);
Unmarshaller unmarshaller = context.createUnmarshaller();
InputStream inputStream = connection.getInputStream();
AllFlowStatistics result = (AllFlowStatistics) unmarshaller.unmarshal(inputStream);
System.out.println("We have these statistics:");
/*****************************************************
flowstatics object 用来取回数据,通过get方法来显示
******************************************************/
for (FlowStatistics statistics : result.getFlowStatistics()) {
System.out.println(statistics.getNode().getNodeIDString());
System.out.println(statistics.getNode().getType());
for (FlowOnNode flowOnNode : statistics.getFlowStat()) {
System.out.println("\t" + flowOnNode.getByteCount());
System.out.println("\t" + flowOnNode.getDurationNanoseconds());
System.out.println("\t" + flowOnNode.getDurationSeconds());
System.out.println("\t" + flowOnNode.getPacketCount());
System.out.println("\t" + flowOnNode.getTableId());
System.out.println("\t" + flowOnNode.getFlow());
}
}
} catch (Exception e) {
System.out.println(e.getLocalizedMessage());
}
}
}
/*********************************************************************
Jersey Statistics Client
*********************************************************************/
package org.opendaylight.controller.topology;
import org.opendaylight.controller.sal.reader.FlowOnNode;
import org.opendaylight.controller.statistics.northbound.AllFlowStatistics;
import org.opendaylight.controller.statistics.northbound.FlowStatistics;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.filter.HTTPBasicAuthFilter;
public class JerseyStatisticsClient {
public static void main(String[] args) {
System.out.println("Starting Topology JAXB client.");
String baseURL = "http://127.0.0.1:8080/one/nb/v2/statistics";
String containerName = "default";
String user = "admin";
String password = "admin";
/*******connect statistics module,open connection********/
try {
Client client = com.sun.jersey.api.client.Client.create();
client.addFilter(new HTTPBasicAuthFilter(user, password));
/*******创建jesery client示例data通过client获取传到allflowstatistic object********/
AllFlowStatistics result = client.resource(
baseURL + "/" + containerName + "/flowstats").get(
AllFlowStatistics.class);
/*********************************************************************
The AllFlowStatistics object is cast to a FlowStatistics object
*********************************************************************/
System.out.println("We have these statistics:");
for (FlowStatistics statistics : result.getFlowStatistics()) {
System.out.println(statistics.getNode().getNodeIDString());
System.out.println(statistics.getNode().getType());
for (FlowOnNode flowOnNode : statistics.getFlowStat()) {
System.out.println("\t" + flowOnNode.getByteCount());
System.out.println("\t"
+ flowOnNode.getDurationNanoseconds());
System.out.println("\t" + flowOnNode.getDurationSeconds());
System.out.println("\t" + flowOnNode.getPacketCount());
System.out.println("\t" + flowOnNode.getTableId());
System.out.println("\t" + flowOnNode.getFlow());
}
}
} catch (Exception e) {
System.out.println(e.getLocalizedMessage());
}
}
}
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;
import org.apache.commons.codec.binary.Base64;
import org.opendaylight.controller.sal.reader.FlowOnNode;
import org.opendaylight.controller.statistics.northbound.AllFlowStatistics;
import org.opendaylight.controller.statistics.northbound.FlowStatistics;
public class JAXBStatisticsClient {
public static void main(String[] args) {
System.out.println("Starting Statistics JAXB client.");
String baseURL = "http://127.0.0.1:8080/one/nb/v2/statistics";
String containerName = "default";
String user = "admin";
String password = "admin";
/*****************************************************
连接数据模块,打开链接
******************************************************/
URL url;
try {
url = new java.net.URL(baseURL + "/" + containerName + "/flowstats");
String authString = user + ":" + password;
byte[] authEncBytes = Base64.encodeBase64(authString.getBytes());
//String.getBytes 是返回一个字节数组 Base64.encodeBase64是对这个数组重新编码
String authStringEnc = new String(authEncBytes);
URLConnection connection = url.openConnection();
connection.setRequestProperty("Authorization", "Basic "
+ authStringEnc);
connection.setRequestProperty("Content-Type", "application/xml");
connection.setRequestProperty("Accept", "application/xml");
//设置头信息
connection.connect();
/*****************************************************
创建JAXB context来解组(unmarshalled)数据来导入到allflowstatics object中
******************************************************/
JAXBContext context = JAXBContext.newInstance(AllFlowStatistics.class);
Unmarshaller unmarshaller = context.createUnmarshaller();
InputStream inputStream = connection.getInputStream();
AllFlowStatistics result = (AllFlowStatistics) unmarshaller.unmarshal(inputStream);
System.out.println("We have these statistics:");
/*****************************************************
flowstatics object 用来取回数据,通过get方法来显示
******************************************************/
for (FlowStatistics statistics : result.getFlowStatistics()) {
System.out.println(statistics.getNode().getNodeIDString());
System.out.println(statistics.getNode().getType());
for (FlowOnNode flowOnNode : statistics.getFlowStat()) {
System.out.println("\t" + flowOnNode.getByteCount());
System.out.println("\t" + flowOnNode.getDurationNanoseconds());
System.out.println("\t" + flowOnNode.getDurationSeconds());
System.out.println("\t" + flowOnNode.getPacketCount());
System.out.println("\t" + flowOnNode.getTableId());
System.out.println("\t" + flowOnNode.getFlow());
}
}
} catch (Exception e) {
System.out.println(e.getLocalizedMessage());
}
}
}
/*********************************************************************
Jersey Statistics Client
*********************************************************************/
package org.opendaylight.controller.topology;
import org.opendaylight.controller.sal.reader.FlowOnNode;
import org.opendaylight.controller.statistics.northbound.AllFlowStatistics;
import org.opendaylight.controller.statistics.northbound.FlowStatistics;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.filter.HTTPBasicAuthFilter;
public class JerseyStatisticsClient {
public static void main(String[] args) {
System.out.println("Starting Topology JAXB client.");
String baseURL = "http://127.0.0.1:8080/one/nb/v2/statistics";
String containerName = "default";
String user = "admin";
String password = "admin";
/*******connect statistics module,open connection********/
try {
Client client = com.sun.jersey.api.client.Client.create();
client.addFilter(new HTTPBasicAuthFilter(user, password));
/*******创建jesery client示例data通过client获取传到allflowstatistic object********/
AllFlowStatistics result = client.resource(
baseURL + "/" + containerName + "/flowstats").get(
AllFlowStatistics.class);
/*********************************************************************
The AllFlowStatistics object is cast to a FlowStatistics object
*********************************************************************/
System.out.println("We have these statistics:");
for (FlowStatistics statistics : result.getFlowStatistics()) {
System.out.println(statistics.getNode().getNodeIDString());
System.out.println(statistics.getNode().getType());
for (FlowOnNode flowOnNode : statistics.getFlowStat()) {
System.out.println("\t" + flowOnNode.getByteCount());
System.out.println("\t"
+ flowOnNode.getDurationNanoseconds());
System.out.println("\t" + flowOnNode.getDurationSeconds());
System.out.println("\t" + flowOnNode.getPacketCount());
System.out.println("\t" + flowOnNode.getTableId());
System.out.println("\t" + flowOnNode.getFlow());
}
}
} catch (Exception e) {
System.out.println(e.getLocalizedMessage());
}
}
}