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