Package org.apache.avro.generic

Examples of org.apache.avro.generic.GenericRecordBuilder


   }
  
   private byte[] encodeMessage (Schema headerSchema, Schema bodySchema) {
     
      GenericRecord bodyRecord = new GenericData.Record(bodySchema);
      GenericRecordBuilder builder = new GenericRecordBuilder(headerSchema);

      GenericRecord headerRecord = builder.build();
      bodyRecord.put("header", headerRecord)
     
      ByteArrayOutputStream out = new ByteArrayOutputStream();
     
      DatumWriter<GenericRecord> writer = new GenericDatumWriter<GenericRecord>(bodySchema);
View Full Code Here


   }
  
   public ByteArrayOutputStream getHello(ByteArrayOutputStream out) {
     
      GenericRecord ofpHelloRecord = new GenericData.Record(ofpHelloSchema);
      GenericRecordBuilder builder = new GenericRecordBuilder(helloHeaderSchema);

      GenericRecord ofpHelloHeaderRecord = builder.build();
      ofpHelloRecord.put("header", ofpHelloHeaderRecord)
     
      DatumWriter<GenericRecord> writer = new GenericDatumWriter<GenericRecord>(ofpHelloSchema);
      Encoder encoder = EncoderFactory.get().binaryNonEncoder(out, null);
     
View Full Code Here

   }

   public ByteArrayOutputStream getSwitchFeaturesRequest(ByteArrayOutputStream out) {
     
      GenericRecord ofpSwitchFeaturesRequestRecord = new GenericData.Record(ofpSwitchFeaturesRequestSchema);
      GenericRecordBuilder builder = new GenericRecordBuilder(switchFeaturesRequestHeaderSchema);

      GenericRecord ofpSwitchFeaturesRequestHeaderRecord = builder.build();
      ofpSwitchFeaturesRequestRecord.put("header", ofpSwitchFeaturesRequestHeaderRecord)
     
      DatumWriter<GenericRecord> writer = new GenericDatumWriter<GenericRecord>(ofpSwitchFeaturesRequestSchema);
      Encoder encoder = EncoderFactory.get().binaryNonEncoder(out, null);
     
View Full Code Here

    }
  
   public ByteArrayOutputStream getSwitchFeaturesReply(ByteArrayOutputStream out) {
     
      GenericRecord ofpSwitchFeaturesReplyRecord = new GenericData.Record(ofpSwitchFeaturesSchema);
      GenericRecordBuilder builder = new GenericRecordBuilder(switchFeaturesHeaderSchema);
     
      GenericRecord ofpSwitchFeaturesReplyHeaderRecord = builder.build();
      ofpSwitchFeaturesReplyRecord.put("header", ofpSwitchFeaturesReplyHeaderRecord)
     
      DatumWriter<GenericRecord> writer = new GenericDatumWriter<GenericRecord>(ofpSwitchFeaturesSchema);
      Encoder encoder = EncoderFactory.get().binaryNonEncoder(out, null);
     
View Full Code Here

  
   public ByteArrayOutputStream getSetSwitchConfig(ByteArrayOutputStream out) {
     
      GenericRecord ofpSetSwitchConfigRecord = new GenericData.Record(ofpSetSwitchConfigSchema);
     
      GenericRecordBuilder builder = new GenericRecordBuilder(ofpSetSwitchConfigHeaderSchema);
      GenericRecord ofpSetSwitchConfigHeaderRecord = builder.build();
     
      ofpSetSwitchConfigRecord.put("header", ofpSetSwitchConfigHeaderRecord);
     
      byte[] fl = {0,0};
      GenericData.Fixed flags = new GenericData.Fixed(ofpSetSwitchConfigSchema, fl);
View Full Code Here

   }
  
   public ByteArrayOutputStream getSwitchConfigRequest(ByteArrayOutputStream out) {
     
      GenericRecord ofpGetConfigRequestRecord = new GenericData.Record(ofpGetConfigRequestSchema);
      GenericRecordBuilder builder = new GenericRecordBuilder(getConfigRequestHeaderSchema);
     
      GenericRecord ofpGetConfigRequestHeaderRecord = builder.build();
      ofpGetConfigRequestRecord.put("header", ofpGetConfigRequestHeaderRecord)
     
      DatumWriter<GenericRecord> writer = new GenericDatumWriter<GenericRecord>(ofpGetConfigRequestSchema);
      Encoder encoder = EncoderFactory.get().binaryNonEncoder(out, null);
     
View Full Code Here

   }
  
   public byte[] encodeSwitchConfigRequest () {
     
      GenericRecord ofpGetConfigRequestRecord = new GenericData.Record(ofpGetConfigRequestSchema);
      GenericRecordBuilder builder = new GenericRecordBuilder(getConfigRequestHeaderSchema);
     
      GenericRecord ofpGetConfigRequestHeaderRecord = builder.build();
      ofpGetConfigRequestRecord.put("header", ofpGetConfigRequestHeaderRecord)
     
      ByteArrayOutputStream out = new ByteArrayOutputStream();
      DatumWriter<GenericRecord> writer = new GenericDatumWriter<GenericRecord>(ofpGetConfigRequestSchema);
      Encoder encoder = EncoderFactory.get().binaryNonEncoder(out, null);
View Full Code Here

    public byte[] encodeFlowMod (OFMessageFlowModRef fmRef) {
     
      GenericRecord ofpFlowModRecord = new GenericData.Record(ofpFlowModSchema);
     
      GenericRecordBuilder builder = null;

      builder = new GenericRecordBuilder (flowModHeaderSchema);
      GenericRecord flowModHeaderRecord = builder.build();
     
      Schema flowModBodySchema = protocol.getType("of13.flow_mod_body_add");
      builder = new GenericRecordBuilder(flowModBodySchema);
      GenericRecord flowModBodyRecord = builder.build();
     
      GenericRecord ofpMatchRecord = null;
      Schema ofpMatchSchema = null;
     
      List <GenericRecord> instructions = new ArrayList<>();
      List <GenericRecord> matches = new ArrayList<>();
     
   // TODO Improvs: I dislike this *.getMatches().getIterator();
//      Iterator<Tuple<String, String>> matchIter = fmRef.getMatches().getIterator();
      List<Tuple<String, String>> matchList = fmRef.getMatches().getMatches();
      for (Tuple<String, String> match : matchList) {
         //Tuple<String, String> match = matchIter.next();
         String name = match.getName();
        
         // TODO Improvs: Replace Switch with a structure... say, HashMap
         // TODO Improvs: How to control type compatibility between Shema types and incoming tlvs?
         switch (name) {
         case "ingress_port":
            matches.add(getMatch("of13.oxm_tlv_ingress_port", getUint32Fixed(Integer.parseInt(match.getValue()))));
            break;
        
         case "in_phy_port":
            matches.add(getMatch("of13.oxm_tlv_in_phy_port", getUint32Fixed(Integer.parseInt(match.getValue()))));
            break;

         case "metadata":
            matches.add(getMatch("of13.oxm_tlv_metadata", getUint64Fixed(Long.parseLong(match.getValue()))));
            break;

         case "eth_dst":
            matches.add(getMatch("of13.oxm_tlv_eth_dst", getUint48Fixed(get_mac_addr(match.getValue()))));
            break;
           
         case "eth_src":
            matches.add(getMatch("of13.oxm_tlv_eth_src", getUint48Fixed(get_mac_addr(match.getValue()))));
            break;

         case "eth_type":
            short ethType = U16.t(Integer.valueOf(match.getValue().replaceFirst("0x", ""), 16));
            matches.add(getMatch("of13.oxm_tlv_eth_type", getUint32Fixed(ethType)));
            break;           

         case "vlan_vid":
            short vid = U16.t(Integer.valueOf(match.getValue()));
            matches.add(getMatch("of13.oxm_tlv_vlan_vid", getUint32Fixed(vid)));
            break;           
        
         case "vlan_pcp":
            short pcp = U16.t(Integer.valueOf(match.getValue()));
            matches.add(getMatch("of13.oxm_tlv_vlan_vid", getUint32Fixed(pcp)));
            break;           

         case "ip_dscp":
            byte tmp = U8.t(Short.valueOf(match.getValue()));
            matches.add(getMatch("of13.oxm_tlv_ip_dscp", getUint8Fixed(tmp)));
            break;

         case "ip_ecn":
            tmp = U8.t(Short.valueOf(match.getValue()));
            matches.add(getMatch("of13.oxm_tlv_ip_ecn", getUint8Fixed(tmp)));
            break;

         case "ip_proto":
            tmp = U8.t(Short.valueOf(match.getValue()));
            matches.add(getMatch("of13.oxm_tlv_ip_proto", getUint8Fixed(tmp)));
            break;

         case "ipv4_src":
            matches.add(getMatch("of13.oxm_tlv_ipv4_src", getUint32Fixed(get_ipv4(match.getValue()))));
            break;

         case "ipv4_dst":
            matches.add(getMatch("of13.oxm_tlv_ipv4_dst", getUint32Fixed(get_ipv4(match.getValue()))));
            break;

         case "tcp_src":
            matches.add(getMatch("of13.oxm_tlv_tcp_src", getUint16Fixed(U16.t(Integer.valueOf(match.getValue())))));
            break;
           
         case "tcp_dst":
            matches.add(getMatch("of13.oxm_tlv_tcp_dst", getUint16Fixed(U16.t(Integer.valueOf(match.getValue())))));
            break;

         case "udp_src":
            matches.add(getMatch("of13.oxm_tlv_udp_src", getUint16Fixed(U16.t(Integer.valueOf(match.getValue())))));
            break;

         case "udp_dst":
            matches.add(getMatch("of13.oxm_tlv_udp_dst", getUint16Fixed(U16.t(Integer.valueOf(match.getValue())))));
            break;

         case "sctp_src":
            matches.add(getMatch("of13.oxm_tlv_sctp_src", getUint16Fixed(U16.t(Integer.valueOf(match.getValue())))));
            break;

         case "sctp_dst":
            matches.add(getMatch("of13.oxm_tlv_sctp_dst", getUint16Fixed(U16.t(Integer.valueOf(match.getValue())))));
            break;
           
         case "icmpv4_type":
            tmp = U8.t(Short.valueOf(match.getValue()));
            matches.add(getMatch("of13.oxm_tlv_icmpv4_type", getUint8Fixed(tmp)));
            break;

         case "icmpv4_code":
            tmp = U8.t(Short.valueOf(match.getValue()));
            matches.add(getMatch("of13.oxm_tlv_icmpv4_code", getUint8Fixed(tmp)));
            break;

         case "arp_op":
            matches.add(getMatch("of13.oxm_tlv_arp_op", getUint16Fixed(U16.t(Integer.parseInt(match.getValue())))));
            break;

         case "arp_spa":
            matches.add(getMatch("of13.oxm_tlv_arp_spa", getUint32Fixed(Integer.parseInt(match.getValue()))));
            break;

         case "arp_tpa":
            matches.add(getMatch("of13.oxm_tlv_arp_tpa", getUint32Fixed(Integer.parseInt(match.getValue()))));
            break;
           
         case "arp_sha":
            matches.add(getMatch("of13.oxm_tlv_arp_sha", getUint48Fixed(Long.parseLong(match.getValue()))));
            break;
           
         case "arp_tha":
            matches.add(getMatch("of13.oxm_tlv_arp_tha", getUint48Fixed(Long.parseLong(match.getValue()))));
            break;

         case "ipv6_src":
            matches.add(getMatch("of13.oxm_tlv_ipv6_src", getUint128Fixed(get_ipv6(match.getValue()))));
            break;

         case "ipv6_dst":
            matches.add(getMatch("of13.oxm_tlv_ipv6_dst", getUint128Fixed(get_ipv6(match.getValue()))));
            break;

         case "ipv6_flabel":
            matches.add(getMatch("of13.oxm_tlv_ipv6_flabel", getUint32Fixed(Integer.parseInt(match.getValue()))));
            break;

         case "icmpv6_type":
            tmp = U8.t(Short.valueOf(match.getValue()));
            matches.add(getMatch("of13.oxm_tlv_icmpv6_type", getUint8Fixed(tmp)));
            break;
        
         case "icmpv6_code":
            tmp = U8.t(Short.valueOf(match.getValue()));
            matches.add(getMatch("of13.oxm_tlv_icmpv6_code", getUint8Fixed(tmp)));
            break;

         case "ipv6_nd_target":
            matches.add(getMatch("of13.oxm_tlv_ipv6_nd_target", getUint128Fixed(get_ipv6(match.getValue()))));
            break;

         case "ipv6_nd_sll":
            matches.add(getMatch("of13.oxm_tlv_ipv6_nd_sll", getUint48Fixed(Long.parseLong(match.getValue()))));
            break;

         case "ipv6_nd_tll":
            matches.add(getMatch("of13.oxm_tlv_ipv6_nd_tll", getUint48Fixed(Long.parseLong(match.getValue()))));
            break;

         case "mpls_label":
            matches.add(getMatch("of13.oxm_tlv_mpls_label", getUint32Fixed(Integer.parseInt(match.getValue()))));
            break;

         case "mpls_tc":
            tmp = U8.t(Short.valueOf(match.getValue()));
            matches.add(getMatch("of13.oxm_tlv_mpls_tc", getUint8Fixed(tmp)));
            break;

         case "mpls_bos":
            tmp = U8.t(Short.valueOf(match.getValue()));
            matches.add(getMatch("of13.oxm_tlv_mpls_bos", getUint8Fixed(tmp)));
            break;

         case "pbb_isid":
            matches.add(getMatch("of13.oxm_tlv_pbb_isid", getUint32Fixed(Integer.parseInt(match.getValue()))));
            break;

         case "tunnel_id":
            matches.add(getMatch("of13.oxm_tlv_tunnel_id", getUint32Fixed(Integer.parseInt(match.getValue()))));
            break;

         case "ipv6_exthdr":
            matches.add(getMatch("of13.oxm_tlv_ipv6_exthdr", getUint32Fixed(Integer.parseInt(match.getValue()))));
            break;
           
         default:
            break;

         }
      }
     
      Schema oxmTlvSchema = protocol.getType("of13.oxm_tlv");
     
      Schema oxmTlvFieldsSchema = protocol.getType("of13.oxm_tlv_fields");
      GenericRecord oxmTlvFieldsRecord = new GenericData.Record(oxmTlvFieldsSchema);
      oxmTlvFieldsRecord.put("oxm_tlvs", new GenericData.Array<>(Schema.createArray(oxmTlvSchema), matches));

      /*
       * Build match header
       */
      Schema matchHeaderSchema = protocol.getType("of13.match_header");
      GenericRecordBuilder matchHeaderBuilder = new GenericRecordBuilder(matchHeaderSchema);
      GenericRecord matchHeaderRecord = matchHeaderBuilder.build();
     
      /*
       * Calculating oxm_tlvs length
       */
      ByteArrayOutputStream oxmOut = new ByteArrayOutputStream();
      DatumWriter<GenericRecord> oxmWriter = new GenericDatumWriter<GenericRecord>(oxmTlvFieldsSchema);
      Encoder oxmEncoder = EncoderFactory.get().binaryNonEncoder(oxmOut, null);
     
      int closingPadLength = 4;
     
      try {
         oxmWriter.write(oxmTlvFieldsRecord, oxmEncoder);
         oxmEncoder.flush();
        
         int matchLength = oxmOut.size() + 4;
         closingPadLength = (int) ((matchLength + 7)/8*8 - matchLength);
        
         Schema uint16Schema = protocol.getType("of13.uint_16");
        
         byte len[] = {(byte)(matchLength >> 8), (byte)(255 & matchLength)};
         GenericData.Fixed lenght = new GenericData.Fixed(uint16Schema, len);
        
         matchHeaderRecord.put("length", lenght);
      } catch (IOException e1) {
         // TODO Auto-generated catch block
         e1.printStackTrace();
      }     
     
      /*
       * Build closing pad
       */
      ByteBuffer clP = ByteBuffer.allocate(closingPadLength);
//      GenericData.Fixed closingPadRecord = new GenericData.Fixed(Schema.createFixed("", "", "of", closingPadLength), clP.toByteArray());
 
      /*
       * Assemble ofp_match structure
       */
      ofpMatchSchema = protocol.getType("of13.ofp_match");
      ofpMatchRecord = new GenericData.Record(ofpMatchSchema);
      ofpMatchRecord.put("header", matchHeaderRecord);
      ofpMatchRecord.put("fields", oxmTlvFieldsRecord);
      ofpMatchRecord.put("closing_pad", clP);
     
     
      // TODO Improvs: I dislike this *.getInstructions().getIterator();
      //Iterator<Tuple<String, OFStructureInstruction>> instrIter = fmRef.getInstructions().getIterator();
      List<Tuple<String, OFStructureInstructionRef>> instrList = fmRef.getInstructions().getInstructions();
      Schema instrHeaderSchema = null;
      Schema instrSchema = null;
      GenericRecord instrHeaderRecord = null;
      GenericRecord instrRecord = null;
      for (Tuple<String, OFStructureInstructionRef> tuple : instrList) {
         boolean isActions = false;
         //Tuple<String, OFStructureInstruction> tuple = instrIter.next();
         String name = tuple.getName();
         OFStructureInstructionRef instruction = tuple.getValue();
        
         // TODO Improvs: Replace Switch with a structure... say, HashMap
         // TODO Improvs: How to control type compatibility between Schema types and incoming tlvs?
         switch (name) {
         case "apply_actions":
            instrHeaderSchema = protocol.getType("of13.instruction_apply_actions_header");
            instrSchema = protocol.getType("of13.ofp_instruction_apply_actions");
            instrRecord = new GenericData.Record(instrSchema);
            isActions = true;
            break;
         case "write_actions":
            instrHeaderSchema = protocol.getType("of13.instruction_write_actions_header");
            instrSchema = protocol.getType("of13.ofp_instruction_write_actions");
            instrRecord = new GenericData.Record(instrSchema);
            isActions = true;           
            break;
         case "clear_actions":
            instrHeaderSchema = protocol.getType("of13.instruction_clear_actions_header");
            instrSchema = protocol.getType("of13.ofp_instruction_clear_actions");
            instrRecord = new GenericData.Record(instrSchema);
            isActions = true;           
            break;
         case "goto_table":
            instrHeaderSchema = protocol.getType("of13.instruction_goto_table_header");
            instrSchema = protocol.getType("of13.ofp_instruction_goto_table");
            instrRecord = new GenericData.Record(instrSchema);
            instrRecord.put("table_id", getUint8Fixed((byte)15));
            instrRecord.put("pad", getPad3(0));
            isActions = false;           
            break;
         case "write_metadata":
            instrHeaderSchema = protocol.getType("of13.instruction_write_metadata_header")
            instrSchema = protocol.getType("of13.ofp_instruction_write_metadata");
            instrRecord = new GenericData.Record(instrSchema);
            isActions = false;           
            break;
         case "meter":
            instrHeaderSchema = protocol.getType("of13.instruction_meter_header");
            instrSchema = protocol.getType("of13.ofp_instruction_meter");
            instrRecord = new GenericData.Record(instrSchema);
            isActions = false;           
            break;
         }
        
         GenericRecordBuilder instrHeaderBuilder = new GenericRecordBuilder(instrHeaderSchema);
         instrHeaderRecord = instrHeaderBuilder.build();
         instrRecord.put("header", instrHeaderRecord);
        

         if (isActions) {
            List<Tuple<String, String>> actionList = instruction.getActions().getActions();
//            Iterator<Tuple<String, String>> actionIter = instruction.getActions().getIterator();
            Schema ofpActionSchema = protocol.getType("of13.ofp_action");
            List<GenericRecord> actions = new LinkedList<GenericRecord>();
            GenericRecord actionSetRecord = null;

            for (Tuple<String, String> action : actionList) {
//               Tuple<String, String> action = actionIter.next();
               String actName = action.getName();

               GenericRecord ofpActionRecord = new GenericData.Record(ofpActionSchema);
              
               switch (actName) {
               case "output":
                  Schema ofpActionOutSchema = protocol.getType("of13.ofp_action_output");
                  GenericRecordBuilder actionBuilder = new GenericRecordBuilder(ofpActionOutSchema);
                  GenericRecord ofpActionOutRecord = actionBuilder.build();
                 
                  ofpActionOutRecord.put("port", getUint32Fixed(Integer.decode(action.getValue())));
                  ofpActionRecord.put("action", ofpActionOutRecord);

                  break;
View Full Code Here

      else {
         flowModBodySchema = protocol.getType("of13.flow_mod_body_add");
         isDelete = false;
      }
     
      GenericRecordBuilder flowModBodyBuilder = new GenericRecordBuilder(flowModBodySchema);
      GenericRecord flowModBodyRecord = flowModBodyBuilder.build();
      GenericRecord actionSetRecord = null; // TODO Empty actions instead of null!
     
      List <GenericRecord> instructions = new ArrayList<>();
      List <GenericRecord> matches = new ArrayList<>();
     
      for (String key : args.keySet()) {
         if (args.get(key) == null)
            continue;
        
         /*
          * PRIORITY
          */
         if (key.equals("priority")) {
            short priority = U16.t(Integer.valueOf((String) args.get(key)));
            byte temp [] = {(byte)(priority >> 8), (byte)(priority) };
           
            GenericData.Fixed priorityFixed = new GenericData.Fixed(uint_16Schema, temp);
         
            flowModBodyRecord.put("priority", priorityFixed);
         /*
          * ACTIONS  
          */
         } else if ((key.equals("goto_table")) || (key.equals("write_metadata")) || (key.equals("meter"))) {
            continue;
           
         } else if ( (key.equals("apply_actions")) || (key.equals("write_actions")) || (key.equals("clear_actions"))) {
            actionSetRecord = parseActionString((String) args.get(key));
           
            /*
             * Build Write Actions Instruction for Test
             */
            Schema instrHeaderSchema = null;
            Schema instrSchema = null;
            GenericRecord instrHeaderRecord = null;
            GenericRecord instrRecord = null;
           
            if (key.equals("apply_actions")) {
               instrHeaderSchema = protocol.getType("of13.instruction_apply_actions_header");
               instrSchema = protocol.getType("of13.ofp_instruction_apply_actions");
               instrRecord = new GenericData.Record(instrSchema);
            else if (key.equals("write_actions")) {
               instrHeaderSchema = protocol.getType("of13.instruction_write_actions_header");
               instrSchema = protocol.getType("of13.ofp_instruction_write_actions");
               instrRecord = new GenericData.Record(instrSchema);
            } else if (key.equals("clear_actions")) {
               instrHeaderSchema = protocol.getType("of13.instruction_clear_actions_header");
               instrSchema = protocol.getType("of13.ofp_instruction_clear_actions");
               instrRecord = new GenericData.Record(instrSchema);
            }
           
            GenericRecordBuilder instrHeaderBuilder = new GenericRecordBuilder(instrHeaderSchema);
            instrHeaderRecord = instrHeaderBuilder.build();
            instrRecord.put("header", instrHeaderRecord);
            instrRecord.put("actions", actionSetRecord);
           
            instrHeaderRecord.put("length", getUint16Fixed(calculateLength(instrSchema, instrRecord)));
            instrRecord.put("header", instrHeaderRecord);
           
            /*
             * Create ofp_instruction_write_actions
             */

            Schema ofpInstrSchema = protocol.getType("of13.ofp_instruction");
            GenericRecord ofpInstrRecord = new GenericData.Record(ofpInstrSchema);

            ofpInstrRecord.put("instruction", instrRecord);
            instructions.add(ofpInstrRecord);
         /*
          * MATCH - INGRESS PORT
          */
         } else if (key.equals("in_port")) {
           
           
            /*Schema oxmTlvIngressPortSchema = protocol.getType("of13.oxm_tlv_ingress_port");
            GenericRecord oxmTlvIngressPortRecord = new GenericData.Record(oxmTlvIngressPortSchema);
        
            int inPort = Integer.valueOf((String) args.get(key));
            oxmTlvIngressPortRecord.put("tlv", getUint32Fixed(inPort));
           
            Schema oxmTlvSchema = protocol.getType("of13.oxm_tlv");
            GenericRecord oxmTlvRecord = new GenericData.Record(oxmTlvSchema);
            oxmTlvRecord.put("match", oxmTlvIngressPortRecord);*/
           
            matches.add(getMatch("of13.oxm_tlv_ingress_port", getUint32Fixed(Integer.valueOf((String) args.get(key)))));
           
            /*
             * MATCH - IN_PHY PORT
             */
         } else if (key.equals("in_phy_port")) {
            Schema oxmTlvInPhyPortSchema = protocol.getType("of13.oxm_tlv_in_phy_port");
            GenericRecord oxmTlvInPhyPortRecord = new GenericData.Record(oxmTlvInPhyPortSchema);
              
            int inPort = Integer.valueOf((String) args.get(key));

            oxmTlvInPhyPortRecord.put("tlv", getUint32Fixed(inPort));
              
            Schema oxmTlvSchema = protocol.getType("of13.oxm_tlv");
            GenericRecord oxmTlvRecord = new GenericData.Record(oxmTlvSchema);
            oxmTlvRecord.put("match", oxmTlvInPhyPortRecord);
              
            matches.add(oxmTlvRecord);
           
            /*
             * MATCH - METADATA
             */
         } else if (key.equals("metadata")) {
            Schema oxmTlvMetadataSchema = protocol.getType("of13.oxm_tlv_metadata");
            GenericRecord oxmTlvMetadataRecord = new GenericData.Record(oxmTlvMetadataSchema);
              
            long mdata = Integer.valueOf((String) args.get(key));
            oxmTlvMetadataRecord.put("tlv", getUint64Fixed(mdata));
              
            Schema oxmTlvSchema = protocol.getType("of13.oxm_tlv");
            GenericRecord oxmTlvRecord = new GenericData.Record(oxmTlvSchema);
            oxmTlvRecord.put("match", oxmTlvMetadataRecord);
              
            matches.add(oxmTlvRecord);
           
           /*
            * MATCH - ETH_SRC
            */
         } else if (key.equals("dl_src")) {

            Schema oxmTlvFieldSchema = protocol.getType("of13.oxm_tlv_eth_src");
            GenericRecord oxmTlvFieldRecord = new GenericData.Record(oxmTlvFieldSchema);
           
            oxmTlvFieldRecord.put("tlv", getUint48Fixed(get_mac_addr((String) args.get(key))));
            Schema oxmTlvSchema = protocol.getType("of13.oxm_tlv");
            GenericRecord oxmTlvRecord = new GenericData.Record(oxmTlvSchema);
            oxmTlvRecord.put("match", oxmTlvFieldRecord);
           
            matches.add(oxmTlvRecord);

          /*
           * MATCH - ETH_DST
           */
      } else if (key.equals("dl_dst")) {
     
         Schema oxmTlvFieldSchema = protocol.getType("of13.oxm_tlv_eth_dst");
         GenericRecord oxmTlvFieldRecord = new GenericData.Record(oxmTlvFieldSchema);

         oxmTlvFieldRecord.put("tlv", getUint48Fixed(get_mac_addr((String) args.get(key))));
         Schema oxmTlvSchema = protocol.getType("of13.oxm_tlv");
         GenericRecord oxmTlvRecord = new GenericData.Record(oxmTlvSchema);
         oxmTlvRecord.put("match", oxmTlvFieldRecord);
        
         matches.add(oxmTlvRecord);
         
         /*
          * MATCH - ETH_TYPE
          */
      } else if (key.equals("dl_type")) {
         Schema oxmTlvEthTypeSchema = protocol.getType("of13.oxm_tlv_eth_type");
         GenericRecord oxmTlvEthTypeRecord = new GenericData.Record(oxmTlvEthTypeSchema);

         short ethType = U16.t(Integer.valueOf(((String) args.get(key)).replaceFirst("0x", ""), 16));
        
         oxmTlvEthTypeRecord.put("tlv", getUint16Fixed(ethType));
           
         Schema oxmTlvSchema = protocol.getType("of13.oxm_tlv");
         GenericRecord oxmTlvRecord = new GenericData.Record(oxmTlvSchema);
         oxmTlvRecord.put("match", oxmTlvEthTypeRecord);
  
         matches.add(oxmTlvRecord);
           
         if (this.delayedIpv4Src != null)
            matches.add(this.delayedIpv4Src);
         if (this.delayedIpv4Dst != null)
               matches.add(this.delayedIpv4Dst);
         if (this.delayedIpv6Src != null)
               matches.add(this.delayedIpv6Src);
         if (this.delayedIpv6Dst != null)
               matches.add(this.delayedIpv6Dst);
         if (this.delayedIpProto != null)
               matches.add(this.delayedIpProto);

         this.isEtherType = true;

        /*
         * VLAN_VID
         */
        } else if (key.equals("vlan_vid")) {
          
           Schema oxmTlvVlanVidSchema = protocol.getType("of13.oxm_tlv_vlan_vid");
           GenericRecord oxmTlvVlanVidRecord = new GenericData.Record(oxmTlvVlanVidSchema);
          
           short vid = U16.t(Integer.valueOf((String) args.get(key)));
           oxmTlvVlanVidRecord.put("tlv", getUint16Fixed(vid));
          
           Schema oxmTlvSchema = protocol.getType("of13.oxm_tlv");
           GenericRecord oxmTlvRecord = new GenericData.Record(oxmTlvSchema);
           oxmTlvRecord.put("match", oxmTlvVlanVidRecord);
          
           matches.add(oxmTlvRecord);
          
           if (this.delayedVlanPcp != null)
              matches.add(this.delayedVlanPcp);
           else
              this.isVlanVid = true;
          
         /*
          * VLAN_PCP
          */
         } else if (key.equals("dl_vlan_pcp")) {
           
            Schema oxmTlvFieldSchema = protocol.getType("of13.oxm_tlv_vlan_pcp");
            GenericRecord oxmTlvFieldRecord = new GenericData.Record(oxmTlvFieldSchema);
           
            byte tmp = U8.t(Short.valueOf((String) args.get(key)));
            oxmTlvFieldRecord.put("tlv", getUint8Fixed(tmp));
           
            Schema oxmTlvSchema = protocol.getType("of13.oxm_tlv");
            GenericRecord oxmTlvRecord = new GenericData.Record(oxmTlvSchema);
            oxmTlvRecord.put("match", oxmTlvFieldRecord);
           
            if (this.isVlanVid == true)
               matches.add(oxmTlvRecord);
            else
               this.delayedVlanPcp = oxmTlvRecord;


            /*
             * MATCH - IPV6_SRC
             */
         } else if (key.equals("ipv6_src")) {

            Schema oxmTlvFieldSchema = protocol.getType("of13.oxm_tlv_ipv6_src");
            GenericRecord oxmTlvFieldRecord = new GenericData.Record(oxmTlvFieldSchema);
            
            oxmTlvFieldRecord.put("tlv", getUint128Fixed(get_ipv6((String) args.get(key))));
            Schema oxmTlvSchema = protocol.getType("of13.oxm_tlv");
            GenericRecord oxmTlvRecord = new GenericData.Record(oxmTlvSchema);
            oxmTlvRecord.put("match", oxmTlvFieldRecord);
            
            if (this.isEtherType)
               matches.add(oxmTlvRecord);
            else
               this.delayedIpv6Src = oxmTlvRecord;
           
            /*
             * MATCH - IPV6_SRC
             */
         } else if (key.equals("ipv6_dst")) {

            Schema oxmTlvFieldSchema = protocol.getType("of13.oxm_tlv_ipv6_dst");
            GenericRecord oxmTlvFieldRecord = new GenericData.Record(oxmTlvFieldSchema);
            
            oxmTlvFieldRecord.put("tlv", getUint128Fixed(get_ipv6((String) args.get(key))));
            Schema oxmTlvSchema = protocol.getType("of13.oxm_tlv");
            GenericRecord oxmTlvRecord = new GenericData.Record(oxmTlvSchema);
            oxmTlvRecord.put("match", oxmTlvFieldRecord);
            
            if (this.isEtherType)
               matches.add(oxmTlvRecord);
            else
               this.delayedIpv6Dst = oxmTlvRecord;
           

         /*
          * OXM_OF_IP_DSCP
          */
         } else if (key.equals("ip_dscp")) {
           
            Schema oxmTlvFieldSchema = protocol.getType("of13.oxm_tlv_ip_dscp");
            GenericRecord oxmTlvFieldRecord = new GenericData.Record(oxmTlvFieldSchema);
           
            byte tmp = U8.t(Short.valueOf((String) args.get(key)));
           
            oxmTlvFieldRecord.put("tlv", getUint8Fixed(tmp));
           
            Schema oxmTlvSchema = protocol.getType("of13.oxm_tlv");
            GenericRecord oxmTlvRecord = new GenericData.Record(oxmTlvSchema);
            oxmTlvRecord.put("match", oxmTlvFieldRecord);
           
            matches.add(oxmTlvRecord);

         /*
          * OXM_OF_IP_ECN
          */
         } else if (key.equals("ip_ecn")) {
           
            Schema oxmTlvFieldSchema = protocol.getType("of13.oxm_tlv_ip_ecn");
            GenericRecord oxmTlvFieldRecord = new GenericData.Record(oxmTlvFieldSchema);
           
            byte tmp = U8.t(Short.valueOf((String) args.get(key)));
            oxmTlvFieldRecord.put("tlv", getUint8Fixed(tmp));
           
            Schema oxmTlvSchema = protocol.getType("of13.oxm_tlv");
            GenericRecord oxmTlvRecord = new GenericData.Record(oxmTlvSchema);
            oxmTlvRecord.put("match", oxmTlvFieldRecord);
           
            matches.add(oxmTlvRecord);

         /*
          * OXM_OF_IP_PROTO
          */
         } else if (key.equals("nw_proto")) {
           
            Schema oxmTlvFieldSchema = protocol.getType("of13.oxm_tlv_ip_proto");
            GenericRecord oxmTlvFieldRecord = new GenericData.Record(oxmTlvFieldSchema);
           
            byte tmp = U8.t(Short.valueOf((String) args.get(key)));
            oxmTlvFieldRecord.put("tlv", getUint8Fixed(tmp));
           
            Schema oxmTlvSchema = protocol.getType("of13.oxm_tlv");
            GenericRecord oxmTlvRecord = new GenericData.Record(oxmTlvSchema);
            oxmTlvRecord.put("match", oxmTlvFieldRecord);
           
            if (this.isEtherType) {
               matches.add(oxmTlvRecord);
               this.isIpProto = true;
            }
            else {
               this.delayedIpProto = oxmTlvRecord;
            }
           
            if (this.delayedTcpSrc != null)
               matches.add(this.delayedTcpSrc);
            if (this.delayedTcpDst != null)
               matches.add(this.delayedTcpDst);
            if (this.delayedUdpSrc != null)
               matches.add(this.delayedUdpSrc);
            if (this.delayedUdpDst != null)
               matches.add(this.delayedUdpDst);
/*            if (this.delayedIpv6Src != null)
               matches.add(this.delayedIpv6Src);
            if (this.delayedIpv6Dst != null)
               matches.add(this.delayedIpv6Dst);
            if (this.delayedIpProto != null)
               matches.add(this.delayedIpProto);*/

            /*
             * MATCH - IPV4_SRC
             */
         } else if (key.equals("nw_src")) {
           
            Schema oxmTlvFieldSchema = protocol.getType("of13.oxm_tlv_ipv4_src");
            GenericRecord oxmTlvFieldRecord = new GenericData.Record(oxmTlvFieldSchema);
            
            oxmTlvFieldRecord.put("tlv", getUint32Fixed(get_ipv4((String) args.get(key))));

            Schema oxmTlvSchema = protocol.getType("of13.oxm_tlv");
            GenericRecord oxmTlvRecord = new GenericData.Record(oxmTlvSchema);
            oxmTlvRecord.put("match", oxmTlvFieldRecord);
           
            if (this.isEtherType)
               matches.add(oxmTlvRecord);
            else
               this.delayedIpv4Src = oxmTlvRecord;
           

            /*
             * MATCH - IPV4_DST
             */
         } else if (key.equals("nw_dst")) {
            
            Schema oxmTlvFieldSchema = protocol.getType("of13.oxm_tlv_ipv4_dst");
            GenericRecord oxmTlvFieldRecord = new GenericData.Record(oxmTlvFieldSchema);
            
            oxmTlvFieldRecord.put("tlv", getUint32Fixed(get_ipv4((String) args.get(key))));

            Schema oxmTlvSchema = protocol.getType("of13.oxm_tlv");
            GenericRecord oxmTlvRecord = new GenericData.Record(oxmTlvSchema);
            oxmTlvRecord.put("match", oxmTlvFieldRecord);

            if (this.isEtherType)
               matches.add(oxmTlvRecord);
            else
               this.delayedIpv4Dst = oxmTlvRecord;
           
            /*
             * MATCH - TCP_SRC
             */
         } else if (key.equals("tp_src")) {
            
            Schema oxmTlvFieldSchema = protocol.getType("of13.oxm_tlv_tcp_src");
            GenericRecord oxmTlvFieldRecord = new GenericData.Record(oxmTlvFieldSchema);
            
            oxmTlvFieldRecord.put("tlv", getUint16Fixed(U16.t(Integer.valueOf((String) args.get(key)))));

            Schema oxmTlvSchema = protocol.getType("of13.oxm_tlv");
            GenericRecord oxmTlvRecord = new GenericData.Record(oxmTlvSchema);
            oxmTlvRecord.put("match", oxmTlvFieldRecord);

            if (this.isIpProto)
               matches.add(oxmTlvRecord);
            else
               this.delayedTcpSrc = oxmTlvRecord;

            /*
             * MATCH - TCP_DST
             */
         } else if (key.equals("tp_dst")) {
            
            Schema oxmTlvFieldSchema = protocol.getType("of13.oxm_tlv_tcp_dst");
            GenericRecord oxmTlvFieldRecord = new GenericData.Record(oxmTlvFieldSchema);
            
            oxmTlvFieldRecord.put("tlv", getUint16Fixed(U16.t(Integer.valueOf((String) args.get(key)))));

            Schema oxmTlvSchema = protocol.getType("of13.oxm_tlv");
            GenericRecord oxmTlvRecord = new GenericData.Record(oxmTlvSchema);
            oxmTlvRecord.put("match", oxmTlvFieldRecord);

            if (this.isIpProto)
               matches.add(oxmTlvRecord);

            else
               this.delayedTcpDst = oxmTlvRecord;

            /*
             * MATCH - UDP_SRC
             */
         } else if (key.equals("udp_src")) {

            if (this.isIpProto)
               matches.add(getMatch("of13.oxm_tlv_udp_src", getUint16Fixed(U16.t(Integer.valueOf((String) args.get(key))))));
            else
               this.delayedUdpSrc = getMatch("of13.oxm_tlv_udp_src", getUint16Fixed(U16.t(Integer.valueOf((String) args.get(key)))));

            /*
             * MATCH - UDP_DST
             */
         } else if (key.equals("udp_dst")) {

            if (this.isIpProto)
               matches.add(getMatch("of13.oxm_tlv_udp_dst", getUint16Fixed(U16.t(Integer.valueOf((String) args.get(key))))));
            else
               this.delayedUdpDst = getMatch("of13.oxm_tlv_udp_dst", getUint16Fixed(U16.t(Integer.valueOf((String) args.get(key)))));
           
           
         }
      }

      Schema oxmTlvSchema = protocol.getType("of13.oxm_tlv");
      GenericArray<GenericRecord> oxmTlvsArray = new GenericData.Array<>(Schema.createArray(oxmTlvSchema), matches);
     
      Schema oxmTlvFieldsSchema = protocol.getType("of13.oxm_tlv_fields");
      GenericRecord oxmTlvFieldsRecord = new GenericData.Record(oxmTlvFieldsSchema);
      oxmTlvFieldsRecord.put("oxm_tlvs", oxmTlvsArray);

      /*
       * Build match header
       */
      Schema matchHeaderSchema = protocol.getType("of13.match_header");
      GenericRecordBuilder matchHeaderBuilder = new GenericRecordBuilder(matchHeaderSchema);
      GenericRecord matchHeaderRecord = matchHeaderBuilder.build();
     
      /*
       * Calculating oxm_tlvs length
       */
      ByteArrayOutputStream oxmOut = new ByteArrayOutputStream();
      DatumWriter<GenericRecord> oxmWriter = new GenericDatumWriter<GenericRecord>(oxmTlvFieldsSchema);
      Encoder oxmEncoder = EncoderFactory.get().binaryNonEncoder(oxmOut, null);
     
      int closingPadLength = 4;
     
      try {
         oxmWriter.write(oxmTlvFieldsRecord, oxmEncoder);
         oxmEncoder.flush();
        
         int matchLength = oxmOut.size() + 4;
         closingPadLength = (int) ((matchLength + 7)/8*8 - matchLength);
        
         Schema uint16Schema = protocol.getType("of13.uint_16");
        
         byte len[] = {(byte)(matchLength >> 8), (byte)(255 & matchLength)};
         GenericData.Fixed lenght = new GenericData.Fixed(uint16Schema, len);
        
         matchHeaderRecord.put("length", lenght);
      } catch (IOException e1) {
         // TODO Auto-generated catch block
         e1.printStackTrace();
      }     
     
      /*
       * Build closing pad
       */
      ByteBuffer clP = ByteBuffer.allocate(closingPadLength);
//      GenericData.Fixed closingPadRecord = new GenericData.Fixed(Schema.createFixed("", "", "of", closingPadLength), clP.toByteArray());
 
      /*
       * Assemble ofp_match structure
       */
      Schema ofpMatchSchema = protocol.getType("of13.ofp_match");
      GenericRecord ofpMatchRecord = new GenericData.Record(ofpMatchSchema);
      ofpMatchRecord.put("header", matchHeaderRecord);
      ofpMatchRecord.put("fields", oxmTlvFieldsRecord);
      ofpMatchRecord.put("closing_pad", clP);

     
/* I N S T R U C T I O N S */     
      /*
       * Build Instruction records
       * Build GoTo Instruction for Test
       */
      Schema ofpInstrSchema = protocol.getType("of13.ofp_instruction");
  /*    GenericRecord ofpInstrRecord = new GenericData.Record(ofpInstrSchema);
     
      Schema ofpInstrGoToSchema = protocol.getType("of13.ofp_instruction_goto_table");
      GenericRecordBuilder instrBuilder = new GenericRecordBuilder(ofpInstrGoToSchema);
      GenericRecord ofpInstrGoToRecord = instrBuilder.build();
      ofpInstrRecord.put("instruction", ofpInstrGoToRecord);
     
      instructions.add(ofpInstrRecord);
*/
      GenericArray<GenericRecord> instrArray = new GenericData.Array<>(Schema.createArray(ofpInstrSchema), instructions);
     
      /*
       * Create Instruction Set
       */
      Schema instrSetSchema = protocol.getType("of13.instruction_set");
      GenericRecord instrSetRecord = new GenericData.Record(instrSetSchema);
      instrSetRecord.put("set", instrArray);

/* F L O W  M O D  M E S S A G E */ 
      /*
       * Create FlowMod Header
       */
      GenericRecordBuilder flowModHeaderBuilder = new GenericRecordBuilder(flowModHeaderSchema);
      GenericRecord flowModHeaderRecord = flowModHeaderBuilder.build();
     
      /*
       * Assemble Flow_mod message
       */
      ofpFlowModRecord.put("header", flowModHeaderRecord);     
View Full Code Here

      Schema ofpActionSchema = protocol.getType("of13.ofp_action");
      GenericRecord ofpActionBaseRecord = new GenericData.Record(ofpActionSchema);
       
   
      Schema ofpActionOutSchema = protocol.getType("of13.ofp_action_output");
      GenericRecordBuilder actionBuilder = new GenericRecordBuilder(ofpActionOutSchema);
      GenericRecord ofpActionOutRecord = actionBuilder.build();
     
      n = Pattern.compile("output=(?:((?:0x)?\\d+)|(all)|(controller)|(local)|(ingress-port)|(normal)|(flood))").matcher(subaction);
      if (n.matches()) {

//          int port = OFPort.OFPP_NONE.getValue();
View Full Code Here

TOP

Related Classes of org.apache.avro.generic.GenericRecordBuilder

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.