Java Code Examples for org.jboss.netty.buffer.ChannelBuffer#readByte()

The following examples show how to use org.jboss.netty.buffer.ChannelBuffer#readByte() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: OFMatch.java    From floodlight_with_topoguard with Apache License 2.0 6 votes vote down vote up
/**
 * Read this message off the wire from the specified ByteBuffer
 *
 * @param data
 */
public void readFrom(ChannelBuffer data) {
    this.wildcards = data.readInt();
    this.inputPort = data.readShort();
    this.dataLayerSource = new byte[6];
    data.readBytes(this.dataLayerSource);
    this.dataLayerDestination = new byte[6];
    data.readBytes(this.dataLayerDestination);
    this.dataLayerVirtualLan = data.readShort();
    this.dataLayerVirtualLanPriorityCodePoint = data.readByte();
    data.readByte(); // pad
    this.dataLayerType = data.readShort();
    this.networkTypeOfService = data.readByte();
    this.networkProtocol = data.readByte();
    data.readByte(); // pad
    data.readByte(); // pad
    this.networkSource = data.readInt();
    this.networkDestination = data.readInt();
    this.transportSource = data.readShort();
    this.transportDestination = data.readShort();
}
 
Example 2
Source File: BgpPeerFrameDecoderTest.java    From onos with Apache License 2.0 5 votes vote down vote up
/**
 * Processes BGP open message.
 *
 * @param ctx Channel handler context
 * @param message open message
 */
private void processBgpOpen(ChannelHandlerContext ctx,
                            ChannelBuffer message) {
    int minLength =
        MINIMUM_OPEN_MSG_LENGTH - MINIMUM_COMMON_HEADER_LENGTH;
    if (message.readableBytes() < minLength) {
        log.debug("Error: Bad message length");
        ctx.getChannel().close();
        return;
    }

    message.readByte(); // read version
    message.readShort(); // read AS number
    message.readShort(); // read Hold timer
    message.readInt(); // read BGP Identifier
    // Optional Parameters
    int optParamLen = message.readUnsignedByte();
    if (message.readableBytes() < optParamLen) {
        log.debug("Error: Bad message length");
        ctx.getChannel().close();
        return;
    }
    message.readBytes(optParamLen);

    // Open message received
    receivedOpenMessageLatch.countDown();
}
 
Example 3
Source File: OFMirrorSetVendorData.java    From floodlight_with_topoguard with Apache License 2.0 5 votes vote down vote up
/**
 * Read the vendor data from the channel buffer
 * @param data: the channel buffer from which we are deserializing
 * @param length: the length to the end of the enclosing message
 */
public void readFrom(ChannelBuffer data, int length) {
    super.readFrom(data, length);
    reportMirrorPorts = data.readByte();
    pad1 = data.readByte();
    pad2 = data.readByte();
    pad3 = data.readByte();
}
 
Example 4
Source File: OFNetmaskVendorData.java    From floodlight_with_topoguard with Apache License 2.0 5 votes vote down vote up
/**
 * Read the vendor data from the channel buffer
 * @param data: the channel buffer from which we are deserializing
 * @param length: the length to the end of the enclosing message
 */
public void readFrom(ChannelBuffer data, int length) {
    super.readFrom(data, length);
    tableIndex = data.readByte();
    pad1 = data.readByte();
    pad2 = data.readByte();
    pad3 = data.readByte();
    netMask = data.readInt();
}
 
Example 5
Source File: OFBsnPktinSuppressionSetRequestVendorData.java    From floodlight_with_topoguard with Apache License 2.0 5 votes vote down vote up
@Override
public void readFrom(ChannelBuffer data, int length) {
    super.readFrom(data, length);
    suppressionEnabled = (data.readByte() != 0);
    data.readByte();
    idleTimeout = data.readShort();
    hardTimeout = data.readShort();
    priority = data.readShort();
    cookie = data.readLong();
}
 
Example 6
Source File: ExternalLsa.java    From onos with Apache License 2.0 5 votes vote down vote up
/**
 * Reads from channel buffer and populate instance.
 *
 * @param channelBuffer channelBuffer instance
 * @throws OspfParseException might throws exception while parsing buffer
 */
