opendaylight sample2

本文介绍了如何利用JAXB和Jersey客户端从远程服务器获取网络设备的流量统计信息,包括节点ID、类型、字节计数、持续时间等详细数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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());
 }
 }
}































































评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值