blob: 06376cae60d57eaa9c74d42ec6f67af03eede391 [file] [log] [blame]
Carmelo Casconef7aa3f92017-07-06 23:56:50 -04001/*
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
17package org.onosproject.p4runtime.ctl;
18
19import com.google.common.collect.Maps;
20import io.grpc.ManagedChannel;
21import io.grpc.ManagedChannelBuilder;
22import io.grpc.NameResolverProvider;
23import io.grpc.internal.DnsNameResolverProvider;
24import org.apache.felix.scr.annotations.Activate;
25import org.apache.felix.scr.annotations.Component;
26import org.apache.felix.scr.annotations.Deactivate;
27import org.apache.felix.scr.annotations.Reference;
28import org.apache.felix.scr.annotations.ReferenceCardinality;
29import org.apache.felix.scr.annotations.Service;
30import org.onosproject.event.AbstractListenerManager;
31import org.onosproject.grpc.api.GrpcChannelId;
32import org.onosproject.grpc.api.GrpcController;
33import org.onosproject.net.DeviceId;
34import org.onosproject.p4runtime.api.P4RuntimeClient;
35import org.onosproject.p4runtime.api.P4RuntimeController;
36import org.onosproject.p4runtime.api.P4RuntimeEvent;
37import org.onosproject.p4runtime.api.P4RuntimeEventListener;
38import org.slf4j.Logger;
39
40import java.io.IOException;
41import java.util.Map;
Carmelo Cascone59f57de2017-07-11 19:55:09 -040042import java.util.concurrent.locks.ReadWriteLock;
43import java.util.concurrent.locks.ReentrantReadWriteLock;
Carmelo Casconef7aa3f92017-07-06 23:56:50 -040044
45import static com.google.common.base.Preconditions.checkNotNull;
46import static java.lang.String.format;
47import static java.util.concurrent.Executors.newSingleThreadExecutor;
48import static org.slf4j.LoggerFactory.getLogger;
49
50/**
51 * P4Runtime controller implementation.
52 */
53@Component(immediate = true)
54@Service
55public 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 Cascone59f57de2017-07-11 19:55:09 -040065 private final Map<DeviceId, P4RuntimeClient> clients = Maps.newHashMap();
66 private final Map<DeviceId, GrpcChannelId> channelIds = Maps.newHashMap();
67
Carmelo Casconef7aa3f92017-07-06 23:56:50 -040068 // TODO: should use a cache to delete unused locks.
Carmelo Cascone59f57de2017-07-11 19:55:09 -040069 private final Map<DeviceId, ReadWriteLock> deviceLocks = Maps.newConcurrentMap();
Carmelo Casconef7aa3f92017-07-06 23:56:50 -040070
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 Cascone59f57de2017-07-11 19:55:09 -040089 deviceLocks.putIfAbsent(deviceId, new ReentrantReadWriteLock());
90 deviceLocks.get(deviceId).writeLock().lock();
Carmelo Casconef7aa3f92017-07-06 23:56:50 -040091
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 Cascone59f57de2017-07-11 19:55:09 -0400101 deviceLocks.get(deviceId).writeLock().unlock();
Carmelo Casconef7aa3f92017-07-06 23:56:50 -0400102 }
103 }
104
105 private boolean doCreateClient(DeviceId deviceId, int p4DeviceId, ManagedChannelBuilder channelBuilder) {
Carmelo Cascone59f57de2017-07-11 19:55:09 -0400106
Carmelo Casconef7aa3f92017-07-06 23:56:50 -0400107 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 Cascone59f57de2017-07-11 19:55:09 -0400132 deviceLocks.putIfAbsent(deviceId, new ReentrantReadWriteLock());
133 deviceLocks.get(deviceId).readLock().lock();
Carmelo Casconef7aa3f92017-07-06 23:56:50 -0400134
135 try {
136 return clients.get(deviceId);
137 } finally {
Carmelo Cascone59f57de2017-07-11 19:55:09 -0400138 deviceLocks.get(deviceId).readLock().unlock();
Carmelo Casconef7aa3f92017-07-06 23:56:50 -0400139 }
140 }
141
142 @Override
143 public void removeClient(DeviceId deviceId) {
144
Carmelo Cascone59f57de2017-07-11 19:55:09 -0400145 deviceLocks.putIfAbsent(deviceId, new ReentrantReadWriteLock());
146 deviceLocks.get(deviceId).writeLock().lock();
Carmelo Casconef7aa3f92017-07-06 23:56:50 -0400147
148 try {
149 if (clients.containsKey(deviceId)) {
150 clients.get(deviceId).shutdown();
151 grpcController.disconnectChannel(channelIds.get(deviceId));
152 clients.remove(deviceId);
Carmelo Cascone59f57de2017-07-11 19:55:09 -0400153 channelIds.remove(deviceId);
Carmelo Casconef7aa3f92017-07-06 23:56:50 -0400154 }
155 } finally {
Carmelo Cascone59f57de2017-07-11 19:55:09 -0400156 deviceLocks.get(deviceId).writeLock().unlock();
Carmelo Casconef7aa3f92017-07-06 23:56:50 -0400157 }
158 }
159
160 @Override
161 public boolean hasClient(DeviceId deviceId) {
162
Carmelo Cascone59f57de2017-07-11 19:55:09 -0400163 deviceLocks.putIfAbsent(deviceId, new ReentrantReadWriteLock());
164 deviceLocks.get(deviceId).readLock().lock();
Carmelo Casconef7aa3f92017-07-06 23:56:50 -0400165
166 try {
167 return clients.containsKey(deviceId);
168 } finally {
Carmelo Cascone59f57de2017-07-11 19:55:09 -0400169 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 Casconef7aa3f92017-07-06 23:56:50 -0400188 }
189 }
190
191 void postEvent(P4RuntimeEvent event) {
192 post(event);
193 }
194}