blob: 6c86d615e2b221fce23dd5cc3a0ecda24ef80345 [file] [log] [blame]
Yi Tsengf33c0772017-06-06 14:56:18 -07001/*
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 */
16package org.onosproject.bmv2.model;
17
18import com.google.common.collect.ImmutableSet;
19import com.google.common.collect.Maps;
20import org.onosproject.net.driver.Behaviour;
21import org.onosproject.net.pi.model.PiPipeconf;
22import org.onosproject.net.pi.model.PiPipeconfId;
23import org.onosproject.net.pi.model.PiPipelineModel;
24
25import java.nio.ByteBuffer;
26import java.util.Collection;
27import java.util.Map;
28import java.util.Optional;
29import java.util.Set;
30
31import static com.google.common.base.Preconditions.checkNotNull;
32
33/**
34 * BMv2 pipeline configuration (pipeconf).
35 */
36public final class Bmv2Pipeconf implements PiPipeconf {
37 private final PiPipeconfId id;
38 private final Bmv2PipelineModel pipelineModel;
39 private final Set<Class<? extends Behaviour>> behaviours;
40 private final Map<ExtensionType, ByteBuffer> extensions;
41
42 /**
43 * Builds a new BMv2 pipeline configuration (pipeconf) by given information.
44 *
45 * @param id the pipeconf id
46 * @param pipelineModel the pipeline model
47 * @param behaviours the behaviors of the pipeline
48 * @param extensions the extensions of the pipeline
49 */
50 public Bmv2Pipeconf(PiPipeconfId id,
51 Bmv2PipelineModel pipelineModel,
52 Set<Class<? extends Behaviour>> behaviours,
53 Map<ExtensionType, ByteBuffer> extensions) {
54 checkNotNull(id, "Pipeconf Id can't be null");
55 checkNotNull(pipelineModel, "Pipeline model can't be null");
56
57 this.id = id;
58 this.pipelineModel = pipelineModel;
59 this.behaviours = behaviours == null ? ImmutableSet.of() : behaviours;
60 this.extensions = extensions == null ? Maps.newHashMap() : extensions;
61 }
62
63 @Override
64 public PiPipeconfId id() {
65 return id;
66 }
67
68 @Override
69 public PiPipelineModel pipelineModel() {
70 return pipelineModel;
71 }
72
73 @Override
74 public Collection<Class<? extends Behaviour>> behaviours() {
75 return behaviours;
76 }
77
78 @Override
79 public Optional<Class<? extends Behaviour>> implementation(Class<? extends Behaviour> behaviour) {
80 return behaviours.stream()
81 .filter(behaviour::isAssignableFrom)
82 .findAny();
83 }
84
85 @Override
86 public boolean hasBehaviour(Class<? extends Behaviour> behaviourClass) {
87 return behaviours.stream()
88 .anyMatch(behaviourClass::isAssignableFrom);
89 }
90
91 @Override
92 public Optional<ByteBuffer> extension(ExtensionType type) {
93 return Optional.ofNullable(extensions.get(type));
94 }
95}