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