blob: 5c40c9013fe3213e6f15021a8c4f7c4737114ef5 [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
Jian Lic9b4bf12017-06-26 23:50:32 +090018import com.google.common.annotations.Beta;
19import com.google.common.util.concurrent.ListenableFuture;
20import io.grpc.Channel;
HIGUCHI Yutae3e90632016-05-11 16:44:01 -070021import org.onosproject.grpc.net.link.LinkProviderServiceRpcGrpc;
22import org.onosproject.grpc.net.link.LinkProviderServiceRpcGrpc.LinkProviderServiceRpcFutureStub;
23import org.onosproject.grpc.net.link.LinkService.LinkDetectedMsg;
24import org.onosproject.grpc.net.link.LinkService.LinkVanishedMsg;
25import org.onosproject.grpc.net.link.LinkService.Void;
Jian Lic9b4bf12017-06-26 23:50:32 +090026import org.onosproject.grpc.net.link.models.LinkEnumsProto.LinkTypeProto;
27import org.onosproject.grpc.net.models.ConnectPointProtoOuterClass.ConnectPointProto;
HIGUCHI Yuta7c1583c2015-12-03 23:08:54 -080028import org.onosproject.net.ConnectPoint;
29import org.onosproject.net.DeviceId;
30import org.onosproject.net.Link.Type;
31import org.onosproject.net.link.LinkDescription;
32import org.onosproject.net.link.LinkProvider;
33import org.onosproject.net.link.LinkProviderService;
34import org.onosproject.net.provider.AbstractProviderService;
35import org.onosproject.net.provider.ProviderId;
36import org.slf4j.Logger;
37import org.slf4j.LoggerFactory;
38
Jian Lic9b4bf12017-06-26 23:50:32 +090039import java.util.concurrent.ExecutionException;
40import java.util.concurrent.TimeUnit;
41import java.util.concurrent.TimeoutException;
HIGUCHI Yuta7c1583c2015-12-03 23:08:54 -080042
Jian Lic9b4bf12017-06-26 23:50:32 +090043import static org.onosproject.incubator.protobuf.models.ProtobufUtils.asMap;
HIGUCHI Yuta7c1583c2015-12-03 23:08:54 -080044
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
51class 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 SHIMIZU6e718042016-10-06 19:04:14 -070084 } catch (ExecutionException | TimeoutException e) {
HIGUCHI Yuta7c1583c2015-12-03 23:08:54 -080085 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 SHIMIZU6e718042016-10-06 19:04:14 -0700104 } catch (ExecutionException | TimeoutException e) {
HIGUCHI Yuta7c1583c2015-12-03 23:08:54 -0800105 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 SHIMIZU6e718042016-10-06 19:04:14 -0700124 } catch (ExecutionException | TimeoutException e) {
HIGUCHI Yuta7c1583c2015-12-03 23:08:54 -0800125 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 SHIMIZU6e718042016-10-06 19:04:14 -0700144 } catch (ExecutionException | TimeoutException e) {
HIGUCHI Yuta7c1583c2015-12-03 23:08:54 -0800145 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 Yutae3e90632016-05-11 16:44:01 -0700227 * @param type {@link org.onosproject.net.Link.Type Link.Type}
Jian Lic9b4bf12017-06-26 23:50:32 +0900228 * @return gRPC LinkTypeProto
HIGUCHI Yuta7c1583c2015-12-03 23:08:54 -0800229 */
Jian Lic9b4bf12017-06-26 23:50:32 +0900230 private LinkTypeProto translate(Type type) {
HIGUCHI Yuta7c1583c2015-12-03 23:08:54 -0800231 switch (type) {
232 case DIRECT:
Jian Lic9b4bf12017-06-26 23:50:32 +0900233 return LinkTypeProto.DIRECT;
HIGUCHI Yuta7c1583c2015-12-03 23:08:54 -0800234 case EDGE:
Jian Lic9b4bf12017-06-26 23:50:32 +0900235 return LinkTypeProto.EDGE;
HIGUCHI Yuta7c1583c2015-12-03 23:08:54 -0800236 case INDIRECT:
Jian Lic9b4bf12017-06-26 23:50:32 +0900237 return LinkTypeProto.INDIRECT;
HIGUCHI Yuta7c1583c2015-12-03 23:08:54 -0800238 case OPTICAL:
Jian Lic9b4bf12017-06-26 23:50:32 +0900239 return LinkTypeProto.OPTICAL;
HIGUCHI Yuta7c1583c2015-12-03 23:08:54 -0800240 case TUNNEL:
Jian Lic9b4bf12017-06-26 23:50:32 +0900241 return LinkTypeProto.TUNNEL;
HIGUCHI Yuta7c1583c2015-12-03 23:08:54 -0800242 case VIRTUAL:
Jian Lic9b4bf12017-06-26 23:50:32 +0900243 return LinkTypeProto.VIRTUAL;
HIGUCHI Yuta7c1583c2015-12-03 23:08:54 -0800244
245 default:
Jian Lic9b4bf12017-06-26 23:50:32 +0900246 return LinkTypeProto.DIRECT;
HIGUCHI Yuta7c1583c2015-12-03 23:08:54 -0800247
248 }
249 }
250
251 /**
252 * Translates ONOS object to gRPC message.
253 *
254 * @param cp {@link ConnectPoint}
Jian Lic9b4bf12017-06-26 23:50:32 +0900255 * @return gRPC ConnectPointProto
HIGUCHI Yuta7c1583c2015-12-03 23:08:54 -0800256 */
Jian Lic9b4bf12017-06-26 23:50:32 +0900257 private ConnectPointProto translate(ConnectPoint cp) {
258 return ConnectPointProto.newBuilder()
HIGUCHI Yuta7c1583c2015-12-03 23:08:54 -0800259 .setDeviceId(cp.deviceId().toString())
260 .setPortNumber(cp.port().toString())
261 .build();
262 }
263
264}