Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [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.p4runtime.ctl; |
| 18 | |
| 19 | import com.google.common.collect.Maps; |
| 20 | import io.grpc.ManagedChannel; |
| 21 | import io.grpc.ManagedChannelBuilder; |
| 22 | import io.grpc.NameResolverProvider; |
| 23 | import io.grpc.internal.DnsNameResolverProvider; |
| 24 | import org.apache.felix.scr.annotations.Activate; |
| 25 | import org.apache.felix.scr.annotations.Component; |
| 26 | import org.apache.felix.scr.annotations.Deactivate; |
| 27 | import org.apache.felix.scr.annotations.Reference; |
| 28 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 29 | import org.apache.felix.scr.annotations.Service; |
| 30 | import org.onosproject.event.AbstractListenerManager; |
| 31 | import org.onosproject.grpc.api.GrpcChannelId; |
| 32 | import org.onosproject.grpc.api.GrpcController; |
| 33 | import org.onosproject.net.DeviceId; |
| 34 | import org.onosproject.p4runtime.api.P4RuntimeClient; |
| 35 | import org.onosproject.p4runtime.api.P4RuntimeController; |
| 36 | import org.onosproject.p4runtime.api.P4RuntimeEvent; |
| 37 | import org.onosproject.p4runtime.api.P4RuntimeEventListener; |
| 38 | import org.slf4j.Logger; |
| 39 | |
| 40 | import java.io.IOException; |
| 41 | import java.util.Map; |
Carmelo Cascone | 59f57de | 2017-07-11 19:55:09 -0400 | [diff] [blame] | 42 | import java.util.concurrent.locks.ReadWriteLock; |
| 43 | import java.util.concurrent.locks.ReentrantReadWriteLock; |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 44 | |
| 45 | import static com.google.common.base.Preconditions.checkNotNull; |
| 46 | import static java.lang.String.format; |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 47 | import static org.slf4j.LoggerFactory.getLogger; |
| 48 | |
| 49 | /** |
| 50 | * P4Runtime controller implementation. |
| 51 | */ |
| 52 | @Component(immediate = true) |
| 53 | @Service |
| 54 | public class P4RuntimeControllerImpl |
| 55 | extends AbstractListenerManager<P4RuntimeEvent, P4RuntimeEventListener> |
| 56 | implements P4RuntimeController { |
| 57 | |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 58 | private final Logger log = getLogger(getClass()); |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 59 | private final NameResolverProvider nameResolverProvider = new DnsNameResolverProvider(); |
Carmelo Cascone | 59f57de | 2017-07-11 19:55:09 -0400 | [diff] [blame] | 60 | private final Map<DeviceId, P4RuntimeClient> clients = Maps.newHashMap(); |
| 61 | private final Map<DeviceId, GrpcChannelId> channelIds = Maps.newHashMap(); |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 62 | // TODO: should use a cache to delete unused locks. |
Carmelo Cascone | 59f57de | 2017-07-11 19:55:09 -0400 | [diff] [blame] | 63 | private final Map<DeviceId, ReadWriteLock> deviceLocks = Maps.newConcurrentMap(); |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 64 | |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 65 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Carmelo Cascone | c8e8498 | 2017-07-26 15:34:42 -0400 | [diff] [blame] | 66 | public GrpcController grpcController; |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 67 | |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 68 | @Activate |
| 69 | public void activate() { |
| 70 | log.info("Started"); |
| 71 | } |
| 72 | |
| 73 | |
| 74 | @Deactivate |
| 75 | public void deactivate() { |
| 76 | grpcController = null; |
| 77 | log.info("Stopped"); |
| 78 | } |
| 79 | |
| 80 | |
| 81 | @Override |
| 82 | public boolean createClient(DeviceId deviceId, int p4DeviceId, ManagedChannelBuilder channelBuilder) { |
| 83 | checkNotNull(deviceId); |
| 84 | checkNotNull(channelBuilder); |
| 85 | |
Carmelo Cascone | 59f57de | 2017-07-11 19:55:09 -0400 | [diff] [blame] | 86 | deviceLocks.putIfAbsent(deviceId, new ReentrantReadWriteLock()); |
| 87 | deviceLocks.get(deviceId).writeLock().lock(); |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 88 | |
| 89 | log.info("Creating client for {} (with internal device id {})...", deviceId, p4DeviceId); |
| 90 | |
| 91 | try { |
| 92 | if (clients.containsKey(deviceId)) { |
| 93 | throw new IllegalStateException(format("A client already exists for %s", deviceId)); |
| 94 | } else { |
| 95 | return doCreateClient(deviceId, p4DeviceId, channelBuilder); |
| 96 | } |
| 97 | } finally { |
Carmelo Cascone | 59f57de | 2017-07-11 19:55:09 -0400 | [diff] [blame] | 98 | deviceLocks.get(deviceId).writeLock().unlock(); |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 99 | } |
| 100 | } |
| 101 | |
| 102 | private boolean doCreateClient(DeviceId deviceId, int p4DeviceId, ManagedChannelBuilder channelBuilder) { |
Carmelo Cascone | 59f57de | 2017-07-11 19:55:09 -0400 | [diff] [blame] | 103 | |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 104 | GrpcChannelId channelId = GrpcChannelId.of(deviceId, "p4runtime"); |
| 105 | |
| 106 | // Channel defaults. |
| 107 | channelBuilder.nameResolverFactory(nameResolverProvider); |
| 108 | |
| 109 | ManagedChannel channel; |
| 110 | try { |
| 111 | channel = grpcController.connectChannel(channelId, channelBuilder); |
| 112 | } catch (IOException e) { |
| 113 | log.warn("Unable to connect to gRPC server of {}: {}", deviceId, e.getMessage()); |
| 114 | return false; |
| 115 | } |
| 116 | |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 117 | P4RuntimeClient client = new P4RuntimeClientImpl(deviceId, p4DeviceId, channel, this); |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 118 | |
| 119 | channelIds.put(deviceId, channelId); |
| 120 | clients.put(deviceId, client); |
| 121 | |
| 122 | return true; |
| 123 | } |
| 124 | |
| 125 | @Override |
| 126 | public P4RuntimeClient getClient(DeviceId deviceId) { |
| 127 | |
Carmelo Cascone | 59f57de | 2017-07-11 19:55:09 -0400 | [diff] [blame] | 128 | deviceLocks.putIfAbsent(deviceId, new ReentrantReadWriteLock()); |
| 129 | deviceLocks.get(deviceId).readLock().lock(); |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 130 | |
| 131 | try { |
| 132 | return clients.get(deviceId); |
| 133 | } finally { |
Carmelo Cascone | 59f57de | 2017-07-11 19:55:09 -0400 | [diff] [blame] | 134 | deviceLocks.get(deviceId).readLock().unlock(); |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 135 | } |
| 136 | } |
| 137 | |
| 138 | @Override |
| 139 | public void removeClient(DeviceId deviceId) { |
| 140 | |
Carmelo Cascone | 59f57de | 2017-07-11 19:55:09 -0400 | [diff] [blame] | 141 | deviceLocks.putIfAbsent(deviceId, new ReentrantReadWriteLock()); |
| 142 | deviceLocks.get(deviceId).writeLock().lock(); |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 143 | |
| 144 | try { |
| 145 | if (clients.containsKey(deviceId)) { |
| 146 | clients.get(deviceId).shutdown(); |
| 147 | grpcController.disconnectChannel(channelIds.get(deviceId)); |
| 148 | clients.remove(deviceId); |
Carmelo Cascone | 59f57de | 2017-07-11 19:55:09 -0400 | [diff] [blame] | 149 | channelIds.remove(deviceId); |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 150 | } |
| 151 | } finally { |
Carmelo Cascone | 59f57de | 2017-07-11 19:55:09 -0400 | [diff] [blame] | 152 | deviceLocks.get(deviceId).writeLock().unlock(); |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 153 | } |
| 154 | } |
| 155 | |
| 156 | @Override |
| 157 | public boolean hasClient(DeviceId deviceId) { |
| 158 | |
Carmelo Cascone | 59f57de | 2017-07-11 19:55:09 -0400 | [diff] [blame] | 159 | deviceLocks.putIfAbsent(deviceId, new ReentrantReadWriteLock()); |
| 160 | deviceLocks.get(deviceId).readLock().lock(); |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 161 | |
| 162 | try { |
| 163 | return clients.containsKey(deviceId); |
| 164 | } finally { |
Carmelo Cascone | 59f57de | 2017-07-11 19:55:09 -0400 | [diff] [blame] | 165 | deviceLocks.get(deviceId).readLock().unlock(); |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | @Override |
| 170 | public boolean isReacheable(DeviceId deviceId) { |
| 171 | |
| 172 | deviceLocks.putIfAbsent(deviceId, new ReentrantReadWriteLock()); |
| 173 | deviceLocks.get(deviceId).readLock().lock(); |
| 174 | |
| 175 | try { |
| 176 | if (!clients.containsKey(deviceId)) { |
| 177 | log.warn("No client for {}, can't check for reachability", deviceId); |
| 178 | return false; |
| 179 | } |
| 180 | |
| 181 | return grpcController.isChannelOpen(channelIds.get(deviceId)); |
| 182 | } finally { |
| 183 | deviceLocks.get(deviceId).readLock().unlock(); |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 184 | } |
| 185 | } |
| 186 | |
| 187 | void postEvent(P4RuntimeEvent event) { |
| 188 | post(event); |
| 189 | } |
| 190 | } |