Carmelo Cascone | 59f57de | 2017-07-11 19:55:09 -0400 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Carmelo Cascone | 59f57de | 2017-07-11 19:55:09 -0400 | [diff] [blame] | 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.drivers.bmv2; |
| 18 | |
Carmelo Cascone | d61fdb3 | 2017-10-30 10:09:57 -0700 | [diff] [blame] | 19 | import org.apache.commons.io.IOUtils; |
| 20 | import org.onosproject.drivers.p4runtime.AbstractP4RuntimePipelineProgrammable; |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 21 | import org.onosproject.net.behaviour.PiPipelineProgrammable; |
Carmelo Cascone | 59f57de | 2017-07-11 19:55:09 -0400 | [diff] [blame] | 22 | import org.onosproject.net.pi.model.PiPipeconf; |
Carmelo Cascone | ca94bcf | 2017-10-27 14:16:59 -0700 | [diff] [blame] | 23 | import org.onosproject.pipelines.basic.PipeconfLoader; |
Carmelo Cascone | 59f57de | 2017-07-11 19:55:09 -0400 | [diff] [blame] | 24 | |
Carmelo Cascone | d61fdb3 | 2017-10-30 10:09:57 -0700 | [diff] [blame] | 25 | import java.io.IOException; |
| 26 | import java.nio.ByteBuffer; |
Carmelo Cascone | 59f57de | 2017-07-11 19:55:09 -0400 | [diff] [blame] | 27 | import java.util.Optional; |
Carmelo Cascone | 59f57de | 2017-07-11 19:55:09 -0400 | [diff] [blame] | 28 | |
| 29 | import static org.onosproject.net.pi.model.PiPipeconf.ExtensionType.BMV2_JSON; |
Carmelo Cascone | 59f57de | 2017-07-11 19:55:09 -0400 | [diff] [blame] | 30 | |
| 31 | /** |
Carmelo Cascone | d61fdb3 | 2017-10-30 10:09:57 -0700 | [diff] [blame] | 32 | * Implementation of the PiPipelineProgrammable behavior for BMv2. |
Carmelo Cascone | 59f57de | 2017-07-11 19:55:09 -0400 | [diff] [blame] | 33 | */ |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 34 | public class Bmv2PipelineProgrammable extends AbstractP4RuntimePipelineProgrammable |
| 35 | implements PiPipelineProgrammable { |
Carmelo Cascone | 59f57de | 2017-07-11 19:55:09 -0400 | [diff] [blame] | 36 | |
| 37 | @Override |
Carmelo Cascone | d61fdb3 | 2017-10-30 10:09:57 -0700 | [diff] [blame] | 38 | public ByteBuffer createDeviceDataBuffer(PiPipeconf pipeconf) { |
| 39 | if (!pipeconf.extension(BMV2_JSON).isPresent()) { |
| 40 | log.warn("Missing extension {} in pipeconf {}", BMV2_JSON, pipeconf.id()); |
| 41 | return null; |
Carmelo Cascone | 59f57de | 2017-07-11 19:55:09 -0400 | [diff] [blame] | 42 | } |
Carmelo Cascone | 59f57de | 2017-07-11 19:55:09 -0400 | [diff] [blame] | 43 | try { |
Carmelo Cascone | d61fdb3 | 2017-10-30 10:09:57 -0700 | [diff] [blame] | 44 | return ByteBuffer.wrap(IOUtils.toByteArray(pipeconf.extension(BMV2_JSON).get())); |
| 45 | } catch (IOException e) { |
| 46 | log.warn("Unable to read {} from pipeconf {}", BMV2_JSON, pipeconf.id()); |
| 47 | return null; |
Carmelo Cascone | 59f57de | 2017-07-11 19:55:09 -0400 | [diff] [blame] | 48 | } |
Carmelo Cascone | 59f57de | 2017-07-11 19:55:09 -0400 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | @Override |
| 52 | public Optional<PiPipeconf> getDefaultPipeconf() { |
Carmelo Cascone | ca94bcf | 2017-10-27 14:16:59 -0700 | [diff] [blame] | 53 | return Optional.of(PipeconfLoader.BASIC_PIPECONF); |
Carmelo Cascone | 59f57de | 2017-07-11 19:55:09 -0400 | [diff] [blame] | 54 | } |
| 55 | } |