Pingping Lin
Committed by Gerrit Code Review

add a vBNG application

This is a virtual Broadband Network Gateway (BNG) application.
It mainly has 3 functions:
(1) assigns and replies a public IP address to a REST request
    with a private IP address
(2) maintains the mapping from the private IP address to the
    public IP address
(3) installs point to point intents for the host configured
    with private IP address to access Internet

Change-Id: Ie78614a1703422a57f3c325540b927cc4ae603f4
...@@ -43,6 +43,7 @@ ...@@ -43,6 +43,7 @@
43 <module>routing</module> 43 <module>routing</module>
44 <module>routing-api</module> 44 <module>routing-api</module>
45 <module>reactive-routing</module> 45 <module>reactive-routing</module>
46 + <module>virtualbng</module>
46 <module>bgprouter</module> 47 <module>bgprouter</module>
47 <module>test</module> 48 <module>test</module>
48 <module>segmentrouting</module> 49 <module>segmentrouting</module>
......
1 +<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2 +<!--
3 + ~ Copyright 2015 Open Networking Laboratory
4 + ~
5 + ~ Licensed under the Apache License, Version 2.0 (the "License");
6 + ~ you may not use this file except in compliance with the License.
7 + ~ You may obtain a copy of the License at
8 + ~
9 + ~ http://www.apache.org/licenses/LICENSE-2.0
10 + ~
11 + ~ Unless required by applicable law or agreed to in writing, software
12 + ~ distributed under the License is distributed on an "AS IS" BASIS,
13 + ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 + ~ See the License for the specific language governing permissions and
15 + ~ limitations under the License.
16 + -->
17 +<features xmlns="http://karaf.apache.org/xmlns/features/v1.2.0" name="${project.artifactId}-${project.version}">
18 + <repository>mvn:${project.groupId}/${project.artifactId}/${project.version}/xml/features</repository>
19 + <feature name="${project.artifactId}" version="${project.version}"
20 + description="${project.description}">
21 + <bundle>mvn:${project.groupId}/onos-app-virtualbng/${project.version}</bundle>
22 + <feature>onos-thirdparty-web</feature>
23 + </feature>
24 +</features>
1 +<?xml version="1.0" encoding="UTF-8"?>
2 +<!--
3 + ~ Copyright 2015 Open Networking Laboratory
4 + ~
5 + ~ Licensed under the Apache License, Version 2.0 (the "License");
6 + ~ you may not use this file except in compliance with the License.
7 + ~ You may obtain a copy of the License at
8 + ~
9 + ~ http://www.apache.org/licenses/LICENSE-2.0
10 + ~
11 + ~ Unless required by applicable law or agreed to in writing, software
12 + ~ distributed under the License is distributed on an "AS IS" BASIS,
13 + ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 + ~ See the License for the specific language governing permissions and
15 + ~ limitations under the License.
16 + -->
17 +<project xmlns="http://maven.apache.org/POM/4.0.0"
18 + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
19 + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
20 + <modelVersion>4.0.0</modelVersion>
21 +
22 + <parent>
23 + <groupId>org.onosproject</groupId>
24 + <artifactId>onos-apps</artifactId>
25 + <version>1.2.0-SNAPSHOT</version>
26 + <relativePath>../pom.xml</relativePath>
27 + </parent>
28 +
29 + <artifactId>onos-app-virtualbng</artifactId>
30 + <packaging>bundle</packaging>
31 +
32 + <description>A virtual Broadband Network Gateway(BNG) application</description>
33 +
34 + <properties>
35 + <onos.app.name>org.onosproject.virtualbng</onos.app.name>
36 + <web.context>/onos/virtualbng</web.context>
37 + </properties>
38 +
39 + <dependencies>
40 + <dependency>
41 + <groupId>org.onosproject</groupId>
42 + <artifactId>onlab-rest</artifactId>
43 + <version>${project.version}</version>
44 + </dependency>
45 + <dependency>
46 + <groupId>javax.ws.rs</groupId>
47 + <artifactId>jsr311-api</artifactId>
48 + <version>1.1.1</version>
49 + </dependency>
50 + </dependencies>
51 +
52 + <build>
53 + <plugins>
54 + <plugin>
55 + <groupId>org.apache.felix</groupId>
56 + <artifactId>maven-bundle-plugin</artifactId>
57 + <extensions>true</extensions>
58 + <configuration>
59 + <instructions>
60 + <_wab>src/main/webapp/</_wab>
61 + <Bundle-SymbolicName>
62 + ${project.groupId}.${project.artifactId}
63 + </Bundle-SymbolicName>
64 + <Import-Package>
65 + org.slf4j,
66 + javax.ws.rs,
67 + com.sun.jersey.api.core,
68 + com.sun.jersey.spi.container.servlet,
69 + com.sun.jersey.server.impl.container.servlet,
70 + com.fasterxml.jackson.databind,
71 + com.google.common.*,
72 + org.onlab.packet.*,
73 + org.onlab.rest.*,
74 + org.onosproject.*
75 + </Import-Package>
76 + <Web-ContextPath>${web.context}</Web-ContextPath>
77 + </instructions>
78 + </configuration>
79 + </plugin>
80 + </plugins>
81 + </build>
82 +
83 +</project>
...\ 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.virtualbng;
17 +
18 +import com.fasterxml.jackson.annotation.JsonCreator;
19 +import com.fasterxml.jackson.annotation.JsonProperty;
20 +
21 +import java.util.Collections;
22 +import java.util.List;
23 +
24 +import org.onlab.packet.IpAddress;
25 +import org.onlab.packet.IpPrefix;
26 +
27 +/**
28 + * Contains the configuration data for virtual BNG that has been read from a
29 + * JSON-formatted configuration file.
30 + */
31 +public final class VbngConfiguration {
32 +
33 + private final List<IpPrefix> localPublicIpPrefixes;
34 + private final IpAddress nextHopIpAddress;
35 +
36 + /**
37 + * Default constructor.
38 + */
39 + private VbngConfiguration() {
40 + localPublicIpPrefixes = null;
41 + nextHopIpAddress = null;
42 + }
43 +
44 + /**
45 + * Constructor.
46 + *
47 + * @param nextHopIpAddress the IP address of the next hop
48 + * @param prefixes the public IP prefix list for local SDN network
49 + */
50 + @JsonCreator
51 + public VbngConfiguration(@JsonProperty("localPublicIpPrefixes")
52 + List<IpPrefix> prefixes,
53 + @JsonProperty("nextHopIpAddress")
54 + IpAddress nextHopIpAddress) {
55 + localPublicIpPrefixes = prefixes;
56 + this.nextHopIpAddress = nextHopIpAddress;
57 + }
58 +
59 + /**
60 + * Gets a list of public IP prefixes configured for local SDN network.
61 + *
62 + * @return the list of public IP prefixes
63 + */
64 + public List<IpPrefix> getLocalPublicIpPrefixes() {
65 + return Collections.unmodifiableList(localPublicIpPrefixes);
66 + }
67 +
68 + /**
69 + * Gets the IP address configured for the next hop (upstream gateway).
70 + *
71 + * @return the IP address of the next hop
72 + */
73 + public IpAddress getNextHopIpAddress() {
74 + return nextHopIpAddress;
75 + }
76 +}
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.virtualbng;
17 +
18 +import com.fasterxml.jackson.databind.ObjectMapper;
19 +
20 +import java.io.File;
21 +import java.io.FileNotFoundException;
22 +import java.io.IOException;
23 +import java.util.Iterator;
24 +import java.util.Map;
25 +import java.util.Map.Entry;
26 +import java.util.concurrent.ConcurrentHashMap;
27 +
28 +import org.apache.felix.scr.annotations.Activate;
29 +import org.apache.felix.scr.annotations.Component;
30 +import org.apache.felix.scr.annotations.Deactivate;
31 +import org.apache.felix.scr.annotations.Service;
32 +import org.onlab.packet.IpAddress;
33 +import org.onlab.packet.IpPrefix;
34 +import org.slf4j.Logger;
35 +import org.slf4j.LoggerFactory;
36 +
37 +/**
38 + * Implementation of ConfigurationService which reads virtual BNG
39 + * configuration from a file.
40 + */
41 +@Component(immediate = true)
42 +@Service
43 +public class VbngConfigurationManager implements VbngConfigurationService {
44 +
45 + private final Logger log = LoggerFactory.getLogger(getClass());
46 +
47 + private static final String CONFIG_DIR = "../config";
48 + private static final String DEFAULT_CONFIG_FILE = "virtualbng.json";
49 + private String configFileName = DEFAULT_CONFIG_FILE;
50 +
51 + // If all the IP addresses of one IP prefix are assigned, then we
52 + // mark the value of this IP prefix as false, otherwise as true.
53 + private Map<IpPrefix, Boolean> localPublicIpPrefixes =
54 + new ConcurrentHashMap<>();
55 +
56 + // Map from private IP address to public IP address
57 + private Map<IpAddress, IpAddress> ipAddressMap =
58 + new ConcurrentHashMap<>();
59 +
60 + private IpAddress nextHopIpAddress;
61 +
62 + @Activate
63 + public void activate() {
64 + readConfiguration();
65 + log.info("vBNG configuration service started");
66 + }
67 +
68 + @Deactivate
69 + public void deactivate() {
70 + log.info("vBNG configuration service stopped");
71 + }
72 +
73 + /**
74 + * Instructs the configuration reader to read the configuration from the
75 + * file.
76 + */
77 + public void readConfiguration() {
78 + readConfiguration(configFileName);
79 + }
80 +
81 + /**
82 + * Reads virtual BNG information contained in configuration file.
83 + *
84 + * @param configFilename the name of the configuration file for the virtual
85 + * BNG application
86 + */
87 + private void readConfiguration(String configFilename) {
88 + File configFile = new File(CONFIG_DIR, configFilename);
89 + ObjectMapper mapper = new ObjectMapper();
90 +
91 + try {
92 + log.info("Loading config: {}", configFile.getAbsolutePath());
93 + VbngConfiguration config = mapper.readValue(configFile,
94 + VbngConfiguration.class);
95 + for (IpPrefix prefix : config.getLocalPublicIpPrefixes()) {
96 + localPublicIpPrefixes.put(prefix, true);
97 + }
98 + nextHopIpAddress = config.getNextHopIpAddress();
99 +
100 + } catch (FileNotFoundException e) {
101 + log.warn("Configuration file not found: {}", configFileName);
102 + } catch (IOException e) {
103 + log.error("Error loading configuration", e);
104 + }
105 + }
106 +
107 + @Override
108 + public IpAddress getNextHopIpAddress() {
109 + return nextHopIpAddress;
110 + }
111 +
112 + // TODO handle the case: the number of public IP addresses is not enough
113 + // for 1:1 mapping from public IP to private IP.
114 + @Override
115 + public IpAddress getAvailablePublicIpAddress(IpAddress privateIpAddress) {
116 + // If there is already a mapping entry for the private IP address,
117 + // then fetch the public IP address in the mapping entry and return it.
118 + IpAddress publicIpAddress = ipAddressMap.get(privateIpAddress);
119 + if (publicIpAddress != null) {
120 + return publicIpAddress;
121 + }
122 + // There is no mapping for the private IP address.
123 + Iterator<Entry<IpPrefix, Boolean>> prefixes =
124 + localPublicIpPrefixes.entrySet().iterator();
125 + while (prefixes.hasNext()) {
126 + Entry<IpPrefix, Boolean> prefix = prefixes.next();
127 + if (!prefix.getValue()) {
128 + continue;
129 + }
130 +
131 + if (prefix.getKey().prefixLength() == 32) {
132 + updateIpPrefixStatus(prefix.getKey(), false);
133 + publicIpAddress = prefix.getKey().address();
134 + ipAddressMap.put(privateIpAddress, publicIpAddress);
135 + return publicIpAddress;
136 + }
137 +
138 + int prefixLen = prefix.getKey().prefixLength();
139 + int availableIpNum = (int) Math.pow(2,
140 + IpPrefix.MAX_INET_MASK_LENGTH - prefixLen) - 1;
141 + for (int i = 1; i <= availableIpNum; i++) {
142 + publicIpAddress =
143 + increaseIpAddress(prefix.getKey().address(), i);
144 + if (publicIpAddress == null) {
145 + return null;
146 + }
147 + if (ipAddressMap.values().contains(publicIpAddress)) {
148 + continue;
149 + } else if (i == availableIpNum) {
150 + // All the IP addresses are assigned out
151 + // Update this IP prefix status to false
152 + // Note: in this version we do not consider the
153 + // IP recycling issue.
154 + updateIpPrefixStatus(prefix.getKey(), false);
155 + ipAddressMap.put(privateIpAddress, publicIpAddress);
156 + return publicIpAddress;
157 + } else {
158 + ipAddressMap.put(privateIpAddress, publicIpAddress);
159 + return publicIpAddress;
160 + }
161 + }
162 + }
163 + return null;
164 + }
165 +
166 + /**
167 + * Generates a new IP address base on a given IP address plus a number to
168 + * increase.
169 + *
170 + * @param ipAddress the IP address to increase
171 + * @param num the number for ipAddress to add
172 + * @return the new IP address after increase
173 + */
174 + private IpAddress increaseIpAddress(IpAddress ipAddress, int num) {
175 + if (ipAddress.isIp6()) {
176 + log.info("vBNG currently does not handle IPv6");
177 + return null;
178 + }
179 + return IpAddress.valueOf(ipAddress.getIp4Address().toInt() + num);
180 + }
181 +
182 + /**
183 + * Updates the IP prefix status in the local public IP prefix table.
184 + *
185 + * @param ipPprefix the IP prefix to update
186 + * @param b the new value for the IP prefix
187 + */
188 + private void updateIpPrefixStatus(IpPrefix ipPprefix, boolean b) {
189 + localPublicIpPrefixes.replace(ipPprefix, b);
190 + }
191 +
192 +}
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.virtualbng;
17 +
18 +import org.onlab.packet.IpAddress;
19 +
20 +/**
21 + * Provides information about the virtual BNG configuration.
22 + */
23 +public interface VbngConfigurationService {
24 +
25 + /**
26 + * Gets the IP address configured for the next hop.
27 + *
28 + * @return the IP address of next hop
29 + */
30 + public IpAddress getNextHopIpAddress();
31 +
32 + /**
33 + * Gets an available public IP address from local public IP prefixes.
34 + *
35 + * @param privateIpAddress a private IP address
36 + * @return an available public IP address if it exists, otherwise null
37 + */
38 + public IpAddress getAvailablePublicIpAddress(IpAddress privateIpAddress);
39 +}
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.virtualbng;
17 +
18 +import static com.google.common.base.Preconditions.checkNotNull;
19 +
20 +import java.util.Map;
21 +import java.util.concurrent.ConcurrentHashMap;
22 +
23 +import org.apache.felix.scr.annotations.Activate;
24 +import org.apache.felix.scr.annotations.Component;
25 +import org.apache.felix.scr.annotations.Deactivate;
26 +import org.apache.felix.scr.annotations.Reference;
27 +import org.apache.felix.scr.annotations.ReferenceCardinality;
28 +import org.apache.felix.scr.annotations.Service;
29 +import org.onlab.packet.Ethernet;
30 +import org.onlab.packet.IpAddress;
31 +import org.onlab.packet.IpPrefix;
32 +import org.onlab.packet.MacAddress;
33 +import org.onosproject.core.ApplicationId;
34 +import org.onosproject.core.CoreService;
35 +import org.onosproject.net.ConnectPoint;
36 +import org.onosproject.net.Host;
37 +import org.onosproject.net.flow.DefaultTrafficSelector;
38 +import org.onosproject.net.flow.DefaultTrafficTreatment;
39 +import org.onosproject.net.flow.TrafficSelector;
40 +import org.onosproject.net.flow.TrafficTreatment;
41 +import org.onosproject.net.host.HostService;
42 +import org.onosproject.net.intent.IntentService;
43 +import org.onosproject.net.intent.Key;
44 +import org.onosproject.net.intent.PointToPointIntent;
45 +import org.slf4j.Logger;
46 +import org.slf4j.LoggerFactory;
47 +
48 +/**
49 + * This is a virtual Broadband Network Gateway (BNG) application. It mainly
50 + * has 3 functions:
51 + * (1) assigns and replies a public IP address to a REST request with a private
52 + * IP address
53 + * (2) maintains the mapping from the private IP address to the public IP address
54 + * (3) installs point to point intents for the host configured with private IP
55 + * address to access Internet
56 + */
57 +@Component(immediate = true)
58 +@Service
59 +public class VbngManager implements VbngService {
60 +
61 + private static final String APP_NAME = "org.onosproject.virtualbng";
62 +
63 + private final Logger log = LoggerFactory.getLogger(getClass());
64 +
65 + @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
66 + protected CoreService coreService;
67 +
68 + @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
69 + protected HostService hostService;
70 +
71 + @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
72 + protected IntentService intentService;
73 +
74 + @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
75 + protected VbngConfigurationService vbngConfigurationService;
76 +
77 + private ApplicationId appId;
78 + private Map<IpAddress, PointToPointIntent> p2pIntentsFromHost;
79 + private Map<IpAddress, PointToPointIntent> p2pIntentsToHost;
80 +
81 + @Activate
82 + public void activate() {
83 + appId = coreService.registerApplication(APP_NAME);
84 + p2pIntentsFromHost = new ConcurrentHashMap<>();
85 + p2pIntentsToHost = new ConcurrentHashMap<>();
86 + log.info("vBNG Started");
87 + }
88 +
89 + @Deactivate
90 + public void deactivate() {
91 + log.info("vBNG Stopped");
92 + }
93 +
94 + @Override
95 + public IpAddress createVbng(IpAddress privateIpAddress) {
96 +
97 + IpAddress nextHopIpAddress =
98 + vbngConfigurationService.getNextHopIpAddress();
99 + if (nextHopIpAddress == null) {
100 + log.info("Did not find next hop IP address");
101 + return null;
102 + }
103 +
104 + IpAddress publicIpAddress =
105 + vbngConfigurationService.getAvailablePublicIpAddress(
106 + privateIpAddress);
107 + if (publicIpAddress == null) {
108 + log.info("Did not find an available public IP address to use.");
109 + return null;
110 + }
111 + log.info("Private IP to Public IP mapping: {} --> {}",
112 + privateIpAddress, publicIpAddress);
113 +
114 + // Setup paths between the host configured with private IP and
115 + // next hop
116 + setupForwardingPaths(privateIpAddress, publicIpAddress,
117 + nextHopIpAddress);
118 + return publicIpAddress;
119 + }
120 +
121 + /**
122 + * Sets up forwarding paths in both two directions between host configured
123 + * with private IP and next hop.
124 + *
125 + * @param privateIp the private IP address of a local host
126 + * @param publicIp the public IP address assigned for the private IP address
127 + * @param nextHopIpAddress the next hop IP address outside local SDN network
128 + */
129 + private void setupForwardingPaths(IpAddress privateIp, IpAddress publicIp,
130 + IpAddress nextHopIpAddress) {
131 + checkNotNull(privateIp);
132 + checkNotNull(publicIp);
133 + checkNotNull(nextHopIpAddress);
134 +
135 + // If there are already intents for private IP address in the system,
136 + // we will do nothing and directly return.
137 + if (p2pIntentsFromHost.containsKey(privateIp)
138 + && p2pIntentsToHost.containsKey(privateIp)) {
139 + return;
140 + }
141 +
142 + Host localHost = null;
143 + Host nextHopHost = null;
144 + if (!hostService.getHostsByIp(nextHopIpAddress).isEmpty()) {
145 + nextHopHost = hostService.getHostsByIp(nextHopIpAddress)
146 + .iterator().next();
147 + } else {
148 + // TODO to write a new thread to install intents after ONOS
149 + // discovers the next hop host
150 + hostService.startMonitoringIp(nextHopIpAddress);
151 + if (hostService.getHostsByIp(privateIp).isEmpty()) {
152 + hostService.startMonitoringIp(privateIp);
153 + }
154 + return;
155 + }
156 +
157 + if (!hostService.getHostsByIp(privateIp).isEmpty()) {
158 + localHost =
159 + hostService.getHostsByIp(privateIp).iterator().next();
160 + } else {
161 + // TODO to write a new thread to install intents after ONOS
162 + // discovers the next hop host
163 + hostService.startMonitoringIp(privateIp);
164 + return;
165 + }
166 +
167 + ConnectPoint nextHopConnectPoint =
168 + new ConnectPoint(nextHopHost.location().elementId(),
169 + nextHopHost.location().port());
170 + ConnectPoint localHostConnectPoint =
171 + new ConnectPoint(localHost.location().elementId(),
172 + localHost.location().port());
173 +
174 + // Generate and install intent for traffic from host configured with
175 + // private IP
176 + if (!p2pIntentsFromHost.containsKey(privateIp)) {
177 + PointToPointIntent toNextHopIntent
178 + = srcMatchIntentGenerator(privateIp,
179 + publicIp,
180 + nextHopHost.mac(),
181 + nextHopConnectPoint,
182 + localHostConnectPoint
183 + );
184 + p2pIntentsFromHost.put(privateIp, toNextHopIntent);
185 + intentService.submit(toNextHopIntent);
186 + }
187 +
188 + // Generate and install intent for traffic to host configured with
189 + // private IP
190 + if (!p2pIntentsToHost.containsKey(privateIp)) {
191 + PointToPointIntent toLocalHostIntent
192 + = dstMatchIntentGenerator(publicIp,
193 + privateIp,
194 + localHost.mac(),
195 + localHostConnectPoint,
196 + nextHopConnectPoint);
197 + p2pIntentsToHost.put(nextHopIpAddress, toLocalHostIntent);
198 + intentService.submit(toLocalHostIntent);
199 + }
200 +
201 + return;
202 + }
203 +
204 + /**
205 + * PointToPointIntent Generator.
206 + * <p>
207 + * The intent will match the source IP address in packet, rewrite the
208 + * source IP address, and rewrite the destination MAC address.
209 + * </p>
210 + *
211 + * @param srcIpAddress the source IP address in packet to match
212 + * @param newSrcIpAddress the new source IP address to set
213 + * @param dstMacAddress the destination MAC address to set
214 + * @param dstConnectPoint the egress point
215 + * @param srcConnectPoint the ingress point
216 + * @return a PointToPointIntent
217 + */
218 + private PointToPointIntent srcMatchIntentGenerator(
219 + IpAddress srcIpAddress,
220 + IpAddress newSrcIpAddress,
221 + MacAddress dstMacAddress,
222 + ConnectPoint dstConnectPoint,
223 + ConnectPoint srcConnectPoint) {
224 + checkNotNull(srcIpAddress);
225 + checkNotNull(newSrcIpAddress);
226 + checkNotNull(dstMacAddress);
227 + checkNotNull(dstConnectPoint);
228 + checkNotNull(srcConnectPoint);
229 +
230 + TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
231 + selector.matchEthType(Ethernet.TYPE_IPV4);
232 + selector.matchIPSrc(IpPrefix.valueOf(srcIpAddress,
233 + IpPrefix.MAX_INET_MASK_LENGTH));
234 +
235 + TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
236 + treatment.setEthDst(dstMacAddress);
237 + treatment.setIpSrc(newSrcIpAddress);
238 +
239 + Key key = Key.of(srcIpAddress.toString() + "MatchSrc", appId);
240 + PointToPointIntent intent = PointToPointIntent.builder()
241 + .appId(appId)
242 + .key(key)
243 + .selector(selector.build())
244 + .treatment(treatment.build())
245 + .egressPoint(dstConnectPoint)
246 + .ingressPoint(srcConnectPoint)
247 + .build();
248 +
249 + log.info("Generated a PointToPointIntent for traffic from local host "
250 + + ": {}", intent);
251 + return intent;
252 + }
253 +
254 + /**
255 + * PointToPointIntent Generator.
256 + * <p>
257 + * The intent will match the destination IP address in packet, rewrite the
258 + * destination IP address, and rewrite the destination MAC address.
259 + * </p>
260 + *
261 + * @param dstIpAddress the destination IP address in packet to match
262 + * @param newDstIpAddress the new destination IP address to set
263 + * @param dstMacAddress the destination MAC address to set
264 + * @param dstConnectPoint the egress point
265 + * @param srcConnectPoint the ingress point
266 + * @return a PointToPointIntent
267 + */
268 + private PointToPointIntent dstMatchIntentGenerator(
269 + IpAddress dstIpAddress,
270 + IpAddress newDstIpAddress,
271 + MacAddress dstMacAddress,
272 + ConnectPoint dstConnectPoint,
273 + ConnectPoint srcConnectPoint) {
274 + checkNotNull(dstIpAddress);
275 + checkNotNull(newDstIpAddress);
276 + checkNotNull(dstMacAddress);
277 + checkNotNull(dstConnectPoint);
278 + checkNotNull(srcConnectPoint);
279 +
280 + TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
281 + selector.matchEthType(Ethernet.TYPE_IPV4);
282 + selector.matchIPDst(IpPrefix.valueOf(dstIpAddress,
283 + IpPrefix.MAX_INET_MASK_LENGTH));
284 +
285 + TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
286 + treatment.setEthDst(dstMacAddress);
287 + treatment.setIpDst(newDstIpAddress);
288 +
289 + Key key = Key.of(newDstIpAddress.toString() + "MatchDst", appId);
290 + PointToPointIntent intent = PointToPointIntent.builder()
291 + .appId(appId)
292 + .key(key)
293 + .selector(selector.build())
294 + .treatment(treatment.build())
295 + .egressPoint(dstConnectPoint)
296 + .ingressPoint(srcConnectPoint)
297 + .build();
298 + log.info("Generated a PointToPointIntent for traffic to local host "
299 + + ": {}", intent);
300 +
301 + return intent;
302 + }
303 +
304 +
305 +}
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.virtualbng;
17 +
18 +import static org.slf4j.LoggerFactory.getLogger;
19 +
20 +import javax.ws.rs.POST;
21 +import javax.ws.rs.Path;
22 +import javax.ws.rs.PathParam;
23 +
24 +import org.onlab.packet.IpAddress;
25 +import org.onlab.rest.BaseResource;
26 +import org.slf4j.Logger;
27 +
28 +/**
29 + * This class provides REST services to virtual BNG.
30 + */
31 +@Path("privateip")
32 +public class VbngResource extends BaseResource {
33 +
34 + private final Logger log = getLogger(getClass());
35 +
36 + @POST
37 + @Path("{privateip}")
38 + public String privateIpNotification(@PathParam("privateip")
39 + String privateIp) {
40 + if (privateIp == null) {
41 + log.info("Private IP address is null");
42 + return "0";
43 + }
44 + log.info("Received a private IP address : {}", privateIp);
45 + IpAddress privateIpAddress = IpAddress.valueOf(privateIp);
46 +
47 + VbngService vbngService = get(VbngService.class);
48 +
49 + IpAddress publicIpAddress = null;
50 + synchronized (this) {
51 + // Create a virtual BNG
52 + publicIpAddress = vbngService.createVbng(privateIpAddress);
53 + }
54 +
55 + if (publicIpAddress != null) {
56 + return publicIpAddress.toString();
57 + } else {
58 + return "0";
59 + }
60 + }
61 +}
...\ 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.virtualbng;
17 +
18 +import org.onlab.packet.IpAddress;
19 +
20 +/**
21 + * Provides service of the virtual BNG.
22 + */
23 +public interface VbngService {
24 +
25 + /**
26 + * Creates a virtual BNG.
27 + * <p>
28 + * It firstly finds out an available local public IP address. Then, it
29 + * sets up paths between the host configured with private IP and
30 + * next hop. Finally it returns the public IP address.
31 + * </p>
32 + *
33 + * @param privateIpAddress the private IP address
34 + * @return the public address if a virtual BGN is successfully created,
35 + * otherwise return null
36 + */
37 + public IpAddress createVbng(IpAddress privateIpAddress);
38 +
39 +}
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 +/**
18 + * A virtual Broadband Network Gateway (BNG) application.
19 + */
20 +package org.onosproject.virtualbng;
1 +<?xml version="1.0" encoding="UTF-8"?>
2 +<!--
3 + ~ Copyright 2015 Open Networking Laboratory
4 + ~
5 + ~ Licensed under the Apache License, Version 2.0 (the "License");
6 + ~ you may not use this file except in compliance with the License.
7 + ~ You may obtain a copy of the License at
8 + ~
9 + ~ http://www.apache.org/licenses/LICENSE-2.0
10 + ~
11 + ~ Unless required by applicable law or agreed to in writing, software
12 + ~ distributed under the License is distributed on an "AS IS" BASIS,
13 + ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 + ~ See the License for the specific language governing permissions and
15 + ~ limitations under the License.
16 + -->
17 +<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee"
18 + xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
19 + xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
20 + id="ONOS" version="2.5">
21 + <display-name>ONOS Virual BNG APP REST API</display-name>
22 +
23 + <servlet>
24 + <servlet-name>JAX-RS Service</servlet-name>
25 + <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
26 + <init-param>
27 + <param-name>com.sun.jersey.config.property.resourceConfigClass</param-name>
28 + <param-value>com.sun.jersey.api.core.ClassNamesResourceConfig</param-value>
29 + </init-param>
30 + <init-param>
31 + <param-name>com.sun.jersey.config.property.classnames</param-name>
32 + <param-value>
33 + org.onosproject.virtualbng.VbngResource
34 + </param-value>
35 + </init-param>
36 + <load-on-startup>1</load-on-startup>
37 + </servlet>
38 +
39 + <servlet-mapping>
40 + <servlet-name>JAX-RS Service</servlet-name>
41 + <url-pattern>/*</url-pattern>
42 + </servlet-mapping>
43 +
44 +</web-app>