Friday, April 25, 2014

Opendayligtht (ODL) Controller: Write Flow entries into the Switch Programmatically

public void addAllFlowsToSwitch(List afc, NodeBuilder nodeBuilder) {
        /*
         ActionBuilder > list > ApplyActionsBuilder > ApplyActionsCaseBuilder > InstructionBuilder > list > InstructionsBuilder > flowBuilder
         */
        for(Flow flow : afc) {
            if (flow != null) {
                System.out.println("Adding Flow=" + flow.toString());

                FlowBuilder ffb = new FlowBuilder();
                ffb.fieldsFrom(flow);

                //ffb.setTableId(flow.getTableId());

                ffb.setTableId((short)0x8);
                ffb.setCookie(BigInteger.valueOf(0x8));

                if(flow.getId() != null) {
                    //ffb.setId(new FlowId(flow.getId().getValue()));
                    ffb.setId(new FlowId("1234"));
                }
                //FlowKey fkey = new FlowKey(new FlowId(flow.getId().getValue()));
                FlowKey fkey = new FlowKey(new FlowId("1234"));
                ffb.setKey(fkey);

                DataModification, DataObject> modification = dataBrokerService.beginTransaction();

                //System.out.println("addAllFlowsToSwitch: tid=" + ffb.getTableId() + "fc= " + ffb.getCookie() + "fkey=" + ffb.getKey().toString());

                InstanceIdentifier flowRef = InstanceIdentifier.builder(Nodes.class)
                    .child(Node.class, nodeBuilder.getKey()).augmentation(FlowCapableNode.class)
                    .child(Table.class, new TableKey(ffb.getTableId())).child(Flow.class, ffb.getKey()).build();
                modification.putConfigurationData(nodeBuilderToInstanceId(nodeBuilder), nodeBuilder.build());
                modification.putConfigurationData(flowRef, ffb.build());

                Future> commitFuture = modification.commit();
                try {
                    RpcResult result = commitFuture.get();
                    TransactionStatus status = result.getResult();

                } catch (InterruptedException e) {
                    LOG.error(e.getMessage(), e);
                } catch (ExecutionException e) {
                    LOG.error(e.getMessage(), e);
                }
            }
        }
    }
We are using two-phase commit procedure to write Flows onto Switches.