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