blob: b284eaaa85475ca884947c2bad466837f50b54cb [file] [log] [blame]
Carmelo Cascone770507f2017-09-14 20:58:04 +02001/*
2 * Copyright 2017-present Open Networking Foundation
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.p4tutorial.pipeconf;
18
19import org.apache.felix.scr.annotations.Activate;
20import org.apache.felix.scr.annotations.Component;
21import org.apache.felix.scr.annotations.Deactivate;
22import org.apache.felix.scr.annotations.Reference;
23import org.apache.felix.scr.annotations.ReferenceCardinality;
24import org.onosproject.bmv2.model.Bmv2PipelineModelParser;
25import org.onosproject.driver.pipeline.DefaultSingleTablePipeline;
26import org.onosproject.net.behaviour.Pipeliner;
27import org.onosproject.net.device.PortStatisticsDiscovery;
28import org.onosproject.net.pi.model.DefaultPiPipeconf;
29import org.onosproject.net.pi.model.PiPipeconf;
30import org.onosproject.net.pi.model.PiPipeconfId;
31import org.onosproject.net.pi.model.PiPipelineInterpreter;
32import org.onosproject.net.pi.model.PiPipelineModel;
33import org.onosproject.net.pi.runtime.PiPipeconfService;
34
35import java.net.URL;
36
37import static org.onosproject.net.pi.model.PiPipeconf.ExtensionType.BMV2_JSON;
38import static org.onosproject.net.pi.model.PiPipeconf.ExtensionType.P4_INFO_TEXT;
39
40/**
41 * Component that produces and registers a pipeconf when loaded.
42 */
43@Component(immediate = true)
44public final class PipeconfFactory {
45
46 public static final PiPipeconfId PIPECONF_ID = new PiPipeconfId("p4-tutorial-pipeconf");
47 private static final URL P4INFO_URL = PipeconfFactory.class.getResource("/main.p4info");
48 private static final URL BMV2_JSON_URL = PipeconfFactory.class.getResource("/main.json");
49 private static final PiPipelineModel PIPELINE_MODEL = Bmv2PipelineModelParser.parse(BMV2_JSON_URL);
50
51 private static final PiPipeconf PIPECONF = DefaultPiPipeconf.builder()
52 .withId(PIPECONF_ID)
53 .withPipelineModel(PIPELINE_MODEL)
54 .addBehaviour(PiPipelineInterpreter.class, PipelineInterpreterImpl.class)
55 .addBehaviour(PortStatisticsDiscovery.class, PortStatisticsDiscoveryImpl.class)
56 // Since main.p4 defines only 1 table, we re-use the existing single-table pipeliner.
57 .addBehaviour(Pipeliner.class, DefaultSingleTablePipeline.class)
58 .addExtension(P4_INFO_TEXT, P4INFO_URL)
59 .addExtension(BMV2_JSON, BMV2_JSON_URL)
60 .build();
61
62 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
63 private PiPipeconfService piPipeconfService;
64
65 @Activate
66 public void activate() {
67 // Registers the pipeconf at component activation.
68 piPipeconfService.register(PIPECONF);
69 }
70
71 @Deactivate
72 public void deactivate() {
73 piPipeconfService.remove(PIPECONF.id());
74 }
75}