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.cache.Cache; |
| 20 | import com.google.common.cache.CacheBuilder; |
| 21 | import com.google.common.cache.RemovalNotification; |
| 22 | import io.grpc.stub.StreamObserver; |
Yuta HIGUCHI | 4c7c90a | 2016-07-06 14:56:49 -0700 | [diff] [blame] | 23 | import org.apache.commons.lang3.tuple.Pair; |
Yuta HIGUCHI | 9efba1e | 2016-07-09 11:07:13 -0700 | [diff] [blame] | 24 | import org.onosproject.grpc.net.link.LinkProviderServiceRpcGrpc.LinkProviderServiceRpcImplBase; |
HIGUCHI Yuta | e3e9063 | 2016-05-11 16:44:01 -0700 | [diff] [blame] | 25 | import org.onosproject.grpc.net.link.LinkService.LinkDetectedMsg; |
| 26 | import org.onosproject.grpc.net.link.LinkService.LinkVanishedMsg; |
| 27 | import org.onosproject.grpc.net.link.LinkService.Void; |
Jian Li | c9b4bf1 | 2017-06-26 23:50:32 +0900 | [diff] [blame] | 28 | import org.onosproject.grpc.net.link.models.LinkDescriptionProtoOuterClass.LinkDescriptionProto; |
| 29 | import org.onosproject.grpc.net.link.models.LinkEnumsProto.LinkTypeProto; |
| 30 | import org.onosproject.grpc.net.models.ConnectPointProtoOuterClass.ConnectPointProto; |
| 31 | import org.onosproject.grpc.net.models.ConnectPointProtoOuterClass.ConnectPointProto.ElementIdCase; |
Jian Li | d40252c | 2017-12-08 04:13:20 +0900 | [diff] [blame] | 32 | import org.onosproject.incubator.protobuf.models.net.LinkProtoTranslator; |
HIGUCHI Yuta | 7c1583c | 2015-12-03 23:08:54 -0800 | [diff] [blame] | 33 | import org.onosproject.net.ConnectPoint; |
| 34 | import org.onosproject.net.DeviceId; |
Jian Li | c9b4bf1 | 2017-06-26 23:50:32 +0900 | [diff] [blame] | 35 | import org.onosproject.net.Link.Type; |
Yuta HIGUCHI | 4c7c90a | 2016-07-06 14:56:49 -0700 | [diff] [blame] | 36 | import org.onosproject.net.LinkKey; |
HIGUCHI Yuta | 7c1583c | 2015-12-03 23:08:54 -0800 | [diff] [blame] | 37 | import org.onosproject.net.PortNumber; |
| 38 | import org.onosproject.net.SparseAnnotations; |
| 39 | import org.onosproject.net.link.DefaultLinkDescription; |
| 40 | import org.onosproject.net.link.LinkDescription; |
| 41 | import org.onosproject.net.link.LinkProviderService; |
| 42 | import org.slf4j.Logger; |
| 43 | import org.slf4j.LoggerFactory; |
| 44 | |
Jian Li | c9b4bf1 | 2017-06-26 23:50:32 +0900 | [diff] [blame] | 45 | import java.util.concurrent.ScheduledExecutorService; |
| 46 | import java.util.concurrent.TimeUnit; |
HIGUCHI Yuta | 7c1583c | 2015-12-03 23:08:54 -0800 | [diff] [blame] | 47 | |
Jian Li | c9b4bf1 | 2017-06-26 23:50:32 +0900 | [diff] [blame] | 48 | import static com.google.common.base.Preconditions.checkArgument; |
| 49 | import static com.google.common.base.Preconditions.checkNotNull; |
| 50 | import static com.google.common.cache.RemovalListeners.asynchronous; |
| 51 | import static org.onosproject.net.DeviceId.deviceId; |
| 52 | import static org.onosproject.net.LinkKey.linkKey; |
HIGUCHI Yuta | 7c1583c | 2015-12-03 23:08:54 -0800 | [diff] [blame] | 53 | |
Yuta HIGUCHI | 4c7c90a | 2016-07-06 14:56:49 -0700 | [diff] [blame] | 54 | // Only single instance will be created and bound to gRPC LinkProviderService |
HIGUCHI Yuta | 7c1583c | 2015-12-03 23:08:54 -0800 | [diff] [blame] | 55 | /** |
| 56 | * Server-side implementation of gRPC version of LinkProviderService. |
| 57 | */ |
| 58 | @Beta |
| 59 | final class LinkProviderServiceServerProxy |
Yuta HIGUCHI | 9efba1e | 2016-07-09 11:07:13 -0700 | [diff] [blame] | 60 | extends LinkProviderServiceRpcImplBase { |
HIGUCHI Yuta | 7c1583c | 2015-12-03 23:08:54 -0800 | [diff] [blame] | 61 | |
Yuta HIGUCHI | 4c7c90a | 2016-07-06 14:56:49 -0700 | [diff] [blame] | 62 | /** |
| 63 | * Silence time in seconds, until link gets treated as vanished. |
| 64 | */ |
| 65 | private static final int EVICT_LIMIT = 3 * 3; |
| 66 | |
HIGUCHI Yuta | 7c1583c | 2015-12-03 23:08:54 -0800 | [diff] [blame] | 67 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 68 | |
| 69 | private final GrpcRemoteServiceServer server; |
| 70 | |
HIGUCHI Yuta | 6381a24 | 2016-03-13 23:29:10 -0700 | [diff] [blame] | 71 | // TODO implement aging mechanism to automatically remove |
| 72 | // stale links reported by dead client, etc. |
Yuta HIGUCHI | 4c7c90a | 2016-07-06 14:56:49 -0700 | [diff] [blame] | 73 | /** |
| 74 | * Evicting Cache to track last seen time. |
| 75 | */ |
| 76 | private final Cache<Pair<String, LinkKey>, LinkDescription> lastSeen; |
HIGUCHI Yuta | 6381a24 | 2016-03-13 23:29:10 -0700 | [diff] [blame] | 77 | |
HIGUCHI Yuta | 7c1583c | 2015-12-03 23:08:54 -0800 | [diff] [blame] | 78 | LinkProviderServiceServerProxy(GrpcRemoteServiceServer server) { |
| 79 | this.server = checkNotNull(server); |
Yuta HIGUCHI | 4c7c90a | 2016-07-06 14:56:49 -0700 | [diff] [blame] | 80 | ScheduledExecutorService executor = server.getSharedExecutor(); |
| 81 | lastSeen = CacheBuilder.newBuilder() |
| 82 | .expireAfterWrite(EVICT_LIMIT, TimeUnit.SECONDS) |
| 83 | .removalListener(asynchronous(this::onRemove, executor)) |
| 84 | .build(); |
| 85 | |
| 86 | executor.scheduleWithFixedDelay(lastSeen::cleanUp, |
| 87 | EVICT_LIMIT, EVICT_LIMIT, TimeUnit.SECONDS); |
| 88 | } |
| 89 | |
| 90 | private void onRemove(RemovalNotification<Pair<String, LinkKey>, LinkDescription> n) { |
| 91 | if (n.wasEvicted()) { |
| 92 | getLinkProviderServiceFor(n.getKey().getLeft()) |
| 93 | .linkVanished(n.getValue()); |
| 94 | } |
HIGUCHI Yuta | 7c1583c | 2015-12-03 23:08:54 -0800 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Gets or creates {@link LinkProviderService} registered for given ProviderId scheme. |
| 99 | * |
| 100 | * @param scheme ProviderId scheme. |
| 101 | * @return {@link LinkProviderService} |
| 102 | */ |
| 103 | private LinkProviderService getLinkProviderServiceFor(String scheme) { |
| 104 | return server.getLinkProviderServiceFor(scheme); |
| 105 | } |
| 106 | |
| 107 | @Override |
| 108 | public void linkDetected(LinkDetectedMsg request, |
| 109 | StreamObserver<Void> responseObserver) { |
| 110 | |
| 111 | try { |
| 112 | onLinkDetected(request, responseObserver); |
| 113 | // If onNext call was not mandatory, it can be removed. |
| 114 | responseObserver.onNext(Void.getDefaultInstance()); |
| 115 | responseObserver.onCompleted(); |
| 116 | } catch (Exception e) { |
| 117 | log.error("Exception caught", e); |
| 118 | responseObserver.onError(e); |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | private void onLinkDetected(LinkDetectedMsg request, |
| 123 | StreamObserver<Void> responseObserver) { |
| 124 | String scheme = request.getProviderId(); |
| 125 | |
| 126 | LinkProviderService linkProviderService = getLinkProviderServiceFor(scheme); |
| 127 | |
| 128 | LinkDescription linkDescription = translate(request.getLinkDescription()); |
| 129 | linkProviderService.linkDetected(linkDescription); |
Yuta HIGUCHI | 4c7c90a | 2016-07-06 14:56:49 -0700 | [diff] [blame] | 130 | lastSeen.put(Pair.of(scheme, linkKey(linkDescription)), linkDescription); |
HIGUCHI Yuta | 7c1583c | 2015-12-03 23:08:54 -0800 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | @Override |
| 134 | public void linkVanished(LinkVanishedMsg request, |
| 135 | StreamObserver<Void> responseObserver) { |
| 136 | try { |
| 137 | onLinksVanished(request, responseObserver); |
| 138 | // If onNext call was not mandatory, it can be removed. |
| 139 | responseObserver.onNext(Void.getDefaultInstance()); |
| 140 | responseObserver.onCompleted(); |
| 141 | } catch (Exception e) { |
| 142 | log.error("Exception caught", e); |
| 143 | responseObserver.onError(e); |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | private void onLinksVanished(LinkVanishedMsg request, |
| 148 | StreamObserver<Void> responseObserver) { |
| 149 | String scheme = request.getProviderId(); |
| 150 | switch (request.getSubjectCase()) { |
| 151 | case CONNECT_POINT: |
| 152 | ConnectPoint cp = translate(request.getConnectPoint()); |
| 153 | getLinkProviderServiceFor(scheme).linksVanished(cp); |
| 154 | break; |
| 155 | case DEVICE_ID: |
| 156 | DeviceId did = deviceId(request.getDeviceId()); |
| 157 | getLinkProviderServiceFor(scheme).linksVanished(did); |
| 158 | break; |
| 159 | case LINK_DESCRIPTION: |
| 160 | LinkDescription desc = translate(request.getLinkDescription()); |
| 161 | getLinkProviderServiceFor(scheme).linkVanished(desc); |
Yuta HIGUCHI | 4c7c90a | 2016-07-06 14:56:49 -0700 | [diff] [blame] | 162 | lastSeen.invalidate(Pair.of(scheme, linkKey(desc))); |
HIGUCHI Yuta | 7c1583c | 2015-12-03 23:08:54 -0800 | [diff] [blame] | 163 | break; |
| 164 | case SUBJECT_NOT_SET: |
| 165 | default: |
| 166 | // do nothing |
| 167 | break; |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * Translates gRPC message to corresponding ONOS object. |
| 173 | * |
| 174 | * @param connectPoint gRPC message. |
| 175 | * @return {@link ConnectPoint} |
| 176 | */ |
Jian Li | c9b4bf1 | 2017-06-26 23:50:32 +0900 | [diff] [blame] | 177 | private ConnectPoint translate(ConnectPointProto connectPoint) { |
HIGUCHI Yuta | 7c1583c | 2015-12-03 23:08:54 -0800 | [diff] [blame] | 178 | checkArgument(connectPoint.getElementIdCase() == ElementIdCase.DEVICE_ID, |
| 179 | "Only DeviceId supported."); |
| 180 | return new ConnectPoint(deviceId(connectPoint.getDeviceId()), |
| 181 | PortNumber.fromString(connectPoint.getPortNumber())); |
| 182 | } |
| 183 | |
| 184 | /** |
| 185 | * Translates gRPC message to corresponding ONOS object. |
| 186 | * |
| 187 | * @param linkDescription gRPC message |
| 188 | * @return {@link LinkDescription} |
| 189 | */ |
Jian Li | c9b4bf1 | 2017-06-26 23:50:32 +0900 | [diff] [blame] | 190 | private LinkDescription translate(LinkDescriptionProto linkDescription) { |
HIGUCHI Yuta | 7c1583c | 2015-12-03 23:08:54 -0800 | [diff] [blame] | 191 | ConnectPoint src = translate(linkDescription.getSrc()); |
| 192 | ConnectPoint dst = translate(linkDescription.getDst()); |
Jian Li | c9b4bf1 | 2017-06-26 23:50:32 +0900 | [diff] [blame] | 193 | Type type = translate(linkDescription.getType()); |
Jian Li | d40252c | 2017-12-08 04:13:20 +0900 | [diff] [blame] | 194 | SparseAnnotations annotations = LinkProtoTranslator.asAnnotations(linkDescription.getAnnotations()); |
HIGUCHI Yuta | 7c1583c | 2015-12-03 23:08:54 -0800 | [diff] [blame] | 195 | return new DefaultLinkDescription(src, dst, type, annotations); |
| 196 | } |
| 197 | |
| 198 | /** |
| 199 | * Translates gRPC message to corresponding ONOS object. |
| 200 | * |
| 201 | * @param type gRPC message enum |
Jian Li | c9b4bf1 | 2017-06-26 23:50:32 +0900 | [diff] [blame] | 202 | * @return {@link Type Link.Type} |
HIGUCHI Yuta | 7c1583c | 2015-12-03 23:08:54 -0800 | [diff] [blame] | 203 | */ |
Jian Li | c9b4bf1 | 2017-06-26 23:50:32 +0900 | [diff] [blame] | 204 | private Type translate(LinkTypeProto type) { |
HIGUCHI Yuta | 7c1583c | 2015-12-03 23:08:54 -0800 | [diff] [blame] | 205 | switch (type) { |
| 206 | case DIRECT: |
Jian Li | c9b4bf1 | 2017-06-26 23:50:32 +0900 | [diff] [blame] | 207 | return Type.DIRECT; |
HIGUCHI Yuta | 7c1583c | 2015-12-03 23:08:54 -0800 | [diff] [blame] | 208 | case EDGE: |
Jian Li | c9b4bf1 | 2017-06-26 23:50:32 +0900 | [diff] [blame] | 209 | return Type.EDGE; |
HIGUCHI Yuta | 7c1583c | 2015-12-03 23:08:54 -0800 | [diff] [blame] | 210 | case INDIRECT: |
Jian Li | c9b4bf1 | 2017-06-26 23:50:32 +0900 | [diff] [blame] | 211 | return Type.INDIRECT; |
HIGUCHI Yuta | 7c1583c | 2015-12-03 23:08:54 -0800 | [diff] [blame] | 212 | case OPTICAL: |
Jian Li | c9b4bf1 | 2017-06-26 23:50:32 +0900 | [diff] [blame] | 213 | return Type.INDIRECT; |
HIGUCHI Yuta | 7c1583c | 2015-12-03 23:08:54 -0800 | [diff] [blame] | 214 | case TUNNEL: |
Jian Li | c9b4bf1 | 2017-06-26 23:50:32 +0900 | [diff] [blame] | 215 | return Type.TUNNEL; |
HIGUCHI Yuta | 7c1583c | 2015-12-03 23:08:54 -0800 | [diff] [blame] | 216 | case VIRTUAL: |
Jian Li | c9b4bf1 | 2017-06-26 23:50:32 +0900 | [diff] [blame] | 217 | return Type.VIRTUAL; |
HIGUCHI Yuta | 7c1583c | 2015-12-03 23:08:54 -0800 | [diff] [blame] | 218 | |
| 219 | case UNRECOGNIZED: |
| 220 | default: |
Jian Li | c9b4bf1 | 2017-06-26 23:50:32 +0900 | [diff] [blame] | 221 | return Type.DIRECT; |
HIGUCHI Yuta | 7c1583c | 2015-12-03 23:08:54 -0800 | [diff] [blame] | 222 | } |
| 223 | } |
| 224 | |
| 225 | } |