Merge "BUG-113: make SimpleFamilyRegistry abstract"
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / PCEPEROSubobjectParser.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;
9
10 import java.util.ArrayList;
11 import java.util.List;
12
13 import org.opendaylight.protocol.concepts.IPv4Prefix;
14 import org.opendaylight.protocol.concepts.IPv6Prefix;
15 import org.opendaylight.protocol.pcep.PCEPDeserializerException;
16 import org.opendaylight.protocol.pcep.impl.subobject.EROExplicitExclusionRouteSubobjectParser;
17 import org.opendaylight.protocol.pcep.impl.subobject.EROLabelSubobjectParser;
18 import org.opendaylight.protocol.pcep.impl.subobject.EROPathKeyWith128PCEIDSubobjectParser;
19 import org.opendaylight.protocol.pcep.impl.subobject.EROPathKeyWith32PCEIDSubobjectParser;
20 import org.opendaylight.protocol.pcep.impl.subobject.EROUnnumberedInterfaceSubobjectParser;
21 import org.opendaylight.protocol.pcep.subobject.EROAsNumberSubobject;
22 import org.opendaylight.protocol.pcep.subobject.EROExplicitExclusionRouteSubobject;
23 import org.opendaylight.protocol.pcep.subobject.EROIPPrefixSubobject;
24 import org.opendaylight.protocol.pcep.subobject.EROLabelSubobject;
25 import org.opendaylight.protocol.pcep.subobject.EROPathKeyWith128PCEIDSubobject;
26 import org.opendaylight.protocol.pcep.subobject.EROPathKeyWith32PCEIDSubobject;
27 import org.opendaylight.protocol.pcep.subobject.EROUnnumberedInterfaceSubobject;
28 import org.opendaylight.protocol.pcep.subobject.ExplicitRouteSubobject;
29 import org.opendaylight.protocol.util.ByteArray;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 /**
34  * Parser for {@link org.opendaylight.protocol.pcep.PCEPSubobject PCEPSubobject}
35  */
36 public class PCEPEROSubobjectParser {
37
38         private static final Logger logger = LoggerFactory.getLogger(PCEPEROSubobjectParser.class);
39
40         /**
41          * Type identifier for {@link org.opendaylight.protocol.pcep.PCEPSubobject PCEPSubobject}
42          */
43         public enum PCEPSubobjectType {
44                 IPv4_PREFIX(1), IPv6_PREFIX(2), LABEL(3), UNNUMBERED_INTERFACE_ID(4), AS_NUMBER(32), EXRS(33), PROTECTION(37), PK_32(64), PK_128(65);
45
46                 private final int indicator;
47
48                 PCEPSubobjectType(final int indicator) {
49                         this.indicator = indicator;
50                 }
51
52                 public int getIndicator() {
53                         return this.indicator;
54                 }
55
56                 public static PCEPSubobjectType getFromInt(final int type) throws PCEPDeserializerException {
57
58                         for (final PCEPSubobjectType type_e : PCEPSubobjectType.values()) {
59                                 if (type_e.getIndicator() == type)
60                                         return type_e;
61                         }
62
63                         throw new PCEPDeserializerException("Unknown Subobject type. Passed: " + type + "; Known: " + PCEPSubobjectType.values() + ".");
64                 }
65         }
66
67         /*
68          * Fields lengths in Bytes
69          */
70         public static final int TYPE_FLAG_F_LENGTH = 1;
71         public static final int LENGTH_F_LENGTH = 1;
72
73         /*
74          * Fields offsets in Bytes
75          */
76         public static final int TYPE_FLAG_F_OFFSET = 0;
77         public static final int LENGTH_F_OFFSET = TYPE_FLAG_F_OFFSET + TYPE_FLAG_F_LENGTH;
78         public static final int SO_CONTENTS_OFFSET = LENGTH_F_OFFSET + LENGTH_F_LENGTH;
79
80         public static List<ExplicitRouteSubobject> parse(final byte[] bytes) throws PCEPDeserializerException {
81                 if (bytes == null)
82                         throw new IllegalArgumentException("Byte array is mandatory.");
83
84                 final List<ExplicitRouteSubobject> subobjsList = new ArrayList<ExplicitRouteSubobject>();
85                 boolean loose_flag;
86                 PCEPSubobjectType type;
87                 byte[] soContentsBytes;
88                 int length;
89                 int offset = 0;
90
91                 while (offset < bytes.length) {
92
93                         loose_flag = ((bytes[offset + TYPE_FLAG_F_OFFSET] & (1 << 7)) != 0) ? true : false;
94                         length = ByteArray.bytesToInt(ByteArray.subByte(bytes, offset + LENGTH_F_OFFSET, LENGTH_F_LENGTH));
95
96                         type = PCEPSubobjectType.getFromInt((bytes[offset + TYPE_FLAG_F_OFFSET] & 0xff) & ~(1 << 7));
97
98                         if (length > bytes.length - offset)
99                                 throw new PCEPDeserializerException("Wrong length specified. Passed: " + length + "; Expected: <= "
100                                                 + (bytes.length - offset));
101
102                         soContentsBytes = new byte[length - SO_CONTENTS_OFFSET];
103                         System.arraycopy(bytes, offset + SO_CONTENTS_OFFSET, soContentsBytes, 0, length - SO_CONTENTS_OFFSET);
104
105                         logger.debug("Attempt to parse subobject from bytes: {}", ByteArray.bytesToHexString(soContentsBytes));
106                         final ExplicitRouteSubobject subObj = parseSpecificSubobject(type, soContentsBytes, loose_flag);
107                         logger.debug("Subobject was parsed. {}", subObj);
108
109                         subobjsList.add(subObj);
110
111                         offset += length;
112                 }
113
114                 return subobjsList;
115         }
116
117         public static byte[] put(final List<ExplicitRouteSubobject> objsToSerialize) {
118                 final List<byte[]> bytesList = new ArrayList<byte[]>(objsToSerialize.size());
119
120                 int length = 0;
121                 for (final ExplicitRouteSubobject obj : objsToSerialize) {
122                         final byte[] bytes = put(obj);
123                         length += bytes.length;
124                         bytesList.add(bytes);
125                 }
126
127                 final byte[] retBytes = new byte[length];
128
129                 int offset = 0;
130                 for (final byte[] bytes : bytesList) {
131                         System.arraycopy(bytes, 0, retBytes, offset, bytes.length);
132                         offset += bytes.length;
133                 }
134
135                 return retBytes;
136         }
137
138         public static byte[] put(final ExplicitRouteSubobject objToSerialize) {
139                 int typeIndicator = 0;
140
141                 byte[] soContentsBytes = null;
142
143                 if (objToSerialize instanceof EROIPPrefixSubobject<?>
144                                 && ((EROIPPrefixSubobject<?>) objToSerialize).getPrefix() instanceof IPv4Prefix) {
145                         typeIndicator = PCEPSubobjectType.IPv4_PREFIX.getIndicator();
146                         // soContentsBytes = EROIpPrefixSubobjectParser.put(objToSerialize);
147                 } else if (objToSerialize instanceof EROIPPrefixSubobject<?>
148                                 && ((EROIPPrefixSubobject<?>) objToSerialize).getPrefix() instanceof IPv6Prefix) {
149                         typeIndicator = PCEPSubobjectType.IPv6_PREFIX.getIndicator();
150                         // soContentsBytes = EROIPv6PrefixSubobjectParser.put(objToSerialize);
151                 } else if (objToSerialize instanceof EROAsNumberSubobject) {
152                         typeIndicator = PCEPSubobjectType.AS_NUMBER.getIndicator();
153                         // soContentsBytes = EROAsNumberSubobjectParser.put(objToSerialize);
154                 } else if (objToSerialize instanceof EROUnnumberedInterfaceSubobject) {
155                         typeIndicator = PCEPSubobjectType.UNNUMBERED_INTERFACE_ID.getIndicator();
156                         soContentsBytes = EROUnnumberedInterfaceSubobjectParser.put(objToSerialize);
157                 } else if (objToSerialize instanceof EROLabelSubobject) {
158                         typeIndicator = PCEPSubobjectType.LABEL.getIndicator();
159                         soContentsBytes = EROLabelSubobjectParser.put((EROLabelSubobject) objToSerialize);
160                 } else if (objToSerialize instanceof EROExplicitExclusionRouteSubobject) {
161                         typeIndicator = PCEPSubobjectType.EXRS.getIndicator();
162                         soContentsBytes = EROExplicitExclusionRouteSubobjectParser.put((EROExplicitExclusionRouteSubobject) objToSerialize);
163                 } else if (objToSerialize instanceof EROPathKeyWith32PCEIDSubobject) {
164                         typeIndicator = PCEPSubobjectType.PK_32.getIndicator();
165                         soContentsBytes = EROPathKeyWith32PCEIDSubobjectParser.put((EROPathKeyWith32PCEIDSubobject) objToSerialize);
166                 } else if (objToSerialize instanceof EROPathKeyWith128PCEIDSubobject) {
167                         typeIndicator = PCEPSubobjectType.PK_128.getIndicator();
168                         soContentsBytes = EROPathKeyWith128PCEIDSubobjectParser.put((EROPathKeyWith128PCEIDSubobject) objToSerialize);
169                 } else
170                         throw new IllegalArgumentException("Unknown instance of PCEPSubobject. Passed: " + objToSerialize.getClass() + ".");
171
172                 final byte[] bytes = new byte[SO_CONTENTS_OFFSET + soContentsBytes.length];
173
174                 bytes[TYPE_FLAG_F_OFFSET] = (byte) (ByteArray.cutBytes(ByteArray.intToBytes(typeIndicator), (Integer.SIZE / 8) - TYPE_FLAG_F_LENGTH)[0] | (objToSerialize.isLoose() ? 1 << 7
175                                 : 0));
176                 bytes[LENGTH_F_OFFSET] = ByteArray.cutBytes(ByteArray.intToBytes(soContentsBytes.length + SO_CONTENTS_OFFSET), (Integer.SIZE / 8)
177                                 - LENGTH_F_LENGTH)[0];
178
179                 System.arraycopy(soContentsBytes, 0, bytes, SO_CONTENTS_OFFSET, soContentsBytes.length);
180
181                 return bytes;
182         }
183
184         private static ExplicitRouteSubobject parseSpecificSubobject(final PCEPSubobjectType type, final byte[] soContentsBytes,
185                         final boolean loose_flag) throws PCEPDeserializerException {
186
187                 switch (type) {
188                 case IPv4_PREFIX:
189                         // return EROIpPrefixSubobjectParser.parse(soContentsBytes, loose_flag);
190                 case IPv6_PREFIX:
191                         // return EROIPv6PrefixSubobjectParser.parse(soContentsBytes, loose_flag);
192                 case UNNUMBERED_INTERFACE_ID:
193                         return EROUnnumberedInterfaceSubobjectParser.parse(soContentsBytes, loose_flag);
194                 case AS_NUMBER:
195                         // return EROAsNumberSubobjectParser.parse(soContentsBytes, loose_flag);
196                 case LABEL:
197                         return EROLabelSubobjectParser.parse(soContentsBytes, loose_flag);
198                 case EXRS:
199                         return EROExplicitExclusionRouteSubobjectParser.parse(soContentsBytes, loose_flag);
200                 case PK_32:
201                         return EROPathKeyWith32PCEIDSubobjectParser.parse(soContentsBytes, loose_flag);
202                 case PK_128:
203                         return EROPathKeyWith128PCEIDSubobjectParser.parse(soContentsBytes, loose_flag);
204                 default:
205                         throw new PCEPDeserializerException("Unknown Subobject type. Passed: " + type + ".");
206                 }
207         }
208 }