blob: 8a5a2dd3238d09ac1246bfca45563dd522cc06bf [file] [log] [blame]
Carmelo Casconee3a7c742017-09-01 01:25:52 +02001/*
2 * Copyright 2017-present Open Networking Foundation
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.drivers.p4runtime;
18
19import io.grpc.ManagedChannelBuilder;
20import io.grpc.netty.NettyChannelBuilder;
21import org.onosproject.net.Device;
22import org.onosproject.net.DeviceId;
23import org.onosproject.net.device.DeviceService;
24import org.onosproject.net.driver.AbstractHandlerBehaviour;
25import org.onosproject.net.pi.model.PiPipeconf;
26import org.onosproject.net.pi.runtime.PiPipeconfService;
27import org.onosproject.p4runtime.api.P4RuntimeClient;
28import org.onosproject.p4runtime.api.P4RuntimeController;
29import org.slf4j.Logger;
30import org.slf4j.LoggerFactory;
31
32/**
33 * Abstract implementation of a behaviour handler for a P4Runtime device.
34 */
35public class AbstractP4RuntimeHandlerBehaviour extends AbstractHandlerBehaviour {
36
37 public static final String P4RUNTIME_SERVER_ADDR_KEY = "p4runtime_ip";
38 public static final String P4RUNTIME_SERVER_PORT_KEY = "p4runtime_port";
39 public static final String P4RUNTIME_DEVICE_ID_KEY = "p4runtime_deviceId";
40
41 protected final Logger log = LoggerFactory.getLogger(getClass());
42
43 // Initialized by setupBehaviour()
44 protected DeviceId deviceId;
45 protected DeviceService deviceService;
46 protected Device device;
47 protected P4RuntimeController controller;
48 protected PiPipeconf pipeconf;
49 protected P4RuntimeClient client;
50
51 /**
52 * Initializes this behaviour attributes. Returns true if the operation was successful, false otherwise. This method
53 * assumes that the P4runtime controller already has a client for this device and that the device has been created
54 * in the core.
55 *
56 * @return true if successful, false otherwise
57 */
58 protected boolean setupBehaviour() {
59 deviceId = handler().data().deviceId();
60
61 deviceService = handler().get(DeviceService.class);
62 device = deviceService.getDevice(deviceId);
63 if (device == null) {
64 log.warn("Unable to find device with id {}, aborting operation", deviceId);
65 return false;
66 }
67
68 controller = handler().get(P4RuntimeController.class);
69 if (!controller.hasClient(deviceId)) {
70 log.warn("Unable to find client for {}, aborting operation", deviceId);
71 return false;
72 }
73 client = controller.getClient(deviceId);
74
75 PiPipeconfService piPipeconfService = handler().get(PiPipeconfService.class);
76 if (!piPipeconfService.ofDevice(deviceId).isPresent() ||
77 !piPipeconfService.getPipeconf(piPipeconfService.ofDevice(deviceId).get()).isPresent()) {
78 log.warn("Unable to get the pipeconf of {}, aborting operation", deviceId);
79 return false;
80 }
81 pipeconf = piPipeconfService.getPipeconf(piPipeconfService.ofDevice(deviceId).get()).get();
82
83 return true;
84 }
85
86 /**
87 * Create a P4Runtime client for this device. Returns true if the operation was successful, false otherwise.
88 *
89 * @return true if successful, false otherwise
90 */
91 protected boolean createClient() {
92 deviceId = handler().data().deviceId();
93 controller = handler().get(P4RuntimeController.class);
94
95 String serverAddr = this.data().value(P4RUNTIME_SERVER_ADDR_KEY);
96 String serverPortString = this.data().value(P4RUNTIME_SERVER_PORT_KEY);
97 String p4DeviceIdString = this.data().value(P4RUNTIME_DEVICE_ID_KEY);
98
99 if (serverAddr == null || serverPortString == null || p4DeviceIdString == null) {
100 log.warn("Unable to create client for {}, missing driver data key (required is {}, {}, and {})",
101 deviceId, P4RUNTIME_SERVER_ADDR_KEY, P4RUNTIME_SERVER_PORT_KEY, P4RUNTIME_DEVICE_ID_KEY);
102 return false;
103 }
104
105 ManagedChannelBuilder channelBuilder = NettyChannelBuilder
106 .forAddress(serverAddr, Integer.valueOf(serverPortString))
107 .usePlaintext(true);
108
109 if (!controller.createClient(deviceId, Long.parseUnsignedLong(p4DeviceIdString), channelBuilder)) {
110 log.warn("Unable to create client for {}, aborting operation", deviceId);
111 return false;
112 }
113
114 return true;
115 }
116}