Committed by
Gerrit Code Review
[Emu][ONOS-2593, ONOS-2594] BGP SBI controller and session handler
Change-Id: Ia95717ff173b2e3e1198bdd0fafef7cb0aa8f734
Showing
20 changed files
with
1467 additions
and
0 deletions
This diff is collapsed. Click to expand it.
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 | + | ||
17 | +package org.onosproject.bgp.controller; | ||
18 | + | ||
19 | +import org.onosproject.bgpio.protocol.BGPMessage; | ||
20 | + | ||
21 | +/** | ||
22 | + * Abstraction of an BGP controller. Serves as a one stop shop for obtaining BGP devices and (un)register listeners | ||
23 | + * on bgp events | ||
24 | + */ | ||
25 | +public interface BGPController { | ||
26 | + | ||
27 | + /** | ||
28 | + * Send a message to a particular bgp peer. | ||
29 | + * | ||
30 | + * @param bgpId the id of the peer to send message. | ||
31 | + * @param msg the message to send | ||
32 | + */ | ||
33 | + void writeMsg(BGPId bgpId, BGPMessage msg); | ||
34 | + | ||
35 | + /** | ||
36 | + * Process a message and notify the appropriate listeners. | ||
37 | + * | ||
38 | + * @param bgpId id of the peer the message arrived on | ||
39 | + * @param msg the message to process. | ||
40 | + */ | ||
41 | + void processBGPPacket(BGPId bgpId, BGPMessage msg); | ||
42 | + | ||
43 | + /** | ||
44 | + * Get the BGPConfig class to the caller. | ||
45 | + * | ||
46 | + * @return configuration object | ||
47 | + */ | ||
48 | + BGPCfg getConfig(); | ||
49 | +} | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
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 | + | ||
17 | +package org.onosproject.bgp.controller; | ||
18 | + | ||
19 | +import org.onlab.packet.IpAddress; | ||
20 | +import java.net.URI; | ||
21 | +import java.net.URISyntaxException; | ||
22 | +import java.util.Objects; | ||
23 | + | ||
24 | +import static com.google.common.base.Preconditions.checkArgument; | ||
25 | + | ||
26 | +/** | ||
27 | + * The class representing a network peer bgp ip. | ||
28 | + * This class is immutable. | ||
29 | + */ | ||
30 | +public final class BGPId { | ||
31 | + | ||
32 | + private static final String SCHEME = "bgp"; | ||
33 | + private static final long UNKNOWN = 0; | ||
34 | + private final IpAddress ipAddress; | ||
35 | + | ||
36 | + /** | ||
37 | + * Private constructor. | ||
38 | + */ | ||
39 | + private BGPId(IpAddress ipAddress) { | ||
40 | + this.ipAddress = ipAddress; | ||
41 | + } | ||
42 | + | ||
43 | + /** | ||
44 | + * Create a BGPId from ip address. | ||
45 | + * | ||
46 | + * @param ipAddress IP address | ||
47 | + * @return object of BGPId | ||
48 | + */ | ||
49 | + public static BGPId bgpId(IpAddress ipAddress) { | ||
50 | + return new BGPId(ipAddress); | ||
51 | + } | ||
52 | + | ||
53 | + /** | ||
54 | + * Returns the ip address. | ||
55 | + * | ||
56 | + * @return ipAddress | ||
57 | + */ | ||
58 | + public IpAddress ipAddress() { | ||
59 | + return ipAddress; | ||
60 | + } | ||
61 | + | ||
62 | + /** | ||
63 | + * Convert the BGPId value to a ':' separated hexadecimal string. | ||
64 | + * | ||
65 | + * @return the BGPId value as a ':' separated hexadecimal string. | ||
66 | + */ | ||
67 | + @Override | ||
68 | + public String toString() { | ||
69 | + return ipAddress.toString(); | ||
70 | + } | ||
71 | + | ||
72 | + @Override | ||
73 | + public boolean equals(Object other) { | ||
74 | + if (!(other instanceof BGPId)) { | ||
75 | + return false; | ||
76 | + } | ||
77 | + | ||
78 | + BGPId otherBGPid = (BGPId) other; | ||
79 | + return Objects.equals(ipAddress, otherBGPid.ipAddress); | ||
80 | + } | ||
81 | + | ||
82 | + @Override | ||
83 | + public int hashCode() { | ||
84 | + return Objects.hash(ipAddress); | ||
85 | + } | ||
86 | + | ||
87 | + /** | ||
88 | + * Returns BGPId created from the given device URI. | ||
89 | + * | ||
90 | + * @param uri device URI | ||
91 | + * @return object of BGPId | ||
92 | + */ | ||
93 | + public static BGPId bgpId(URI uri) { | ||
94 | + checkArgument(uri.getScheme().equals(SCHEME), "Unsupported URI scheme"); | ||
95 | + return new BGPId(IpAddress.valueOf(uri.getSchemeSpecificPart())); | ||
96 | + } | ||
97 | + | ||
98 | + /** | ||
99 | + * Produces device URI from the given DPID. | ||
100 | + * | ||
101 | + * @param bgpId device bgpId | ||
102 | + * @return device URI | ||
103 | + */ | ||
104 | + public static URI uri(BGPId bgpId) { | ||
105 | + return uri(bgpId.ipAddress()); | ||
106 | + } | ||
107 | + | ||
108 | + /** | ||
109 | + * Produces device URI from the given DPID long. | ||
110 | + * | ||
111 | + * @param ipAddress device ip address | ||
112 | + * @return device URI | ||
113 | + */ | ||
114 | + public static URI uri(IpAddress ipAddress) { | ||
115 | + try { | ||
116 | + return new URI(SCHEME, ipAddress.toString(), null); | ||
117 | + } catch (URISyntaxException e) { | ||
118 | + return null; | ||
119 | + } | ||
120 | + } | ||
121 | +} | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
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 | + | ||
17 | +package org.onosproject.bgp.controller; | ||
18 | + | ||
19 | +/** | ||
20 | + * A representation of a packet context which allows any provider to view a packet in event, but may block the response | ||
21 | + * to the event if blocked has been called. This packet context can be used to react to the packet in event with a | ||
22 | + * packet out. | ||
23 | + */ | ||
24 | +public interface BGPPacketStats { | ||
25 | + /** | ||
26 | + * Returns the count for no of packets sent out. | ||
27 | + * | ||
28 | + * @return int value of no of packets sent | ||
29 | + */ | ||
30 | + int outPacketCount(); | ||
31 | + | ||
32 | + /** | ||
33 | + * Returns the count for no of packets received. | ||
34 | + * | ||
35 | + * @return int value of no of packets sent | ||
36 | + */ | ||
37 | + int inPacketCount(); | ||
38 | + | ||
39 | + /** | ||
40 | + * Returns the count for no of wrong packets received. | ||
41 | + * | ||
42 | + * @return int value of no of wrong packets received | ||
43 | + */ | ||
44 | + int wrongPacketCount(); | ||
45 | + | ||
46 | + /** | ||
47 | + * Returns the time. | ||
48 | + * | ||
49 | + * @return the time | ||
50 | + */ | ||
51 | + long getTime(); | ||
52 | +} | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
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 | + | ||
17 | +package org.onosproject.bgpio.exceptions; | ||
18 | + | ||
19 | +import org.jboss.netty.buffer.ChannelBuffer; | ||
20 | + | ||
21 | +/** | ||
22 | + * Custom Exception for BGP IO. | ||
23 | + */ | ||
24 | +public class BGPParseException extends Exception { | ||
25 | + | ||
26 | + private static final long serialVersionUID = 1L; | ||
27 | + private byte errorCode; | ||
28 | + private byte errorSubCode; | ||
29 | + private ChannelBuffer data; | ||
30 | + | ||
31 | + /** | ||
32 | + * Default constructor to create a new exception. | ||
33 | + */ | ||
34 | + public BGPParseException() { | ||
35 | + super(); | ||
36 | + } | ||
37 | + | ||
38 | + /** | ||
39 | + * Constructor to create exception from message and cause. | ||
40 | + * | ||
41 | + * @param message the detail of exception in string | ||
42 | + * @param cause underlying cause of the error | ||
43 | + */ | ||
44 | + public BGPParseException(final String message, final Throwable cause) { | ||
45 | + super(message, cause); | ||
46 | + } | ||
47 | + | ||
48 | + /** | ||
49 | + * Constructor to create exception from message. | ||
50 | + * | ||
51 | + * @param message the detail of exception in string | ||
52 | + */ | ||
53 | + public BGPParseException(final String message) { | ||
54 | + super(message); | ||
55 | + } | ||
56 | + | ||
57 | + /** | ||
58 | + * Constructor to create exception from cause. | ||
59 | + * | ||
60 | + * @param cause underlying cause of the error | ||
61 | + */ | ||
62 | + public BGPParseException(final Throwable cause) { | ||
63 | + super(cause); | ||
64 | + } | ||
65 | + | ||
66 | + /** | ||
67 | + * Constructor to create exception from error code and error subcode. | ||
68 | + * | ||
69 | + * @param errorCode error code of BGP message | ||
70 | + * @param errorSubCode error subcode of BGP message | ||
71 | + * @param data error data of BGP message | ||
72 | + */ | ||
73 | + public BGPParseException(final byte errorCode, final byte errorSubCode, final ChannelBuffer data) { | ||
74 | + super(); | ||
75 | + this.errorCode = errorCode; | ||
76 | + this.errorSubCode = errorSubCode; | ||
77 | + this.data = data; | ||
78 | + } | ||
79 | + | ||
80 | + /** | ||
81 | + * Returns errorcode for this exception. | ||
82 | + * | ||
83 | + * @return errorcode for this exception | ||
84 | + */ | ||
85 | + public byte getErrorCode() { | ||
86 | + return this.errorCode; | ||
87 | + } | ||
88 | + | ||
89 | + /** | ||
90 | + * Returns error Subcode for this exception. | ||
91 | + * | ||
92 | + * @return error Subcode for this exception | ||
93 | + */ | ||
94 | + public byte getErrorSubCode() { | ||
95 | + return this.errorSubCode; | ||
96 | + } | ||
97 | + | ||
98 | + /** | ||
99 | + * Returns error data for this exception. | ||
100 | + * | ||
101 | + * @return error data for this exception | ||
102 | + */ | ||
103 | + public ChannelBuffer getData() { | ||
104 | + return this.data; | ||
105 | + } | ||
106 | +} | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
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 | + | ||
17 | +package org.onosproject.bgpio.protocol; | ||
18 | + | ||
19 | +import org.jboss.netty.buffer.ChannelBuffer; | ||
20 | +import org.onosproject.bgpio.exceptions.BGPParseException; | ||
21 | +import org.onosproject.bgpio.types.BGPHeader; | ||
22 | + | ||
23 | +/** | ||
24 | + * Abstraction of an entity providing BGP Messages. | ||
25 | + */ | ||
26 | +public interface BGPMessage extends Writeable { | ||
27 | + /** | ||
28 | + * Returns BGP Header of BGP Message. | ||
29 | + * | ||
30 | + * @return BGP Header of BGP Message | ||
31 | + */ | ||
32 | + BGPHeader getHeader(); | ||
33 | + | ||
34 | + /** | ||
35 | + * Returns version of BGP Message. | ||
36 | + * | ||
37 | + * @return version of BGP Message | ||
38 | + */ | ||
39 | + BGPVersion getVersion(); | ||
40 | + | ||
41 | + /** | ||
42 | + * Returns BGP Type of BGP Message. | ||
43 | + * | ||
44 | + * @return BGP Type of BGP Message | ||
45 | + */ | ||
46 | + BGPType getType(); | ||
47 | + | ||
48 | + @Override | ||
49 | + void writeTo(ChannelBuffer cb) throws BGPParseException; | ||
50 | + | ||
51 | + /** | ||
52 | + * Builder interface with get and set functions to build BGP Message. | ||
53 | + */ | ||
54 | + interface Builder { | ||
55 | + /** | ||
56 | + * Builds BGP Message. | ||
57 | + * | ||
58 | + * @return BGP Message | ||
59 | + * @throws BGPParseException while building bgp message | ||
60 | + */ | ||
61 | + BGPMessage build() throws BGPParseException; | ||
62 | + | ||
63 | + /** | ||
64 | + * Returns BGP Version of BGP Message. | ||
65 | + * | ||
66 | + * @return BGP Version of BGP Message | ||
67 | + */ | ||
68 | + BGPVersion getVersion(); | ||
69 | + | ||
70 | + /** | ||
71 | + * Returns BGP Type of BGP Message. | ||
72 | + * | ||
73 | + * @return BGP Type of BGP Message | ||
74 | + */ | ||
75 | + BGPType getType(); | ||
76 | + | ||
77 | + /** | ||
78 | + * Returns BGP Header of BGP Message. | ||
79 | + * | ||
80 | + * @return BGP Header of BGP Message | ||
81 | + */ | ||
82 | + BGPHeader getHeader(); | ||
83 | + | ||
84 | + /** | ||
85 | + * Sets BgpHeader and return its builder. | ||
86 | + * | ||
87 | + * @param bgpMsgHeader BGP Message Header | ||
88 | + * @return builder by setting BGP message header | ||
89 | + */ | ||
90 | + Builder setHeader(BGPHeader bgpMsgHeader); | ||
91 | + } | ||
92 | +} | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
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; | ||
17 | + | ||
18 | +import org.jboss.netty.buffer.ChannelBuffer; | ||
19 | +import org.onosproject.bgpio.exceptions.BGPParseException; | ||
20 | +import org.onosproject.bgpio.types.BGPHeader; | ||
21 | + | ||
22 | +/** | ||
23 | + * Abstraction of an entity providing BGP Message Reader. | ||
24 | + */ | ||
25 | +public interface BGPMessageReader<T> { | ||
26 | + | ||
27 | + /** | ||
28 | + * Reads the Objects in the BGP Message and Returns BGP Message. | ||
29 | + * | ||
30 | + * @param cb Channel Buffer | ||
31 | + * @param bgpHeader BGP message header | ||
32 | + * @return BGP Message | ||
33 | + * @throws BGPParseException while parsing BGP message. | ||
34 | + */ | ||
35 | + T readFrom(ChannelBuffer cb, BGPHeader bgpHeader) throws BGPParseException; | ||
36 | +} | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
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 | + | ||
17 | +package org.onosproject.bgpio.protocol; | ||
18 | + | ||
19 | +/** | ||
20 | + * Enum to Provide the Different types of BGP messages. | ||
21 | + */ | ||
22 | +public enum BGPType { | ||
23 | + | ||
24 | + NONE(0), OPEN(1), UPDATE(2), NOTIFICATION(3), KEEP_ALIVE(4); | ||
25 | + | ||
26 | + int value; | ||
27 | + | ||
28 | + /** | ||
29 | + * Assign value with the value val as the types of BGP message. | ||
30 | + * | ||
31 | + * @param val type of BGP message | ||
32 | + */ | ||
33 | + BGPType(int val) { | ||
34 | + value = val; | ||
35 | + } | ||
36 | + | ||
37 | + /** | ||
38 | + * Returns value as type of BGP message. | ||
39 | + * | ||
40 | + * @return value type of BGP message | ||
41 | + */ | ||
42 | + public byte getType() { | ||
43 | + return (byte) value; | ||
44 | + } | ||
45 | +} | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
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 | + | ||
17 | +package org.onosproject.bgpio.protocol; | ||
18 | + | ||
19 | +/** | ||
20 | + * Enum to provide BGP Message Version. | ||
21 | + */ | ||
22 | +public enum BGPVersion { | ||
23 | + | ||
24 | + BGP_4(4); | ||
25 | + | ||
26 | + public final int packetVersion; | ||
27 | + | ||
28 | + /** | ||
29 | + * Assign BGP PacketVersion with specified packetVersion. | ||
30 | + * | ||
31 | + * @param packetVersion version of BGP | ||
32 | + */ | ||
33 | + BGPVersion(final int packetVersion) { | ||
34 | + this.packetVersion = packetVersion; | ||
35 | + } | ||
36 | + | ||
37 | + /** | ||
38 | + * Returns Packet version of BGP Message. | ||
39 | + * | ||
40 | + * @return packetVersion | ||
41 | + */ | ||
42 | + public int getPacketVersion() { | ||
43 | + return packetVersion; | ||
44 | + } | ||
45 | +} | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
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 | + | ||
17 | +package org.onosproject.bgpio.protocol; | ||
18 | + | ||
19 | +import org.jboss.netty.buffer.ChannelBuffer; | ||
20 | +import org.onosproject.bgpio.exceptions.BGPParseException; | ||
21 | + | ||
22 | +/** | ||
23 | + * Abstraction of an entity providing functionality to write byte streams of | ||
24 | + * Messages to channel buffer. | ||
25 | + */ | ||
26 | +public interface Writeable { | ||
27 | + | ||
28 | + /** | ||
29 | + * Writes byte streams of messages to channel buffer. | ||
30 | + * | ||
31 | + * @param cb channelBuffer | ||
32 | + * @throws BGPParseException when error occurs while writing BGP message to channel buffer | ||
33 | + */ | ||
34 | + void writeTo(ChannelBuffer cb) throws BGPParseException; | ||
35 | +} | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
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.types; | ||
17 | + | ||
18 | +import org.jboss.netty.buffer.ChannelBuffer; | ||
19 | +import org.slf4j.Logger; | ||
20 | +import org.slf4j.LoggerFactory; | ||
21 | + | ||
22 | +/** | ||
23 | + * Provides BGP Message Header which is common for all the Messages. | ||
24 | + */ | ||
25 | + | ||
26 | +public class BGPHeader { | ||
27 | + | ||
28 | + /* 0 1 2 3 | ||
29 | + 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 | ||
30 | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | ||
31 | + | | | ||
32 | + + + | ||
33 | + | | | ||
34 | + + + | ||
35 | + | Marker | | ||
36 | + + + | ||
37 | + | | | ||
38 | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | ||
39 | + | Length | Type | | ||
40 | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | ||
41 | + */ | ||
42 | + | ||
43 | + protected static final Logger log = LoggerFactory.getLogger(BGPHeader.class); | ||
44 | + | ||
45 | + public static final int MARKER_LENGTH = 16; | ||
46 | + public static final short DEFAULT_HEADER_LENGTH = 19; | ||
47 | + | ||
48 | + private byte[] marker; | ||
49 | + private byte type; | ||
50 | + private short length; | ||
51 | + | ||
52 | + /** | ||
53 | + * Reset fields. | ||
54 | + */ | ||
55 | + public BGPHeader() { | ||
56 | + this.marker = null; | ||
57 | + this.length = 0; | ||
58 | + this.type = 0; | ||
59 | + } | ||
60 | + | ||
61 | + /** | ||
62 | + * Constructors to initialize parameters. | ||
63 | + * | ||
64 | + * @param marker field in BGP header | ||
65 | + * @param length message length | ||
66 | + * @param type message type | ||
67 | + */ | ||
68 | + public BGPHeader(byte[] marker, short length, byte type) { | ||
69 | + this.marker = marker; | ||
70 | + this.length = length; | ||
71 | + this.type = type; | ||
72 | + } | ||
73 | + | ||
74 | + /** | ||
75 | + * Sets marker field. | ||
76 | + * | ||
77 | + * @param value marker field | ||
78 | + */ | ||
79 | + public void setMarker(byte[] value) { | ||
80 | + this.marker = value; | ||
81 | + } | ||
82 | + | ||
83 | + /** | ||
84 | + * Sets message type. | ||
85 | + * | ||
86 | + * @param value message type | ||
87 | + */ | ||
88 | + public void setType(byte value) { | ||
89 | + this.type = value; | ||
90 | + } | ||
91 | + | ||
92 | + /** | ||
93 | + * Sets message length. | ||
94 | + * | ||
95 | + * @param value message length | ||
96 | + */ | ||
97 | + public void setLength(short value) { | ||
98 | + this.length = value; | ||
99 | + } | ||
100 | + | ||
101 | + /** | ||
102 | + * Returns message length. | ||
103 | + * | ||
104 | + * @return message length | ||
105 | + */ | ||
106 | + public short getLength() { | ||
107 | + return this.length; | ||
108 | + } | ||
109 | + | ||
110 | + /** | ||
111 | + * Returns message marker. | ||
112 | + * | ||
113 | + * @return message marker | ||
114 | + */ | ||
115 | + public byte[] getMarker() { | ||
116 | + return this.marker; | ||
117 | + } | ||
118 | + | ||
119 | + /** | ||
120 | + * Returns message type. | ||
121 | + * | ||
122 | + * @return message type | ||
123 | + */ | ||
124 | + public byte getType() { | ||
125 | + return this.type; | ||
126 | + } | ||
127 | + | ||
128 | + /** | ||
129 | + * Writes Byte stream of BGP header to channel buffer. | ||
130 | + * | ||
131 | + * @param cb ChannelBuffer | ||
132 | + * @return length index of message header | ||
133 | + */ | ||
134 | + public int write(ChannelBuffer cb) { | ||
135 | + | ||
136 | + cb.writeBytes(getMarker(), 0, MARKER_LENGTH); | ||
137 | + | ||
138 | + int headerLenIndex = cb.writerIndex(); | ||
139 | + cb.writeShort((short) 0); | ||
140 | + cb.writeByte(type); | ||
141 | + | ||
142 | + return headerLenIndex; | ||
143 | + } | ||
144 | + | ||
145 | + /** | ||
146 | + * Read from channel buffer and Returns BGP header. | ||
147 | + * | ||
148 | + * @param cb ChannelBuffer | ||
149 | + * @return object of BGPHeader | ||
150 | + */ | ||
151 | + public static BGPHeader read(ChannelBuffer cb) { | ||
152 | + | ||
153 | + byte[] marker = new byte[MARKER_LENGTH]; | ||
154 | + byte type; | ||
155 | + short length; | ||
156 | + cb.readBytes(marker, 0, MARKER_LENGTH); | ||
157 | + length = cb.readShort(); | ||
158 | + type = cb.readByte(); | ||
159 | + return new BGPHeader(marker, length, type); | ||
160 | + } | ||
161 | +} | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
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 | + | ||
17 | +package org.onosproject.bgpio.types; | ||
18 | + | ||
19 | +import org.jboss.netty.buffer.ChannelBuffer; | ||
20 | + | ||
21 | +/** | ||
22 | + * Abstraction which Provides the BGP of TLV format. | ||
23 | + */ | ||
24 | +public interface BGPValueType { | ||
25 | + /** | ||
26 | + * Returns the Type of BGP Message. | ||
27 | + * | ||
28 | + * @return short value of type | ||
29 | + */ | ||
30 | + short getType(); | ||
31 | + | ||
32 | + /** | ||
33 | + * Writes the byte Stream of BGP Message to channel buffer. | ||
34 | + * | ||
35 | + * @param cb channel buffer | ||
36 | + * @return length written to channel buffer | ||
37 | + */ | ||
38 | + int write(ChannelBuffer cb); | ||
39 | +} | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
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 | + | ||
17 | +package org.onosproject.bgp.controller.impl; | ||
18 | + | ||
19 | +import org.jboss.netty.handler.timeout.IdleStateAwareChannelHandler; | ||
20 | + | ||
21 | +/** | ||
22 | + * Channel handler deals with the bgp peer connection and dispatches messages from peer to the appropriate locations. | ||
23 | + */ | ||
24 | +class BGPChannelHandler extends IdleStateAwareChannelHandler { | ||
25 | + | ||
26 | + // TODO: implement FSM and session handling mechanism | ||
27 | + /** | ||
28 | + * Create a new unconnected BGPChannelHandler. | ||
29 | + * | ||
30 | + * @param bgpCtrlImpl bgp controller implementation object | ||
31 | + */ | ||
32 | + BGPChannelHandler(BGPControllerImpl bgpCtrlImpl) { | ||
33 | + } | ||
34 | +} | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
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 | + | ||
17 | +package org.onosproject.bgp.controller.impl; | ||
18 | + | ||
19 | +import static org.onlab.util.Tools.groupedThreads; | ||
20 | +import java.util.concurrent.ExecutorService; | ||
21 | +import java.util.concurrent.Executors; | ||
22 | +import org.apache.felix.scr.annotations.Activate; | ||
23 | +import org.apache.felix.scr.annotations.Component; | ||
24 | +import org.apache.felix.scr.annotations.Deactivate; | ||
25 | +import org.apache.felix.scr.annotations.Service; | ||
26 | +import org.onosproject.bgp.controller.BGPCfg; | ||
27 | +import org.onosproject.bgp.controller.BGPController; | ||
28 | +import org.onosproject.bgp.controller.BGPId; | ||
29 | +import org.onosproject.bgpio.protocol.BGPMessage; | ||
30 | +import org.slf4j.Logger; | ||
31 | +import org.slf4j.LoggerFactory; | ||
32 | + | ||
33 | +@Component(immediate = true) | ||
34 | +@Service | ||
35 | +public class BGPControllerImpl implements BGPController { | ||
36 | + | ||
37 | + private static final Logger log = LoggerFactory.getLogger(BGPControllerImpl.class); | ||
38 | + | ||
39 | + private final ExecutorService executorMsgs = Executors.newFixedThreadPool(32, | ||
40 | + groupedThreads("onos/bgp", | ||
41 | + "event-stats-%d")); | ||
42 | + | ||
43 | + private final ExecutorService executorBarrier = Executors.newFixedThreadPool(4, | ||
44 | + groupedThreads("onos/bgp", | ||
45 | + "event-barrier-%d")); | ||
46 | + | ||
47 | + final Controller ctrl = new Controller(this); | ||
48 | + | ||
49 | + private BGPConfig bgpconfig = new BGPConfig(); | ||
50 | + | ||
51 | + @Activate | ||
52 | + public void activate() { | ||
53 | + this.ctrl.start(); | ||
54 | + log.info("Started"); | ||
55 | + } | ||
56 | + | ||
57 | + @Deactivate | ||
58 | + public void deactivate() { | ||
59 | + // Close all connected peers | ||
60 | + this.ctrl.stop(); | ||
61 | + log.info("Stopped"); | ||
62 | + } | ||
63 | + | ||
64 | + @Override | ||
65 | + public void writeMsg(BGPId bgpId, BGPMessage msg) { | ||
66 | + // TODO: Send message | ||
67 | + } | ||
68 | + | ||
69 | + @Override | ||
70 | + public void processBGPPacket(BGPId bgpId, BGPMessage msg) { | ||
71 | + | ||
72 | + switch (msg.getType()) { | ||
73 | + case OPEN: | ||
74 | + // TODO: Process Open message | ||
75 | + break; | ||
76 | + case KEEP_ALIVE: | ||
77 | + // TODO: Process keepalive message | ||
78 | + break; | ||
79 | + case NOTIFICATION: | ||
80 | + // TODO: Process notificatoin message | ||
81 | + break; | ||
82 | + case UPDATE: | ||
83 | + // TODO: Process update message | ||
84 | + break; | ||
85 | + default: | ||
86 | + // TODO: Process other message | ||
87 | + break; | ||
88 | + } | ||
89 | + } | ||
90 | + | ||
91 | + /** | ||
92 | + * Get controller instance. | ||
93 | + * | ||
94 | + * @return ctrl the controller. | ||
95 | + */ | ||
96 | + public Controller getController() { | ||
97 | + return ctrl; | ||
98 | + } | ||
99 | + | ||
100 | + @Override | ||
101 | + public BGPCfg getConfig() { | ||
102 | + return this.bgpconfig; | ||
103 | + } | ||
104 | +} | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
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.bgp.controller.impl; | ||
17 | + | ||
18 | +import java.util.LinkedList; | ||
19 | +import java.util.List; | ||
20 | + | ||
21 | +import org.jboss.netty.buffer.ChannelBuffer; | ||
22 | +import org.jboss.netty.channel.Channel; | ||
23 | +import org.jboss.netty.channel.ChannelHandlerContext; | ||
24 | +import org.jboss.netty.handler.codec.frame.FrameDecoder; | ||
25 | +import org.onosproject.bgpio.protocol.BGPMessage; | ||
26 | +import org.onlab.util.HexDump; | ||
27 | +import org.slf4j.Logger; | ||
28 | +import org.slf4j.LoggerFactory; | ||
29 | + | ||
30 | +/** | ||
31 | + * Decode an bgp message from a Channel, for use in a netty pipeline. | ||
32 | + */ | ||
33 | +public class BGPMessageDecoder extends FrameDecoder { | ||
34 | + | ||
35 | + protected static final Logger log = LoggerFactory.getLogger(BGPMessageDecoder.class); | ||
36 | + | ||
37 | + @Override | ||
38 | + protected Object decode(ChannelHandlerContext ctx, Channel channel, ChannelBuffer buffer) throws Exception { | ||
39 | + | ||
40 | + List<BGPMessage> msgList = new LinkedList<BGPMessage>(); | ||
41 | + | ||
42 | + log.debug("MESSAGE IS RECEIVED."); | ||
43 | + if (!channel.isConnected()) { | ||
44 | + log.info("Channel is not connected."); | ||
45 | + return null; | ||
46 | + } | ||
47 | + | ||
48 | + HexDump.dump(buffer); | ||
49 | + | ||
50 | + // TODO: decode bgp messages | ||
51 | + return msgList; | ||
52 | + } | ||
53 | +} | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
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.bgp.controller.impl; | ||
17 | + | ||
18 | +import java.util.List; | ||
19 | + | ||
20 | +import org.jboss.netty.buffer.ChannelBuffer; | ||
21 | +import org.jboss.netty.buffer.ChannelBuffers; | ||
22 | +import org.jboss.netty.channel.Channel; | ||
23 | +import org.jboss.netty.channel.ChannelHandlerContext; | ||
24 | +import org.jboss.netty.handler.codec.oneone.OneToOneEncoder; | ||
25 | +import org.onosproject.bgpio.protocol.BGPMessage; | ||
26 | +import org.onlab.util.HexDump; | ||
27 | + | ||
28 | +import org.slf4j.Logger; | ||
29 | +import org.slf4j.LoggerFactory; | ||
30 | + | ||
31 | +/** | ||
32 | + * Encode an bgp message for output into a ChannelBuffer, for use in a | ||
33 | + * netty pipeline. | ||
34 | + */ | ||
35 | +public class BGPMessageEncoder extends OneToOneEncoder { | ||
36 | + protected static final Logger log = LoggerFactory.getLogger(BGPMessageEncoder.class); | ||
37 | + | ||
38 | + @Override | ||
39 | + protected Object encode(ChannelHandlerContext ctx, Channel channel, Object msg) throws Exception { | ||
40 | + log.debug("BGPMessageEncoder::encode"); | ||
41 | + if (!(msg instanceof List)) { | ||
42 | + log.debug("Invalid msg."); | ||
43 | + return msg; | ||
44 | + } | ||
45 | + | ||
46 | + @SuppressWarnings("unchecked") | ||
47 | + List<BGPMessage> msglist = (List<BGPMessage>) msg; | ||
48 | + | ||
49 | + ChannelBuffer buf = ChannelBuffers.dynamicBuffer(); | ||
50 | + | ||
51 | + log.debug("SENDING MESSAGE"); | ||
52 | + for (BGPMessage pm : msglist) { | ||
53 | + pm.writeTo(buf); | ||
54 | + } | ||
55 | + | ||
56 | + HexDump.dump(buf); | ||
57 | + | ||
58 | + return buf; | ||
59 | + } | ||
60 | +} | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
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.bgp.controller.impl; | ||
17 | + | ||
18 | +import org.onosproject.bgp.controller.BGPPacketStats; | ||
19 | + | ||
20 | +/** | ||
21 | + * A representation of a packet context which allows any provider | ||
22 | + * to view a packet in event, but may block the response to the | ||
23 | + * event if blocked has been called. This packet context can be used | ||
24 | + * to react to the packet in event with a packet out. | ||
25 | + */ | ||
26 | +public class BGPPacketStatsImpl implements BGPPacketStats { | ||
27 | + | ||
28 | + private int inPacketCount; | ||
29 | + private int outPacketCount; | ||
30 | + private int wrongPacketCount; | ||
31 | + private long time; | ||
32 | + | ||
33 | + /** | ||
34 | + * Resets parameter. | ||
35 | + */ | ||
36 | + public BGPPacketStatsImpl() { | ||
37 | + this.inPacketCount = 0; | ||
38 | + this.outPacketCount = 0; | ||
39 | + this.wrongPacketCount = 0; | ||
40 | + this.time = 0; | ||
41 | + } | ||
42 | + | ||
43 | + /** | ||
44 | + * Get the outgoing packet count number. | ||
45 | + * | ||
46 | + * @return | ||
47 | + * packet count | ||
48 | + */ | ||
49 | + public int outPacketCount() { | ||
50 | + return outPacketCount; | ||
51 | + } | ||
52 | + | ||
53 | + /** | ||
54 | + * Get the incoming packet count number. | ||
55 | + * | ||
56 | + * @return | ||
57 | + * packet count | ||
58 | + */ | ||
59 | + public int inPacketCount() { | ||
60 | + return inPacketCount; | ||
61 | + } | ||
62 | + | ||
63 | + /** | ||
64 | + * Get the wrong packet count number. | ||
65 | + * | ||
66 | + * @return | ||
67 | + * packet count | ||
68 | + */ | ||
69 | + public int wrongPacketCount() { | ||
70 | + return wrongPacketCount; | ||
71 | + } | ||
72 | + | ||
73 | + /** | ||
74 | + * Increments the received packet counter. | ||
75 | + */ | ||
76 | + public void addInPacket() { | ||
77 | + this.inPacketCount++; | ||
78 | + } | ||
79 | + | ||
80 | + /** | ||
81 | + * Increments the sent packet counter. | ||
82 | + */ | ||
83 | + public void addOutPacket() { | ||
84 | + this.outPacketCount++; | ||
85 | + } | ||
86 | + | ||
87 | + /** | ||
88 | + * Increments the sent packet counter by specified value. | ||
89 | + * | ||
90 | + * @param value of no of packets sent | ||
91 | + */ | ||
92 | + public void addOutPacket(int value) { | ||
93 | + this.outPacketCount = this.outPacketCount + value; | ||
94 | + } | ||
95 | + | ||
96 | + /** | ||
97 | + * Increments the wrong packet counter. | ||
98 | + */ | ||
99 | + public void addWrongPacket() { | ||
100 | + this.wrongPacketCount++; | ||
101 | + } | ||
102 | + | ||
103 | + /** | ||
104 | + * Resets wrong packet count. | ||
105 | + */ | ||
106 | + public void resetWrongPacket() { | ||
107 | + this.wrongPacketCount = 0; | ||
108 | + } | ||
109 | + | ||
110 | + /** | ||
111 | + * Get the time. | ||
112 | + * | ||
113 | + * @return | ||
114 | + * time | ||
115 | + */ | ||
116 | + public long getTime() { | ||
117 | + return this.time; | ||
118 | + } | ||
119 | + | ||
120 | + /** | ||
121 | + * Sets the time. | ||
122 | + * | ||
123 | + * @param time value to set | ||
124 | + */ | ||
125 | + public void setTime(long time) { | ||
126 | + this.time = time; | ||
127 | + } | ||
128 | +} | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
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 | + | ||
17 | +package org.onosproject.bgp.controller.impl; | ||
18 | + | ||
19 | +import org.jboss.netty.channel.ChannelPipeline; | ||
20 | +import org.jboss.netty.channel.ChannelPipelineFactory; | ||
21 | +import org.jboss.netty.channel.Channels; | ||
22 | +import org.jboss.netty.handler.timeout.ReadTimeoutHandler; | ||
23 | +import org.jboss.netty.util.ExternalResourceReleasable; | ||
24 | +import org.jboss.netty.util.HashedWheelTimer; | ||
25 | +import org.jboss.netty.util.Timer; | ||
26 | + | ||
27 | +/** | ||
28 | + * Creates a ChannelPipeline for a server-side bgp channel. | ||
29 | + */ | ||
30 | +public class BGPPipelineFactory | ||
31 | + implements ChannelPipelineFactory, ExternalResourceReleasable { | ||
32 | + | ||
33 | + static final Timer TIMER = new HashedWheelTimer(); | ||
34 | + protected ReadTimeoutHandler readTimeoutHandler; | ||
35 | + BGPControllerImpl bgpCtrlImpl; | ||
36 | + | ||
37 | + /** | ||
38 | + * Constructor to initialize the values. | ||
39 | + * | ||
40 | + * @param ctrlImpl parent ctrlImpl | ||
41 | + * @param isServBgp if it is a server or not | ||
42 | + */ | ||
43 | + public BGPPipelineFactory(BGPControllerImpl ctrlImpl, boolean isServBgp) { | ||
44 | + super(); | ||
45 | + bgpCtrlImpl = ctrlImpl; | ||
46 | + /* hold time*/ | ||
47 | + readTimeoutHandler = new ReadTimeoutHandler(TIMER, bgpCtrlImpl.getConfig().getHoldTime()); | ||
48 | + } | ||
49 | + | ||
50 | + @Override | ||
51 | + public ChannelPipeline getPipeline() throws Exception { | ||
52 | + BGPChannelHandler handler = new BGPChannelHandler(bgpCtrlImpl); | ||
53 | + | ||
54 | + ChannelPipeline pipeline = Channels.pipeline(); | ||
55 | + pipeline.addLast("bgpmessagedecoder", new BGPMessageDecoder()); | ||
56 | + pipeline.addLast("bgpmessageencoder", new BGPMessageEncoder()); | ||
57 | + pipeline.addLast("holdTime", readTimeoutHandler); | ||
58 | + pipeline.addLast("PassiveHandler", handler); | ||
59 | + | ||
60 | + return pipeline; | ||
61 | + } | ||
62 | + | ||
63 | + @Override | ||
64 | + public void releaseExternalResources() { | ||
65 | + TIMER.stop(); | ||
66 | + } | ||
67 | +} | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
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.bgp.controller.impl; | ||
17 | + | ||
18 | +import static org.onlab.util.Tools.groupedThreads; | ||
19 | + | ||
20 | +import java.lang.management.ManagementFactory; | ||
21 | +import java.lang.management.RuntimeMXBean; | ||
22 | +import java.net.InetSocketAddress; | ||
23 | +import java.util.HashMap; | ||
24 | +import java.util.Map; | ||
25 | +import java.util.concurrent.Executors; | ||
26 | + | ||
27 | +import org.jboss.netty.bootstrap.ServerBootstrap; | ||
28 | +import org.jboss.netty.channel.ChannelPipelineFactory; | ||
29 | +import org.jboss.netty.channel.group.ChannelGroup; | ||
30 | +import org.jboss.netty.channel.group.DefaultChannelGroup; | ||
31 | +import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory; | ||
32 | +import org.slf4j.Logger; | ||
33 | +import org.slf4j.LoggerFactory; | ||
34 | + | ||
35 | +/** | ||
36 | + * The main controller class. Handles all setup and network listeners - Distributed ownership control of bgp peer | ||
37 | + * through IControllerRegistryService | ||
38 | + */ | ||
39 | +public class Controller { | ||
40 | + | ||
41 | + protected static final Logger log = LoggerFactory.getLogger(Controller.class); | ||
42 | + | ||
43 | + private ChannelGroup cg; | ||
44 | + | ||
45 | + // Configuration options | ||
46 | + private static final short BGP_PORT_NUM = 179; | ||
47 | + private int workerThreads = 16; | ||
48 | + | ||
49 | + // Start time of the controller | ||
50 | + protected long systemStartTime; | ||
51 | + | ||
52 | + private NioServerSocketChannelFactory serverExecFactory; | ||
53 | + | ||
54 | + // Perf. related configuration | ||
55 | + protected static final int SEND_BUFFER_SIZE = 4 * 1024 * 1024; | ||
56 | + | ||
57 | + BGPControllerImpl bgpCtrlImpl; | ||
58 | + | ||
59 | + /** | ||
60 | + * Constructor to initialize parameter. | ||
61 | + * | ||
62 | + * @param bgpCtrlImpl BGP controller Impl instance | ||
63 | + */ | ||
64 | + public Controller(BGPControllerImpl bgpCtrlImpl) { | ||
65 | + this.bgpCtrlImpl = bgpCtrlImpl; | ||
66 | + } | ||
67 | + | ||
68 | + // *************** | ||
69 | + // Getters/Setters | ||
70 | + // *************** | ||
71 | + | ||
72 | + /** | ||
73 | + * To get system start time. | ||
74 | + * | ||
75 | + * @return system start time in milliseconds | ||
76 | + */ | ||
77 | + public long getSystemStartTime() { | ||
78 | + return (this.systemStartTime); | ||
79 | + } | ||
80 | + | ||
81 | + // ************** | ||
82 | + // Initialization | ||
83 | + // ************** | ||
84 | + | ||
85 | + /** | ||
86 | + * Tell controller that we're ready to accept bgp peer connections. | ||
87 | + */ | ||
88 | + public void run() { | ||
89 | + | ||
90 | + try { | ||
91 | + final ServerBootstrap bootstrap = createServerBootStrap(); | ||
92 | + | ||
93 | + bootstrap.setOption("reuseAddr", true); | ||
94 | + bootstrap.setOption("child.keepAlive", true); | ||
95 | + bootstrap.setOption("child.tcpNoDelay", true); | ||
96 | + bootstrap.setOption("child.sendBufferSize", Controller.SEND_BUFFER_SIZE); | ||
97 | + | ||
98 | + ChannelPipelineFactory pfact = new BGPPipelineFactory(bgpCtrlImpl, true); | ||
99 | + | ||
100 | + bootstrap.setPipelineFactory(pfact); | ||
101 | + InetSocketAddress sa = new InetSocketAddress(getBgpPortNum()); | ||
102 | + cg = new DefaultChannelGroup(); | ||
103 | + cg.add(bootstrap.bind(sa)); | ||
104 | + log.info("Listening for Peer connection on {}", sa); | ||
105 | + } catch (Exception e) { | ||
106 | + throw new RuntimeException(e); | ||
107 | + } | ||
108 | + } | ||
109 | + | ||
110 | + /** | ||
111 | + * Creates server boot strap. | ||
112 | + * | ||
113 | + * @return ServerBootStrap | ||
114 | + */ | ||
115 | + private ServerBootstrap createServerBootStrap() { | ||
116 | + | ||
117 | + if (workerThreads == 0) { | ||
118 | + serverExecFactory = new NioServerSocketChannelFactory( | ||
119 | + Executors.newCachedThreadPool(groupedThreads("onos/bgp", "boss-%d")), | ||
120 | + Executors.newCachedThreadPool(groupedThreads("onos/bgp", "worker-%d"))); | ||
121 | + return new ServerBootstrap(serverExecFactory); | ||
122 | + } else { | ||
123 | + serverExecFactory = new NioServerSocketChannelFactory( | ||
124 | + Executors.newCachedThreadPool(groupedThreads("onos/bgp", "boss-%d")), | ||
125 | + Executors.newCachedThreadPool(groupedThreads("onos/bgp", "worker-%d")), | ||
126 | + workerThreads); | ||
127 | + return new ServerBootstrap(serverExecFactory); | ||
128 | + } | ||
129 | + } | ||
130 | + | ||
131 | + /** | ||
132 | + * Initialize internal data structures. | ||
133 | + */ | ||
134 | + public void init() { | ||
135 | + // These data structures are initialized here because other | ||
136 | + // module's startUp() might be called before ours | ||
137 | + this.systemStartTime = System.currentTimeMillis(); | ||
138 | + } | ||
139 | + | ||
140 | + // ************** | ||
141 | + // Utility methods | ||
142 | + // ************** | ||
143 | + | ||
144 | + public Map<String, Long> getMemory() { | ||
145 | + Map<String, Long> m = new HashMap<>(); | ||
146 | + Runtime runtime = Runtime.getRuntime(); | ||
147 | + m.put("total", runtime.totalMemory()); | ||
148 | + m.put("free", runtime.freeMemory()); | ||
149 | + return m; | ||
150 | + } | ||
151 | + | ||
152 | + public Long getUptime() { | ||
153 | + RuntimeMXBean rb = ManagementFactory.getRuntimeMXBean(); | ||
154 | + return rb.getUptime(); | ||
155 | + } | ||
156 | + | ||
157 | + /** | ||
158 | + * Starts the BGP controller. | ||
159 | + */ | ||
160 | + public void start() { | ||
161 | + log.info("Started"); | ||
162 | + this.init(); | ||
163 | + this.run(); | ||
164 | + } | ||
165 | + | ||
166 | + /** | ||
167 | + * Stops the BGP controller. | ||
168 | + */ | ||
169 | + public void stop() { | ||
170 | + log.info("Stopped"); | ||
171 | + serverExecFactory.shutdown(); | ||
172 | + cg.close(); | ||
173 | + } | ||
174 | + | ||
175 | + /** | ||
176 | + * Returns port number. | ||
177 | + * | ||
178 | + * @return port number | ||
179 | + */ | ||
180 | + public static short getBgpPortNum() { | ||
181 | + return BGP_PORT_NUM; | ||
182 | + } | ||
183 | +} | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
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.onlab.util; | ||
17 | + | ||
18 | +import org.jboss.netty.buffer.ChannelBuffer; | ||
19 | +import org.slf4j.Logger; | ||
20 | +import org.slf4j.LoggerFactory; | ||
21 | + | ||
22 | +/** | ||
23 | + * HexDump class an utility to dump buffer in hex format. | ||
24 | + */ | ||
25 | +public final class HexDump { | ||
26 | + protected static final Logger log = LoggerFactory.getLogger(HexDump.class); | ||
27 | + | ||
28 | + private HexDump() { | ||
29 | + } | ||
30 | + | ||
31 | + /** | ||
32 | + * Dump the buffer content in hex format. | ||
33 | + * | ||
34 | + * @param buff buffer content to dump in hex format | ||
35 | + */ | ||
36 | + public static void dump(ChannelBuffer buff) { | ||
37 | + try { | ||
38 | + byte[] yTemp; | ||
39 | + yTemp = buff.array(); | ||
40 | + | ||
41 | + int iStartIndex = buff.readerIndex(); | ||
42 | + int iEndIndex = buff.writerIndex(); | ||
43 | + do { | ||
44 | + StringBuilder sb = new StringBuilder(); | ||
45 | + for (int k = 0; (k < 16) && (iStartIndex < iEndIndex); ++k) { | ||
46 | + if (0 == k % 4) { | ||
47 | + sb.append(String.format(" ")); // blank after 4 bytes | ||
48 | + } | ||
49 | + sb.append(String.format("%02X ", yTemp[iStartIndex++])); | ||
50 | + } | ||
51 | + log.debug(sb.toString()); | ||
52 | + } while (iStartIndex < iEndIndex); | ||
53 | + } catch (Exception e) { | ||
54 | + log.error("[HexDump] Invalid buffer: " + e.toString()); | ||
55 | + } | ||
56 | + } | ||
57 | +} |
-
Please register or login to post a comment