blob: a5a5f644ba6224f68b4baf921aa88198a84203af [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
Carmelo Cascone770507f2017-09-14 20:58:04 +020019import org.onosproject.driver.pipeline.DefaultSingleTablePipeline;
20import org.onosproject.net.behaviour.Pipeliner;
21import org.onosproject.net.device.PortStatisticsDiscovery;
22import org.onosproject.net.pi.model.DefaultPiPipeconf;
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;
Carmelo Cascone39c28ca2017-11-15 13:03:57 -080027import org.onosproject.net.pi.service.PiPipeconfService;
Carmelo Cascone87892e22017-11-13 16:01:29 -080028import org.onosproject.p4runtime.model.P4InfoParser;
29import org.onosproject.p4runtime.model.P4InfoParserException;
Carmelo Casconea4dc3c12019-02-12 17:30:00 -080030import org.osgi.service.component.annotations.Activate;
31import org.osgi.service.component.annotations.Component;
32import org.osgi.service.component.annotations.Deactivate;
33import org.osgi.service.component.annotations.Reference;
34import org.osgi.service.component.annotations.ReferenceCardinality;
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 Casconea4dc3c12019-02-12 17:30:00 -080052 private static final URL P4INFO_URL = PipeconfFactory.class.getResource("/mytunnel_p4info.txt");
Carmelo Cascone700648c2018-04-11 12:02:16 -070053 private static final URL BMV2_JSON_URL = PipeconfFactory.class.getResource("/mytunnel.json");
Carmelo Cascone770507f2017-09-14 20:58:04 +020054
Ray Milkeyd84f89b2018-08-17 14:54:17 -070055 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Carmelo Cascone770507f2017-09-14 20:58:04 +020056 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 {
Carmelo Cascone75a9a892019-04-22 12:12:23 -070073 piPipeconfService.unregister(PIPECONF_ID);
Jianwei Maofd9f7442018-05-24 21:36:44 +080074 } 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}