Jian Li | 8780edc | 2017-08-26 02:44:29 +0900 | [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 | */ |
Jian Li | 4c703b9 | 2017-10-17 00:35:53 +0900 | [diff] [blame] | 16 | package org.onosproject.incubator.protobuf.services.nb; |
Jian Li | 8780edc | 2017-08-26 02:44:29 +0900 | [diff] [blame] | 17 | |
| 18 | import com.google.common.annotations.Beta; |
Jian Li | fc88978 | 2017-10-17 00:47:53 +0900 | [diff] [blame] | 19 | import io.grpc.BindableService; |
Jian Li | 8780edc | 2017-08-26 02:44:29 +0900 | [diff] [blame] | 20 | import io.grpc.stub.StreamObserver; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 21 | import org.osgi.service.component.annotations.Activate; |
| 22 | import org.osgi.service.component.annotations.Component; |
| 23 | import org.osgi.service.component.annotations.Deactivate; |
| 24 | import org.osgi.service.component.annotations.Reference; |
| 25 | import org.osgi.service.component.annotations.ReferenceCardinality; |
Jian Li | 8780edc | 2017-08-26 02:44:29 +0900 | [diff] [blame] | 26 | import org.onosproject.app.ApplicationService; |
| 27 | import org.onosproject.app.ApplicationState; |
| 28 | import org.onosproject.core.Application; |
| 29 | import org.onosproject.core.ApplicationId; |
| 30 | import org.onosproject.grpc.nb.app.ApplicationServiceGrpc.ApplicationServiceImplBase; |
Jian Li | 4c703b9 | 2017-10-17 00:35:53 +0900 | [diff] [blame] | 31 | import org.onosproject.grpc.nb.app.ApplicationServiceNb; |
Jian Li | 8780edc | 2017-08-26 02:44:29 +0900 | [diff] [blame] | 32 | import org.onosproject.grpc.nb.app.ApplicationServiceNb.getApplicationReply; |
| 33 | import org.onosproject.grpc.nb.app.ApplicationServiceNb.getApplicationsReply; |
| 34 | import org.onosproject.grpc.nb.app.ApplicationServiceNb.getIdReply; |
| 35 | import org.onosproject.grpc.nb.app.ApplicationServiceNb.getPermissionsReply; |
| 36 | import org.onosproject.grpc.nb.app.ApplicationServiceNb.getStateReply; |
| 37 | import org.onosproject.incubator.protobuf.models.core.ApplicationEnumsProtoTranslator; |
| 38 | import org.onosproject.incubator.protobuf.models.core.ApplicationIdProtoTranslator; |
| 39 | import org.onosproject.incubator.protobuf.models.core.ApplicationProtoTranslator; |
| 40 | import org.onosproject.incubator.protobuf.models.security.PermissionProtoTranslator; |
Jian Li | fc88978 | 2017-10-17 00:47:53 +0900 | [diff] [blame] | 41 | import org.onosproject.protobuf.api.GrpcServiceRegistry; |
| 42 | import org.slf4j.Logger; |
| 43 | |
| 44 | import static org.slf4j.LoggerFactory.getLogger; |
Jian Li | 8780edc | 2017-08-26 02:44:29 +0900 | [diff] [blame] | 45 | |
| 46 | /** |
| 47 | * A server that provides access to the methods exposed by {@link ApplicationService}. |
| 48 | */ |
| 49 | |
| 50 | @Beta |
| 51 | @Component(immediate = true) |
| 52 | public class GrpcNbApplicationService { |
| 53 | |
Jian Li | fc88978 | 2017-10-17 00:47:53 +0900 | [diff] [blame] | 54 | private final Logger log = getLogger(getClass()); |
| 55 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 56 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Jian Li | fc88978 | 2017-10-17 00:47:53 +0900 | [diff] [blame] | 57 | protected GrpcServiceRegistry registry; |
| 58 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 59 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Jian Li | 8780edc | 2017-08-26 02:44:29 +0900 | [diff] [blame] | 60 | protected ApplicationService applicationService; |
| 61 | |
Ray Milkey | 06297ed | 2018-01-22 17:13:41 -0800 | [diff] [blame] | 62 | private ApplicationServiceNbServerInternal instance = null; |
Jian Li | fc88978 | 2017-10-17 00:47:53 +0900 | [diff] [blame] | 63 | |
| 64 | |
Jian Li | 8780edc | 2017-08-26 02:44:29 +0900 | [diff] [blame] | 65 | @Activate |
| 66 | public void activate() { |
Jian Li | fc88978 | 2017-10-17 00:47:53 +0900 | [diff] [blame] | 67 | registry.register(getInnerInstance()); |
| 68 | log.info("Started."); |
Jian Li | 8780edc | 2017-08-26 02:44:29 +0900 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | @Deactivate |
| 72 | public void deactivate() { |
Jian Li | fc88978 | 2017-10-17 00:47:53 +0900 | [diff] [blame] | 73 | registry.unregister(getInnerInstance()); |
| 74 | log.info("Stopped"); |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Register Application Service, Used for unit testing purposes. |
| 79 | * |
| 80 | * @return An instance of binding Application service |
| 81 | */ |
| 82 | public InProcessServer<BindableService> registerInProcessServer() { |
| 83 | InProcessServer<BindableService> inprocessServer = |
| 84 | new InProcessServer(GrpcNbApplicationService.ApplicationServiceNbServerInternal.class); |
| 85 | inprocessServer.addServiceToBind(getInnerInstance()); |
| 86 | |
| 87 | return inprocessServer; |
Jian Li | 8780edc | 2017-08-26 02:44:29 +0900 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | private class ApplicationServiceNbServerInternal extends ApplicationServiceImplBase { |
| 91 | |
| 92 | public ApplicationServiceNbServerInternal() { |
| 93 | super(); |
| 94 | } |
| 95 | |
| 96 | @Override |
| 97 | public void getApplications(ApplicationServiceNb.getApplicationsRequest request, |
| 98 | StreamObserver<getApplicationsReply> responseObserver) { |
| 99 | getApplicationsReply.Builder replyBuilder = getApplicationsReply.newBuilder(); |
| 100 | |
| 101 | applicationService.getApplications().forEach(a -> |
| 102 | replyBuilder.addApplication(ApplicationProtoTranslator.translate(a))); |
| 103 | responseObserver.onNext(replyBuilder.build()); |
| 104 | responseObserver.onCompleted(); |
| 105 | } |
| 106 | |
| 107 | @Override |
| 108 | public void getId(ApplicationServiceNb.getIdRequest request, |
| 109 | StreamObserver<getIdReply> responseObserver) { |
| 110 | ApplicationId appId = applicationService.getId(request.getName()); |
| 111 | |
| 112 | responseObserver.onNext(getIdReply.newBuilder() |
| 113 | .setApplicationId(ApplicationIdProtoTranslator.translate(appId)).build()); |
| 114 | responseObserver.onCompleted(); |
| 115 | } |
| 116 | |
| 117 | @Override |
| 118 | public void getApplication(ApplicationServiceNb.getApplicationRequest request, |
| 119 | StreamObserver<getApplicationReply> responseObserver) { |
| 120 | |
| 121 | Application application = applicationService.getApplication( |
| 122 | ApplicationIdProtoTranslator.translate(request.getApplicationId())); |
| 123 | |
| 124 | responseObserver.onNext(getApplicationReply.newBuilder() |
| 125 | .setApplication(ApplicationProtoTranslator |
| 126 | .translate(application)).build()); |
| 127 | responseObserver.onCompleted(); |
| 128 | } |
| 129 | |
| 130 | @Override |
| 131 | public void getState(ApplicationServiceNb.getStateRequest request, |
| 132 | StreamObserver<getStateReply> responseObserver) { |
| 133 | ApplicationState state = applicationService.getState( |
| 134 | ApplicationIdProtoTranslator.translate(request.getApplicationId())); |
| 135 | |
| 136 | responseObserver.onNext(getStateReply |
| 137 | .newBuilder().setState(ApplicationEnumsProtoTranslator |
| 138 | .translate(state)).build()); |
| 139 | responseObserver.onCompleted(); |
| 140 | } |
| 141 | |
| 142 | @Override |
| 143 | public void getPermissions(ApplicationServiceNb.getPermissionsRequest request, |
| 144 | StreamObserver<getPermissionsReply> responseObserver) { |
| 145 | getPermissionsReply.Builder replyBuilder = getPermissionsReply.newBuilder(); |
| 146 | |
| 147 | applicationService.getPermissions(ApplicationIdProtoTranslator |
| 148 | .translate(request.getApplicationId())) |
| 149 | .forEach(p -> replyBuilder.addPermission( |
| 150 | PermissionProtoTranslator.translate(p))); |
| 151 | responseObserver.onNext(replyBuilder.build()); |
| 152 | responseObserver.onCompleted(); |
| 153 | } |
| 154 | } |
Jian Li | fc88978 | 2017-10-17 00:47:53 +0900 | [diff] [blame] | 155 | |
| 156 | private ApplicationServiceNbServerInternal getInnerInstance() { |
| 157 | if (instance == null) { |
| 158 | instance = new ApplicationServiceNbServerInternal(); |
| 159 | } |
| 160 | return instance; |
| 161 | } |
Jian Li | 8780edc | 2017-08-26 02:44:29 +0900 | [diff] [blame] | 162 | } |