HIGUCHI Yuta | 7c1583c | 2015-12-03 23:08:54 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 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.onosproject.incubator.rpc.grpc; |
| 17 | |
| 18 | import static org.onosproject.incubator.rpc.grpc.GrpcDeviceUtils.asMap; |
| 19 | |
| 20 | import java.util.concurrent.ExecutionException; |
| 21 | import java.util.concurrent.TimeUnit; |
| 22 | import java.util.concurrent.TimeoutException; |
| 23 | |
| 24 | import org.onosproject.grpc.Link.LinkDetectedMsg; |
| 25 | import org.onosproject.grpc.Link.LinkType; |
| 26 | import org.onosproject.grpc.Link.LinkVanishedMsg; |
| 27 | import org.onosproject.grpc.Link.Void; |
| 28 | import org.onosproject.grpc.LinkProviderServiceRpcGrpc; |
| 29 | import org.onosproject.grpc.LinkProviderServiceRpcGrpc.LinkProviderServiceRpcFutureStub; |
| 30 | import org.onosproject.net.ConnectPoint; |
| 31 | import org.onosproject.net.DeviceId; |
| 32 | import org.onosproject.net.Link.Type; |
| 33 | import org.onosproject.net.link.LinkDescription; |
| 34 | import org.onosproject.net.link.LinkProvider; |
| 35 | import org.onosproject.net.link.LinkProviderService; |
| 36 | import org.onosproject.net.provider.AbstractProviderService; |
| 37 | import org.onosproject.net.provider.ProviderId; |
| 38 | import org.slf4j.Logger; |
| 39 | import org.slf4j.LoggerFactory; |
| 40 | |
| 41 | import com.google.api.client.repackaged.com.google.common.annotations.Beta; |
| 42 | import com.google.common.util.concurrent.ListenableFuture; |
| 43 | |
| 44 | import io.grpc.Channel; |
| 45 | |
| 46 | /** |
| 47 | * Proxy object to handle LinkProviderService calls. |
| 48 | * |
| 49 | * RPC wise, this will initiate a RPC call on each method invocation. |
| 50 | */ |
| 51 | @Beta |
| 52 | class LinkProviderServiceClientProxy |
| 53 | extends AbstractProviderService<LinkProvider> |
| 54 | implements LinkProviderService { |
| 55 | |
| 56 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 57 | |
| 58 | private final Channel channel; |
| 59 | |
| 60 | /** |
| 61 | * Constructs {@link LinkProviderServiceClientProxy}. |
| 62 | * |
| 63 | * @param provider {@link LinkProvider}. Only ProviderId scheme is used. |
| 64 | * @param channel channel to use to call RPC |
| 65 | */ |
| 66 | protected LinkProviderServiceClientProxy(LinkProvider provider, Channel channel) { |
| 67 | super(provider); |
| 68 | this.channel = channel; |
| 69 | } |
| 70 | |
| 71 | @Override |
| 72 | public void linkDetected(LinkDescription linkDescription) { |
| 73 | checkValidity(); |
| 74 | |
| 75 | LinkProviderServiceRpcFutureStub newStub = LinkProviderServiceRpcGrpc.newFutureStub(channel); |
| 76 | ListenableFuture<Void> future = newStub.linkDetected(detectMsg(provider().id(), linkDescription)); |
| 77 | |
| 78 | try { |
| 79 | // There's no need to wait, but just checking server |
| 80 | future.get(500, TimeUnit.MILLISECONDS); |
| 81 | } catch (InterruptedException e) { |
| 82 | log.error("linkDetected({}) failed", linkDescription, e); |
| 83 | invalidate(); |
| 84 | Thread.currentThread().interrupt(); |
| 85 | } catch (ExecutionException e) { |
| 86 | log.error("linkDetected({}) failed", linkDescription, e); |
| 87 | invalidate(); |
| 88 | } catch (TimeoutException e) { |
| 89 | log.error("linkDetected({}) failed", linkDescription, e); |
| 90 | invalidate(); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | @Override |
| 95 | public void linkVanished(LinkDescription linkDescription) { |
| 96 | checkValidity(); |
| 97 | |
| 98 | LinkProviderServiceRpcFutureStub newStub = LinkProviderServiceRpcGrpc.newFutureStub(channel); |
| 99 | ListenableFuture<Void> future = newStub.linkVanished(vanishMsg(provider().id(), linkDescription)); |
| 100 | |
| 101 | try { |
| 102 | // There's no need to wait, but just checking server |
| 103 | future.get(500, TimeUnit.MILLISECONDS); |
| 104 | } catch (InterruptedException e) { |
| 105 | log.error("linkVanished({}) failed", linkDescription, e); |
| 106 | invalidate(); |
| 107 | Thread.currentThread().interrupt(); |
| 108 | } catch (ExecutionException e) { |
| 109 | log.error("linkVanished({}) failed", linkDescription, e); |
| 110 | invalidate(); |
| 111 | } catch (TimeoutException e) { |
| 112 | log.error("linkVanished({}) failed", linkDescription, e); |
| 113 | invalidate(); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | @Override |
| 118 | public void linksVanished(ConnectPoint connectPoint) { |
| 119 | checkValidity(); |
| 120 | |
| 121 | LinkProviderServiceRpcFutureStub newStub = LinkProviderServiceRpcGrpc.newFutureStub(channel); |
| 122 | ListenableFuture<Void> future = newStub.linkVanished(vanishMsg(provider().id(), connectPoint)); |
| 123 | |
| 124 | try { |
| 125 | // There's no need to wait, but just checking server |
| 126 | future.get(500, TimeUnit.MILLISECONDS); |
| 127 | } catch (InterruptedException e) { |
| 128 | log.error("linksVanished({}) failed", connectPoint, e); |
| 129 | invalidate(); |
| 130 | Thread.currentThread().interrupt(); |
| 131 | } catch (ExecutionException e) { |
| 132 | log.error("linksVanished({}) failed", connectPoint, e); |
| 133 | invalidate(); |
| 134 | } catch (TimeoutException e) { |
| 135 | log.error("linksVanished({}) failed", connectPoint, e); |
| 136 | invalidate(); |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | @Override |
| 141 | public void linksVanished(DeviceId deviceId) { |
| 142 | checkValidity(); |
| 143 | |
| 144 | LinkProviderServiceRpcFutureStub newStub = LinkProviderServiceRpcGrpc.newFutureStub(channel); |
| 145 | ListenableFuture<Void> future = newStub.linkVanished(vanishMsg(provider().id(), deviceId)); |
| 146 | |
| 147 | try { |
| 148 | // There's no need to wait, but just checking server |
| 149 | future.get(500, TimeUnit.MILLISECONDS); |
| 150 | } catch (InterruptedException e) { |
| 151 | log.error("linksVanished({}) failed", deviceId, e); |
| 152 | invalidate(); |
| 153 | Thread.currentThread().interrupt(); |
| 154 | } catch (ExecutionException e) { |
| 155 | log.error("linksVanished({}) failed", deviceId, e); |
| 156 | invalidate(); |
| 157 | } catch (TimeoutException e) { |
| 158 | log.error("linksVanished({}) failed", deviceId, e); |
| 159 | invalidate(); |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * Builds {@link LinkDetectedMsg}. |
| 165 | * |
| 166 | * @param id ProviderId |
| 167 | * @param linkDescription {@link LinkDescription} |
| 168 | * @return {@link LinkDetectedMsg} |
| 169 | */ |
| 170 | private LinkDetectedMsg detectMsg(ProviderId id, |
| 171 | LinkDescription linkDescription) { |
| 172 | LinkDetectedMsg.Builder builder = LinkDetectedMsg.newBuilder(); |
| 173 | builder.setProviderId(id.scheme()) |
| 174 | .setLinkDescription(builder.getLinkDescriptionBuilder() |
| 175 | .setSrc(translate(linkDescription.src())) |
| 176 | .setDst(translate(linkDescription.dst())) |
| 177 | .setType(translate(linkDescription.type())) |
| 178 | .putAllAnnotations(asMap(linkDescription.annotations())) |
| 179 | .build() |
| 180 | ); |
| 181 | return builder.build(); |
| 182 | } |
| 183 | |
| 184 | /** |
| 185 | * Builds {@link LinkVanishedMsg}. |
| 186 | * |
| 187 | * @param id ProviderId |
| 188 | * @param linkDescription {@link LinkDescription} |
| 189 | * @return {@link LinkVanishedMsg} |
| 190 | */ |
| 191 | private LinkVanishedMsg vanishMsg(ProviderId id, |
| 192 | LinkDescription linkDescription) { |
| 193 | |
| 194 | LinkVanishedMsg.Builder builder = LinkVanishedMsg.newBuilder(); |
| 195 | builder.setProviderId(id.scheme()) |
| 196 | .setLinkDescription(builder.getLinkDescriptionBuilder() |
| 197 | .setSrc(translate(linkDescription.src())) |
| 198 | .setDst(translate(linkDescription.dst())) |
| 199 | .setType(translate(linkDescription.type())) |
| 200 | .putAllAnnotations(asMap(linkDescription.annotations())) |
| 201 | .build() |
| 202 | ); |
| 203 | return builder.build(); |
| 204 | } |
| 205 | |
| 206 | /** |
| 207 | * Builds {@link LinkVanishedMsg}. |
| 208 | * |
| 209 | * @param id ProviderId |
| 210 | * @param connectPoint {@link ConnectPoint} |
| 211 | * @return {@link LinkVanishedMsg} |
| 212 | */ |
| 213 | private LinkVanishedMsg vanishMsg(ProviderId id, |
| 214 | ConnectPoint connectPoint) { |
| 215 | |
| 216 | LinkVanishedMsg.Builder builder = LinkVanishedMsg.newBuilder(); |
| 217 | builder.setProviderId(id.scheme()) |
| 218 | .setConnectPoint(translate(connectPoint)); |
| 219 | return builder.build(); |
| 220 | } |
| 221 | |
| 222 | /** |
| 223 | * Builds {@link LinkVanishedMsg}. |
| 224 | * |
| 225 | * @param id ProviderId |
| 226 | * @param deviceId {@link DeviceId} |
| 227 | * @return {@link LinkVanishedMsg} |
| 228 | */ |
| 229 | private LinkVanishedMsg vanishMsg(ProviderId id, DeviceId deviceId) { |
| 230 | |
| 231 | LinkVanishedMsg.Builder builder = LinkVanishedMsg.newBuilder(); |
| 232 | builder.setProviderId(id.scheme()) |
| 233 | .setDeviceId(deviceId.toString()); |
| 234 | return builder.build(); |
| 235 | } |
| 236 | |
| 237 | /** |
| 238 | * Translates ONOS object to gRPC message. |
| 239 | * |
| 240 | * @param type {@link Link.Type} |
| 241 | * @return gRPC LinkType |
| 242 | */ |
| 243 | private LinkType translate(Type type) { |
| 244 | switch (type) { |
| 245 | case DIRECT: |
| 246 | return LinkType.DIRECT; |
| 247 | case EDGE: |
| 248 | return LinkType.EDGE; |
| 249 | case INDIRECT: |
| 250 | return LinkType.INDIRECT; |
| 251 | case OPTICAL: |
| 252 | return LinkType.OPTICAL; |
| 253 | case TUNNEL: |
| 254 | return LinkType.TUNNEL; |
| 255 | case VIRTUAL: |
| 256 | return LinkType.VIRTUAL; |
| 257 | |
| 258 | default: |
| 259 | return LinkType.DIRECT; |
| 260 | |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | /** |
| 265 | * Translates ONOS object to gRPC message. |
| 266 | * |
| 267 | * @param cp {@link ConnectPoint} |
| 268 | * @return gRPC ConnectPoint |
| 269 | */ |
| 270 | private org.onosproject.grpc.Link.ConnectPoint translate(ConnectPoint cp) { |
| 271 | return org.onosproject.grpc.Link.ConnectPoint.newBuilder() |
| 272 | .setDeviceId(cp.deviceId().toString()) |
| 273 | .setPortNumber(cp.port().toString()) |
| 274 | .build(); |
| 275 | } |
| 276 | |
| 277 | } |