+ this.initializeFlowRules(node, AdminConfigManager.getManager().getIntegrationBridgeName());
+ }
+
+ private void initializeFlowRules(Node node, String bridgeName) {
+ try {
+ // TODO : 3 second sleep hack is to make sure the OF connection is established.
+ // Correct fix is to check the MD-SAL inventory before proceeding and listen
+ // to Inventory update for processing.
+ Thread.sleep(3000);
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ String brIntId = this.getInternalBridgeUUID(node, bridgeName);
+ if (brIntId == null) {
+ logger.error("Failed to initialize Flow Rules for {}", node);
+ return;
+ }
+
+ try {
+ OVSDBConfigService ovsdbTable = (OVSDBConfigService)ServiceHelper.getGlobalInstance(OVSDBConfigService.class, this);
+ Bridge bridge = (Bridge) ovsdbTable.getRow(node, Bridge.NAME.getName(), brIntId);
+ Set<String> dpids = bridge.getDatapath_id();
+ if (dpids == null || dpids.size() == 0) return;
+ Long dpidLong = Long.valueOf(HexEncode.stringToLong((String)dpids.toArray()[0]));
+ writeLLDPRule(dpidLong);
+ } catch (Exception e) {
+ logger.error("Failed to initialize Flow Rules for "+node.toString(), e);
+ }
+ }
+
+ private void writeLLDPRule(Long dpidLong) {
+ String nodeName = "openflow:"+dpidLong;
+ NodeBuilder tn = createNodeBuilder(nodeName);
+ FlowBuilder flow = new FlowBuilder();
+ flow.setMatch(createLLDPMatch().build());
+ flow.setInstructions(this.createSentToControllerInstructions().build());
+ // TODO : Investigate the need for this.
+ FlowKey key = new FlowKey(new FlowId(new Long(123)));
+ flow.setBarrier(false);
+ flow.setTableId((short)0);
+ flow.setKey(key);
+ flow.setFlowName("LLDP_" + nodeName);
+ writeFlow(flow, tn);
+ }
+
+ private void writeFlow(FlowBuilder flow, NodeBuilder nodeBuilder) {
+ IMDSALConsumer mdsalConsumer = (IMDSALConsumer)ServiceHelper.getInstance(IMDSALConsumer.class, "default", this);
+ if (mdsalConsumer == null) {
+ logger.error("ERROR finding MDSAL Service. Its possible that writeFlow is called too soon ?");
+ return;
+ }
+
+ dataBrokerService = mdsalConsumer.getDataBrokerService();
+
+ if (dataBrokerService == null) {
+ logger.error("ERROR finding reference for DataBrokerService. Please check out the MD-SAL support on the Controller.");
+ return;
+ }
+ DataModification<InstanceIdentifier<?>, DataObject> modification = dataBrokerService.beginTransaction();
+ InstanceIdentifier<Flow> path1 = InstanceIdentifier.builder(Nodes.class)
+ .child(org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node.class, nodeBuilder.getKey()).augmentation(FlowCapableNode.class)
+ .child(Table.class, new TableKey(flow.getTableId())).child(Flow.class, flow.getKey())
+ .build();
+ modification.putOperationalData(nodeBuilderToInstanceId(nodeBuilder), nodeBuilder.build());
+ modification.putOperationalData(path1, flow.build());
+ modification.putConfigurationData(nodeBuilderToInstanceId(nodeBuilder), nodeBuilder.build());
+ modification.putConfigurationData(path1, flow.build());
+ Future<RpcResult<TransactionStatus>> commitFuture = modification.commit();
+ try {
+ RpcResult<TransactionStatus> result = commitFuture.get();
+ TransactionStatus status = result.getResult();
+ } catch (InterruptedException e) {
+ logger.error(e.getMessage(), e);
+ } catch (ExecutionException e) {
+ logger.error(e.getMessage(), e);
+ }
+ }
+
+ private static MatchBuilder createLLDPMatch() {
+ MatchBuilder match = new MatchBuilder();
+ EthernetMatchBuilder eth = new EthernetMatchBuilder();
+ EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
+ ethTypeBuilder.setType(new EtherType(0x88CCL));
+ eth.setEthernetType(ethTypeBuilder.build());
+ match.setEthernetMatch(eth.build());
+ return match;
+ }
+
+ private InstructionsBuilder createSentToControllerInstructions() {
+ List<Action> actionList = new ArrayList<Action>();
+ ActionBuilder ab = new ActionBuilder();
+
+ OutputActionBuilder output = new OutputActionBuilder();
+ output.setMaxLength(56);
+ Uri value = new Uri("CONTROLLER");
+ output.setOutputNodeConnector(value);
+ ab.setAction(new OutputActionCaseBuilder().setOutputAction(output.build()).build());
+ ab.setOrder(0);
+ ab.setKey(new ActionKey(0));
+ actionList.add(ab.build());
+ // Create an Apply Action
+ ApplyActionsBuilder aab = new ApplyActionsBuilder();
+ aab.setAction(actionList);
+
+ // Wrap our Apply Action in an Instruction
+ InstructionBuilder ib = new InstructionBuilder();
+ ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
+ ib.setOrder(0);
+ ib.setKey(new InstructionKey(0));
+
+ // Put our Instruction in a list of Instructions
+ InstructionsBuilder isb = new InstructionsBuilder();
+ List<Instruction> instructions = new ArrayList<Instruction>();
+ instructions.add(ib.build());
+ isb.setInstruction(instructions);
+ return isb;