Carmelo Cascone | 00a5996 | 2017-06-16 17:51:49 +0900 | [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.net.pi.impl; |
| 18 | |
| 19 | import com.eclipsesource.json.Json; |
| 20 | import com.google.common.collect.Maps; |
| 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.InputStreamReader; |
| 31 | import java.nio.ByteBuffer; |
| 32 | import java.util.Collection; |
| 33 | import java.util.Map; |
| 34 | import java.util.Optional; |
| 35 | |
| 36 | /** |
| 37 | * Mock pipeconf implementation. |
| 38 | */ |
| 39 | public 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 Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame^] | 46 | protected final Map<Class<? extends Behaviour>, Class<? extends Behaviour>> behaviours; |
Carmelo Cascone | 00a5996 | 2017-06-16 17:51:49 +0900 | [diff] [blame] | 47 | |
| 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 Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame^] | 73 | return behaviours.keySet(); |
Carmelo Cascone | 00a5996 | 2017-06-16 17:51:49 +0900 | [diff] [blame] | 74 | } |
| 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 | } |