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