Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 1 | /* |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 2 | * Copyright 2014 Open Networking Laboratory |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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 |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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. |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 15 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.calendar; |
Thomas Vachuska | 140d585 | 2014-10-16 12:17:45 -0700 | [diff] [blame] | 17 | |
Ray Milkey | caa450b | 2014-10-29 15:54:24 -0700 | [diff] [blame] | 18 | import java.net.URI; |
Thomas Vachuska | 56d1a70 | 2014-11-11 19:14:57 -0800 | [diff] [blame] | 19 | import java.util.concurrent.CountDownLatch; |
| 20 | import java.util.concurrent.TimeUnit; |
| 21 | |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 22 | import org.onosproject.net.ConnectPoint; |
| 23 | import org.onosproject.net.DeviceId; |
| 24 | import org.onosproject.net.intent.Intent; |
| 25 | import org.onosproject.net.intent.IntentEvent; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 26 | import org.onosproject.net.intent.IntentListener; |
| 27 | import org.onosproject.net.intent.IntentService; |
| 28 | import org.onosproject.net.intent.IntentState; |
Hongtao Yin | 621c57a | 2014-10-30 14:28:03 -0700 | [diff] [blame] | 29 | import org.onlab.rest.BaseResource; |
Thomas Vachuska | 56d1a70 | 2014-11-11 19:14:57 -0800 | [diff] [blame] | 30 | |
Hongtao Yin | 621c57a | 2014-10-30 14:28:03 -0700 | [diff] [blame] | 31 | import javax.ws.rs.POST; |
| 32 | import javax.ws.rs.DELETE; |
| 33 | import javax.ws.rs.PathParam; |
| 34 | import javax.ws.rs.core.Response; |
Thomas Vachuska | 56d1a70 | 2014-11-11 19:14:57 -0800 | [diff] [blame] | 35 | |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 36 | import org.onosproject.core.ApplicationId; |
| 37 | import org.onosproject.core.CoreService; |
| 38 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 39 | import org.onosproject.net.flow.TrafficSelector; |
| 40 | import org.onosproject.net.flow.TrafficTreatment; |
| 41 | import org.onosproject.net.intent.PointToPointIntent; |
Ray Milkey | caa450b | 2014-10-29 15:54:24 -0700 | [diff] [blame] | 42 | import org.onlab.packet.Ethernet; |
Thomas Vachuska | 56d1a70 | 2014-11-11 19:14:57 -0800 | [diff] [blame] | 43 | |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 44 | import static org.onosproject.net.PortNumber.portNumber; |
| 45 | import static org.onosproject.net.flow.DefaultTrafficTreatment.builder; |
Thomas Vachuska | 140d585 | 2014-10-16 12:17:45 -0700 | [diff] [blame] | 46 | |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 47 | import static org.onosproject.net.intent.IntentState.FAILED; |
| 48 | import static org.onosproject.net.intent.IntentState.INSTALLED; |
| 49 | import static org.onosproject.net.intent.IntentState.WITHDRAWN; |
Hongtao Yin | 621c57a | 2014-10-30 14:28:03 -0700 | [diff] [blame] | 50 | import static org.slf4j.LoggerFactory.getLogger; |
Thomas Vachuska | 56d1a70 | 2014-11-11 19:14:57 -0800 | [diff] [blame] | 51 | |
Hongtao Yin | 621c57a | 2014-10-30 14:28:03 -0700 | [diff] [blame] | 52 | import org.slf4j.Logger; |
| 53 | |
Thomas Vachuska | 140d585 | 2014-10-16 12:17:45 -0700 | [diff] [blame] | 54 | /** |
| 55 | * Web resource for triggering calendared intents. |
| 56 | */ |
Hongtao Yin | 621c57a | 2014-10-30 14:28:03 -0700 | [diff] [blame] | 57 | @javax.ws.rs.Path("intent") |
Thomas Vachuska | 140d585 | 2014-10-16 12:17:45 -0700 | [diff] [blame] | 58 | public class BandwidthCalendarResource extends BaseResource { |
| 59 | |
Hongtao Yin | 621c57a | 2014-10-30 14:28:03 -0700 | [diff] [blame] | 60 | private static final Logger log = getLogger(BandwidthCalendarResource.class); |
Thomas Vachuska | 56d1a70 | 2014-11-11 19:14:57 -0800 | [diff] [blame] | 61 | private static final long TIMEOUT = 5; // seconds |
Hongtao Yin | 621c57a | 2014-10-30 14:28:03 -0700 | [diff] [blame] | 62 | |
| 63 | @javax.ws.rs.Path("/{src}/{dst}/{srcPort}/{dstPort}/{bandwidth}") |
Thomas Vachuska | 140d585 | 2014-10-16 12:17:45 -0700 | [diff] [blame] | 64 | @POST |
Thomas Vachuska | 140d585 | 2014-10-16 12:17:45 -0700 | [diff] [blame] | 65 | public Response createIntent(@PathParam("src") String src, |
| 66 | @PathParam("dst") String dst, |
| 67 | @PathParam("srcPort") String srcPort, |
| 68 | @PathParam("dstPort") String dstPort, |
| 69 | @PathParam("bandwidth") String bandwidth) { |
Hongtao Yin | 621c57a | 2014-10-30 14:28:03 -0700 | [diff] [blame] | 70 | |
| 71 | log.info("Receiving Create Intent request..."); |
| 72 | log.info("Path Constraints: Src = {} SrcPort = {} Dest = {} DestPort = {} BW = {}", |
Thomas Vachuska | 56d1a70 | 2014-11-11 19:14:57 -0800 | [diff] [blame] | 73 | src, srcPort, dst, dstPort, bandwidth); |
Hongtao Yin | 621c57a | 2014-10-30 14:28:03 -0700 | [diff] [blame] | 74 | |
Thomas Vachuska | 140d585 | 2014-10-16 12:17:45 -0700 | [diff] [blame] | 75 | IntentService service = get(IntentService.class); |
| 76 | |
| 77 | ConnectPoint srcPoint = new ConnectPoint(deviceId(src), portNumber(srcPort)); |
| 78 | ConnectPoint dstPoint = new ConnectPoint(deviceId(dst), portNumber(dstPort)); |
| 79 | |
Ray Milkey | caa450b | 2014-10-29 15:54:24 -0700 | [diff] [blame] | 80 | TrafficSelector selector = buildTrafficSelector(); |
| 81 | TrafficTreatment treatment = builder().build(); |
| 82 | |
Hongtao Yin | 621c57a | 2014-10-30 14:28:03 -0700 | [diff] [blame] | 83 | PointToPointIntent intentP2P = |
Thomas Vachuska | 56d1a70 | 2014-11-11 19:14:57 -0800 | [diff] [blame] | 84 | new PointToPointIntent(appId(), selector, treatment, |
| 85 | srcPoint, dstPoint); |
Ray Milkey | caa450b | 2014-10-29 15:54:24 -0700 | [diff] [blame] | 86 | |
Thomas Vachuska | 56d1a70 | 2014-11-11 19:14:57 -0800 | [diff] [blame] | 87 | CountDownLatch latch = new CountDownLatch(1); |
| 88 | InternalIntentListener listener = new InternalIntentListener(intentP2P, service, latch); |
| 89 | service.addListener(listener); |
| 90 | service.submit(intentP2P); |
| 91 | try { |
| 92 | if (latch.await(TIMEOUT, TimeUnit.SECONDS)) { |
| 93 | log.info("Submitted Calendar App intent: src = {}; dst = {}; " + |
| 94 | "srcPort = {}; dstPort = {}; intentID = {}", |
| 95 | src, dst, srcPort, dstPort, intentP2P.id()); |
| 96 | String reply = intentP2P.id() + " " + listener.getState() + "\n"; |
| 97 | return Response.ok(reply).build(); |
| 98 | } |
| 99 | } catch (InterruptedException e) { |
| 100 | log.warn("Interrupted while waiting for intent {} status", intentP2P.id()); |
| 101 | } |
| 102 | return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build(); |
Hongtao Yin | 621c57a | 2014-10-30 14:28:03 -0700 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | @javax.ws.rs.Path("/cancellation/{intentId}") |
| 106 | @DELETE |
| 107 | public Response withdrawIntent(@PathParam("intentId") String intentId) { |
Thomas Vachuska | 56d1a70 | 2014-11-11 19:14:57 -0800 | [diff] [blame] | 108 | log.info("Receiving Teardown request for {}", intentId); |
| 109 | IntentService service = get(IntentService.class); |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame^] | 110 | // TODO: there needs to be an app id and key here |
| 111 | /* |
Thomas Vachuska | 56d1a70 | 2014-11-11 19:14:57 -0800 | [diff] [blame] | 112 | Intent intent = service.getIntent(IntentId.valueOf(Long.parseLong(intentId))); |
| 113 | if (intent != null) { |
| 114 | service.withdraw(intent); |
| 115 | String reply = "ok\n"; |
| 116 | return Response.ok(reply).build(); |
| 117 | } |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame^] | 118 | */ |
Thomas Vachuska | 56d1a70 | 2014-11-11 19:14:57 -0800 | [diff] [blame] | 119 | return Response.status(Response.Status.NOT_FOUND).build(); |
Hongtao Yin | 621c57a | 2014-10-30 14:28:03 -0700 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | @javax.ws.rs.Path("/modification/{intentId}/{bandwidth}") |
| 123 | @POST |
| 124 | public Response modifyBandwidth(@PathParam("intentId") String intentId, |
Thomas Vachuska | 56d1a70 | 2014-11-11 19:14:57 -0800 | [diff] [blame] | 125 | @PathParam("bandwidth") String bandwidth) { |
Hongtao Yin | 621c57a | 2014-10-30 14:28:03 -0700 | [diff] [blame] | 126 | |
| 127 | log.info("Receiving Modify request..."); |
| 128 | log.info("Modify bw for intentId = {} with new bandwidth = {}", intentId, bandwidth); |
| 129 | |
Thomas Vachuska | 56d1a70 | 2014-11-11 19:14:57 -0800 | [diff] [blame] | 130 | String reply = "ok\n"; |
Hongtao Yin | 621c57a | 2014-10-30 14:28:03 -0700 | [diff] [blame] | 131 | return Response.ok(reply).build(); |
Thomas Vachuska | 140d585 | 2014-10-16 12:17:45 -0700 | [diff] [blame] | 132 | } |
| 133 | |
Ray Milkey | caa450b | 2014-10-29 15:54:24 -0700 | [diff] [blame] | 134 | private TrafficSelector buildTrafficSelector() { |
| 135 | TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder(); |
| 136 | Short ethType = Ethernet.TYPE_IPV4; |
| 137 | |
| 138 | selectorBuilder.matchEthType(ethType); |
| 139 | |
| 140 | return selectorBuilder.build(); |
| 141 | } |
| 142 | |
Thomas Vachuska | 140d585 | 2014-10-16 12:17:45 -0700 | [diff] [blame] | 143 | private DeviceId deviceId(String dpid) { |
| 144 | return DeviceId.deviceId(URI.create("of:" + dpid)); |
| 145 | } |
| 146 | |
Ray Milkey | caa450b | 2014-10-29 15:54:24 -0700 | [diff] [blame] | 147 | protected ApplicationId appId() { |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 148 | return get(CoreService.class).registerApplication("org.onosproject.calendar"); |
Ray Milkey | caa450b | 2014-10-29 15:54:24 -0700 | [diff] [blame] | 149 | } |
Thomas Vachuska | 56d1a70 | 2014-11-11 19:14:57 -0800 | [diff] [blame] | 150 | |
| 151 | // Auxiliary listener to wait until the given intent reaches the installed or failed states. |
| 152 | private final class InternalIntentListener implements IntentListener { |
| 153 | private final Intent intent; |
| 154 | private final IntentService service; |
| 155 | private final CountDownLatch latch; |
| 156 | private IntentState state; |
| 157 | |
| 158 | private InternalIntentListener(Intent intent, IntentService service, |
| 159 | CountDownLatch latch) { |
| 160 | this.intent = intent; |
| 161 | this.service = service; |
| 162 | this.latch = latch; |
| 163 | } |
| 164 | |
| 165 | @Override |
| 166 | public void event(IntentEvent event) { |
| 167 | if (event.subject().equals(intent)) { |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame^] | 168 | state = service.getIntentState(intent.key()); |
Thomas Vachuska | 56d1a70 | 2014-11-11 19:14:57 -0800 | [diff] [blame] | 169 | if (state == INSTALLED || state == FAILED || state == WITHDRAWN) { |
| 170 | latch.countDown(); |
| 171 | } |
| 172 | service.removeListener(this); |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | public IntentState getState() { |
| 177 | return state; |
| 178 | } |
| 179 | } |
Thomas Vachuska | 140d585 | 2014-10-16 12:17:45 -0700 | [diff] [blame] | 180 | } |