blob: 4c2364c2782edeffa3fea915e35bb4929cc20e62 [file] [log] [blame]
Carmelo Casconeca94bcf2017-10-27 14:16:59 -07001/*
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.pipelines.basic;
18
19import com.google.common.collect.ImmutableList;
20import org.apache.felix.scr.annotations.Activate;
21import org.apache.felix.scr.annotations.Component;
22import org.apache.felix.scr.annotations.Deactivate;
23import org.apache.felix.scr.annotations.Reference;
24import org.apache.felix.scr.annotations.ReferenceCardinality;
Carmelo Casconeca94bcf2017-10-27 14:16:59 -070025import 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;
Carmelo Cascone39c28ca2017-11-15 13:03:57 -080033import org.onosproject.net.pi.service.PiPipeconfService;
Carmelo Cascone87892e22017-11-13 16:01:29 -080034import org.onosproject.p4runtime.model.P4InfoParser;
35import org.onosproject.p4runtime.model.P4InfoParserException;
Carmelo Casconeca94bcf2017-10-27 14:16:59 -070036
37import java.net.URL;
38import java.util.Collection;
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 the basic pipeconfs when loaded.
45 */
46@Component(immediate = true)
47public final class PipeconfLoader {
48
49 private static final PiPipeconfId BASIC_PIPECONF_ID = new PiPipeconfId("org.onosproject.pipelines.basic");
50 private static final String BASIC_JSON_PATH = "/p4c-out/bmv2/basic.json";
51 private static final String BASIC_P4INFO = "/p4c-out/bmv2/basic.p4info";
52
Carmelo Casconeca94bcf2017-10-27 14:16:59 -070053 public static final PiPipeconf BASIC_PIPECONF = buildBasicPipeconf();
Carmelo Casconeca94bcf2017-10-27 14:16:59 -070054
Jonghwan Hyunb9358822017-12-06 17:55:27 -080055 private static final PiPipeconfId INT_PIPECONF_ID = new PiPipeconfId("org.onosproject.pipelines.int");
56 private static final String INT_JSON_PATH = "/p4c-out/bmv2/int.json";
57 private static final String INT_P4INFO = "/p4c-out/bmv2/int.p4info";
58
59 public static final PiPipeconf INT_PIPECONF = buildIntPipeconf();
60
61 private static final Collection<PiPipeconf> ALL_PIPECONFS = ImmutableList.of(BASIC_PIPECONF, INT_PIPECONF);
Carmelo Casconeca94bcf2017-10-27 14:16:59 -070062
63 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
64 private PiPipeconfService piPipeconfService;
65
66 @Activate
67 public void activate() {
68 // Registers all pipeconf at component activation.
69 ALL_PIPECONFS.forEach(piPipeconfService::register);
70 }
71
72 @Deactivate
73 public void deactivate() {
74 ALL_PIPECONFS.stream().map(PiPipeconf::id).forEach(piPipeconfService::remove);
75 }
76
77 private static PiPipeconf buildBasicPipeconf() {
78 final URL jsonUrl = PipeconfLoader.class.getResource(BASIC_JSON_PATH);
79 final URL p4InfoUrl = PipeconfLoader.class.getResource(BASIC_P4INFO);
Carmelo Cascone87892e22017-11-13 16:01:29 -080080
Carmelo Casconeca94bcf2017-10-27 14:16:59 -070081 return DefaultPiPipeconf.builder()
82 .withId(BASIC_PIPECONF_ID)
Carmelo Cascone87892e22017-11-13 16:01:29 -080083 .withPipelineModel(parseP4Info(p4InfoUrl))
Carmelo Casconeca94bcf2017-10-27 14:16:59 -070084 .addBehaviour(PiPipelineInterpreter.class, BasicInterpreterImpl.class)
85 .addBehaviour(Pipeliner.class, DefaultSingleTablePipeline.class)
86 .addBehaviour(PortStatisticsDiscovery.class, PortStatisticsDiscoveryImpl.class)
87 .addExtension(P4_INFO_TEXT, p4InfoUrl)
88 .addExtension(BMV2_JSON, jsonUrl)
89 // Put here other target-specific extensions,
90 // e.g. Tofino's bin and context.json.
91 .build();
92 }
93
Jonghwan Hyunb9358822017-12-06 17:55:27 -080094 private static PiPipeconf buildIntPipeconf() {
95 final URL jsonUrl = PipeconfLoader.class.getResource(INT_JSON_PATH);
96 final URL p4InfoUrl = PipeconfLoader.class.getResource(INT_P4INFO);
97
98 // INT behavior is controlled using pipeline-specific flow rule,
99 // not using flow objectives, so we just borrow pipeliner to basic pipeconf.
100 return DefaultPiPipeconf.builder()
101 .withId(INT_PIPECONF_ID)
102 .withPipelineModel(parseP4Info(p4InfoUrl))
Carmelo Cascone255125d2018-04-11 14:03:22 -0700103 .addBehaviour(PiPipelineInterpreter.class, BasicInterpreterImpl.class)
Jonghwan Hyunb9358822017-12-06 17:55:27 -0800104 .addBehaviour(Pipeliner.class, DefaultSingleTablePipeline.class)
105 .addBehaviour(PortStatisticsDiscovery.class, PortStatisticsDiscoveryImpl.class)
106 .addExtension(P4_INFO_TEXT, p4InfoUrl)
107 .addExtension(BMV2_JSON, jsonUrl)
108 .build();
109 }
110
Carmelo Cascone87892e22017-11-13 16:01:29 -0800111 private static PiPipelineModel parseP4Info(URL p4InfoUrl) {
112 try {
113 return P4InfoParser.parse(p4InfoUrl);
114 } catch (P4InfoParserException e) {
Ray Milkey986a47a2018-01-25 11:38:51 -0800115 throw new IllegalStateException(e);
Carmelo Cascone87892e22017-11-13 16:01:29 -0800116 }
117 }
Carmelo Casconeca94bcf2017-10-27 14:16:59 -0700118}