blob: 758f6289110a39939e16bee6c7e5a0764c164e8f [file] [log] [blame]
alshabibfd23d312014-11-11 18:14:47 -08001/*
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 */
16package org.onlab.onos.demo;
17
18import com.google.common.collect.Lists;
19import com.google.common.util.concurrent.ThreadFactoryBuilder;
20import org.apache.felix.scr.annotations.Activate;
21import org.apache.felix.scr.annotations.Component;
22import org.apache.felix.scr.annotations.Deactivate;
23import org.apache.felix.scr.annotations.Reference;
24import org.apache.felix.scr.annotations.ReferenceCardinality;
25import org.apache.felix.scr.annotations.Service;
26import org.onlab.onos.core.ApplicationId;
27import org.onlab.onos.core.CoreService;
28import org.onlab.onos.net.Host;
29import org.onlab.onos.net.flow.DefaultTrafficSelector;
30import org.onlab.onos.net.flow.DefaultTrafficTreatment;
31import org.onlab.onos.net.flow.TrafficSelector;
32import org.onlab.onos.net.flow.TrafficTreatment;
33import org.onlab.onos.net.host.HostService;
alshabib19678cc2014-11-12 11:06:08 -080034import org.onlab.onos.net.intent.Constraint;
alshabibfd23d312014-11-11 18:14:47 -080035import org.onlab.onos.net.intent.HostToHostIntent;
36import org.onlab.onos.net.intent.Intent;
37import org.onlab.onos.net.intent.IntentService;
38import org.slf4j.Logger;
39
40
41import java.util.HashSet;
42import java.util.List;
43import java.util.Set;
44import java.util.concurrent.ExecutorService;
45import java.util.concurrent.Executors;
46
47import static org.slf4j.LoggerFactory.getLogger;
48
49/**
50 * Application to set up demos.
51 */
52@Component(immediate = true)
53@Service
54public 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();
alshabib19678cc2014-11-12 11:06:08 -0800117 List<Constraint> constraint = Lists.newArrayList();
alshabibfd23d312014-11-11 18:14:47 -0800118 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,
alshabib19678cc2014-11-12 11:06:08 -0800124 constraint);
alshabibfd23d312014-11-11 18:14:47 -0800125 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