Merge "Change the type of some leafs to uint32"
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / 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.impl.object;
9
10 import org.opendaylight.protocol.concepts.Ipv4Util;
11 import org.opendaylight.protocol.pcep.spi.AbstractObjectWithTlvsParser;
12 import org.opendaylight.protocol.pcep.spi.ObjectUtil;
13 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
14 import org.opendaylight.protocol.pcep.spi.PCEPErrors;
15 import org.opendaylight.protocol.pcep.spi.TlvHandlerRegistry;
16 import org.opendaylight.protocol.pcep.spi.UnknownObject;
17 import org.opendaylight.protocol.util.ByteArray;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Object;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.ObjectHeader;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Tlv;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.endpoints.AddressFamily;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.endpoints.address.family.Ipv4Case;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.endpoints.address.family.Ipv4CaseBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.endpoints.address.family.ipv4._case.Ipv4Builder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.endpoints.object.EndpointsObj;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.endpoints.object.EndpointsObjBuilder;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 /**
31  * Parser for IPv4 {@link EndpointsObj}
32  */
33 public class PCEPEndPointsIpv4ObjectParser extends AbstractObjectWithTlvsParser<EndpointsObjBuilder> {
34
35         private static final Logger LOG = LoggerFactory.getLogger(PCEPEndPointsIpv4ObjectParser.class);
36
37         public static final int CLASS = 4;
38         public static final int TYPE = 1;
39
40         /*
41          * fields lengths and offsets for IPv4 in bytes
42          */
43         private static final int SRC4_F_LENGTH = 4;
44         private static final int DEST4_F_LENGTH = 4;
45
46         private static final int SRC4_F_OFFSET = 0;
47         private static final int DEST4_F_OFFSET = SRC4_F_OFFSET + SRC4_F_LENGTH;
48
49         public PCEPEndPointsIpv4ObjectParser(final TlvHandlerRegistry tlvReg) {
50                 super(tlvReg);
51         }
52
53         @Override
54         public Object parseObject(final ObjectHeader header, final byte[] bytes) throws PCEPDeserializerException {
55                 if (bytes == null) {
56                         throw new IllegalArgumentException("Array of bytes is mandatory");
57                 }
58                 final EndpointsObjBuilder builder = new EndpointsObjBuilder();
59                 builder.setIgnore(header.isIgnore());
60                 builder.setProcessingRule(header.isProcessingRule());
61                 if (!builder.isProcessingRule()) {
62                         LOG.debug("Processed bit not set on Endpoints OBJECT, ignoring it.");
63                         return new UnknownObject(PCEPErrors.P_FLAG_NOT_SET, builder.build());
64                 }
65                 if (bytes.length != SRC4_F_LENGTH + DEST4_F_LENGTH) {
66                         throw new PCEPDeserializerException("Wrong length of array of bytes.");
67                 }
68                 final Ipv4Builder b = new Ipv4Builder();
69                 b.setSourceIpv4Address(Ipv4Util.addressForBytes(ByteArray.subByte(bytes, SRC4_F_OFFSET, SRC4_F_LENGTH)));
70                 b.setDestinationIpv4Address((Ipv4Util.addressForBytes(ByteArray.subByte(bytes, DEST4_F_OFFSET, DEST4_F_LENGTH))));
71                 builder.setAddressFamily(new Ipv4CaseBuilder().setIpv4(b.build()).build());
72                 return builder.build();
73         }
74
75         @Override
76         public void addTlv(final EndpointsObjBuilder builder, final Tlv tlv) {
77                 // No tlvs defined
78         }
79
80         @Override
81         public byte[] serializeObject(final Object object) {
82                 if (!(object instanceof EndpointsObj)) {
83                         throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + object.getClass() + ". Needed EndpointsObject.");
84                 }
85                 final EndpointsObj ePObj = (EndpointsObj) object;
86
87                 final AddressFamily afi = ePObj.getAddressFamily();
88
89                 if (!(afi instanceof Ipv4Case)) {
90                         throw new IllegalArgumentException("Wrong instance of NetworkAddress. Passed " + afi.getClass() + ". Needed IPv4");
91                 }
92                 final byte[] retBytes = new byte[SRC4_F_LENGTH + DEST4_F_LENGTH];
93                 ByteArray.copyWhole(Ipv4Util.bytesForAddress((((Ipv4Case) afi).getIpv4()).getSourceIpv4Address()), retBytes, SRC4_F_OFFSET);
94                 ByteArray.copyWhole(Ipv4Util.bytesForAddress((((Ipv4Case) afi).getIpv4()).getDestinationIpv4Address()), retBytes, DEST4_F_OFFSET);
95                 return ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), retBytes);
96         }
97
98         @Override
99         public int getObjectType() {
100                 return TYPE;
101         }
102
103         @Override
104         public int getObjectClass() {
105                 return CLASS;
106         }
107 }