public void readFrom(ChannelBuffer channelBuffer) throws OspfParseException {

    try {
        byte[] tempByteArray = new byte[OspfUtil.FOUR_BYTES];
        channelBuffer.readBytes(tempByteArray, 0, OspfUtil.FOUR_BYTES);
        this.setNetworkMask(Ip4Address.valueOf(tempByteArray));

        while (channelBuffer.readableBytes() >= OspfUtil.EXTERNAL_DESTINATION_LENGTH) {
            OspfExternalDestination externalDestination = new OspfExternalDestination();

            //E Bit - use to find type1 or type2
            int eIsSet = channelBuffer.readByte();
            if (eIsSet != 0) {
                externalDestination.setType1orType2Metric(true);
            } else {
                externalDestination.setType1orType2Metric(false);
            }
            externalDestination.setMetric(channelBuffer.readUnsignedMedium());
            tempByteArray = new byte[OspfUtil.FOUR_BYTES];
            channelBuffer.readBytes(tempByteArray, 0, OspfUtil.FOUR_BYTES);
            externalDestination.setForwardingAddress(Ip4Address.valueOf(tempByteArray));
            externalDestination.setExternalRouterTag(channelBuffer.readInt());

            this.addExternalDestination(externalDestination);
        }
    } catch (Exception e) {
        log.debug("Error::ExternalLSA:: {}", e.getMessage());
        throw new OspfParseException(OspfErrorType.OSPF_MESSAGE_ERROR, OspfErrorType.BAD_MESSAGE);
    }
}
 
Example 7
Source File: BgpEvpnNlriImpl.java    From onos with Apache License 2.0 5 votes vote down vote up
/**
 * Reads from channelBuffer and parses Evpn Nlri.
 *
 * @param cb ChannelBuffer
 * @return object of BgpEvpnNlriVer4
 * @throws BgpParseException while parsing Bgp Evpn Nlri
 */
public static BgpEvpnNlriImpl read(ChannelBuffer cb)
        throws BgpParseException {

    BgpEvpnNlriData routeNlri = null;

    if (cb.readableBytes() > 0) {
        ChannelBuffer tempBuf = cb.copy();
        byte type = cb.readByte();
        byte length = cb.readByte();
        if (cb.readableBytes() < length) {
            throw new BgpParseException(BgpErrorType.UPDATE_MESSAGE_ERROR,
                                        BgpErrorType.OPTIONAL_ATTRIBUTE_ERROR,
                                        tempBuf.readBytes(cb.readableBytes()
                                                                  + TYPE_AND_LEN));
        }
        ChannelBuffer tempCb = cb.readBytes(length);
        switch (type) {
            case BgpEvpnRouteType2Nlri.TYPE:
                routeNlri = BgpEvpnRouteType2Nlri.read(tempCb);
                break;
            default:
                log.info("Discarding, EVPN route type {}", type);
                throw new BgpParseException(BgpErrorType.UPDATE_MESSAGE_ERROR,
                                            BgpErrorType.MISSING_WELLKNOWN_ATTRIBUTE, null);
                //        break;
        }
        return new BgpEvpnNlriImpl(type, routeNlri);
    } else {
        return new BgpEvpnNlriImpl();
    }

}
 
Example 8
Source File: MplsProtocolMaskSubTlv.java    From onos with Apache License 2.0 5 votes vote down vote up
/**
 * Reads the channel buffer and returns object of MPLS Protocol Mask Tlv.
 *
 * @param c input channel buffer
 * @return object of MPLS Protocol Mask Tlv
 */
public static PcepValueType read(ChannelBuffer c) {
    byte temp = c.readByte();
    boolean bLFlag;
    boolean bRFlag;

    bLFlag = (temp & LFLAG_SET) == LFLAG_SET;
    bRFlag = (temp & RFLAG_SET) == RFLAG_SET;

    return new MplsProtocolMaskSubTlv(bLFlag, bRFlag);
}
 
Example 9
Source File: PcepMetricObjectVer1.java    From onos with Apache License 2.0 5 votes vote down vote up
/**
 * Reads from channel buffer and returns object of PcepMetricObject.
 *
 * @param cb of channel buffer.
 * @return object of PcepMetricObject
 * @throws PcepParseException when metric object is not present in channel buffer
 */
