我正在编写一个Opendaylight应用程序,它将在删除,添加或更新时提取所有流规则。
要在添加,删除或更新流时获取通知,应用程序应提供扩展salFlowListener接口的侦听器。 但是,当我创建应用程序目录结构时,从在线的Opendaylight教程中不清楚逻辑的放置位置。
此外,使用YANG模型扩充通知服务时会出现编译错误。
这是获取通知的正确方法,是否可以参考任何明确的在线教程?
感谢。
答案 0 :(得分:0)
public class ChangeListener implements DataChangeListener {
private static final Logger logger = LoggerFactory.getLogger(DhcontrollerListener.class);
private DataBroker dataBroker;
public DhcontrollerListener(DataBroker broker) {
this.dataBroker = broker;
//flow
InstanceIdentifier<Flow> flowPath = InstanceIdentifier.builder(Nodes.class)
.child(Node.class).augmentation(FlowCapableNode.class).child(Table.class)
.child(Flow.class).build();
dataBroker.registerDataChangeListener(LogicalDatastoreType.OPERATIONAL, flowPath, this,
DataChangeScope.BASE);
}
@Override
public void onDataChanged(AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> change) {
if (change == null) {
logger.info("DhcontrollerListener ===>>> onDataChanged: change is null");
return;
}
handleCreatedFlow(change);
handleUpdatedFlow(change);
handleDeletedFlow(change);
}
private void handleCreatedFlow(AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> change) {
Map<InstanceIdentifier<?>, DataObject> createdData = change.getCreatedData();
if (createdData == null) {
logger.info("handleCreatedFlow ===>>> getRemovedPaths==null");
return;
}
for (Map.Entry<InstanceIdentifier<?>, DataObject> entry : createdData.entrySet()) {
final DataObject dataObject = entry.getValue();
if (dataObject instanceof Flow) {
Flow flow = (Flow) dataObject;
logger.info("create flow : " + flow.getCookie() + " " + flow.getKey().toString());
}
}
}
private void handleUpdatedFlow(AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> change) {
Map<InstanceIdentifier<?>, DataObject> updatedData = change.getCreatedData();
if (updatedData == null) {
logger.info("handleUpdatedFlow ===>>> getRemovedPaths==null");
return;
}
for (Map.Entry<InstanceIdentifier<?>, DataObject> entry : updatedData.entrySet()) {
final DataObject dataObject = entry.getValue();
if (dataObject instanceof Flow) {
Flow flow = (Flow) dataObject;
logger.info("update flow : " + flow.getCookie() + " " + flow.getKey().toString());
}
}
}
private void handleDeletedFlow(AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> change) {
Map<InstanceIdentifier<?>, DataObject> originalData = change.getOriginalData();
Set<InstanceIdentifier<?>> removedData = change.getRemovedPaths();
if (removedData == null) {
logger.info("handleDeletedFlow ===>>> getRemovedPaths==null");
return;
}
for (InstanceIdentifier<?> instanceIdentifier : removedData) {
final DataObject dataObject = originalData.get(instanceIdentifier);
if (dataObject instanceof Flow) {
Flow flow = (Flow) dataObject;
logger.info("remove flow : " + flow.getCookie() + " " + flow.getPriority() + " "
+ flow.getMatch().getIpMatch() + " " + flow.getKey().toString());
}
}
}
}
我该怎么做是设置InstanceIdentifier。设置不同的InstanceIdentifier,您还可以监听节点,表等的更改。