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