alshabib | fd23d31 | 2014-11-11 18:14:47 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 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.onos.demo; |
| 17 | |
| 18 | import com.google.common.collect.Lists; |
| 19 | import com.google.common.util.concurrent.ThreadFactoryBuilder; |
| 20 | import org.apache.felix.scr.annotations.Activate; |
| 21 | import org.apache.felix.scr.annotations.Component; |
| 22 | import org.apache.felix.scr.annotations.Deactivate; |
| 23 | import org.apache.felix.scr.annotations.Reference; |
| 24 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 25 | import org.apache.felix.scr.annotations.Service; |
| 26 | import org.onlab.onos.core.ApplicationId; |
| 27 | import org.onlab.onos.core.CoreService; |
| 28 | import org.onlab.onos.net.Host; |
| 29 | import org.onlab.onos.net.flow.DefaultTrafficSelector; |
| 30 | import org.onlab.onos.net.flow.DefaultTrafficTreatment; |
| 31 | import org.onlab.onos.net.flow.TrafficSelector; |
| 32 | import org.onlab.onos.net.flow.TrafficTreatment; |
| 33 | import org.onlab.onos.net.host.HostService; |
alshabib | 19678cc | 2014-11-12 11:06:08 -0800 | [diff] [blame^] | 34 | import org.onlab.onos.net.intent.Constraint; |
alshabib | fd23d31 | 2014-11-11 18:14:47 -0800 | [diff] [blame] | 35 | import org.onlab.onos.net.intent.HostToHostIntent; |
| 36 | import org.onlab.onos.net.intent.Intent; |
| 37 | import org.onlab.onos.net.intent.IntentService; |
| 38 | import org.slf4j.Logger; |
| 39 | |
| 40 | |
| 41 | import java.util.HashSet; |
| 42 | import java.util.List; |
| 43 | import java.util.Set; |
| 44 | import java.util.concurrent.ExecutorService; |
| 45 | import java.util.concurrent.Executors; |
| 46 | |
| 47 | import static org.slf4j.LoggerFactory.getLogger; |
| 48 | |
| 49 | /** |
| 50 | * Application to set up demos. |
| 51 | */ |
| 52 | @Component(immediate = true) |
| 53 | @Service |
| 54 | public class DemoInstaller implements DemoAPI { |
| 55 | |
| 56 | private final Logger log = getLogger(getClass()); |
| 57 | |
| 58 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 59 | protected CoreService coreService; |
| 60 | |
| 61 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 62 | protected IntentService intentService; |
| 63 | |
| 64 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 65 | protected HostService hostService; |
| 66 | |
| 67 | private ExecutorService worker; |
| 68 | |
| 69 | private ApplicationId appId; |
| 70 | |
| 71 | private final Set<Intent> existingIntents = new HashSet<>(); |
| 72 | |
| 73 | |
| 74 | |
| 75 | @Activate |
| 76 | public void activate() { |
| 77 | appId = coreService.registerApplication("org.onlab.onos.demo.installer"); |
| 78 | worker = Executors.newFixedThreadPool(1, |
| 79 | new ThreadFactoryBuilder() |
| 80 | .setNameFormat("demo-app-worker") |
| 81 | .build()); |
| 82 | log.info("Started with Application ID {}", appId.id()); |
| 83 | } |
| 84 | |
| 85 | @Deactivate |
| 86 | public void deactivate() { |
| 87 | worker.shutdownNow(); |
| 88 | log.info("Stopped"); |
| 89 | } |
| 90 | |
| 91 | @Override |
| 92 | public void setup(InstallType type) { |
| 93 | switch (type) { |
| 94 | case MESH: |
| 95 | log.debug("Installing mesh intents"); |
| 96 | worker.execute(new MeshInstaller()); |
| 97 | break; |
| 98 | case RANDOM: |
| 99 | throw new IllegalArgumentException("Not yet implemented."); |
| 100 | default: |
| 101 | throw new IllegalArgumentException("What is it you want exactly?"); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | @Override |
| 106 | public void tearDown() { |
| 107 | worker.submit(new UnInstaller()); |
| 108 | } |
| 109 | |
| 110 | |
| 111 | private class MeshInstaller implements Runnable { |
| 112 | |
| 113 | @Override |
| 114 | public void run() { |
| 115 | TrafficSelector selector = DefaultTrafficSelector.builder().build(); |
| 116 | TrafficTreatment treatment = DefaultTrafficTreatment.builder().build(); |
alshabib | 19678cc | 2014-11-12 11:06:08 -0800 | [diff] [blame^] | 117 | List<Constraint> constraint = Lists.newArrayList(); |
alshabib | fd23d31 | 2014-11-11 18:14:47 -0800 | [diff] [blame] | 118 | List<Host> hosts = Lists.newArrayList(hostService.getHosts()); |
| 119 | while (!hosts.isEmpty()) { |
| 120 | Host src = hosts.remove(0); |
| 121 | for (Host dst : hosts) { |
| 122 | HostToHostIntent intent = new HostToHostIntent(appId, src.id(), dst.id(), |
| 123 | selector, treatment, |
alshabib | 19678cc | 2014-11-12 11:06:08 -0800 | [diff] [blame^] | 124 | constraint); |
alshabib | fd23d31 | 2014-11-11 18:14:47 -0800 | [diff] [blame] | 125 | existingIntents.add(intent); |
| 126 | intentService.submit(intent); |
| 127 | } |
| 128 | } |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | |
| 133 | private class UnInstaller implements Runnable { |
| 134 | @Override |
| 135 | public void run() { |
| 136 | for (Intent i : existingIntents) { |
| 137 | intentService.withdraw(i); |
| 138 | } |
| 139 | } |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | |