Frank Wang | cfffbaa | 2017-05-06 16:11:08 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-present Open Networking Foundation |
| 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 | |
| 17 | package org.onosproject.incubator.protobuf.services.nb; |
| 18 | |
| 19 | import com.google.common.annotations.Beta; |
| 20 | import io.grpc.BindableService; |
| 21 | import io.grpc.stub.StreamObserver; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 22 | import org.osgi.service.component.annotations.Activate; |
| 23 | import org.osgi.service.component.annotations.Component; |
| 24 | import org.osgi.service.component.annotations.Deactivate; |
| 25 | import org.osgi.service.component.annotations.Reference; |
| 26 | import org.osgi.service.component.annotations.ReferenceCardinality; |
Frank Wang | cfffbaa | 2017-05-06 16:11:08 +0800 | [diff] [blame] | 27 | import org.onlab.packet.IpAddress; |
| 28 | import org.onlab.packet.MacAddress; |
| 29 | import org.onlab.packet.VlanId; |
| 30 | import org.onosproject.net.DeviceId; |
| 31 | import org.onosproject.net.HostId; |
| 32 | import org.onosproject.net.Host; |
| 33 | import org.onosproject.net.host.HostService; |
| 34 | import org.onosproject.protobuf.api.GrpcServiceRegistry; |
| 35 | import org.slf4j.Logger; |
| 36 | import org.onosproject.grpc.nb.net.host.HostServiceGrpc.HostServiceImplBase; |
| 37 | import org.onosproject.incubator.protobuf.models.net.ConnectPointProtoTranslator; |
| 38 | import org.onosproject.incubator.protobuf.models.net.HostProtoTranslator; |
| 39 | |
| 40 | import static org.onosproject.grpc.nb.net.host.HostServiceNb.getHostCountRequest; |
| 41 | import static org.onosproject.grpc.nb.net.host.HostServiceNb.getHostCountReply; |
| 42 | import static org.onosproject.grpc.nb.net.host.HostServiceNb.getHostsRequest; |
| 43 | import static org.onosproject.grpc.nb.net.host.HostServiceNb.getHostsReply; |
| 44 | import static org.onosproject.grpc.nb.net.host.HostServiceNb.getHostRequest; |
| 45 | import static org.onosproject.grpc.nb.net.host.HostServiceNb.getHostReply; |
| 46 | import static org.onosproject.grpc.nb.net.host.HostServiceNb.getHostsByVlanRequest; |
| 47 | import static org.onosproject.grpc.nb.net.host.HostServiceNb.getHostsByVlanReply; |
| 48 | import static org.onosproject.grpc.nb.net.host.HostServiceNb.getHostsByMacRequest; |
| 49 | import static org.onosproject.grpc.nb.net.host.HostServiceNb.getHostsByMacReply; |
| 50 | import static org.onosproject.grpc.nb.net.host.HostServiceNb.getHostsByIpRequest; |
| 51 | import static org.onosproject.grpc.nb.net.host.HostServiceNb.getHostsByIpReply; |
| 52 | import static org.onosproject.grpc.nb.net.host.HostServiceNb.getConnectedHostsRequest; |
| 53 | import static org.onosproject.grpc.nb.net.host.HostServiceNb.getConnectedHostsReply; |
| 54 | import static org.onosproject.grpc.nb.net.host.HostServiceNb.startMonitoringIpRequest; |
| 55 | import static org.onosproject.grpc.nb.net.host.HostServiceNb.startMonitoringIpReply; |
| 56 | import static org.onosproject.grpc.nb.net.host.HostServiceNb.stopMonitoringIpRequest; |
| 57 | import static org.onosproject.grpc.nb.net.host.HostServiceNb.stopMonitoringIpReply; |
| 58 | import static org.onosproject.grpc.nb.net.host.HostServiceNb.requestMacRequest; |
| 59 | import static org.onosproject.grpc.nb.net.host.HostServiceNb.requestMacReply; |
| 60 | import static org.slf4j.LoggerFactory.getLogger; |
| 61 | |
| 62 | /** |
| 63 | * A server that provides access to the methods exposed by {@link HostService}. |
| 64 | */ |
| 65 | @Beta |
| 66 | @Component(immediate = true) |
| 67 | public class GrpcNbHostService { |
| 68 | |
| 69 | private final Logger log = getLogger(getClass()); |
| 70 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 71 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Frank Wang | cfffbaa | 2017-05-06 16:11:08 +0800 | [diff] [blame] | 72 | protected GrpcServiceRegistry registry; |
| 73 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 74 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Frank Wang | cfffbaa | 2017-05-06 16:11:08 +0800 | [diff] [blame] | 75 | protected HostService hostService; |
| 76 | |
Ray Milkey | 06297ed | 2018-01-22 17:13:41 -0800 | [diff] [blame] | 77 | private HostServiceNBServerInternal instance = null; |
Frank Wang | cfffbaa | 2017-05-06 16:11:08 +0800 | [diff] [blame] | 78 | |
| 79 | @Activate |
| 80 | public void activate() { |
| 81 | |
| 82 | registry.register(getInnerInstance()); |
| 83 | log.info("Started"); |
| 84 | } |
| 85 | |
| 86 | @Deactivate |
| 87 | public void deactivate() { |
| 88 | |
| 89 | registry.unregister(getInnerInstance()); |
| 90 | log.info("Stoped"); |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Register Host Service, Used for unit testing purposes. |
| 95 | * |
| 96 | * @return An instance of binding Host service |
| 97 | */ |
| 98 | public InProcessServer<BindableService> registerInProcessServer() { |
| 99 | InProcessServer<BindableService> inprocessServer = |
| 100 | new InProcessServer(HostServiceNBServerInternal.class); |
| 101 | inprocessServer.addServiceToBind(getInnerInstance()); |
| 102 | |
| 103 | return inprocessServer; |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Host Service NorthBound implementation. |
| 108 | */ |
| 109 | private final class HostServiceNBServerInternal extends HostServiceImplBase { |
| 110 | |
| 111 | private HostServiceNBServerInternal() { |
| 112 | super(); |
| 113 | } |
| 114 | |
| 115 | @Override |
| 116 | public void getHostCount(getHostCountRequest request, |
| 117 | StreamObserver<getHostCountReply> responseObserver) { |
| 118 | |
| 119 | responseObserver.onNext(getHostCountReply.newBuilder() |
| 120 | .setHostCount(hostService.getHostCount()) |
| 121 | .build()); |
| 122 | responseObserver.onCompleted(); |
| 123 | } |
| 124 | |
| 125 | @Override |
| 126 | public void getHosts(getHostsRequest request, |
| 127 | StreamObserver<getHostsReply> responseObserver) { |
| 128 | |
| 129 | getHostsReply.Builder replyBuilder = getHostsReply.newBuilder(); |
| 130 | |
| 131 | hostService.getHosts().forEach(d -> replyBuilder.addHost(HostProtoTranslator.translate(d))); |
| 132 | responseObserver.onNext(replyBuilder.build()); |
| 133 | responseObserver.onCompleted(); |
| 134 | } |
| 135 | |
| 136 | @Override |
| 137 | public void getHost(getHostRequest request, |
| 138 | StreamObserver<getHostReply> responseObserver) { |
| 139 | |
| 140 | Host host = hostService.getHost(HostId.hostId(request.getHostId().toString())); |
| 141 | |
| 142 | responseObserver.onNext(getHostReply.newBuilder().setHost(HostProtoTranslator.translate(host)).build()); |
| 143 | responseObserver.onCompleted(); |
| 144 | } |
| 145 | |
| 146 | @Override |
| 147 | public void getHostsByVlan(getHostsByVlanRequest request, |
| 148 | StreamObserver<getHostsByVlanReply> responseObserver) { |
| 149 | |
| 150 | getHostsByVlanReply.Builder replyBuilder = getHostsByVlanReply.newBuilder(); |
| 151 | |
| 152 | hostService.getHostsByVlan(VlanId.vlanId(request.getVlanId())).forEach( |
| 153 | d -> replyBuilder.addHost(HostProtoTranslator.translate(d))); |
| 154 | responseObserver.onNext(replyBuilder.build()); |
| 155 | responseObserver.onCompleted(); |
| 156 | } |
| 157 | |
| 158 | @Override |
| 159 | public void getHostsByMac(getHostsByMacRequest request, |
| 160 | StreamObserver<getHostsByMacReply> responseObserver) { |
| 161 | |
| 162 | getHostsByMacReply.Builder replyBuilder = getHostsByMacReply.newBuilder(); |
| 163 | |
| 164 | hostService.getHostsByMac(MacAddress.valueOf(request.getMac())).forEach( |
| 165 | d -> replyBuilder.addHost(HostProtoTranslator.translate(d))); |
| 166 | responseObserver.onNext(replyBuilder.build()); |
| 167 | responseObserver.onCompleted(); |
| 168 | } |
| 169 | |
| 170 | @Override |
| 171 | public void getHostsByIp(getHostsByIpRequest request, |
| 172 | StreamObserver<getHostsByIpReply> responseObserver) { |
| 173 | |
| 174 | getHostsByIpReply.Builder replyBuilder = getHostsByIpReply.newBuilder(); |
| 175 | |
| 176 | hostService.getHostsByIp(IpAddress.valueOf(request.getIpAddress())).forEach( |
| 177 | d -> replyBuilder.addHost(HostProtoTranslator.translate(d))); |
| 178 | responseObserver.onNext(replyBuilder.build()); |
| 179 | responseObserver.onCompleted(); |
| 180 | } |
| 181 | |
| 182 | @Override |
| 183 | public void getConnectedHosts(getConnectedHostsRequest request, |
| 184 | StreamObserver<getConnectedHostsReply> responseObserver) { |
| 185 | |
| 186 | getConnectedHostsReply.Builder replyBuilder = getConnectedHostsReply.newBuilder(); |
| 187 | |
| 188 | if (getConnectedHostsRequest.ConnectedHostCase.DEVICEID == request.getConnectedHostCase()) { |
| 189 | hostService.getConnectedHosts(DeviceId.deviceId(request.getDeviceId())) |
| 190 | .forEach(d -> replyBuilder.addHost(HostProtoTranslator.translate(d))); |
| 191 | } else if (getConnectedHostsRequest.ConnectedHostCase.CONNECT_POINT == request.getConnectedHostCase()) { |
| 192 | hostService.getConnectedHosts(ConnectPointProtoTranslator.translate(request.getConnectPoint()).get()) |
| 193 | .forEach(d -> replyBuilder.addHost(HostProtoTranslator.translate(d))); |
| 194 | } else { |
| 195 | log.warn("Both DeviceId and ConnectPoint are not set."); |
| 196 | } |
| 197 | |
| 198 | responseObserver.onNext(replyBuilder.build()); |
| 199 | responseObserver.onCompleted(); |
| 200 | } |
| 201 | |
| 202 | @Override |
| 203 | public void startMonitoringIp(startMonitoringIpRequest request, |
| 204 | StreamObserver<startMonitoringIpReply> responseObserver) { |
| 205 | |
| 206 | hostService.startMonitoringIp(IpAddress.valueOf(request.getIpAddress())); |
| 207 | responseObserver.onNext(startMonitoringIpReply.getDefaultInstance()); |
| 208 | responseObserver.onCompleted(); |
| 209 | } |
| 210 | |
| 211 | @Override |
| 212 | public void stopMonitoringIp(stopMonitoringIpRequest request, |
| 213 | StreamObserver<stopMonitoringIpReply> responseObserver) { |
| 214 | |
| 215 | hostService.stopMonitoringIp(IpAddress.valueOf(request.getIpAddress())); |
| 216 | responseObserver.onNext(stopMonitoringIpReply.getDefaultInstance()); |
| 217 | responseObserver.onCompleted(); |
| 218 | } |
| 219 | |
| 220 | @Override |
| 221 | public void requestMac(requestMacRequest request, |
| 222 | StreamObserver<requestMacReply> responseObserver) { |
| 223 | |
| 224 | hostService.requestMac(IpAddress.valueOf(request.getIpAddress())); |
| 225 | responseObserver.onNext(requestMacReply.getDefaultInstance()); |
| 226 | responseObserver.onCompleted(); |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | private HostServiceNBServerInternal getInnerInstance() { |
| 231 | if (instance == null) { |
| 232 | instance = new HostServiceNBServerInternal(); |
| 233 | } |
| 234 | return instance; |
| 235 | } |
| 236 | } |