六、OpenDaylight应用基础开发(DataStore)

1 DataStore相关概念

  1. ODL社区实现的内存数据库,其存储的数据结构是由YANG定义的树状的结构
  2. 基于事务的访问与操作
  3. 支持数据变更通知
  4. 支持事务链
  5. DataStore is an intelligent in-memory cache with tree-like structures that would be able to track dependencies, calculate change sets and maintain the relationships between commit handlers, notification listeners and the actual data.)

几个基本概念

  • Data Tree-All state-related data are modeled and represented as data tree, with possibility to address any element / subtree
    1. Operational Data Tree - Reported state of the system, published by the providers using MD-SAL. Represents a feedback loop for applications to observe state of the network / system.
    2. Configuration Data Tree - Intended state of the system or network, populated by consumers, which expresses their intention.
  • Instance Identifier
    1. Unique identifier of node / subtree in data tree, which provides unambiguous information, how to reference and retrieve node / subtree from conceptual data trees.
    2. Transaction - MD-SAL Data Broker provides transactional access to conceptual data trees representing configuration and operational state.

2 DataStore基于简单事务的读写操作

2.1 访问DataStore

 <reference id="dataBroker"
    interface="org.opendaylight.controller.md.sal.binding.api.DataBroker"
    odl:type="default" />

获取到OSGI的DataBroker服务接口

2.2 DataBroker服务接口

public interface DataBroker extends DataTreeChangeService {
    ReadOnlyTransaction newReadOnlyTransaction();
    ReadWriteTransaction newReadWriteTransaction();
    WriteTransaction newWriteOnlyTransaction();

    BindingTransactionChain  
        createTransactionChain(TransactionChainListener listener);
}

public interface DataTreeChangeService {
 ListenerRegistration<L> registerDataTreeChangeListener(@Nonnull 
        DataTreeIdentifier<T> treeId, @Nonnull L listener);
}

2.3 ReadOnlyTransaction

public interface ReadOnlyTransaction {

CheckedFuture<Optional<T>> read(LogicalDatastoreType store, InstanceIdentifier<T> path);

  @Override 
   void close();
}

2.4 WriteTransaction

public interface WriteTransaction {
 <T extends DataObject> void put(LogicalDatastoreType store, InstanceIdentifier<T> path, T data);

 <T extends DataObject> void merge(LogicalDatastoreType store, InstanceIdentifier<T> path, T data);
 <T extends DataObject> void merge(LogicalDatastoreType store, InstanceIdentifier<T> path, T data,            boolean createMissingParents);

@Override    
void delete(LogicalDatastoreType store, InstanceIdentifier<?> path);
 boolean cancel();
 CheckedFuture<Void,TransactionCommitFailedException> submit();
}

3 DataStore的数据变更通知

3.1 DataChangeEvent

注册监听

 ListenerRegistration<L> registerDataTreeChangeListener(@Nonnull 
        DataTreeIdentifier<T> treeId, @Nonnull L listener);

处理变更通知

3.2 数据变更监听接口

org.opendaylight.mdsal.binding.api. DataTreeChangeListener {
 void onDataTreeChanged(@Nonnull Collection<DataTreeModification<T>> changes);}
}

ClusterDataTreeChangeListener

最新版本里DataChangeListener已经被社区废弃。

4 事务链机制的介绍与使用

4.1 事务链设计初衷

数据操作的顺序保证

AsyncWriteTransaction t1 = broker.newWriteOnlyTransaction(); 
t1.put(id, data); 
t1.submit(); 
AsyncReadTransaction t2 = broker.newReadOnlyTransaction(); 
 Optional maybeData = t2.read(id).get(); 

4.2 实现原理简单介绍

事务链里的事务按序提交,每个事务可以看到前面的事务的操作结果。事务链不能保证事务链里的一连串事务的原子性,事务会按照提交的顺序被尽快提交(A chain of transactions. Transactions in a chain need to be committed in sequence and each transaction should see the effects of previous committed transactions as they occurred. A chain makes no guarantees of atomicity across the chained transactions - the transactions are committed as soon as possible in the order that they were submitted.)

4.3 TransactionChain实现原理简介

  • 事务创建 - 事务链创建事务时,会先判断前面的事务是否已提交,如果没提交,会直接返回错误。
  • 事务提交 – 事务提交是先在本地提交,不会立刻向集群中其他节点上的副本同步。如果此时有新事务创建请求,则处理新事务,如果空闲了,才会把本地提交同步到整个集群。

4.4 相关接口介绍

DataBroker:
 @Override    BindingTransactionChain create
TransactionChain(TransactionChainListener listener); 

public interface BindingTransactionChain  {
    @Override    ReadTransaction newReadOnlyTransaction();
    @Override    WriteTransaction newWriteOnlyTransaction();
}

5 实践练习

OpenDaylight+Mininet环境,加载feature odl-openflowplugin-flow-services
编程实现监听opendaylight-inventory的node节点
编程实现监听到交换机节点上线(node节点创建),下发一条默认上送所有未匹配到的报文到控制器的流表(通过写库方式实现)
监听PacketRecieved消息,打印PacketIn报文

解析PacketIn报文,对于解析出的LLDP报文,生成网络拓扑信息,把link通过事务链的方式写入network-topology中

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值