Carmelo Cascone | 59f57de | 2017-07-11 19:55:09 -0400 | [diff] [blame^] | 1 | /* |
| 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 | |
| 17 | package org.onosproject.drivers.bmv2; |
| 18 | |
| 19 | import com.eclipsesource.json.Json; |
| 20 | import com.google.common.collect.ImmutableMap; |
| 21 | import org.onosproject.bmv2.model.Bmv2PipelineModelParser; |
| 22 | import org.onosproject.net.driver.Behaviour; |
| 23 | import org.onosproject.net.pi.model.PiPipeconf; |
| 24 | import org.onosproject.net.pi.model.PiPipeconfId; |
| 25 | import org.onosproject.net.pi.model.PiPipelineInterpreter; |
| 26 | import org.onosproject.net.pi.model.PiPipelineModel; |
| 27 | |
| 28 | import java.io.BufferedReader; |
| 29 | import java.io.IOException; |
| 30 | import java.io.InputStream; |
| 31 | import java.io.InputStreamReader; |
| 32 | import java.util.Collection; |
| 33 | import java.util.Map; |
| 34 | import java.util.Optional; |
| 35 | |
| 36 | /** |
| 37 | * Implementation of the default pipeconf for a BMv2 device. |
| 38 | */ |
| 39 | public final class Bmv2DefaultPipeconf implements PiPipeconf { |
| 40 | |
| 41 | private static final String PIPECONF_ID = "bmv2-default-pipeconf"; |
| 42 | private static final String JSON_PATH = "/default.json"; |
| 43 | private static final String P4INFO_PATH = "/default.p4info"; |
| 44 | |
| 45 | private final PiPipeconfId id; |
| 46 | private final PiPipelineModel pipelineModel; |
| 47 | private final InputStream jsonConfigStream = this.getClass().getResourceAsStream(JSON_PATH); |
| 48 | private final InputStream p4InfoStream = this.getClass().getResourceAsStream(P4INFO_PATH); |
| 49 | private final Map<Class<? extends Behaviour>, Class<? extends Behaviour>> behaviours; |
| 50 | |
| 51 | Bmv2DefaultPipeconf() { |
| 52 | this.id = new PiPipeconfId(PIPECONF_ID); |
| 53 | try { |
| 54 | this.pipelineModel = Bmv2PipelineModelParser.parse( |
| 55 | Json.parse(new BufferedReader(new InputStreamReader(jsonConfigStream))).asObject()); |
| 56 | } catch (IOException e) { |
| 57 | throw new RuntimeException(e); |
| 58 | } |
| 59 | |
| 60 | this.behaviours = ImmutableMap.of( |
| 61 | PiPipelineInterpreter.class, Bmv2DefaultInterpreter.class |
| 62 | // TODO: reuse default single table pipeliner. |
| 63 | ); |
| 64 | } |
| 65 | |
| 66 | @Override |
| 67 | public PiPipeconfId id() { |
| 68 | return this.id; |
| 69 | } |
| 70 | |
| 71 | @Override |
| 72 | public PiPipelineModel pipelineModel() { |
| 73 | return pipelineModel; |
| 74 | } |
| 75 | |
| 76 | @Override |
| 77 | public Collection<Class<? extends Behaviour>> behaviours() { |
| 78 | return behaviours.keySet(); |
| 79 | } |
| 80 | |
| 81 | @Override |
| 82 | public Optional<Class<? extends Behaviour>> implementation(Class<? extends Behaviour> behaviour) { |
| 83 | return Optional.ofNullable(behaviours.get(behaviour)); |
| 84 | } |
| 85 | |
| 86 | @Override |
| 87 | public boolean hasBehaviour(Class<? extends Behaviour> behaviourClass) { |
| 88 | return behaviours.containsKey(behaviourClass); |
| 89 | } |
| 90 | |
| 91 | @Override |
| 92 | public Optional<InputStream> extension(ExtensionType type) { |
| 93 | |
| 94 | switch (type) { |
| 95 | case BMV2_JSON: |
| 96 | return Optional.of(jsonConfigStream); |
| 97 | case P4_INFO_TEXT: |
| 98 | return Optional.of(p4InfoStream); |
| 99 | default: |
| 100 | return Optional.empty(); |
| 101 | } |
| 102 | } |
| 103 | } |