49cb74bd802798b2b8e4eba02d17c8dc4d43a99b
[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 java.util.List;
19
20 import org.jboss.netty.buffer.ChannelBuffer;
21 import org.onosproject.bgpio.exceptions.BgpParseException;
22 import org.onosproject.bgpio.protocol.BgpPrefixLSNlri;
23 import org.onosproject.bgpio.protocol.NlriType;
24 import org.onosproject.bgpio.protocol.linkstate.BgpNodeLSNlriVer4.ProtocolType;
25 import org.onosproject.bgpio.types.BgpValueType;
26 import org.onosproject.bgpio.types.RouteDistinguisher;
27 import org.onosproject.bgpio.util.Constants;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 import com.google.common.base.MoreObjects;
32
33 /**
34  * Implementation of Prefix IPV4 LS NLRI.
35  */
36 public class BgpPrefixIPv4LSNlriVer4 implements BgpPrefixLSNlri {
37
38     /*
39      * REFERENCE : draft-ietf-idr-ls-distribution-11
40      *       0                   1                   2                   3
41           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
42          +-+-+-+-+-+-+-+-+
43          |  Protocol-ID  |
44          +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
45          |                           Identifier                          |
46          |                            (64 bits)                          |
47          +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
48          //              Local Node Descriptor (variable)               //
49          +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
50          //                Prefix Descriptors (variable)                //
51          +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
52
53                 Figure : The IPv4/IPv6 Topology Prefix NLRI format
54      */
55
56     protected static final Logger log = LoggerFactory.getLogger(BgpPrefixIPv4LSNlriVer4.class);
57
58     public static final int PREFIX_IPV4_NLRITYPE = 3;
59     public static final int IDENTIFIER_LENGTH = 16;
60     private long identifier;
61     private byte protocolId;
62     private RouteDistinguisher routeDistinguisher;
63     private boolean isVpn;
64     private BgpPrefixLSIdentifier bgpPrefixLSIdentifier;
65
66     /**
67      * Resets parameters.
68      */
69     public BgpPrefixIPv4LSNlriVer4() {
70         this.identifier = 0;
71         this.protocolId = 0;
72         this.bgpPrefixLSIdentifier = null;
73         this.routeDistinguisher = null;
74         this.isVpn = false;
75     }
76
77     /**
78      * Constructor to initialize parameters for BGP PrefixLSNlri.
79      *
80      * @param identifier field in BGP PrefixLSNlri
81      * @param protocolId protocol Id
82      * @param bgpPrefixLSIdentifier prefix LS Identifier
83      * @param routeDistinguisher RouteDistinguisher
84      * @param isVpn vpn availability in message
85      */
86     public BgpPrefixIPv4LSNlriVer4(long identifier, byte protocolId, BgpPrefixLSIdentifier bgpPrefixLSIdentifier,
87                                    RouteDistinguisher routeDistinguisher, boolean isVpn) {
88         this.identifier = identifier;
89         this.protocolId = protocolId;
90         this.bgpPrefixLSIdentifier = bgpPrefixLSIdentifier;
91         this.routeDistinguisher = routeDistinguisher;
92         this.isVpn = isVpn;
93     }
94
95     /**
96      * Reads from channelBuffer and parses Prefix LS Nlri.
97      *
98      * @param cb ChannelBuffer
99      * @param afi Address family identifier
100      * @param safi Subsequent address family identifier
101      * @return object of BGPPrefixIPv4LSNlriVer4
102      * @throws BgpParseException while parsing Prefix LS Nlri
103      */
104     public static BgpPrefixIPv4LSNlriVer4 read(ChannelBuffer cb, short afi, byte safi) throws BgpParseException {
105
106         boolean isVpn = false;
107         RouteDistinguisher routeDistinguisher = null;
108         if ((afi == Constants.AFI_VALUE) && (safi == Constants.VPN_SAFI_VALUE)) {
109             routeDistinguisher = new RouteDistinguisher();
110             routeDistinguisher = RouteDistinguisher.read(cb);
111             isVpn = true;
112         } else {
113             isVpn = false;
114         }
115         byte protocolId = cb.readByte();
116         long identifier = cb.readLong();
117
118         BgpPrefixLSIdentifier bgpPrefixLSIdentifier = new BgpPrefixLSIdentifier();
119         bgpPrefixLSIdentifier = BgpPrefixLSIdentifier.parsePrefixIdendifier(cb, protocolId);
120         return new BgpPrefixIPv4LSNlriVer4(identifier, protocolId, bgpPrefixLSIdentifier, routeDistinguisher, isVpn);
121     }
122
123     @Override
124     public NlriType getNlriType() {
125         return NlriType.PREFIX_IPV4;
126     }
127
128     @Override
129     public NodeDescriptors getLocalNodeDescriptors() {
130         return this.bgpPrefixLSIdentifier.getLocalNodeDescriptors();
131     }
132
133     @Override
134     public long getIdentifier() {
135         return this.identifier;
136     }
137
138     /**
139      * Set the prefix LS identifier.
140      *
141      * @param bgpPrefixLSIdentifier prefix identifier to set
142      */
143     public void setPrefixLSIdentifier(BgpPrefixLSIdentifier bgpPrefixLSIdentifier) {
144         this.bgpPrefixLSIdentifier = bgpPrefixLSIdentifier;
145     }
146
147     @Override
148     public ProtocolType getProtocolId() throws BgpParseException {
149         switch (protocolId) {
150         case Constants.ISIS_LEVELONE:
151             return ProtocolType.ISIS_LEVEL_ONE;
152         case Constants.ISIS_LEVELTWO:
153             return ProtocolType.ISIS_LEVEL_TWO;
154         case Constants.OSPFV2:
155             return ProtocolType.OSPF_V2;
156         case Constants.DIRECT:
157             return ProtocolType.DIRECT;
158         case Constants.STATIC_CONFIGURATION:
159             return ProtocolType.STATIC_CONFIGURATION;
160         case Constants.OSPFV3:
161             return ProtocolType.OSPF_V3;
162         default:
163             throw new BgpParseException("protocol id not valid");
164         }
165     }
166
167     /**
168      * Returns whether VPN is present or not.
169      *
170      * @return whether VPN is present or not
171      */
172     public boolean isVpnPresent() {
173         return this.isVpn;
174     }
175
176     /**
177      * Returns Prefix Identifier.
178      *
179      * @return Prefix Identifier
180      */
181     public BgpPrefixLSIdentifier getPrefixIdentifier() {
182         return this.bgpPrefixLSIdentifier;
183     }
184
185     @Override
186     public RouteDistinguisher getRouteDistinguisher() {
187         return this.routeDistinguisher;
188     }
189
190     @Override
191     public List<BgpValueType> getPrefixdescriptor() {
192         return this.bgpPrefixLSIdentifier.getPrefixdescriptor();
193     }
194
195     @Override
196     public String toString() {
197         return MoreObjects.toStringHelper(getClass())
198                 .omitNullValues()
199                 .add("protocolId", protocolId)
200                 .add("identifier", identifier)
201                 .add("RouteDistinguisher ", routeDistinguisher)
202                 .add("bgpPrefixLSIdentifier", bgpPrefixLSIdentifier)
203                 .toString();
204     }
205 }