public static PcepMetricObject read(ChannelBuffer cb) throws PcepParseException {

    log.debug("MetricObject::read");
    PcepObjectHeader metricObjHeader;
    int iMetricVal;
    byte yFlag; // 6-flags
    boolean bCFlag;
    boolean bBFlag;
    byte bType;

    metricObjHeader = PcepObjectHeader.read(cb);

    if (metricObjHeader.getObjClass() != METRIC_OBJ_CLASS) {
        throw new PcepParseException("This object is not a Metric Object. Object Class: "
                + metricObjHeader.getObjClass());
    }

    //take only metric buffer.
    ChannelBuffer tempCb = cb.readBytes(metricObjHeader.getObjLen() - OBJECT_HEADER_LENGTH);

    tempCb.readShort();
    yFlag = tempCb.readByte();
    bType = tempCb.readByte();
    bCFlag = (yFlag & CFLAG_CHECK) == CFLAG_CHECK;
    bBFlag = (yFlag & BFLAG_SET) == BFLAG_SET;
    iMetricVal = tempCb.readInt();

    return new PcepMetricObjectVer1(metricObjHeader, iMetricVal, yFlag, bCFlag, bBFlag, bType);
}
 
Example 10
Source File: BgpLinkAttrIgpMetric.java    From onos with Apache License 2.0 5 votes vote down vote up
/**
 * Reads the BGP link attributes IGP Metric.
 *
 * @param cb Channel buffer
 * @return object of type BgpLinkAttrIgpMetric
 * @throws BgpParseException while parsing BgpLinkAttrIgpMetric
 */
public static BgpLinkAttrIgpMetric read(ChannelBuffer cb)
        throws BgpParseException {

    int linkigp;
    int igpMetric = 0;
    int igpMetricLen = 0;

    short lsAttrLength = cb.readShort();

    if (cb.readableBytes() < lsAttrLength
            || lsAttrLength > ATTRLINK_MAX_LEN) {
        Validation.validateLen(BgpErrorType.UPDATE_MESSAGE_ERROR,
                               BgpErrorType.ATTRIBUTE_LENGTH_ERROR,
                               lsAttrLength);
    }

    switch (lsAttrLength) {
    case ISIS_SMALL_METRIC:
        igpMetric = cb.readByte();
        igpMetricLen = ISIS_SMALL_METRIC;
        break;
    case OSPF_LINK_METRIC:
        igpMetric = cb.readShort();
        igpMetricLen = OSPF_LINK_METRIC;
        break;
    case ISIS_WIDE_METRIC:
        linkigp = cb.readShort();
        igpMetric = cb.readByte();
        igpMetric = (linkigp << 8) | igpMetric;
        igpMetricLen = ISIS_WIDE_METRIC;
        break;
    default: // validation is already in place
        break;
    }

    return BgpLinkAttrIgpMetric.of(igpMetric, igpMetricLen);
}
 
Example 11
Source File: BgpFsIpProtocol.java    From onos with Apache License 2.0 5 votes vote down vote up
/**
 * Reads the channel buffer and returns object.
 *
 * @param cb channelBuffer
 * @return object of flow spec IP protocol
 * @throws BgpParseException while parsing BgpFsIpProtocol
 */
public static BgpFsIpProtocol read(ChannelBuffer cb) throws BgpParseException {
    List<BgpFsOperatorValue> operatorValue = new LinkedList<>();
    byte option;
    byte proto;

    do {
        option = (byte) cb.readByte();
        proto = cb.readByte();
        operatorValue.add(new BgpFsOperatorValue(option, new byte[] {(byte) proto}));
    } while ((option & Constants.BGP_FLOW_SPEC_END_OF_LIST_MASK) == 0);

    return new BgpFsIpProtocol(operatorValue);
}
 
Example 12
Source File: PcepRsvpObjectHeader.java    From onos with Apache License 2.0 5 votes vote down vote up
/**
 * Reads the PcepRsvpObjectHeader.
 *
 * @param cb input channel buffer
 * @return PcepRsvpObjectHeader
 */
