blob: 277af11ef5b50545a902fefbcfbb19d409917077 [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;
Carmelo Casconef7aa3f92017-07-06 23:56:50 -040047import static org.slf4j.LoggerFactory.getLogger;
48
49/**
50 * P4Runtime controller implementation.
51 */
52@Component(immediate = true)
53@Service
54public class P4RuntimeControllerImpl
55 extends AbstractListenerManager<P4RuntimeEvent, P4RuntimeEventListener>
56 implements P4RuntimeController {
57
Carmelo Casconef7aa3f92017-07-06 23:56:50 -040058 private final Logger log = getLogger(getClass());
Carmelo Casconef7aa3f92017-07-06 23:56:50 -040059 private final NameResolverProvider nameResolverProvider = new DnsNameResolverProvider();
Carmelo Cascone59f57de2017-07-11 19:55:09 -040060 private final Map<DeviceId, P4RuntimeClient> clients = Maps.newHashMap();
61 private final Map<DeviceId, GrpcChannelId> channelIds = Maps.newHashMap();
Carmelo Casconef7aa3f92017-07-06 23:56:50 -040062 // TODO: should use a cache to delete unused locks.
Carmelo Cascone59f57de2017-07-11 19:55:09 -040063 private final Map<DeviceId, ReadWriteLock> deviceLocks = Maps.newConcurrentMap();
Carmelo Casconef7aa3f92017-07-06 23:56:50 -040064
Carmelo Cascone8d99b172017-07-18 17:26:31 -040065 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Carmelo Casconec8e84982017-07-26 15:34:42 -040066 public GrpcController grpcController;
Carmelo Cascone8d99b172017-07-18 17:26:31 -040067
Carmelo Casconef7aa3f92017-07-06 23:56:50 -040068 @Activate
69 public void activate() {
Carmelo Cascone2cad9ef2017-08-01 21:52:07 +020070 eventDispatcher.addSink(P4RuntimeEvent.class, listenerRegistry);
Carmelo Casconef7aa3f92017-07-06 23:56:50 -040071 log.info("Started");
72 }
73
74
75 @Deactivate
76 public void deactivate() {
77 grpcController = null;
Carmelo Cascone2cad9ef2017-08-01 21:52:07 +020078 eventDispatcher.removeSink(P4RuntimeEvent.class);
Carmelo Casconef7aa3f92017-07-06 23:56:50 -040079 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
Carmelo Cascone59f57de2017-07-11 19:55:09 -040088 deviceLocks.putIfAbsent(deviceId, new ReentrantReadWriteLock());
89 deviceLocks.get(deviceId).writeLock().lock();
Carmelo Casconef7aa3f92017-07-06 23:56:50 -040090
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 {
Carmelo Cascone59f57de2017-07-11 19:55:09 -0400100 deviceLocks.get(deviceId).writeLock().unlock();
Carmelo Casconef7aa3f92017-07-06 23:56:50 -0400101 }
102 }
103
104 private boolean doCreateClient(DeviceId deviceId, int p4DeviceId, ManagedChannelBuilder channelBuilder) {
Carmelo Cascone59f57de2017-07-11 19:55:09 -0400105
Carmelo Casconef7aa3f92017-07-06 23:56:50 -0400106 GrpcChannelId channelId = GrpcChannelId.of(deviceId, "p4runtime");
107
108 // Channel defaults.
109 channelBuilder.nameResolverFactory(nameResolverProvider);
110
111 ManagedChannel channel;
112 try {
113 channel = grpcController.connectChannel(channelId, channelBuilder);
114 } catch (IOException e) {
115 log.warn("Unable to connect to gRPC server of {}: {}", deviceId, e.getMessage());
116 return false;
117 }
118
Carmelo Cascone8d99b172017-07-18 17:26:31 -0400119 P4RuntimeClient client = new P4RuntimeClientImpl(deviceId, p4DeviceId, channel, this);
Carmelo Casconef7aa3f92017-07-06 23:56:50 -0400120
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
Carmelo Cascone59f57de2017-07-11 19:55:09 -0400130 deviceLocks.putIfAbsent(deviceId, new ReentrantReadWriteLock());
131 deviceLocks.get(deviceId).readLock().lock();
Carmelo Casconef7aa3f92017-07-06 23:56:50 -0400132
133 try {
134 return clients.get(deviceId);
135 } finally {
Carmelo Cascone59f57de2017-07-11 19:55:09 -0400136 deviceLocks.get(deviceId).readLock().unlock();
Carmelo Casconef7aa3f92017-07-06 23:56:50 -0400137 }
138 }
139
140 @Override
141 public void removeClient(DeviceId deviceId) {
142
Carmelo Cascone59f57de2017-07-11 19:55:09 -0400143 deviceLocks.putIfAbsent(deviceId, new ReentrantReadWriteLock());
144 deviceLocks.get(deviceId).writeLock().lock();
Carmelo Casconef7aa3f92017-07-06 23:56:50 -0400145
146 try {
147 if (clients.containsKey(deviceId)) {
148 clients.get(deviceId).shutdown();
149 grpcController.disconnectChannel(channelIds.get(deviceId));
150 clients.remove(deviceId);
Carmelo Cascone59f57de2017-07-11 19:55:09 -0400151 channelIds.remove(deviceId);
Carmelo Casconef7aa3f92017-07-06 23:56:50 -0400152 }
153 } finally {
Carmelo Cascone59f57de2017-07-11 19:55:09 -0400154 deviceLocks.get(deviceId).writeLock().unlock();
Carmelo Casconef7aa3f92017-07-06 23:56:50 -0400155 }
156 }
157
158 @Override
159 public boolean hasClient(DeviceId deviceId) {
160
Carmelo Cascone59f57de2017-07-11 19:55:09 -0400161 deviceLocks.putIfAbsent(deviceId, new ReentrantReadWriteLock());
162 deviceLocks.get(deviceId).readLock().lock();
Carmelo Casconef7aa3f92017-07-06 23:56:50 -0400163
164 try {
165 return clients.containsKey(deviceId);
166 } finally {
Carmelo Cascone59f57de2017-07-11 19:55:09 -0400167 deviceLocks.get(deviceId).readLock().unlock();
168 }
169 }
170
171 @Override
172 public boolean isReacheable(DeviceId deviceId) {
173
174 deviceLocks.putIfAbsent(deviceId, new ReentrantReadWriteLock());
175 deviceLocks.get(deviceId).readLock().lock();
176
177 try {
178 if (!clients.containsKey(deviceId)) {
179 log.warn("No client for {}, can't check for reachability", deviceId);
180 return false;
181 }
182
183 return grpcController.isChannelOpen(channelIds.get(deviceId));
184 } finally {
185 deviceLocks.get(deviceId).readLock().unlock();
Carmelo Casconef7aa3f92017-07-06 23:56:50 -0400186 }
187 }
188
189 void postEvent(P4RuntimeEvent event) {
190 post(event);
191 }
192}