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