public static PcepRsvpObjectHeader read(ChannelBuffer cb) {
    log.debug("read ");
    byte objClassNum;
    byte objClassType;
    short objLen;
    objLen = cb.readShort();
    objClassNum = cb.readByte();
    objClassType = cb.readByte();

    return new PcepRsvpObjectHeader(objClassNum, objClassType, objLen);
}
 
Example 13
Source File: BgpFsIcmpCode.java    From onos with Apache License 2.0 5 votes vote down vote up
/**
 * Reads the channel buffer and returns object.
 *
 * @param cb channelBuffer
 * @return object of flow spec ICMP code
 * @throws BgpParseException while parsing BgpFsIcmpCode
 */
public static BgpFsIcmpCode read(ChannelBuffer cb) throws BgpParseException {
    List<BgpFsOperatorValue> operatorValue = new LinkedList<>();
    byte option;
    byte icmpCode;

    do {
        option = (byte) cb.readByte();
        icmpCode = cb.readByte();
        operatorValue.add(new BgpFsOperatorValue(option, new byte[] {(byte) icmpCode}));
    } while ((option & Constants.BGP_FLOW_SPEC_END_OF_LIST_MASK) == 0);

    return new BgpFsIcmpCode(operatorValue);
}
 
Example 14
Source File: RpcRequestDecode.java    From voyage with Apache License 2.0 5 votes vote down vote up
@Override
protected Object decode(ChannelHandlerContext ctx, Channel channel,
		ChannelBuffer buffer) throws Exception {
	if (buffer.readableBytes() < 2) {
		return null;
	}
	byte byte1 = buffer.readByte();
	byte byte2 = buffer.readByte();
	if (byte1!=Constants.MAGIC_HIGH || byte2!=Constants.MAGIC_LOW) {
		throw new RuntimeException("magic number not right");
	}
	ChannelBufferInputStream in = new ChannelBufferInputStream(buffer);
	RpcRequest request = MySerializerFactory.getInstance(Constants.DEFAULT_RPC_CODE_MODE).decodeRequest(in);
	return request;
}
 
Example 15
Source File: PcepCloseMsgVer1.java    From onos with Apache License 2.0 5 votes vote down vote up
@Override
public PcepCloseMsg readFrom(ChannelBuffer cb) throws PcepParseException {

    if (cb.readableBytes() < PACKET_MINIMUM_LENGTH) {
        throw new PcepParseException("Packet size is less than the minimum length.");
    }
    // fixed value property version == 1
    byte version = cb.readByte();
    version = (byte) (version >> SHIFT_FLAG);
    if (version != PACKET_VERSION) {
        throw new PcepParseException("Wrong version. Expected=PcepVersion.PCEP_1(1), got=" + version);
    }
    // fixed value property type == 7
    byte type = cb.readByte();
    if (type != MSG_TYPE.getType()) {
        throw new PcepParseException("Wrong type. Expected=PcepType.CLOSE(7), got=" + type);
    }
    short length = cb.readShort();
    if (length < PACKET_MINIMUM_LENGTH) {
        throw new PcepParseException("Wrong length. Expected to be >= " + PACKET_MINIMUM_LENGTH + ", was: "
                + length);
    }
    closeObjHeader = PcepObjectHeader.read(cb);
    // Reserved
    cb.readShort();
    // Flags
    cb.readByte();
    // Reason
    yReason = cb.readByte();
    // parse optional TLV
    llOptionalTlv = parseOptionalTlv(cb);
    return new PcepCloseMsgVer1(closeObjHeader, yReason, llOptionalTlv);
}
 
