blob: 78f9f37d5d2da4928be643d7b4865d8d4e595429 [file] [log] [blame]
HIGUCHI Yuta7c1583c2015-12-03 23:08:54 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
HIGUCHI Yuta7c1583c2015-12-03 23:08:54 -08003 *
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.onosproject.incubator.rpc.grpc;
17
Aaron Kruglikov9f95f992017-06-23 14:15:25 +090018import static org.onosproject.incubator.protobuf.models.ProtobufUtils.asMap;
HIGUCHI Yuta7c1583c2015-12-03 23:08:54 -080019
20import java.util.concurrent.ExecutionException;
21import java.util.concurrent.TimeUnit;
22import java.util.concurrent.TimeoutException;
23
HIGUCHI Yutae3e90632016-05-11 16:44:01 -070024import org.onosproject.grpc.net.Link.LinkType;
25import org.onosproject.grpc.net.link.LinkProviderServiceRpcGrpc;
26import org.onosproject.grpc.net.link.LinkProviderServiceRpcGrpc.LinkProviderServiceRpcFutureStub;
27import org.onosproject.grpc.net.link.LinkService.LinkDetectedMsg;
28import org.onosproject.grpc.net.link.LinkService.LinkVanishedMsg;
29import org.onosproject.grpc.net.link.LinkService.Void;
HIGUCHI Yuta7c1583c2015-12-03 23:08:54 -080030import org.onosproject.net.ConnectPoint;
31import org.onosproject.net.DeviceId;
32import org.onosproject.net.Link.Type;
33import org.onosproject.net.link.LinkDescription;
34import org.onosproject.net.link.LinkProvider;
35import org.onosproject.net.link.LinkProviderService;
36import org.onosproject.net.provider.AbstractProviderService;
37import org.onosproject.net.provider.ProviderId;
38import org.slf4j.Logger;
39import org.slf4j.LoggerFactory;
40
Yuta HIGUCHI9efba1e2016-07-09 11:07:13 -070041import com.google.common.annotations.Beta;
HIGUCHI Yuta7c1583c2015-12-03 23:08:54 -080042import com.google.common.util.concurrent.ListenableFuture;
43
44import 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
52class 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();
Sho SHIMIZU6e718042016-10-06 19:04:14 -070085 } catch (ExecutionException | TimeoutException e) {
HIGUCHI Yuta7c1583c2015-12-03 23:08:54 -080086 log.error("linkDetected({}) failed", linkDescription, e);
87 invalidate();
88 }
89 }
90
91 @Override
92 public void linkVanished(LinkDescription linkDescription) {
93 checkValidity();
94
95 LinkProviderServiceRpcFutureStub newStub = LinkProviderServiceRpcGrpc.newFutureStub(channel);
96 ListenableFuture<Void> future = newStub.linkVanished(vanishMsg(provider().id(), linkDescription));
97
98 try {
99 // There's no need to wait, but just checking server
100 future.get(500, TimeUnit.MILLISECONDS);
101 } catch (InterruptedException e) {
102 log.error("linkVanished({}) failed", linkDescription, e);
103 invalidate();
104 Thread.currentThread().interrupt();
Sho SHIMIZU6e718042016-10-06 19:04:14 -0700105 } catch (ExecutionException | TimeoutException e) {
HIGUCHI Yuta7c1583c2015-12-03 23:08:54 -0800106 log.error("linkVanished({}) failed", linkDescription, e);
107 invalidate();
108 }
109 }
110
111 @Override
112 public void linksVanished(ConnectPoint connectPoint) {
113 checkValidity();
114
115 LinkProviderServiceRpcFutureStub newStub = LinkProviderServiceRpcGrpc.newFutureStub(channel);
116 ListenableFuture<Void> future = newStub.linkVanished(vanishMsg(provider().id(), connectPoint));
117
118 try {
119 // There's no need to wait, but just checking server
120 future.get(500, TimeUnit.MILLISECONDS);
121 } catch (InterruptedException e) {
122 log.error("linksVanished({}) failed", connectPoint, e);
123 invalidate();
124 Thread.currentThread().interrupt();
Sho SHIMIZU6e718042016-10-06 19:04:14 -0700125 } catch (ExecutionException | TimeoutException e) {
HIGUCHI Yuta7c1583c2015-12-03 23:08:54 -0800126 log.error("linksVanished({}) failed", connectPoint, e);
127 invalidate();
128 }
129 }
130
131 @Override
132 public void linksVanished(DeviceId deviceId) {
133 checkValidity();
134
135 LinkProviderServiceRpcFutureStub newStub = LinkProviderServiceRpcGrpc.newFutureStub(channel);
136 ListenableFuture<Void> future = newStub.linkVanished(vanishMsg(provider().id(), deviceId));
137
138 try {
139 // There's no need to wait, but just checking server
140 future.get(500, TimeUnit.MILLISECONDS);
141 } catch (InterruptedException e) {
142 log.error("linksVanished({}) failed", deviceId, e);
143 invalidate();
144 Thread.currentThread().interrupt();
Sho SHIMIZU6e718042016-10-06 19:04:14 -0700145 } catch (ExecutionException | TimeoutException e) {
HIGUCHI Yuta7c1583c2015-12-03 23:08:54 -0800146 log.error("linksVanished({}) failed", deviceId, e);
147 invalidate();
148 }
149 }
150
151 /**
152 * Builds {@link LinkDetectedMsg}.
153 *
154 * @param id ProviderId
155 * @param linkDescription {@link LinkDescription}
156 * @return {@link LinkDetectedMsg}
157 */
158 private LinkDetectedMsg detectMsg(ProviderId id,
159 LinkDescription linkDescription) {
160 LinkDetectedMsg.Builder builder = LinkDetectedMsg.newBuilder();
161 builder.setProviderId(id.scheme())
162 .setLinkDescription(builder.getLinkDescriptionBuilder()
163 .setSrc(translate(linkDescription.src()))
164 .setDst(translate(linkDescription.dst()))
165 .setType(translate(linkDescription.type()))
166 .putAllAnnotations(asMap(linkDescription.annotations()))
167 .build()
168 );
169 return builder.build();
170 }
171
172 /**
173 * Builds {@link LinkVanishedMsg}.
174 *
175 * @param id ProviderId
176 * @param linkDescription {@link LinkDescription}
177 * @return {@link LinkVanishedMsg}
178 */
179 private LinkVanishedMsg vanishMsg(ProviderId id,
180 LinkDescription linkDescription) {
181
182 LinkVanishedMsg.Builder builder = LinkVanishedMsg.newBuilder();
183 builder.setProviderId(id.scheme())
184 .setLinkDescription(builder.getLinkDescriptionBuilder()
185 .setSrc(translate(linkDescription.src()))
186 .setDst(translate(linkDescription.dst()))
187 .setType(translate(linkDescription.type()))
188 .putAllAnnotations(asMap(linkDescription.annotations()))
189 .build()
190 );
191 return builder.build();
192 }
193
194 /**
195 * Builds {@link LinkVanishedMsg}.
196 *
197 * @param id ProviderId
198 * @param connectPoint {@link ConnectPoint}
199 * @return {@link LinkVanishedMsg}
200 */
201 private LinkVanishedMsg vanishMsg(ProviderId id,
202 ConnectPoint connectPoint) {
203
204 LinkVanishedMsg.Builder builder = LinkVanishedMsg.newBuilder();
205 builder.setProviderId(id.scheme())
206 .setConnectPoint(translate(connectPoint));
207 return builder.build();
208 }
209
210 /**
211 * Builds {@link LinkVanishedMsg}.
212 *
213 * @param id ProviderId
214 * @param deviceId {@link DeviceId}
215 * @return {@link LinkVanishedMsg}
216 */
217 private LinkVanishedMsg vanishMsg(ProviderId id, DeviceId deviceId) {
218
219 LinkVanishedMsg.Builder builder = LinkVanishedMsg.newBuilder();
220 builder.setProviderId(id.scheme())
221 .setDeviceId(deviceId.toString());
222 return builder.build();
223 }
224
225 /**
226 * Translates ONOS object to gRPC message.
227 *
HIGUCHI Yutae3e90632016-05-11 16:44:01 -0700228 * @param type {@link org.onosproject.net.Link.Type Link.Type}
HIGUCHI Yuta7c1583c2015-12-03 23:08:54 -0800229 * @return gRPC LinkType
230 */
231 private LinkType translate(Type type) {
232 switch (type) {
233 case DIRECT:
234 return LinkType.DIRECT;
235 case EDGE:
236 return LinkType.EDGE;
237 case INDIRECT:
238 return LinkType.INDIRECT;
239 case OPTICAL:
240 return LinkType.OPTICAL;
241 case TUNNEL:
242 return LinkType.TUNNEL;
243 case VIRTUAL:
244 return LinkType.VIRTUAL;
245
246 default:
247 return LinkType.DIRECT;
248
249 }
250 }
251
252 /**
253 * Translates ONOS object to gRPC message.
254 *
255 * @param cp {@link ConnectPoint}
256 * @return gRPC ConnectPoint
257 */
HIGUCHI Yutae3e90632016-05-11 16:44:01 -0700258 private org.onosproject.grpc.net.Link.ConnectPoint translate(ConnectPoint cp) {
259 return org.onosproject.grpc.net.Link.ConnectPoint.newBuilder()
HIGUCHI Yuta7c1583c2015-12-03 23:08:54 -0800260 .setDeviceId(cp.deviceId().toString())
261 .setPortNumber(cp.port().toString())
262 .build();
263 }
264
265}