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; |
| 42 | import java.util.concurrent.locks.Lock; |
| 43 | import java.util.concurrent.locks.ReentrantLock; |
| 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(); |
| 65 | private final Map<DeviceId, P4RuntimeClient> clients = Maps.newConcurrentMap(); |
| 66 | private final Map<DeviceId, GrpcChannelId> channelIds = Maps.newConcurrentMap(); |
| 67 | // TODO: should use a cache to delete unused locks. |
| 68 | private final Map<DeviceId, Lock> deviceLocks = Maps.newConcurrentMap(); |
| 69 | |
| 70 | @Activate |
| 71 | public void activate() { |
| 72 | log.info("Started"); |
| 73 | } |
| 74 | |
| 75 | |
| 76 | @Deactivate |
| 77 | public void deactivate() { |
| 78 | grpcController = null; |
| 79 | log.info("Stopped"); |
| 80 | } |
| 81 | |
| 82 | |
| 83 | @Override |
| 84 | public boolean createClient(DeviceId deviceId, int p4DeviceId, ManagedChannelBuilder channelBuilder) { |
| 85 | checkNotNull(deviceId); |
| 86 | checkNotNull(channelBuilder); |
| 87 | |
| 88 | deviceLocks.putIfAbsent(deviceId, new ReentrantLock()); |
| 89 | deviceLocks.get(deviceId).lock(); |
| 90 | |
| 91 | log.info("Creating client for {} (with internal device id {})...", deviceId, p4DeviceId); |
| 92 | |
| 93 | try { |
| 94 | if (clients.containsKey(deviceId)) { |
| 95 | throw new IllegalStateException(format("A client already exists for %s", deviceId)); |
| 96 | } else { |
| 97 | return doCreateClient(deviceId, p4DeviceId, channelBuilder); |
| 98 | } |
| 99 | } finally { |
| 100 | deviceLocks.get(deviceId).unlock(); |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | private boolean doCreateClient(DeviceId deviceId, int p4DeviceId, ManagedChannelBuilder channelBuilder) { |
| 105 | GrpcChannelId channelId = GrpcChannelId.of(deviceId, "p4runtime"); |
| 106 | |
| 107 | // Channel defaults. |
| 108 | channelBuilder.nameResolverFactory(nameResolverProvider); |
| 109 | |
| 110 | ManagedChannel channel; |
| 111 | try { |
| 112 | channel = grpcController.connectChannel(channelId, channelBuilder); |
| 113 | } catch (IOException e) { |
| 114 | log.warn("Unable to connect to gRPC server of {}: {}", deviceId, e.getMessage()); |
| 115 | return false; |
| 116 | } |
| 117 | |
| 118 | P4RuntimeClient client = new P4RuntimeClientImpl(deviceId, p4DeviceId, channel, this, |
| 119 | newSingleThreadExecutor()); |
| 120 | |
| 121 | channelIds.put(deviceId, channelId); |
| 122 | clients.put(deviceId, client); |
| 123 | |
| 124 | return true; |
| 125 | } |
| 126 | |
| 127 | @Override |
| 128 | public P4RuntimeClient getClient(DeviceId deviceId) { |
| 129 | |
| 130 | deviceLocks.putIfAbsent(deviceId, new ReentrantLock()); |
| 131 | deviceLocks.get(deviceId).lock(); |
| 132 | |
| 133 | try { |
| 134 | return clients.get(deviceId); |
| 135 | } finally { |
| 136 | deviceLocks.get(deviceId).unlock(); |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | @Override |
| 141 | public void removeClient(DeviceId deviceId) { |
| 142 | |
| 143 | deviceLocks.putIfAbsent(deviceId, new ReentrantLock()); |
| 144 | deviceLocks.get(deviceId).lock(); |
| 145 | |
| 146 | try { |
| 147 | if (clients.containsKey(deviceId)) { |
| 148 | clients.get(deviceId).shutdown(); |
| 149 | grpcController.disconnectChannel(channelIds.get(deviceId)); |
| 150 | clients.remove(deviceId); |
| 151 | } |
| 152 | } finally { |
| 153 | deviceLocks.get(deviceId).unlock(); |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | @Override |
| 158 | public boolean hasClient(DeviceId deviceId) { |
| 159 | |
| 160 | deviceLocks.putIfAbsent(deviceId, new ReentrantLock()); |
| 161 | deviceLocks.get(deviceId).lock(); |
| 162 | |
| 163 | try { |
| 164 | return clients.containsKey(deviceId); |
| 165 | } finally { |
| 166 | deviceLocks.get(deviceId).unlock(); |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | void postEvent(P4RuntimeEvent event) { |
| 171 | post(event); |
| 172 | } |
| 173 | } |