BUG-47 : removed PCEPMessage interface, switched to generated Message.
[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.ArrayList;
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.spi.RawMessage;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Message;
19
20 import com.google.common.base.Preconditions;
21
22 /**
23  * A PCEP message parser which also does validation.
24  */
25 public final class PCEPMessageFactory implements ProtocolMessageFactory<Message> {
26         private static final RawPCEPMessageFactory rawFactory = new RawPCEPMessageFactory();
27
28         @Override
29         public List<Message> parse(final byte[] bytes) throws DeserializerException, DocumentedException {
30                 final List<Message> parsed = rawFactory.parse(bytes);
31                 final List<Message> validated = new ArrayList<>(parsed.size());
32
33                 for (final Message msg : parsed) {
34                         Preconditions.checkState(msg instanceof RawMessage);
35                         final RawMessage raw = (RawMessage) msg;
36
37                         try {
38                                 validated.addAll(PCEPMessageValidator.getValidator(raw.getMsgType()).validate(raw.getAllObjects()));
39                         } catch (final PCEPDeserializerException e) {
40                                 // FIXME: at validation time we may want to terminate with:
41                                 // logger.error("Malformed message, terminating. ", e);
42                                 // this.terminate(Reason.MALFORMED_MSG);
43                                 throw e;
44                         }
45                 }
46
47                 return validated;
48         }
49
50         @Override
51         public byte[] put(final Message msg) {
52                 return rawFactory.put(msg);
53         }
54 }