0

I am writing an Opendaylight application that will extract all the flow rules as and when it is deleted, added or updated.

To get the notifications when a flow is added, removed or updated, the application should provide a listener which extends the salFlowListener interface. However, when I create the application directory structure, it is not clear from the Opendaylight tutorials online as to where the logic is to be put.

Additionally, there are compilation errors when the notification-service is augmented using the YANG model.

Is this the right approach to getting the notifications and is any clear tutorials online that I can refer to?

Thanks.

4

1 回答 1

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,还可以监听节点、表等的变化。

于 2016-02-16T02:55:04.530 回答