YANG revision dates mass-update
[bgpcep.git] / bmp / bmp-parser-impl / src / main / java / org / opendaylight / protocol / bmp / parser / message / RouteMonitoringMessageHandler.java
1 /*
2  * Copyright (c) 2015 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
9 package org.opendaylight.protocol.bmp.parser.message;
10
11 import static java.util.Objects.requireNonNull;
12
13 import com.google.common.base.Preconditions;
14 import io.netty.buffer.ByteBuf;
15 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
16 import org.opendaylight.protocol.bgp.parser.BGPParsingException;
17 import org.opendaylight.protocol.bgp.parser.spi.MessageRegistry;
18 import org.opendaylight.protocol.bmp.spi.parser.AbstractBmpPerPeerMessageParser;
19 import org.opendaylight.protocol.bmp.spi.parser.BmpDeserializationException;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.UpdateBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.UpdateMessage;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.message.rev200120.RouteMonitoringMessage;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.message.rev200120.RouteMonitoringMessageBuilder;
24 import org.opendaylight.yangtools.yang.binding.Notification;
25
26 public class RouteMonitoringMessageHandler extends AbstractBmpPerPeerMessageParser<RouteMonitoringMessageBuilder> {
27
28     private static final int MESSAGE_TYPE = 0;
29     private final MessageRegistry msgRegistry;
30
31     public RouteMonitoringMessageHandler(final MessageRegistry bgpMssageRegistry) {
32         super(bgpMssageRegistry);
33         this.msgRegistry = getBgpMessageRegistry();
34     }
35
36     @Override
37     public void serializeMessageBody(final Notification message, final ByteBuf buffer) {
38         super.serializeMessageBody(message, buffer);
39         Preconditions.checkArgument(message instanceof RouteMonitoringMessage,
40                 "An instance of RouteMonitoringMessage is required");
41         final RouteMonitoringMessage routeMonitor = (RouteMonitoringMessage) message;
42         this.msgRegistry.serializeMessage(new UpdateBuilder(routeMonitor.getUpdate()).build(), buffer);
43     }
44
45     @Override
46     public Notification parseMessageBody(final ByteBuf bytes) throws BmpDeserializationException {
47         final RouteMonitoringMessageBuilder routeMonitor = new RouteMonitoringMessageBuilder()
48                 .setPeerHeader(parsePerPeerHeader(bytes));
49         try {
50             final Notification message = this.msgRegistry.parseMessage(bytes, null);
51             requireNonNull(message, "UpdateMessage may not be null");
52             Preconditions.checkArgument(message instanceof UpdateMessage,
53                     "An instance of UpdateMessage is required");
54             final UpdateMessage updateMessage = (UpdateMessage) message;
55             final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.message.rev200120.route
56                     .monitoring.message.Update update = new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml
57                     .ns.yang.bmp.message.rev200120.route.monitoring.message.UpdateBuilder(updateMessage).build();
58             routeMonitor.setUpdate(update);
59         } catch (final BGPDocumentedException | BGPParsingException e) {
60             throw new BmpDeserializationException("Error while parsing Update Message.", e);
61         }
62
63         return routeMonitor.build();
64     }
65
66     @Override
67     public int getBmpMessageType() {
68         return MESSAGE_TYPE;
69     }
70 }