b665ca96cf96ebd751d0e362b6f845869c653e7c
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / PCEPMessageFactory.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.HashMap;
11
12 import org.opendaylight.protocol.framework.DeserializerException;
13 import org.opendaylight.protocol.framework.DocumentedException;
14 import org.opendaylight.protocol.framework.ProtocolMessage;
15 import org.opendaylight.protocol.framework.ProtocolMessageFactory;
16 import org.opendaylight.protocol.pcep.PCEPDeserializerException;
17 import org.opendaylight.protocol.pcep.PCEPDocumentedException;
18 import org.opendaylight.protocol.pcep.PCEPErrors;
19 import org.opendaylight.protocol.pcep.PCEPMessage;
20 import org.opendaylight.protocol.pcep.impl.message.PCCreateMessageParser;
21 import org.opendaylight.protocol.pcep.impl.message.PCEPCloseMessageParser;
22 import org.opendaylight.protocol.pcep.impl.message.PCEPErrorMessageParser;
23 import org.opendaylight.protocol.pcep.impl.message.PCEPKeepAliveMessageParser;
24 import org.opendaylight.protocol.pcep.impl.message.PCEPNotificationMessageParser;
25 import org.opendaylight.protocol.pcep.impl.message.PCEPOpenMessageParser;
26 import org.opendaylight.protocol.pcep.impl.message.PCEPRawMessage;
27 import org.opendaylight.protocol.pcep.impl.message.PCEPReplyMessageParser;
28 import org.opendaylight.protocol.pcep.impl.message.PCEPReportMessageParser;
29 import org.opendaylight.protocol.pcep.impl.message.PCEPRequestMessageParser;
30 import org.opendaylight.protocol.pcep.impl.message.PCEPUpdateRequestMessageParser;
31 import org.opendaylight.protocol.pcep.impl.message.PCEPXRAddTunnelMessageParser;
32 import org.opendaylight.protocol.pcep.impl.message.PCEPXRDeleteTunnelMessageParser;
33 import org.opendaylight.protocol.pcep.message.PCCreateMessage;
34 import org.opendaylight.protocol.pcep.message.PCEPCloseMessage;
35 import org.opendaylight.protocol.pcep.message.PCEPErrorMessage;
36 import org.opendaylight.protocol.pcep.message.PCEPKeepAliveMessage;
37 import org.opendaylight.protocol.pcep.message.PCEPNotificationMessage;
38 import org.opendaylight.protocol.pcep.message.PCEPOpenMessage;
39 import org.opendaylight.protocol.pcep.message.PCEPReplyMessage;
40 import org.opendaylight.protocol.pcep.message.PCEPReportMessage;
41 import org.opendaylight.protocol.pcep.message.PCEPRequestMessage;
42 import org.opendaylight.protocol.pcep.message.PCEPUpdateRequestMessage;
43 import org.opendaylight.protocol.pcep.message.PCEPXRAddTunnelMessage;
44 import org.opendaylight.protocol.pcep.message.PCEPXRDeleteTunnelMessage;
45 import org.opendaylight.protocol.util.ByteArray;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
48
49 import com.google.common.primitives.UnsignedBytes;
50
51 /**
52  * Factory for subclasses of {@link org.opendaylight.protocol.pcep.PCEPMessage PCEPMessage}
53  */
54 public class PCEPMessageFactory implements ProtocolMessageFactory {
55
56         private final static Logger logger = LoggerFactory.getLogger(PCEPMessageFactory.class);
57
58         private final static int TYPE_SIZE = 1; // bytes
59
60         private final static int LENGTH_SIZE = 2; // bytes
61
62         public final static int COMMON_HEADER_LENGTH = 4; // bytes
63
64         /**
65          * Type identifier for {@link org.opendaylight.protocol.pcep.PCEPMessage PCEPMessage}
66          */
67         public enum PCEPMessageType {
68                 OPEN(1), NOTIFICATION(5), KEEPALIVE(2), RESPONSE(4), REQUEST(3), ERROR(6), CLOSE(7), UPDATE_REQUEST(11), STATUS_REPORT(10),
69                 // TODO: replace with actual values by IANA
70                 XR_ADD_TUNNEL(8), XR_DELETE_TUNNEL(9), PCCREATE(12);
71
72                 private final int identifier;
73
74                 PCEPMessageType(final int identifier) {
75                         this.identifier = identifier;
76                 }
77
78                 public int getIdentifier() {
79                         return this.identifier;
80                 }
81
82                 public static PCEPMessageType getFromInt(final int type) throws PCEPDeserializerException {
83
84                         for (final PCEPMessageType type_e : PCEPMessageType.values()) {
85                                 if (type_e.getIdentifier() == type)
86                                         return type_e;
87                         }
88
89                         throw new PCEPDeserializerException("Unknown PCEPMessage Class identifier. Passed: " + type + "; Known: "
90                                         + PCEPMessageType.values() + ".");
91                 }
92         }
93
94         private static class MapOfParsers extends HashMap<PCEPMessageType, PCEPMessageParser> {
95
96                 private static final long serialVersionUID = -5715193806554448822L;
97
98                 private final static MapOfParsers instance = new MapOfParsers();
99
100                 private MapOfParsers() {
101                         this.fillInMap();
102                 }
103
104                 private void fillInMap() {
105                         this.put(PCEPMessageType.OPEN, new PCEPOpenMessageParser());
106                         this.put(PCEPMessageType.KEEPALIVE, new PCEPKeepAliveMessageParser());
107                         this.put(PCEPMessageType.NOTIFICATION, new PCEPNotificationMessageParser());
108                         this.put(PCEPMessageType.ERROR, new PCEPErrorMessageParser());
109                         this.put(PCEPMessageType.RESPONSE, new PCEPReplyMessageParser());
110                         this.put(PCEPMessageType.REQUEST, new PCEPRequestMessageParser());
111                         this.put(PCEPMessageType.UPDATE_REQUEST, new PCEPUpdateRequestMessageParser());
112                         this.put(PCEPMessageType.STATUS_REPORT, new PCEPReportMessageParser());
113                         this.put(PCEPMessageType.CLOSE, new PCEPCloseMessageParser());
114                         this.put(PCEPMessageType.XR_ADD_TUNNEL, new PCEPXRAddTunnelMessageParser());
115                         this.put(PCEPMessageType.XR_DELETE_TUNNEL, new PCEPXRDeleteTunnelMessageParser());
116                         this.put(PCEPMessageType.PCCREATE, new PCCreateMessageParser());
117                 }
118
119                 public static MapOfParsers getInstance() {
120                         return instance;
121                 }
122         }
123
124         /**
125          * 
126          * @param bytes assume array of bytes without common header
127          * @return Parsed specific PCEPMessage
128          * @throws PCEPDeserializerException
129          * @throws PCEPDocumentedException
130          */
131
132         @Override
133         public ProtocolMessage parse(final byte[] bytes) throws DeserializerException, DocumentedException {
134                 if (bytes == null || bytes.length == 0)
135                         throw new IllegalArgumentException("Array of bytes is mandatory.");
136
137                 logger.trace("Attempt to parse message from bytes: {}", ByteArray.bytesToHexString(bytes));
138
139                 final int type = UnsignedBytes.toInt(bytes[1]);
140
141                 final int msgLength = ByteArray.bytesToInt(ByteArray.subByte(bytes, TYPE_SIZE + 1, LENGTH_SIZE));
142
143                 final byte[] msgBody = ByteArray.cutBytes(bytes, TYPE_SIZE + 1 + LENGTH_SIZE);
144
145                 if (msgBody.length != (msgLength - COMMON_HEADER_LENGTH))
146                         throw new DeserializerException("Size don't match size specified in header. Passed: " + msgBody.length + "; Expected: "
147                                         + (msgLength - COMMON_HEADER_LENGTH) + ". " + msgLength);
148
149                 /*
150                  * if PCEPObjectIdentifier.getObjectClassFromInt() dont't throws
151                  * exception and if returned null we know the error type
152                  */
153                 PCEPMessageType msgType;
154                 try {
155                         msgType = PCEPMessageType.getFromInt(type);
156                 } catch (final PCEPDeserializerException e) {
157                         throw new DeserializerException(e.getMessage(), e);
158                 }
159                 if (msgType == null)
160                         throw new DocumentedException("Unhandled message type " + type, new PCEPDocumentedException("Unhandled message type " + type, PCEPErrors.CAPABILITY_NOT_SUPPORTED));
161
162                 PCEPMessage msg;
163                 try {
164                         msg = new PCEPRawMessage(PCEPObjectFactory.parseObjects(msgBody), msgType);
165                 } catch (final PCEPDeserializerException e) {
166                         throw new DeserializerException(e.getMessage(), e);
167                 } catch (final PCEPDocumentedException e) {
168                         throw new DocumentedException(e.getMessage(), e);
169                 }
170                 logger.debug("Message was parsed. {}", msg);
171                 return msg;
172         }
173
174         @Override
175         public byte[] put(final ProtocolMessage msg) {
176                 final PCEPMessage pcepMsg = (PCEPMessage) msg;
177                 if (pcepMsg == null)
178                         throw new IllegalArgumentException("PCEPMessage is mandatory.");
179
180                 final PCEPMessageType msgType;
181
182                 if (pcepMsg instanceof PCEPOpenMessage) {
183                         msgType = PCEPMessageType.OPEN;
184                 } else if (pcepMsg instanceof PCEPKeepAliveMessage) {
185                         msgType = PCEPMessageType.KEEPALIVE;
186                 } else if (pcepMsg instanceof PCEPCloseMessage) {
187                         msgType = PCEPMessageType.CLOSE;
188                 } else if (pcepMsg instanceof PCEPReplyMessage) {
189                         msgType = PCEPMessageType.RESPONSE;
190                 } else if (pcepMsg instanceof PCEPRequestMessage) {
191                         msgType = PCEPMessageType.REQUEST;
192                 } else if (pcepMsg instanceof PCEPNotificationMessage) {
193                         msgType = PCEPMessageType.NOTIFICATION;
194                 } else if (pcepMsg instanceof PCEPErrorMessage) {
195                         msgType = PCEPMessageType.ERROR;
196                 } else if (pcepMsg instanceof PCEPReportMessage) {
197                         msgType = PCEPMessageType.STATUS_REPORT;
198                 } else if (pcepMsg instanceof PCEPUpdateRequestMessage) {
199                         msgType = PCEPMessageType.UPDATE_REQUEST;
200                 } else if (pcepMsg instanceof PCEPXRAddTunnelMessage) {
201                         msgType = PCEPMessageType.XR_ADD_TUNNEL;
202                 } else if (pcepMsg instanceof PCEPXRDeleteTunnelMessage) {
203                         msgType = PCEPMessageType.XR_DELETE_TUNNEL;
204                 } else if (pcepMsg instanceof PCCreateMessage) {
205                         msgType = PCEPMessageType.PCCREATE;
206                 } else {
207                         logger.error("Unknown instance of PCEPMessage. Message class: {}", pcepMsg.getClass());
208                         throw new IllegalArgumentException("Unknown instance of PCEPMessage. Passed " + pcepMsg.getClass());
209                 }
210
211                 logger.trace("Serializing {}", msgType);
212
213                 final byte[] msgBody = MapOfParsers.getInstance().get(msgType).put(pcepMsg);
214
215                 final PCEPMessageHeader msgHeader = new PCEPMessageHeader(msgType.getIdentifier(), msgBody.length
216                                 + PCEPMessageHeader.COMMON_HEADER_LENGTH, PCEPMessage.PCEP_VERSION);
217
218                 final byte[] headerBytes = msgHeader.toBytes();
219                 final byte[] retBytes = new byte[headerBytes.length + msgBody.length];
220
221                 ByteArray.copyWhole(headerBytes, retBytes, 0);
222                 ByteArray.copyWhole(msgBody, retBytes, PCEPMessageHeader.COMMON_HEADER_LENGTH);
223
224                 return retBytes;
225         }
226 }