Example 16
Source File: SyslogUtils.java    From mt-flume with Apache License 2.0 4 votes vote down vote up
public Event extractEvent(ChannelBuffer in){

    /* for protocol debugging
    ByteBuffer bb = in.toByteBuffer();
    int remaining = bb.remaining();
    byte[] buf = new byte[remaining];
    bb.get(buf);
    HexDump.dump(buf, 0, System.out, 0);
    */

    byte b = 0;
    Event e = null;
    boolean doneReading = false;

    try {
      while (!doneReading && in.readable()) {
        b = in.readByte();
        switch (m) {
        case START:
          if (b == '<') {
            m = Mode.PRIO;
          } else if(b == '\n'){
          //If the character is \n, it was because the last event was exactly
          //as long  as the maximum size allowed and
          //the only remaining character was the delimiter - '\n', or
          //multiple delimiters were sent in a row.
          //Just ignore it, and move forward, don't change the mode.
          //This is a no-op, just ignore it.
            logger.debug("Delimiter found while in START mode, ignoring..");

          } else {
            isBadEvent = true;
            baos.write(b);
            //Bad event, just dump everything as if it is data.
            m = Mode.DATA;
          }
          break;
        case PRIO:
          if (b == '>') {
            m = Mode.DATA;
          } else {
            char ch = (char) b;
            prio.append(ch);
            if (!Character.isDigit(ch)) {
              isBadEvent = true;
              //Append the priority to baos:
              String badPrio = "<"+ prio;
              baos.write(badPrio.getBytes());
              //If we hit a bad priority, just write as if everything is data.
              m = Mode.DATA;
            }
          }
          break;
        case DATA:
          // TCP syslog entries are separated by '\n'
          if (b == '\n') {
            e = buildEvent();
            doneReading = true;
          } else {
            baos.write(b);
          }
          if(baos.size() == this.maxSize && !doneReading){
            isIncompleteEvent = true;
            e = buildEvent();
            doneReading = true;
          }
          break;
        }

      }

      // UDP doesn't send a newline, so just use what we received
      if (e == null && isUdp) {
        doneReading = true;
        e = buildEvent();
      }
    //} catch (IndexOutOfBoundsException eF) {
    //    e = buildEvent(prio, baos);
    } catch (IOException e1) {
      //no op
    } finally {
      // no-op
    }

    return e;
  }
 
Example 17
Source File: WideCommunity.java    From onos with Apache License 2.0 4 votes vote down vote up
/**
 * Reads the wide community attribute.
 *
 * @param c ChannelBuffer
 * @return object of WideCommunityAttr
 * @throws BgpParseException while parsing BgpPrefixAttrRouteTag
 */
public static WideCommunity read(ChannelBuffer c) throws BgpParseException {

    WideCommunityAttrHeader wideCommunityHeader;
    int community;
    int localAsn;
    int contextAsn;
    WideCommunityTarget target = null;
    WideCommunityExcludeTarget excludeTarget = null;
    WideCommunityParameter parameter = null;

    short length = c.readShort();

    if (c.readableBytes() < length) {
        Validation.validateLen(BgpErrorType.UPDATE_MESSAGE_ERROR, BgpErrorType.ATTRIBUTE_LENGTH_ERROR, length);
    }

    wideCommunityHeader = WideCommunityAttrHeader.read(c);
    if ((c.readableBytes() < 12) || (c.readableBytes() < wideCommunityHeader.length())) {
        Validation.validateLen(BgpErrorType.UPDATE_MESSAGE_ERROR, BgpErrorType.ATTRIBUTE_LENGTH_ERROR, length);
    }

    community = c.readInt();
    localAsn = c.readInt();
    contextAsn = c.readInt();

    while (c.readableBytes() > 0) {

        if (c.readableBytes() < TYPE_LENGTH_SIZE) {
            Validation.validateLen(BgpErrorType.UPDATE_MESSAGE_ERROR, BgpErrorType.ATTRIBUTE_LENGTH_ERROR,
                                   c.readableBytes());
        }

        byte type = c.readByte();
        length = c.readShort();

        if (c.readableBytes() < length) {
            Validation.validateLen(BgpErrorType.UPDATE_MESSAGE_ERROR, BgpErrorType.ATTRIBUTE_LENGTH_ERROR,
                                   c.readableBytes());
        }

        if (type == WideCommunityTarget.TYPE) {
            target = WideCommunityTarget.read(c);
        } else if (type == WideCommunityExcludeTarget.TYPE) {
            excludeTarget = WideCommunityExcludeTarget.read(c);
        } else if (type == WideCommunityParameter.TYPE) {
            parameter = WideCommunityParameter.read(c);
        }
    }
    return new WideCommunity(wideCommunityHeader, community, localAsn, contextAsn,
                                 target, excludeTarget, parameter);
}
 
