blob: cd4151c980ee2cfc5827e2cec187439cb3c39563 [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;
42import java.util.concurrent.locks.Lock;
43import java.util.concurrent.locks.ReentrantLock;
44
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();
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}