Last active
December 30, 2016 14:10
-
-
Save Yi-Tseng/53efa4606f22b7485373ca6f7b7118ce to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.onosproject.intentdemo; | |
import com.google.common.collect.Sets; | |
import org.apache.felix.scr.annotations.Activate; | |
import org.apache.felix.scr.annotations.Component; | |
import org.apache.felix.scr.annotations.Deactivate; | |
import org.apache.felix.scr.annotations.Reference; | |
import org.onosproject.core.ApplicationId; | |
import org.onosproject.core.CoreService; | |
import org.onosproject.net.FilteredConnectPoint; | |
import org.onosproject.net.Host; | |
import org.onosproject.net.flow.DefaultTrafficSelector; | |
import org.onosproject.net.flow.TrafficSelector; | |
import org.onosproject.net.host.HostEvent; | |
import org.onosproject.net.host.HostListener; | |
import org.onosproject.net.host.HostService; | |
import org.onosproject.net.intent.Intent; | |
import org.onosproject.net.intent.IntentService; | |
import org.onosproject.net.intent.Key; | |
import org.onosproject.net.intent.MultiPointToSinglePointIntent; | |
import java.util.Set; | |
import java.util.stream.Collectors; | |
@Component(immediate = true) | |
public class IntentDemo implements HostListener { | |
public static final String ID = "org.onosproject.intentdemo"; | |
private ApplicationId appId; | |
@Reference | |
CoreService coreService; | |
@Reference | |
IntentService intentService; | |
@Reference | |
HostService hostService; | |
@Activate | |
public void activate() { | |
appId = coreService.registerApplication(ID); | |
hostService.addListener(this); | |
resetConnection(); | |
} | |
@Deactivate | |
public void deactivate() { | |
hostService.removeListener(this); | |
} | |
private void resetConnection() { | |
Set<Host> hosts = Sets.newHashSet(hostService.getHosts()); | |
hosts.forEach(h -> { | |
Intent intent = createToHostIntent(h); | |
if (intent != null) { | |
intentService.submit(intent); | |
} | |
}); | |
} | |
private MultiPointToSinglePointIntent createToHostIntent(Host host) { | |
MultiPointToSinglePointIntent intent; | |
FilteredConnectPoint egressPoint = new FilteredConnectPoint(host.location()); | |
Set<Host> srcHosts = Sets.newHashSet(hostService.getHosts()); | |
srcHosts = srcHosts.stream().filter(h -> !h.equals(host)).collect(Collectors.toSet()); | |
Set<FilteredConnectPoint> ingressPoints = srcHosts.stream() | |
.map(Host::location) | |
.map(FilteredConnectPoint::new) | |
.collect(Collectors.toSet()); | |
if (ingressPoints.size() == 0) { | |
return null; | |
} | |
TrafficSelector selector = DefaultTrafficSelector.builder() | |
.matchEthDst(host.mac()).build(); | |
Key key = Key.of(host.mac().toString(), appId); | |
intent = MultiPointToSinglePointIntent.builder() | |
.appId(appId) | |
.key(key) | |
.filteredIngressPoints(ingressPoints) | |
.filteredEgressPoint(egressPoint) | |
.selector(selector) | |
.build(); | |
return intent; | |
} | |
@Override | |
public void event(HostEvent event) { | |
resetConnection(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment