01d369e45b8579dd76a0f56505e0008b24a3b3c7
[onosfw.git] /
1 /*\r
2  * Copyright 2015 Open Networking Laboratory\r
3  *\r
4  * Licensed under the Apache License, Version 2.0 (the "License");\r
5  * you may not use this file except in compliance with the License.\r
6  * You may obtain a copy of the License at\r
7  *\r
8  *     http://www.apache.org/licenses/LICENSE-2.0\r
9  *\r
10  * Unless required by applicable law or agreed to in writing, software\r
11  * distributed under the License is distributed on an "AS IS" BASIS,\r
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
13  * See the License for the specific language governing permissions and\r
14  * limitations under the License.\r
15  */\r
16 package org.onosproject.bgpio.protocol.linkstate;\r
17 \r
18 import java.util.List;\r
19 \r
20 import org.jboss.netty.buffer.ChannelBuffer;\r
21 import org.onosproject.bgpio.exceptions.BgpParseException;\r
22 import org.onosproject.bgpio.protocol.BgpLinkLsNlri;\r
23 import org.onosproject.bgpio.protocol.NlriType;\r
24 import org.onosproject.bgpio.protocol.linkstate.BgpNodeLSNlriVer4.ProtocolType;\r
25 import org.onosproject.bgpio.types.BgpErrorType;\r
26 import org.onosproject.bgpio.types.BgpValueType;\r
27 import org.onosproject.bgpio.types.RouteDistinguisher;\r
28 import org.onosproject.bgpio.util.Constants;\r
29 import org.slf4j.Logger;\r
30 import org.slf4j.LoggerFactory;\r
31 \r
32 import com.google.common.base.MoreObjects;\r
33 \r
34 /**\r
35  * Implementation of Link LS NLRI.\r
36  */\r
37 public class BgpLinkLsNlriVer4 implements BgpLinkLsNlri {\r
38 \r
39     /*\r
40      * REFERENCE : draft-ietf-idr-ls-distribution-11\r
41           0                   1                   2                   3\r
42           0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1\r
43          +-+-+-+-+-+-+-+-+\r
44          |  Protocol-ID  |\r
45          +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+\r
46          |                           Identifier                          |\r
47          |                            (64 bits)                          |\r
48          +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+\r
49          //               Local Node Descriptors (variable)             //\r
50          +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+\r
51          //               Remote Node Descriptors (variable)            //\r
52          +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+\r
53          //                  Link Descriptors (variable)                //\r
54          +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+\r
55 \r
56                           Figure : The Link NLRI format\r
57      */\r
58     private static final Logger log = LoggerFactory.getLogger(BgpLinkLsNlriVer4.class);\r
59     public static final int LINK_NLRITYPE = 2;\r
60 \r
61     private BgpLinkLSIdentifier linkLSIdentifier;\r
62     private byte protocolId;\r
63     private long identifier;\r
64     private RouteDistinguisher routeDistinguisher;\r
65     private boolean isVpn;\r
66 \r
67     /**\r
68      * Initialize fields.\r
69      */\r
70     public BgpLinkLsNlriVer4() {\r
71         this.protocolId = 0;\r
72         this.identifier = 0;\r
73         this.linkLSIdentifier = null;\r
74         this.routeDistinguisher = null;\r
75         this.isVpn = false;\r
76     }\r
77 \r
78     /**\r
79      * Constructor to initialize parameters for BGP LinkLSNlri.\r
80      *\r
81      * @param protocolId protocol Id\r
82      * @param identifier field in BGP LinkLSNlri\r
83      * @param linkLSIdentifier link LS identifier\r
84      * @param routeDistinguisher route distinguisher from message\r
85      * @param isVpn vpn info availability in message\r
86      */\r
87     public BgpLinkLsNlriVer4(byte protocolId, long identifier, BgpLinkLSIdentifier linkLSIdentifier,\r
88                              RouteDistinguisher routeDistinguisher, boolean isVpn) {\r
89         this.protocolId = protocolId;\r
90         this.identifier = identifier;\r
91         this.linkLSIdentifier = linkLSIdentifier;\r
92         this.routeDistinguisher = routeDistinguisher;\r
93         this.isVpn = isVpn;\r
94     }\r
95 \r
96     /**\r
97      * Reads from channelBuffer and parses Link LS Nlri.\r
98      *\r
99      * @param cb ChannelBuffer\r
100      * @param afi Address Family Identifier\r
101      * @param safi Subsequent Address Family Identifier\r
102      * @return object of this class\r
103      * @throws BgpParseException while parsing Link LS NLRI\r
104      */\r
105     public static BgpLinkLsNlriVer4 read(ChannelBuffer cb, short afi, byte safi) throws BgpParseException {\r
106         boolean isVpn = false;\r
107         RouteDistinguisher routeDistinguisher = null;\r
108         if ((afi == Constants.AFI_VALUE) && (safi == Constants.VPN_SAFI_VALUE)) {\r
109             routeDistinguisher = new RouteDistinguisher();\r
110             routeDistinguisher = RouteDistinguisher.read(cb);\r
111             isVpn = true;\r
112         } else {\r
113             isVpn = false;\r
114         }\r
115         byte protocolId = cb.readByte();\r
116         long identifier = cb.readLong();\r
117 \r
118         BgpLinkLSIdentifier linkLSIdentifier = new BgpLinkLSIdentifier();\r
119         linkLSIdentifier = BgpLinkLSIdentifier.parseLinkIdendifier(cb, protocolId);\r
120         return new BgpLinkLsNlriVer4(protocolId, identifier, linkLSIdentifier, routeDistinguisher, isVpn);\r
121     }\r
122 \r
123     @Override\r
124     public NlriType getNlriType() {\r
125         return NlriType.LINK;\r
126     }\r
127 \r
128     @Override\r
129     public long getIdentifier() {\r
130         return this.identifier;\r
131     }\r
132 \r
133     /**\r
134      * Set the link LS identifier.\r
135      *\r
136      * @param linkLSIdentifier link LS identifier to set\r
137      */\r
138     public void setLinkLSIdentifier(BgpLinkLSIdentifier linkLSIdentifier) {\r
139         this.linkLSIdentifier = linkLSIdentifier;\r
140     }\r
141 \r
142     @Override\r
143     public ProtocolType getProtocolId() throws BgpParseException {\r
144         switch (protocolId) {\r
145         case Constants.ISIS_LEVELONE:\r
146             return ProtocolType.ISIS_LEVEL_ONE;\r
147         case Constants.ISIS_LEVELTWO:\r
148             return ProtocolType.ISIS_LEVEL_TWO;\r
149         case Constants.OSPFV2:\r
150             return ProtocolType.OSPF_V2;\r
151         case Constants.DIRECT:\r
152             return ProtocolType.DIRECT;\r
153         case Constants.STATIC_CONFIGURATION:\r
154             return ProtocolType.STATIC_CONFIGURATION;\r
155         case Constants.OSPFV3:\r
156             return ProtocolType.OSPF_V3;\r
157         default:\r
158             throw new BgpParseException(BgpErrorType.UPDATE_MESSAGE_ERROR, (byte) 0, null);\r
159         }\r
160     }\r
161 \r
162     @Override\r
163     public NodeDescriptors localNodeDescriptors() {\r
164         return this.linkLSIdentifier.localNodeDescriptors();\r
165     }\r
166 \r
167     @Override\r
168     public NodeDescriptors remoteNodeDescriptors() {\r
169         return this.linkLSIdentifier.remoteNodeDescriptors();\r
170     }\r
171 \r
172     /**\r
173      * Returns whether VPN is present or not.\r
174      *\r
175      * @return whether VPN is present or not\r
176      */\r
177     public boolean isVpnPresent() {\r
178         return this.isVpn;\r
179     }\r
180 \r
181     @Override\r
182     public RouteDistinguisher getRouteDistinguisher() {\r
183         return this.routeDistinguisher;\r
184     }\r
185 \r
186     /**\r
187      * Returns link identifier.\r
188      *\r
189      * @return link identifier\r
190      */\r
191     public BgpLinkLSIdentifier getLinkIdentifier() {\r
192         return this.linkLSIdentifier;\r
193     }\r
194 \r
195     @Override\r
196     public List<BgpValueType> linkDescriptors() {\r
197         return this.linkLSIdentifier.linkDescriptors();\r
198     }\r
199 \r
200     @Override\r
201     public String toString() {\r
202         return MoreObjects.toStringHelper(getClass())\r
203                 .omitNullValues()\r
204                 .add("protocolId", protocolId)\r
205                 .add("identifier", identifier)\r
206                 .add("RouteDistinguisher ", routeDistinguisher)\r
207                 .add("linkLSIdentifier", linkLSIdentifier)\r
208                 .toString();\r
209     }\r
210 }\r