2eb58cefe134b8f38702d9936d12af49309fc2a9
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / object / PCEPEndPointsObjectParser.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.concepts.Ipv6Util;
12 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
13 import org.opendaylight.protocol.pcep.spi.PCEPErrors;
14 import org.opendaylight.protocol.pcep.spi.TlvHandlerRegistry;
15 import org.opendaylight.protocol.pcep.spi.UnknownObject;
16 import org.opendaylight.protocol.util.ByteArray;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Object;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.ObjectHeader;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Tlv;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.endpoints.AddressFamily;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.endpoints.address.family.Ipv4Case;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.endpoints.address.family.Ipv4CaseBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.endpoints.address.family.Ipv6Case;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.endpoints.address.family.Ipv6CaseBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.endpoints.address.family.ipv4._case.Ipv4Builder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.endpoints.address.family.ipv6._case.Ipv6Builder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.endpoints.object.EndpointsObj;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.endpoints.object.EndpointsObjBuilder;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 /**
33  * Parser for IPv4 {@link EndpointsObj}
34  */
35 public class PCEPEndPointsObjectParser extends AbstractObjectWithTlvsParser<EndpointsObjBuilder> {
36
37         private static final Logger LOG = LoggerFactory.getLogger(PCEPEndPointsObjectParser.class);
38
39         public static final int CLASS = 4;
40         public static final int TYPE = 1;
41
42         public static final int CLASS_6 = 4;
43         public static final int TYPE_6 = 2;
44
45         /*
46          * fields lengths and offsets for IPv4 in bytes
47          */
48         private static final int SRC4_F_LENGTH = 4;
49         private static final int DEST4_F_LENGTH = 4;
50
51         private static final int SRC4_F_OFFSET = 0;
52         private static final int DEST4_F_OFFSET = SRC4_F_OFFSET + SRC4_F_LENGTH;
53
54         private static final int SRC6_F_LENGTH = 16;
55         private static final int DEST6_F_LENGTH = 16;
56
57         private static final int SRC6_F_OFFSET = 0;
58         private static final int DEST6_F_OFFSET = SRC6_F_OFFSET + SRC6_F_LENGTH;
59
60         public PCEPEndPointsObjectParser(final TlvHandlerRegistry tlvReg) {
61                 super(tlvReg);
62         }
63
64         @Override
65         public Object parseObject(final ObjectHeader header, final byte[] bytes) throws PCEPDeserializerException {
66                 if (bytes == null) {
67                         throw new IllegalArgumentException("Array of bytes is mandatory");
68                 }
69                 final EndpointsObjBuilder builder = new EndpointsObjBuilder();
70                 builder.setIgnore(header.isIgnore());
71                 builder.setProcessingRule(header.isProcessingRule());
72
73                 if (bytes.length == SRC4_F_LENGTH + DEST4_F_LENGTH) {
74                         final Ipv4Builder b = new Ipv4Builder();
75                         b.setSourceIpv4Address(Ipv4Util.addressForBytes(ByteArray.subByte(bytes, SRC4_F_OFFSET, SRC4_F_LENGTH)));
76                         b.setDestinationIpv4Address((Ipv4Util.addressForBytes(ByteArray.subByte(bytes, DEST4_F_OFFSET, DEST4_F_LENGTH))));
77                         builder.setAddressFamily(new Ipv4CaseBuilder().setIpv4(b.build()).build());
78                 } else if (bytes.length == SRC6_F_LENGTH + DEST6_F_LENGTH) {
79                         final Ipv6Builder b = new Ipv6Builder();
80                         b.setSourceIpv6Address(Ipv6Util.addressForBytes(ByteArray.subByte(bytes, SRC6_F_OFFSET, SRC6_F_LENGTH)));
81                         b.setDestinationIpv6Address((Ipv6Util.addressForBytes(ByteArray.subByte(bytes, DEST6_F_OFFSET, DEST6_F_LENGTH))));
82                         builder.setAddressFamily(new Ipv6CaseBuilder().setIpv6(b.build()).build());
83                 } else {
84                         throw new PCEPDeserializerException("Wrong length of array of bytes.");
85                 }
86                 if (!builder.isProcessingRule()) {
87                         LOG.debug("Processed bit not set on Endpoints OBJECT, ignoring it.");
88                         return new UnknownObject(PCEPErrors.P_FLAG_NOT_SET, builder.build());
89                 }
90                 return builder.build();
91         }
92
93         @Override
94         public void addTlv(final EndpointsObjBuilder builder, final Tlv tlv) {
95                 // No tlvs defined
96         }
97
98         @Override
99         public byte[] serializeObject(final Object object) {
100                 if (!(object instanceof EndpointsObj)) {
101                         throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + object.getClass() + ". Needed EndpointsObject.");
102                 }
103
104                 final EndpointsObj ePObj = (EndpointsObj) object;
105
106                 final AddressFamily afi = ePObj.getAddressFamily();
107
108                 if (afi instanceof Ipv4Case) {
109                         final byte[] retBytes = new byte[SRC4_F_LENGTH + DEST4_F_LENGTH];
110                         ByteArray.copyWhole(Ipv4Util.bytesForAddress((((Ipv4Case) afi).getIpv4()).getSourceIpv4Address()), retBytes, SRC4_F_OFFSET);
111                         ByteArray.copyWhole(Ipv4Util.bytesForAddress((((Ipv4Case) afi).getIpv4()).getDestinationIpv4Address()), retBytes,
112                                         DEST4_F_OFFSET);
113                         return retBytes;
114                 } else if (afi instanceof Ipv6Case) {
115                         final byte[] retBytes = new byte[SRC6_F_LENGTH + DEST6_F_LENGTH];
116                         ByteArray.copyWhole(Ipv6Util.bytesForAddress((((Ipv6Case) afi).getIpv6()).getSourceIpv6Address()), retBytes, SRC6_F_OFFSET);
117                         ByteArray.copyWhole(Ipv6Util.bytesForAddress((((Ipv6Case) afi).getIpv6()).getDestinationIpv6Address()), retBytes,
118                                         DEST6_F_OFFSET);
119                         return retBytes;
120                 } else {
121                         throw new IllegalArgumentException("Wrong instance of NetworkAddress. Passed " + afi.getClass() + ". Needed IPv4");
122                 }
123         }
124
125         @Override
126         public int getObjectType() {
127                 return TYPE;
128         }
129
130         @Override
131         public int getObjectClass() {
132                 return CLASS;
133         }
134
135         public int get6ObjectType() {
136                 return TYPE_6;
137         }
138
139         public int get6ObjectClass() {
140                 return CLASS_6;
141         }
142 }