2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.protocol.pcep.impl.object;
10 import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeIpv4Address;
12 import com.google.common.base.Preconditions;
13 import io.netty.buffer.ByteBuf;
14 import io.netty.buffer.Unpooled;
15 import org.opendaylight.protocol.pcep.spi.ObjectParser;
16 import org.opendaylight.protocol.pcep.spi.ObjectSerializer;
17 import org.opendaylight.protocol.pcep.spi.ObjectUtil;
18 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
19 import org.opendaylight.protocol.pcep.spi.PCEPErrors;
20 import org.opendaylight.protocol.pcep.spi.UnknownObject;
21 import org.opendaylight.protocol.util.Ipv4Util;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Object;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.ObjectHeader;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.endpoints.AddressFamily;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.endpoints.address.family.Ipv4Case;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.endpoints.address.family.Ipv4CaseBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.endpoints.address.family.ipv4._case.Ipv4;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.endpoints.address.family.ipv4._case.Ipv4Builder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.endpoints.object.EndpointsObj;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.endpoints.object.EndpointsObjBuilder;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
35 * Parser for IPv4 {@link EndpointsObj}
37 public class PCEPEndPointsIpv4ObjectParser implements ObjectParser, ObjectSerializer {
39 private static final Logger LOG = LoggerFactory.getLogger(PCEPEndPointsIpv4ObjectParser.class);
41 public static final int CLASS = 4;
43 public static final int TYPE = 1;
46 public Object parseObject(final ObjectHeader header, final ByteBuf bytes) throws PCEPDeserializerException {
47 Preconditions.checkArgument(bytes != null && bytes.isReadable(), "Array of bytes is mandatory. Can't be null or empty.");
48 final EndpointsObjBuilder builder = new EndpointsObjBuilder();
49 if (!header.isProcessingRule()) {
50 LOG.debug("Processed bit not set on Endpoints OBJECT, ignoring it.");
51 return new UnknownObject(PCEPErrors.P_FLAG_NOT_SET, builder.build());
53 if (bytes.readableBytes() != Ipv4Util.IP4_LENGTH * 2) {
54 throw new PCEPDeserializerException("Wrong length of array of bytes.");
56 builder.setIgnore(header.isIgnore());
57 builder.setProcessingRule(header.isProcessingRule());
58 final Ipv4Builder b = new Ipv4Builder();
59 b.setSourceIpv4Address(Ipv4Util.addressForByteBuf(bytes));
60 b.setDestinationIpv4Address((Ipv4Util.addressForByteBuf(bytes)));
61 builder.setAddressFamily(new Ipv4CaseBuilder().setIpv4(b.build()).build());
62 return builder.build();
66 public void serializeObject(final Object object, final ByteBuf buffer) {
67 Preconditions.checkArgument(object instanceof EndpointsObj, "Wrong instance of PCEPObject. Passed %s. Needed EndpointsObject.", object.getClass());
68 final EndpointsObj ePObj = (EndpointsObj) object;
69 final AddressFamily afi = ePObj.getAddressFamily();
70 Preconditions.checkArgument(afi instanceof Ipv4Case, "Wrong instance of NetworkAddress. Passed %s. Needed IPv4", afi.getClass());
71 final Ipv4 ipv4 = ((Ipv4Case) afi).getIpv4();
72 final ByteBuf body = Unpooled.buffer(Ipv4Util.IP4_LENGTH + Ipv4Util.IP4_LENGTH);
73 Preconditions.checkArgument(ipv4.getSourceIpv4Address() != null, "SourceIpv4Address is mandatory.");
74 writeIpv4Address(ipv4.getSourceIpv4Address(), body);
75 Preconditions.checkArgument(ipv4.getDestinationIpv4Address() != null, "DestinationIpv4Address is mandatory.");
76 writeIpv4Address(ipv4.getDestinationIpv4Address(), body);
77 ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), body, buffer);