Andrea Campanella | 378e21a | 2017-06-07 12:09:59 +0200 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2017-present Open Networking Laboratory |
| 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.grpc.ctl; |
| 18 | |
| 19 | import com.google.common.collect.ImmutableSet; |
| 20 | import io.grpc.ManagedChannel; |
| 21 | import io.grpc.ManagedChannelBuilder; |
| 22 | import org.apache.felix.scr.annotations.Activate; |
| 23 | import org.apache.felix.scr.annotations.Component; |
| 24 | import org.apache.felix.scr.annotations.Deactivate; |
| 25 | import org.apache.felix.scr.annotations.Service; |
| 26 | import org.onosproject.grpc.api.GrpcChannelId; |
| 27 | import org.onosproject.grpc.api.GrpcController; |
| 28 | import org.onosproject.grpc.api.GrpcObserverHandler; |
| 29 | import org.onosproject.grpc.api.GrpcStreamObserverId; |
| 30 | import org.onosproject.net.DeviceId; |
| 31 | import org.slf4j.Logger; |
| 32 | import org.slf4j.LoggerFactory; |
| 33 | |
| 34 | import java.util.Collection; |
| 35 | import java.util.HashSet; |
| 36 | import java.util.Map; |
| 37 | import java.util.Optional; |
| 38 | import java.util.Set; |
| 39 | import java.util.concurrent.ConcurrentHashMap; |
| 40 | |
| 41 | /** |
| 42 | * Default implementation of the GrpcController. |
| 43 | */ |
| 44 | @Component(immediate = true) |
| 45 | @Service |
| 46 | public class GrpcControllerImpl implements GrpcController { |
| 47 | |
| 48 | public static final Logger log = LoggerFactory |
| 49 | .getLogger(GrpcControllerImpl.class); |
| 50 | |
| 51 | private Map<GrpcStreamObserverId, GrpcObserverHandler> observers; |
| 52 | private Map<GrpcChannelId, ManagedChannel> channels; |
| 53 | private Map<GrpcChannelId, ManagedChannelBuilder<?>> channelBuilders; |
| 54 | |
| 55 | @Activate |
| 56 | public void activate() { |
| 57 | observers = new ConcurrentHashMap<>(); |
| 58 | channels = new ConcurrentHashMap<>(); |
| 59 | channelBuilders = new ConcurrentHashMap<>(); |
| 60 | log.info("Started"); |
| 61 | } |
| 62 | |
| 63 | @Deactivate |
| 64 | public void deactivate() { |
| 65 | channels.values().forEach(ManagedChannel::shutdown); |
| 66 | observers.clear(); |
| 67 | channels.clear(); |
| 68 | channelBuilders.clear(); |
| 69 | log.info("Stopped"); |
| 70 | } |
| 71 | |
| 72 | @Override |
| 73 | public void addObserver(GrpcStreamObserverId observerId, GrpcObserverHandler grpcObserverHandler) { |
| 74 | grpcObserverHandler.bindObserver(channels.get(observerId.serviceId().channelId())); |
| 75 | observers.put(observerId, grpcObserverHandler); |
| 76 | } |
| 77 | |
| 78 | @Override |
| 79 | public void removeObserver(GrpcStreamObserverId observerId) { |
| 80 | observers.get(observerId).removeObserver(); |
| 81 | observers.remove(observerId); |
| 82 | } |
| 83 | |
| 84 | @Override |
| 85 | public Optional<GrpcObserverHandler> getObserverManager(GrpcStreamObserverId observerId) { |
| 86 | return Optional.ofNullable(observers.get(observerId)); |
| 87 | } |
| 88 | |
| 89 | @Override |
| 90 | public ManagedChannel connectChannel(GrpcChannelId channelId, ManagedChannelBuilder<?> channelBuilder) { |
| 91 | ManagedChannel channel = channelBuilder.build(); |
| 92 | |
| 93 | channel.getState(true); |
| 94 | channelBuilders.put(channelId, channelBuilder); |
| 95 | channels.put(channelId, channel); |
| 96 | return channel; |
| 97 | } |
| 98 | |
| 99 | @Override |
| 100 | public void disconnectChannel(GrpcChannelId channelId) { |
| 101 | channels.get(channelId).shutdown(); |
| 102 | channels.remove(channelId); |
| 103 | channelBuilders.remove(channelId); |
| 104 | } |
| 105 | |
| 106 | @Override |
| 107 | public Map<GrpcChannelId, ManagedChannel> getChannels() { |
| 108 | return channels; |
| 109 | } |
| 110 | |
| 111 | @Override |
| 112 | public Collection<ManagedChannel> getChannels(final DeviceId deviceId) { |
| 113 | final Set<ManagedChannel> deviceChannels = new HashSet<>(); |
| 114 | channels.forEach((k, v) -> { |
| 115 | if (k.deviceId().equals(deviceId)) { |
| 116 | deviceChannels.add(v); |
| 117 | } |
| 118 | }); |
| 119 | |
| 120 | return ImmutableSet.copyOf(deviceChannels); |
| 121 | } |
| 122 | |
| 123 | @Override |
| 124 | public Optional<ManagedChannel> getChannel(GrpcChannelId channelId) { |
| 125 | return Optional.ofNullable(channels.get(channelId)); |
| 126 | } |
| 127 | } |