Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2015 Open Networking Laboratory
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 package org.onosproject.virtualbng;
 
 import static com.google.common.base.Preconditions.checkNotNull;
 
 
 import java.util.Map;
 import java.util.Set;
 
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
 
 @Component(immediate = true)
 public class VbngManager implements VbngService {
 
     private static final String APP_NAME = "org.onosproject.virtualbng";
 
     private final Logger log = LoggerFactory.getLogger(getClass());
 
     protected CoreService coreService;
 
     protected HostService hostService;
 
     protected IntentService intentService;
 
 
     private ApplicationId appId;
 
     // This set stores all the private IP addresses we failed to create vBNGs
     // for the first time.
     private Set<IpAddressprivateIpAddressSet;
 
     private HostListener hostListener;
     private IpAddress nextHopIpAddress;
 
     @Activate
     public void activate() {
          = new ConcurrentHashMap<>();
          = new ConcurrentHashMap<>();
          = Sets.newConcurrentHashSet();
         = new InternalHostListener();
        .info("vBNG Started");
    }
    @Deactivate
    public void deactivate() {
        .info("vBNG Stopped");
    }
    @Override
    public IpAddress createVbng(IpAddress privateIpAddress) {
        IpAddress publicIpAddress =
                                                     privateIpAddress);
        if (publicIpAddress == null) {
            .info("Did not find an available public IP address to use.");
            return null;
        }
        .info("[ADD] Private IP to Public IP mapping: {} --> {}",
                 privateIpAddresspublicIpAddress);
        // Setup paths between the host configured with private IP and
        // next hop
        if (!setupForwardingPaths(privateIpAddresspublicIpAddress)) {
            .add(privateIpAddress);
        }
        return publicIpAddress;
    }
    @Override
    public IpAddress deleteVbng(IpAddress privateIpAddress) {
        // Recycle the public IP address assigned to this private IP address.
        // Recycling will also delete the mapping entry from the private IP
        // address to public IP address.
        IpAddress assignedPublicIpAddress = 
                .recycleAssignedPublicIpAddress(privateIpAddress);
        if (assignedPublicIpAddress == null) {
            return null;
        }
        // Remove the private IP address from privateIpAddressSet
        .remove(privateIpAddress);
        // Remove intents
        removeForwardingPaths(privateIpAddress);
        return assignedPublicIpAddress;
    }

    
Removes the forwarding paths in both two directions between host configured with private IP and next hop.

Parameters:
privateIp the private IP address of a local host
    private void removeForwardingPaths(IpAddress privateIp) {
        PointToPointIntent toNextHopIntent =
                .remove(privateIp);
        if (toNextHopIntent != null) {
            .withdraw(toNextHopIntent);
            //intentService.purge(toNextHopIntent);
        }
        PointToPointIntent toLocalHostIntent =
                .remove(privateIp);
        if (toLocalHostIntent != null) {
            .withdraw(toLocalHostIntent);
            //intentService.purge(toLocalHostIntent);
        }
    }

    
Sets up forwarding paths in both two directions between host configured with private IP and next hop.

Parameters:
privateIp the private IP address of a local host
publicIp the public IP address assigned for the private IP address
    private boolean setupForwardingPaths(IpAddress privateIpIpAddress publicIp) {
        checkNotNull(privateIp);
        checkNotNull(publicIp);
        if ( == null) {
            .warn("Did not find next hop IP address");
            return false;
        }
        // If there are already intents for private IP address in the system,
        // we will do nothing and directly return.
        if (.containsKey(privateIp)
                && .containsKey(privateIp)) {
            return true;
        }
        Host localHost = null;
        Host nextHopHost = null;
            nextHopHost = .getHostsByIp()
                    .iterator().next();
        } else {
            if (.getHostsByIp(privateIp).isEmpty()) {
                .startMonitoringIp(privateIp);
            }
            return false;
        }
        if (!.getHostsByIp(privateIp).isEmpty()) {
            localHost =
                    .getHostsByIp(privateIp).iterator().next();
        } else {
            .startMonitoringIp(privateIp);
            return false;
        }
        ConnectPoint nextHopConnectPoint =
                new ConnectPoint(nextHopHost.location().elementId(),
                                 nextHopHost.location().port());
        ConnectPoint localHostConnectPoint =
                new ConnectPoint(localHost.location().elementId(),
                                 localHost.location().port());
        // Generate and install intent for traffic from host configured with
        // private IP
        if (!.containsKey(privateIp)) {
            PointToPointIntent toNextHopIntent
                    = srcMatchIntentGenerator(privateIp,
                                              publicIp,
                                              nextHopHost.mac(),
                                              nextHopConnectPoint,
                                              localHostConnectPoint
                                              );
            .put(privateIptoNextHopIntent);
            .submit(toNextHopIntent);
        }
        // Generate and install intent for traffic to host configured with
        // private IP
        if (!.containsKey(privateIp)) {
            PointToPointIntent toLocalHostIntent
                    = dstMatchIntentGenerator(publicIp,
                                              privateIp,
                                              localHost.mac(),
                                              localHostConnectPoint,
                                              nextHopConnectPoint);
            .put(privateIptoLocalHostIntent);
            .submit(toLocalHostIntent);
        }
        return true;
    }

    
