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