Add new revision for pcep types model
[bgpcep.git] / pcep / base-parser / src / main / java / org / opendaylight / protocol / pcep / parser / object / PCEPEndPointsIpv4ObjectParser.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.protocol.pcep.parser.object;
9
10 import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeIpv4Address;
11
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.CommonObjectParser;
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.rev181109.Object;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.ObjectHeader;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.endpoints.AddressFamily;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.endpoints.address.family.Ipv4Case;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.endpoints.address.family.Ipv4CaseBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.endpoints.address.family.Ipv6Case;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.endpoints.address.family.ipv4._case.Ipv4;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.endpoints.address.family.ipv4._case.Ipv4Builder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.endpoints.object.EndpointsObj;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.endpoints.object.EndpointsObjBuilder;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 /**
36  * Parser for IPv4 {@link EndpointsObj}
37  */
38 public class PCEPEndPointsIpv4ObjectParser extends CommonObjectParser implements ObjectSerializer {
39
40     private static final Logger LOG = LoggerFactory.getLogger(PCEPEndPointsIpv4ObjectParser.class);
41
42     private static final int CLASS = 4;
43     private static final int TYPE = 1;
44
45     public PCEPEndPointsIpv4ObjectParser() {
46         super(CLASS, TYPE);
47     }
48
49     @Override
50     public Object parseObject(final ObjectHeader header, final ByteBuf bytes) throws PCEPDeserializerException {
51         Preconditions.checkArgument(bytes != null && bytes.isReadable(), "Array of bytes is mandatory. Can't be null or empty.");
52         final EndpointsObjBuilder builder = new EndpointsObjBuilder();
53         if (!header.isProcessingRule()) {
54             LOG.debug("Processed bit not set on Endpoints OBJECT, ignoring it.");
55             return new UnknownObject(PCEPErrors.P_FLAG_NOT_SET, builder.build());
56         }
57         if (bytes.readableBytes() != Ipv4Util.IP4_LENGTH * 2) {
58             throw new PCEPDeserializerException("Wrong length of array of bytes.");
59         }
60         builder.setIgnore(header.isIgnore());
61         builder.setProcessingRule(header.isProcessingRule());
62         final Ipv4Builder b = new Ipv4Builder();
63         b.setSourceIpv4Address(Ipv4Util.noZoneAddressForByteBuf(bytes));
64         b.setDestinationIpv4Address(Ipv4Util.noZoneAddressForByteBuf(bytes));
65         builder.setAddressFamily(new Ipv4CaseBuilder().setIpv4(b.build()).build());
66         return builder.build();
67     }
68
69     @Override
70     public void serializeObject(final Object object, final ByteBuf buffer) {
71         Preconditions.checkArgument(object instanceof EndpointsObj, "Wrong instance of PCEPObject. Passed %s. Needed EndpointsObject.", object.getClass());
72         final EndpointsObj ePObj = (EndpointsObj) object;
73         final AddressFamily afi = ePObj.getAddressFamily();
74         if(afi instanceof Ipv6Case) {
75             PCEPEndPointsIpv6ObjectParser.serializeObject(object,buffer);
76         }
77         Preconditions.checkArgument(afi instanceof Ipv4Case, "Wrong instance of NetworkAddress. Passed %s. Needed IPv4", afi.getClass());
78         final Ipv4 ipv4 = ((Ipv4Case) afi).getIpv4();
79         final ByteBuf body = Unpooled.buffer(Ipv4Util.IP4_LENGTH + Ipv4Util.IP4_LENGTH);
80         Preconditions.checkArgument(ipv4.getSourceIpv4Address() != null, "SourceIpv4Address is mandatory.");
81         writeIpv4Address(ipv4.getSourceIpv4Address(), body);
82         Preconditions.checkArgument(ipv4.getDestinationIpv4Address() != null, "DestinationIpv4Address is mandatory.");
83         writeIpv4Address(ipv4.getDestinationIpv4Address(), body);
84         ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), body, buffer);
85     }
86 }