Listener for host events.
    private class InternalHostListener implements HostListener {
        @Override
        public void event(HostEvent event) {
            .debug("Received HostEvent {}"event);
            Host host = event.subject();
            if (event.type() != ..) {
                return;
            }
            for (IpAddress ipAddresshost.ipAddresses()) {
                if (.contains(ipAddress)) {
                    createVbngAgain(ipAddress);
                }
                if ( != null &&
                        ipAddress.equals()) {
                    Iterator<IpAddressipAddresses =
                            .iterator();
                    while (ipAddresses.hasNext()) {
                        IpAddress privateIpAddress = ipAddresses.next();
                        createVbngAgain(privateIpAddress);
                    }
                }
            }
        }
    }

    
Tries to create vBNG again after receiving a host event if the IP address of the host is a private IP address or the next hop IP address.

Parameters:
privateIpAddress the private IP address
    private void createVbngAgain(IpAddress privateIpAddress) {
        IpAddress publicIpAddress = 
                .getAssignedPublicIpAddress(privateIpAddress);
        if (publicIpAddress == null) {
            // We only need to handle the private IP addresses for which we
            // already returned the REST replies with assigned public IP
            // addresses. If a private IP addresses does not have an assigned
            // public IP address, we should not get it an available public IP
            // address here, and we should delete it in the unhandled private
            // IP address set.
            .remove(privateIpAddress);
            return;
        }
        if (setupForwardingPaths(privateIpAddresspublicIpAddress)) {
            // At this moment it is still possible to fail to create a vBNG,
            // because creating a vBNG needs two hosts, one is the local host
            // configured with private IP address, the other is the next hop
            // host.
            .remove(privateIpAddress);
        }
    }

    
PointToPointIntent Generator.

The intent will match the source IP address in packet, rewrite the source IP address, and rewrite the destination MAC address.

Parameters:
srcIpAddress the source IP address in packet to match
newSrcIpAddress the new source IP address to set
dstMacAddress the destination MAC address to set
dstConnectPoint the egress point
srcConnectPoint the ingress point
Returns:
a PointToPointIntent
                                             IpAddress srcIpAddress,
                                             IpAddress newSrcIpAddress,
                                             MacAddress dstMacAddress,
                                             ConnectPoint dstConnectPoint,
                                             ConnectPoint srcConnectPoint) {
        checkNotNull(srcIpAddress);
        checkNotNull(newSrcIpAddress);
        checkNotNull(dstMacAddress);
        checkNotNull(dstConnectPoint);
        checkNotNull(srcConnectPoint);
        TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
        selector.matchEthType(.);
        selector.matchIPSrc(IpPrefix.valueOf(srcIpAddress,
                                             .));
        TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
        treatment.setEthDst(dstMacAddress);
        treatment.setIpSrc(newSrcIpAddress);
        Key key = Key.of(srcIpAddress.toString() + "MatchSrc");
        PointToPointIntent intent = PointToPointIntent.builder()
                .appId()
                .key(key)
                .selector(selector.build())
                .treatment(treatment.build())
                .egressPoint(dstConnectPoint)
                .ingressPoint(srcConnectPoint)
                .build();
        .info("Generated a PointToPointIntent for traffic from local host "
                + ": {}"intent);
        return intent;
    }

    
PointToPointIntent Generator.

The intent will match the destination IP address in packet, rewrite the destination IP address, and rewrite the destination MAC address.

Parameters:
dstIpAddress the destination IP address in packet to match
newDstIpAddress the new destination IP address to set
dstMacAddress the destination MAC address to set
dstConnectPoint the egress point
srcConnectPoint the ingress point
Returns:
a PointToPointIntent
                                                IpAddress dstIpAddress,
                                                IpAddress newDstIpAddress,
                                                MacAddress dstMacAddress,
                                                ConnectPoint dstConnectPoint,
                                                ConnectPoint srcConnectPoint) {
        checkNotNull(dstIpAddress);
        checkNotNull(newDstIpAddress);
        checkNotNull(dstMacAddress);
        checkNotNull(dstConnectPoint);
        checkNotNull(srcConnectPoint);
        TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
        selector.matchEthType(.);
        selector.matchIPDst(IpPrefix.valueOf(dstIpAddress,
                                             .));
        TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
        treatment.setEthDst(dstMacAddress);
        treatment.setIpDst(newDstIpAddress);
        Key key = Key.of(newDstIpAddress.toString() + "MatchDst");
        PointToPointIntent intent = PointToPointIntent.builder()
                .appId()
                .key(key)
                .selector(selector.build())
                .treatment(treatment.build())
                .egressPoint(dstConnectPoint)
                .ingressPoint(srcConnectPoint)
                .build();
        .info("Generated a PointToPointIntent for traffic to local host "
                + ": {}"intent);
        return intent;
    }
New to GrepCode? Check out our FAQ X