blob: 90b5925cb01350123dfb4479e24e210744e93e9b [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;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070020import org.osgi.service.component.annotations.Activate;
21import org.osgi.service.component.annotations.Component;
22import org.osgi.service.component.annotations.Deactivate;
23import org.osgi.service.component.annotations.Reference;
24import org.osgi.service.component.annotations.ReferenceCardinality;
Jonghwan Hyun722275f2018-05-14 15:44:56 -070025import org.onosproject.core.CoreService;
Carmelo Casconeca94bcf2017-10-27 14:16:59 -070026import org.onosproject.driver.pipeline.DefaultSingleTablePipeline;
Jonghwan Hyun722275f2018-05-14 15:44:56 -070027import org.onosproject.inbandtelemetry.api.IntProgrammable;
Carmelo Casconeca94bcf2017-10-27 14:16:59 -070028import org.onosproject.net.behaviour.Pipeliner;
29import org.onosproject.net.device.PortStatisticsDiscovery;
30import org.onosproject.net.pi.model.DefaultPiPipeconf;
31import org.onosproject.net.pi.model.PiPipeconf;
32import org.onosproject.net.pi.model.PiPipeconfId;
33import org.onosproject.net.pi.model.PiPipelineInterpreter;
34import org.onosproject.net.pi.model.PiPipelineModel;
Carmelo Cascone39c28ca2017-11-15 13:03:57 -080035import org.onosproject.net.pi.service.PiPipeconfService;
Carmelo Cascone87892e22017-11-13 16:01:29 -080036import org.onosproject.p4runtime.model.P4InfoParser;
37import org.onosproject.p4runtime.model.P4InfoParserException;
Carmelo Casconeca94bcf2017-10-27 14:16:59 -070038
39import java.net.URL;
40import java.util.Collection;
41
42import static org.onosproject.net.pi.model.PiPipeconf.ExtensionType.BMV2_JSON;
43import static org.onosproject.net.pi.model.PiPipeconf.ExtensionType.P4_INFO_TEXT;
44
45/**
46 * Component that produces and registers the basic pipeconfs when loaded.
47 */
48@Component(immediate = true)
49public final class PipeconfLoader {
50
Jonghwan Hyun722275f2018-05-14 15:44:56 -070051 private static final String APP_NAME = "org.onosproject.pipelines.basic";
Carmelo Casconeca94bcf2017-10-27 14:16:59 -070052 private static final PiPipeconfId BASIC_PIPECONF_ID = new PiPipeconfId("org.onosproject.pipelines.basic");
53 private static final String BASIC_JSON_PATH = "/p4c-out/bmv2/basic.json";
54 private static final String BASIC_P4INFO = "/p4c-out/bmv2/basic.p4info";
55
Carmelo Casconeca94bcf2017-10-27 14:16:59 -070056 public static final PiPipeconf BASIC_PIPECONF = buildBasicPipeconf();
Carmelo Casconeca94bcf2017-10-27 14:16:59 -070057
Jonghwan Hyunb9358822017-12-06 17:55:27 -080058 private static final PiPipeconfId INT_PIPECONF_ID = new PiPipeconfId("org.onosproject.pipelines.int");
59 private static final String INT_JSON_PATH = "/p4c-out/bmv2/int.json";
60 private static final String INT_P4INFO = "/p4c-out/bmv2/int.p4info";
61
62 public static final PiPipeconf INT_PIPECONF = buildIntPipeconf();
63
64 private static final Collection<PiPipeconf> ALL_PIPECONFS = ImmutableList.of(BASIC_PIPECONF, INT_PIPECONF);
Carmelo Casconeca94bcf2017-10-27 14:16:59 -070065
Ray Milkeyd84f89b2018-08-17 14:54:17 -070066 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Carmelo Casconeca94bcf2017-10-27 14:16:59 -070067 private PiPipeconfService piPipeconfService;
68
Ray Milkeyd84f89b2018-08-17 14:54:17 -070069 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Jonghwan Hyun722275f2018-05-14 15:44:56 -070070 private CoreService coreService;
71
Carmelo Casconeca94bcf2017-10-27 14:16:59 -070072 @Activate
73 public void activate() {
Jonghwan Hyun722275f2018-05-14 15:44:56 -070074 coreService.registerApplication(APP_NAME);
Carmelo Casconeca94bcf2017-10-27 14:16:59 -070075 // Registers all pipeconf at component activation.
76 ALL_PIPECONFS.forEach(piPipeconfService::register);
77 }
78
79 @Deactivate
80 public void deactivate() {
81 ALL_PIPECONFS.stream().map(PiPipeconf::id).forEach(piPipeconfService::remove);
82 }
83
84 private static PiPipeconf buildBasicPipeconf() {
85 final URL jsonUrl = PipeconfLoader.class.getResource(BASIC_JSON_PATH);
86 final URL p4InfoUrl = PipeconfLoader.class.getResource(BASIC_P4INFO);
Carmelo Cascone87892e22017-11-13 16:01:29 -080087
Carmelo Casconeca94bcf2017-10-27 14:16:59 -070088 return DefaultPiPipeconf.builder()
89 .withId(BASIC_PIPECONF_ID)
Carmelo Cascone87892e22017-11-13 16:01:29 -080090 .withPipelineModel(parseP4Info(p4InfoUrl))
Carmelo Casconeca94bcf2017-10-27 14:16:59 -070091 .addBehaviour(PiPipelineInterpreter.class, BasicInterpreterImpl.class)
92 .addBehaviour(Pipeliner.class, DefaultSingleTablePipeline.class)
93 .addBehaviour(PortStatisticsDiscovery.class, PortStatisticsDiscoveryImpl.class)
94 .addExtension(P4_INFO_TEXT, p4InfoUrl)
95 .addExtension(BMV2_JSON, jsonUrl)
96 // Put here other target-specific extensions,
97 // e.g. Tofino's bin and context.json.
98 .build();
99 }
100
Jonghwan Hyunb9358822017-12-06 17:55:27 -0800101 private static PiPipeconf buildIntPipeconf() {
102 final URL jsonUrl = PipeconfLoader.class.getResource(INT_JSON_PATH);
103 final URL p4InfoUrl = PipeconfLoader.class.getResource(INT_P4INFO);
104
105 // INT behavior is controlled using pipeline-specific flow rule,
106 // not using flow objectives, so we just borrow pipeliner to basic pipeconf.
107 return DefaultPiPipeconf.builder()
108 .withId(INT_PIPECONF_ID)
109 .withPipelineModel(parseP4Info(p4InfoUrl))
Carmelo Cascone255125d2018-04-11 14:03:22 -0700110 .addBehaviour(PiPipelineInterpreter.class, BasicInterpreterImpl.class)
Jonghwan Hyunb9358822017-12-06 17:55:27 -0800111 .addBehaviour(Pipeliner.class, DefaultSingleTablePipeline.class)
112 .addBehaviour(PortStatisticsDiscovery.class, PortStatisticsDiscoveryImpl.class)
Jonghwan Hyun722275f2018-05-14 15:44:56 -0700113 .addBehaviour(IntProgrammable.class, IntProgrammableImpl.class)
Jonghwan Hyunb9358822017-12-06 17:55:27 -0800114 .addExtension(P4_INFO_TEXT, p4InfoUrl)
115 .addExtension(BMV2_JSON, jsonUrl)
116 .build();
117 }
118
Carmelo Cascone87892e22017-11-13 16:01:29 -0800119 private static PiPipelineModel parseP4Info(URL p4InfoUrl) {
120 try {
121 return P4InfoParser.parse(p4InfoUrl);
122 } catch (P4InfoParserException e) {
Ray Milkey986a47a2018-01-25 11:38:51 -0800123 throw new IllegalStateException(e);
Carmelo Cascone87892e22017-11-13 16:01:29 -0800124 }
125 }
Carmelo Casconeca94bcf2017-10-27 14:16:59 -0700126}