Example 18
Source File: PcepMessageVer1.java    From onos with Apache License 2.0 4 votes vote down vote up
@Override
public PcepMessage readFrom(ChannelBuffer cb) throws PcepParseException, PcepOutOfBoundMessageException {

    if (cb.readableBytes() < MINIMUM_LENGTH) {
        throw new PcepParseException("Packet should have minimum length: " + MINIMUM_LENGTH);
    }

    try {
        int start = cb.readerIndex();
        // fixed value property version == 1
        byte version = cb.readByte();
        version = (byte) (version >> PcepMessageVer1.SHIFT_FLAG);
        if (version != (byte) PACKET_VERSION) {
            throw new PcepParseException("Wrong version. Expected=PcepVersion.Message_1(1), got=" + version);
        }

        byte type = cb.readByte();
        short length = cb.readShort();
        cb.readerIndex(start);

        // Check the out-of-bound message.
        // If the message is out-of-bound then throw PcepOutOfBoundException.
        if ((length - MINIMUM_COMMON_HEADER_LENGTH) > cb.readableBytes()) {
            throw new PcepOutOfBoundMessageException("Message is out-of-bound.");
        }

        if (type == (byte) PcepType.OPEN.getType()) {
            log.debug("OPEN MESSAGE is received");
            return PcepOpenMsgVer1.READER.readFrom(cb.readBytes(length));
        } else if (type == (byte) PcepType.KEEP_ALIVE.getType()) {
            log.debug("KEEPALIVE MESSAGE is received");
            return PcepKeepaliveMsgVer1.READER.readFrom(cb.readBytes(length));
        } else if (type == (byte) PcepType.ERROR.getType()) {
            log.debug("ERROR MESSAGE is received");
            return PcepErrorMsgVer1.READER.readFrom(cb.readBytes(length));
        } else if (type == (byte) PcepType.CLOSE.getType()) {
            log.debug("CLOSE MESSAGE is received");
            return PcepCloseMsgVer1.READER.readFrom(cb.readBytes(length));
        } else if (type == (byte) PcepType.REPORT.getType()) {
            log.debug("REPORT MESSAGE is received");
            return PcepReportMsgVer1.READER.readFrom(cb.readBytes(length));
        } else if (type == (byte) PcepType.UPDATE.getType()) {
            log.debug("UPDATE MESSAGE is received");
            return PcepUpdateMsgVer1.READER.readFrom(cb.readBytes(length));
        } else if (type == (byte) PcepType.INITIATE.getType()) {
            log.debug("INITIATE MESSAGE is received");
            return PcepInitiateMsgVer1.READER.readFrom(cb.readBytes(length));
        } else if (type == (byte) PcepType.LS_REPORT.getType()) {
            log.debug("LS REPORT MESSAGE is received");
            return PcepLSReportMsgVer1.READER.readFrom(cb.readBytes(length));
        } else if (type == (byte) PcepType.LABEL_RANGE_RESERV.getType()) {
            log.debug("LABEL RANGE RESERVE MESSAGE is received");
            return PcepLabelRangeResvMsgVer1.READER.readFrom(cb.readBytes(length));
        } else if (type == (byte) PcepType.LABEL_UPDATE.getType()) {
            log.debug("LABEL UPDATE MESSAGE is received");
            return PcepLabelUpdateMsgVer1.READER.readFrom(cb.readBytes(length));
        } else {
            throw new PcepParseException("ERROR: UNKNOWN MESSAGE is received. Msg Type: " + type);
        }
    } catch (IndexOutOfBoundsException e) {
        throw new PcepParseException(PcepErrorDetailInfo.ERROR_TYPE_1, PcepErrorDetailInfo.ERROR_VALUE_1);
    }
}
 
Example 19
Source File: PcepEroObjectVer1.java    From onos with Apache License 2.0 4 votes vote down vote up
/**
 * Parse list of Sub Objects.
 *
 * @param cb channel buffer
 * @return list of Sub Objects
 * @throws PcepParseException when fails to parse sub object list
 */
