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