blob: f0078ec53aecd1fd3e835ec7474c4c4469a65d5d [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;
Carmelo Cascone770507f2017-09-14 20:58:04 +020024import org.onosproject.driver.pipeline.DefaultSingleTablePipeline;
25import org.onosproject.net.behaviour.Pipeliner;
26import org.onosproject.net.device.PortStatisticsDiscovery;
27import org.onosproject.net.pi.model.DefaultPiPipeconf;
28import org.onosproject.net.pi.model.PiPipeconf;
29import org.onosproject.net.pi.model.PiPipeconfId;
30import org.onosproject.net.pi.model.PiPipelineInterpreter;
31import org.onosproject.net.pi.model.PiPipelineModel;
Carmelo Cascone39c28ca2017-11-15 13:03:57 -080032import org.onosproject.net.pi.service.PiPipeconfService;
Carmelo Cascone87892e22017-11-13 16:01:29 -080033import org.onosproject.p4runtime.model.P4InfoParser;
34import org.onosproject.p4runtime.model.P4InfoParserException;
Jianwei Maofd9f7442018-05-24 21:36:44 +080035import org.slf4j.Logger;
36import org.slf4j.LoggerFactory;
Carmelo Cascone770507f2017-09-14 20:58:04 +020037
38import java.net.URL;
39
40import static org.onosproject.net.pi.model.PiPipeconf.ExtensionType.BMV2_JSON;
41import static org.onosproject.net.pi.model.PiPipeconf.ExtensionType.P4_INFO_TEXT;
42
43/**
44 * Component that produces and registers a pipeconf when loaded.
45 */
46@Component(immediate = true)
47public final class PipeconfFactory {
48
Jianwei Maofd9f7442018-05-24 21:36:44 +080049 private final Logger log = LoggerFactory.getLogger(getClass());
50
Carmelo Cascone770507f2017-09-14 20:58:04 +020051 public static final PiPipeconfId PIPECONF_ID = new PiPipeconfId("p4-tutorial-pipeconf");
Carmelo Cascone700648c2018-04-11 12:02:16 -070052 private static final URL P4INFO_URL = PipeconfFactory.class.getResource("/mytunnel.p4info");
53 private static final URL BMV2_JSON_URL = PipeconfFactory.class.getResource("/mytunnel.json");
Carmelo Cascone770507f2017-09-14 20:58:04 +020054
55 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
56 private PiPipeconfService piPipeconfService;
57
58 @Activate
59 public void activate() {
60 // Registers the pipeconf at component activation.
Jianwei Maofd9f7442018-05-24 21:36:44 +080061 try {
62 piPipeconfService.register(buildPipeconf());
63 } catch (P4InfoParserException e) {
64 log.error("Fail to register {} - Exception: {} - Cause: {}",
65 PIPECONF_ID, e.getMessage(), e.getCause().getMessage());
66 }
Carmelo Cascone770507f2017-09-14 20:58:04 +020067 }
68
69 @Deactivate
70 public void deactivate() {
Jianwei Maofd9f7442018-05-24 21:36:44 +080071 // Unregisters the pipeconf at component deactivation.
72 try {
73 piPipeconfService.remove(PIPECONF_ID);
74 } catch (IllegalStateException e) {
75 log.warn("{} haven't been registered", PIPECONF_ID);
76 }
Carmelo Cascone87892e22017-11-13 16:01:29 -080077 }
78
Jianwei Maofd9f7442018-05-24 21:36:44 +080079 private PiPipeconf buildPipeconf() throws P4InfoParserException {
80
81 final PiPipelineModel pipelineModel = P4InfoParser.parse(P4INFO_URL);
Carmelo Cascone87892e22017-11-13 16:01:29 -080082
83 return DefaultPiPipeconf.builder()
84 .withId(PIPECONF_ID)
85 .withPipelineModel(pipelineModel)
86 .addBehaviour(PiPipelineInterpreter.class, PipelineInterpreterImpl.class)
87 .addBehaviour(PortStatisticsDiscovery.class, PortStatisticsDiscoveryImpl.class)
Carmelo Cascone700648c2018-04-11 12:02:16 -070088 // Since mytunnel.p4 defines only 1 table, we re-use the existing single-table pipeliner.
Carmelo Cascone87892e22017-11-13 16:01:29 -080089 .addBehaviour(Pipeliner.class, DefaultSingleTablePipeline.class)
90 .addExtension(P4_INFO_TEXT, P4INFO_URL)
91 .addExtension(BMV2_JSON, BMV2_JSON_URL)
92 .build();
Carmelo Cascone770507f2017-09-14 20:58:04 +020093 }
94}