protected static LinkedList<PcepValueType> parseSubObjects(ChannelBuffer cb) throws PcepParseException {

    LinkedList<PcepValueType> subObjectList = new LinkedList<>();

    while (0 < cb.readableBytes()) {

        //check the Type of the TLV
        short type = cb.readByte();
        type = (short) (type & (TYPE_SHIFT_VALUE));
        byte hLength = cb.readByte();

        PcepValueType subObj;

        switch (type) {

        case IPv4SubObject.TYPE:
            subObj = IPv4SubObject.read(cb);
            break;
        case IPv6SubObject.TYPE:
            byte[] ipv6Value = new byte[IPv6SubObject.VALUE_LENGTH];
            cb.readBytes(ipv6Value, 0, IPv6SubObject.VALUE_LENGTH);
            subObj = new IPv6SubObject(ipv6Value);
            break;
        case AutonomousSystemNumberSubObject.TYPE:
            subObj = AutonomousSystemNumberSubObject.read(cb);
            break;
        case PathKeySubObject.TYPE:
            subObj = PathKeySubObject.read(cb);
            break;
        case SrEroSubObject.TYPE:
            subObj = SrEroSubObject.read(cb);
            break;
        default:
            throw new PcepParseException("Unexpected sub object. Type: " + (int) type);
        }
        // Check for the padding
        int pad = hLength % 4;
        if (0 < pad) {
            pad = 4 - pad;
            if (pad <= cb.readableBytes()) {
                cb.skipBytes(pad);
            }
        }

        subObjectList.add(subObj);
    }
    if (0 < cb.readableBytes()) {
        throw new PcepParseException("Subobject parsing error. Extra bytes received.");
    }
    return subObjectList;
}
 
Example 20
Source File: As4Path.java    From onos with Apache License 2.0 4 votes vote down vote up
/**
 * Reads from the channel buffer and parses As4Path.
 *
 * @param cb ChannelBuffer
 * @return object of As4Path
 * @throws BgpParseException while parsing As4Path
 */
public static As4Path read(ChannelBuffer cb) throws BgpParseException {
    List<Integer> as4pathSet = new ArrayList<>();
    List<Integer> as4pathSeq = new ArrayList<>();
    ChannelBuffer tempCb = cb.copy();
    Validation validation = Validation.parseAttributeHeader(cb);

    if (cb.readableBytes() < validation.getLength()) {
        Validation.validateLen(BgpErrorType.UPDATE_MESSAGE_ERROR, BgpErrorType.ATTRIBUTE_LENGTH_ERROR,
                validation.getLength());
    }
    //if fourth bit is set length is read as short otherwise as byte , len includes type, length and value
    int len = validation.isShort() ? validation.getLength() + Constants.TYPE_AND_LEN_AS_SHORT : validation
            .getLength() + Constants.TYPE_AND_LEN_AS_BYTE;
    ChannelBuffer data = tempCb.readBytes(len);
    if (validation.getFirstBit() && !validation.getSecondBit() && validation.getThirdBit()) {
        throw new BgpParseException(BgpErrorType.UPDATE_MESSAGE_ERROR, BgpErrorType.ATTRIBUTE_FLAGS_ERROR, data);
    }

    ChannelBuffer tempBuf = cb.readBytes(validation.getLength());
    while (tempBuf.readableBytes() > 0) {
        byte pathSegType = tempBuf.readByte();
        //no of ASes
        byte pathSegLen = tempBuf.readByte();
        //length = no of Ases * ASnum size (4 bytes)
        int length = pathSegLen * ASNUM_SIZE;
        if (tempBuf.readableBytes() < length) {
            Validation.validateLen(BgpErrorType.UPDATE_MESSAGE_ERROR,
                    BgpErrorType.ATTRIBUTE_LENGTH_ERROR, length);
        }
        ChannelBuffer aspathBuf = tempBuf.readBytes(length);
        while (aspathBuf.readableBytes() > 0) {
            int asNum;
            asNum = aspathBuf.readInt();
            switch (pathSegType) {
            case AsPath.ASPATH_SET_TYPE:
                as4pathSet.add(asNum);
                break;
            case AsPath.ASPATH_SEQ_TYPE:
                as4pathSeq.add(asNum);
                break;
            default: log.debug("Other type Not Supported:" + pathSegType);
            }
        }
    }
    return new As4Path(as4pathSet, as4pathSeq);
}