blob: ac2dbd7052e39cc165b8d54d9e5727b6336c808b [file] [log] [blame]
Carmelo Cascone59f57de2017-07-11 19:55:09 -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.drivers.bmv2;
18
19import com.eclipsesource.json.Json;
20import com.google.common.collect.ImmutableMap;
21import org.onosproject.bmv2.model.Bmv2PipelineModelParser;
22import org.onosproject.net.driver.Behaviour;
23import org.onosproject.net.pi.model.PiPipeconf;
24import org.onosproject.net.pi.model.PiPipeconfId;
25import org.onosproject.net.pi.model.PiPipelineInterpreter;
26import org.onosproject.net.pi.model.PiPipelineModel;
27
28import java.io.BufferedReader;
29import java.io.IOException;
30import java.io.InputStream;
31import java.io.InputStreamReader;
32import java.util.Collection;
33import java.util.Map;
34import java.util.Optional;
35
36/**
37 * Implementation of the default pipeconf for a BMv2 device.
38 */
39public 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}