blob: c5f90eb5452417a60bae0e7b9da47e5088cc9f5b [file] [log] [blame]
Carmelo Cascone00a59962017-06-16 17:51:49 +09001/*
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.net.pi.impl;
18
19import com.eclipsesource.json.Json;
20import com.google.common.collect.Maps;
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.InputStreamReader;
31import java.nio.ByteBuffer;
32import java.util.Collection;
33import java.util.Map;
34import java.util.Optional;
35
36/**
37 * Mock pipeconf implementation.
38 */
39public class MockPipeconf implements PiPipeconf {
40
41 private static final String PIPECONF_ID = "org.project.pipeconf.default";
42 private static final String JSON_PATH = "/org/onosproject/net/pi/impl/default.json";
43
44 private final PiPipeconfId id;
45 private final PiPipelineModel pipelineModel;
Andrea Campanellabc112a92017-06-26 19:06:43 +020046 protected final Map<Class<? extends Behaviour>, Class<? extends Behaviour>> behaviours;
Carmelo Cascone00a59962017-06-16 17:51:49 +090047
48 public MockPipeconf() throws IOException {
49 this.id = new PiPipeconfId(PIPECONF_ID);
50 this.pipelineModel = loadDefaultModel();
51 this.behaviours = Maps.newHashMap();
52
53 behaviours.put(PiPipelineInterpreter.class, MockInterpreter.class);
54 }
55
56 static PiPipelineModel loadDefaultModel() throws IOException {
57 return Bmv2PipelineModelParser.parse(Json.parse(new BufferedReader(new InputStreamReader(
58 MockPipeconf.class.getResourceAsStream(JSON_PATH)))).asObject());
59 }
60
61 @Override
62 public PiPipeconfId id() {
63 return this.id;
64 }
65
66 @Override
67 public PiPipelineModel pipelineModel() {
68 return pipelineModel;
69 }
70
71 @Override
72 public Collection<Class<? extends Behaviour>> behaviours() {
Andrea Campanellabc112a92017-06-26 19:06:43 +020073 return behaviours.keySet();
Carmelo Cascone00a59962017-06-16 17:51:49 +090074 }
75
76 @Override
77 public Optional<Class<? extends Behaviour>> implementation(Class<? extends Behaviour> behaviour) {
78 return Optional.ofNullable(behaviours.get(behaviour));
79 }
80
81 @Override
82 public boolean hasBehaviour(Class<? extends Behaviour> behaviourClass) {
83 return behaviours.containsKey(behaviourClass);
84 }
85
86 @Override
87 public Optional<ByteBuffer> extension(ExtensionType type) {
88 return Optional.empty();
89 }
90}