52e0d6018326bc4a5ba5cd306f23ca9a8d23da36
[bgpcep.git] / bgp / bmp-parser-impl / src / main / java / org / opendaylight / protocol / bmp / parser / message / PeerDownHandler.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 import static org.opendaylight.protocol.bmp.parser.message.PeerDownHandler.Reason.REASON_FOUR;
13 import static org.opendaylight.protocol.bmp.parser.message.PeerDownHandler.Reason.REASON_ONE;
14 import static org.opendaylight.protocol.bmp.parser.message.PeerDownHandler.Reason.REASON_THREE;
15 import static org.opendaylight.protocol.bmp.parser.message.PeerDownHandler.Reason.REASON_TWO;
16 import com.google.common.base.Preconditions;
17 import com.google.common.collect.ImmutableMap;
18 import io.netty.buffer.ByteBuf;
19 import java.util.Map;
20 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
21 import org.opendaylight.protocol.bgp.parser.BGPParsingException;
22 import org.opendaylight.protocol.bgp.parser.spi.MessageRegistry;
23 import org.opendaylight.protocol.bmp.spi.parser.AbstractBmpPerPeerMessageParser;
24 import org.opendaylight.protocol.bmp.spi.parser.BmpDeserializationException;
25 import org.opendaylight.protocol.util.ByteBufWriteUtil;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.NotifyBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.NotifyMessage;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.message.rev150512.PeerDownNotification;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.message.rev150512.PeerDownNotificationBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.message.rev150512.peer.down.Data;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.message.rev150512.peer.down.data.FsmEventCode;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.message.rev150512.peer.down.data.FsmEventCodeBuilder;
33 import org.opendaylight.yangtools.yang.binding.Notification;
34
35 public class PeerDownHandler extends AbstractBmpPerPeerMessageParser<PeerDownNotificationBuilder> {
36
37     private static final int MESSAGE_TYPE = 2;
38     private final MessageRegistry msgRegistry;
39
40     public PeerDownHandler(final MessageRegistry bgpMssageRegistry) {
41         super(bgpMssageRegistry);
42         this.msgRegistry = getBgpMessageRegistry();
43     }
44
45     @Override
46     public void serializeMessageBody(final Notification message, final ByteBuf buffer) {
47         super.serializeMessageBody(message, buffer);
48         Preconditions.checkArgument(message instanceof PeerDownNotification, "An instance of PeerDownNotification is required");
49         final PeerDownNotification peerDown = (PeerDownNotification) message;
50         if (peerDown.isLocalSystemClosed()) {
51             if (peerDown.getData() instanceof FsmEventCode) {
52                 ByteBufWriteUtil.writeUnsignedByte(REASON_TWO.getValue(), buffer);
53                 ByteBufWriteUtil.writeUnsignedShort(((FsmEventCode) peerDown.getData()).getFsmEventCode().intValue(), buffer);
54             } else if (peerDown.getData() instanceof
55                 org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.message.rev150512.peer.down.data.Notification) {
56                 ByteBufWriteUtil.writeUnsignedByte(REASON_ONE.getValue(), buffer);
57                 serializePDU(peerDown.getData(), buffer);
58             }
59         } else {
60             if (peerDown.getData() instanceof
61                 org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.message.rev150512.peer.down.data.Notification) {
62                 ByteBufWriteUtil.writeUnsignedByte(REASON_THREE.getValue(), buffer);
63                 serializePDU(peerDown.getData(), buffer);
64             } else {
65                 ByteBufWriteUtil.writeUnsignedByte(REASON_FOUR.getValue(), buffer);
66             }
67         }
68     }
69
70     private void serializePDU(final Data data, final ByteBuf buffer) {
71         final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.message.rev150512.peer.down.data.Notification notification
72             = (org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.message.rev150512.peer.down.data.Notification) data;
73         this.msgRegistry.serializeMessage(new NotifyBuilder(notification.getNotification()).build(), buffer);
74     }
75
76     @Override
77     public Notification parseMessageBody(final ByteBuf bytes) throws BmpDeserializationException {
78         final PeerDownNotificationBuilder peerDown = new PeerDownNotificationBuilder().setPeerHeader(parsePerPeerHeader(bytes));
79         final Reason reason = Reason.forValue(bytes.readUnsignedByte());
80         if (reason != null) {
81             switch (reason) {
82             case REASON_ONE:
83                 peerDown.setLocalSystemClosed(true);
84                 peerDown.setData(parseBgpNotificationMessage(bytes));
85                 break;
86             case REASON_TWO:
87                 peerDown.setLocalSystemClosed(true);
88                 peerDown.setData(new FsmEventCodeBuilder().setFsmEventCode(bytes.readUnsignedShort()).build());
89                 break;
90             case REASON_THREE:
91             case REASON_FOUR:
92                 peerDown.setLocalSystemClosed(false);
93                 peerDown.setData(parseBgpNotificationMessage(bytes));
94                 break;
95             case REASON_FIVE:
96                 peerDown.setLocalSystemClosed(false);
97                 break;
98             default:
99                 break;
100             }
101         }
102
103         return peerDown.build();
104     }
105
106     private org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.message.rev150512.peer.down.data.Notification parseBgpNotificationMessage(final ByteBuf bytes) throws BmpDeserializationException {
107         final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.message.rev150512.peer.down.data.NotificationBuilder notificationCBuilder
108             = new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.message.rev150512.peer.down.data.NotificationBuilder();
109
110         final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.message.rev150512.peer.down.data.notification.NotificationBuilder notificationBuilder
111             = new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.message.rev150512.peer.down.data.notification.NotificationBuilder();
112         try {
113             final Notification not = this.msgRegistry.parseMessage(bytes, null);
114             requireNonNull(not, "Notify message may not be null.");
115             Preconditions.checkArgument(not instanceof NotifyMessage, "An instance of NotifyMessage is required");
116             notificationBuilder.fieldsFrom((NotifyMessage) not);
117             notificationCBuilder.setNotification(notificationBuilder.build());
118         } catch (final BGPDocumentedException | BGPParsingException e) {
119             throw new BmpDeserializationException("Error while parsing BGP Notification message.", e);
120         }
121
122         return notificationCBuilder.build();
123     }
124
125     @Override
126     public int getBmpMessageType() {
127         return MESSAGE_TYPE;
128     }
129
130     public enum Reason {
131
132         REASON_ONE((short) 1), REASON_TWO((short) 2), REASON_THREE((short) 3), REASON_FOUR((short) 4), REASON_FIVE((short) 5);
133
134         private static final Map<Short, Reason> VALUE_MAP;
135
136         static {
137             final ImmutableMap.Builder<Short, Reason> b = ImmutableMap.builder();
138             for (final Reason enumItem : Reason.values()) {
139                 b.put(enumItem.getValue(), enumItem);
140             }
141             VALUE_MAP = b.build();
142         }
143
144         private final short value;
145
146         Reason(final short value) {
147             this.value = value;
148         }
149
150         public static Reason forValue(final short value) {
151             return VALUE_MAP.get(value);
152         }
153
154         public short getValue() {
155             return this.value;
156         }
157     }
158 }