blob: 02dcd294146a273fd1b63b00ef4223ddf22da956 [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;
Jonghwan Hyun722275f2018-05-14 15:44:56 -070020import org.onosproject.core.CoreService;
Jonghwan Hyun722275f2018-05-14 15:44:56 -070021import org.onosproject.inbandtelemetry.api.IntProgrammable;
Carmelo Casconeca94bcf2017-10-27 14:16:59 -070022import org.onosproject.net.behaviour.Pipeliner;
23import org.onosproject.net.device.PortStatisticsDiscovery;
24import org.onosproject.net.pi.model.DefaultPiPipeconf;
25import org.onosproject.net.pi.model.PiPipeconf;
26import org.onosproject.net.pi.model.PiPipeconfId;
27import org.onosproject.net.pi.model.PiPipelineInterpreter;
28import org.onosproject.net.pi.model.PiPipelineModel;
Carmelo Cascone39c28ca2017-11-15 13:03:57 -080029import org.onosproject.net.pi.service.PiPipeconfService;
Carmelo Cascone87892e22017-11-13 16:01:29 -080030import org.onosproject.p4runtime.model.P4InfoParser;
31import org.onosproject.p4runtime.model.P4InfoParserException;
Carmelo Casconea4dc3c12019-02-12 17:30:00 -080032import org.osgi.service.component.annotations.Activate;
33import org.osgi.service.component.annotations.Component;
34import org.osgi.service.component.annotations.Deactivate;
35import org.osgi.service.component.annotations.Reference;
36import org.osgi.service.component.annotations.ReferenceCardinality;
Carmelo Casconeca94bcf2017-10-27 14:16:59 -070037
38import java.net.URL;
39import java.util.Collection;
40
41import static org.onosproject.net.pi.model.PiPipeconf.ExtensionType.BMV2_JSON;
42import static org.onosproject.net.pi.model.PiPipeconf.ExtensionType.P4_INFO_TEXT;
43
44/**
45 * Component that produces and registers the basic pipeconfs when loaded.
46 */
47@Component(immediate = true)
48public final class PipeconfLoader {
49
Jonghwan Hyun722275f2018-05-14 15:44:56 -070050 private static final String APP_NAME = "org.onosproject.pipelines.basic";
Carmelo Casconeca94bcf2017-10-27 14:16:59 -070051 private static final PiPipeconfId BASIC_PIPECONF_ID = new PiPipeconfId("org.onosproject.pipelines.basic");
52 private static final String BASIC_JSON_PATH = "/p4c-out/bmv2/basic.json";
Carmelo Casconea4dc3c12019-02-12 17:30:00 -080053 private static final String BASIC_P4INFO = "/p4c-out/bmv2/basic_p4info.txt";
Carmelo Casconeca94bcf2017-10-27 14:16:59 -070054
Carmelo Casconeca94bcf2017-10-27 14:16:59 -070055 public static final PiPipeconf BASIC_PIPECONF = buildBasicPipeconf();
Carmelo Casconeca94bcf2017-10-27 14:16:59 -070056
Jonghwan Hyunb9358822017-12-06 17:55:27 -080057 private static final PiPipeconfId INT_PIPECONF_ID = new PiPipeconfId("org.onosproject.pipelines.int");
58 private static final String INT_JSON_PATH = "/p4c-out/bmv2/int.json";
Carmelo Casconea4dc3c12019-02-12 17:30:00 -080059 private static final String INT_P4INFO = "/p4c-out/bmv2/int_p4info.txt";
Jonghwan Hyunb9358822017-12-06 17:55:27 -080060
61 public static final PiPipeconf INT_PIPECONF = buildIntPipeconf();
62
63 private static final Collection<PiPipeconf> ALL_PIPECONFS = ImmutableList.of(BASIC_PIPECONF, INT_PIPECONF);
Carmelo Casconeca94bcf2017-10-27 14:16:59 -070064
Ray Milkeyd84f89b2018-08-17 14:54:17 -070065 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Carmelo Casconeca94bcf2017-10-27 14:16:59 -070066 private PiPipeconfService piPipeconfService;
67
Ray Milkeyd84f89b2018-08-17 14:54:17 -070068 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Jonghwan Hyun722275f2018-05-14 15:44:56 -070069 private CoreService coreService;
70
Carmelo Casconeca94bcf2017-10-27 14:16:59 -070071 @Activate
72 public void activate() {
Jonghwan Hyun722275f2018-05-14 15:44:56 -070073 coreService.registerApplication(APP_NAME);
Carmelo Casconeca94bcf2017-10-27 14:16:59 -070074 // Registers all pipeconf at component activation.
75 ALL_PIPECONFS.forEach(piPipeconfService::register);
76 }
77
78 @Deactivate
79 public void deactivate() {
Carmelo Cascone75a9a892019-04-22 12:12:23 -070080 ALL_PIPECONFS.stream().map(PiPipeconf::id).forEach(piPipeconfService::unregister);
Carmelo Casconeca94bcf2017-10-27 14:16:59 -070081 }
82
83 private static PiPipeconf buildBasicPipeconf() {
84 final URL jsonUrl = PipeconfLoader.class.getResource(BASIC_JSON_PATH);
85 final URL p4InfoUrl = PipeconfLoader.class.getResource(BASIC_P4INFO);
Carmelo Cascone87892e22017-11-13 16:01:29 -080086
Carmelo Casconeca94bcf2017-10-27 14:16:59 -070087 return DefaultPiPipeconf.builder()
88 .withId(BASIC_PIPECONF_ID)
Carmelo Cascone87892e22017-11-13 16:01:29 -080089 .withPipelineModel(parseP4Info(p4InfoUrl))
Carmelo Casconeca94bcf2017-10-27 14:16:59 -070090 .addBehaviour(PiPipelineInterpreter.class, BasicInterpreterImpl.class)
Carmelo Cascone776be382018-12-12 19:03:57 -080091 .addBehaviour(Pipeliner.class, BasicPipelinerImpl.class)
Carmelo Casconeca94bcf2017-10-27 14:16:59 -070092 .addBehaviour(PortStatisticsDiscovery.class, PortStatisticsDiscoveryImpl.class)
93 .addExtension(P4_INFO_TEXT, p4InfoUrl)
94 .addExtension(BMV2_JSON, jsonUrl)
95 // Put here other target-specific extensions,
96 // e.g. Tofino's bin and context.json.
97 .build();
98 }
99
Jonghwan Hyunb9358822017-12-06 17:55:27 -0800100 private static PiPipeconf buildIntPipeconf() {
101 final URL jsonUrl = PipeconfLoader.class.getResource(INT_JSON_PATH);
102 final URL p4InfoUrl = PipeconfLoader.class.getResource(INT_P4INFO);
103
104 // INT behavior is controlled using pipeline-specific flow rule,
105 // not using flow objectives, so we just borrow pipeliner to basic pipeconf.
106 return DefaultPiPipeconf.builder()
107 .withId(INT_PIPECONF_ID)
108 .withPipelineModel(parseP4Info(p4InfoUrl))
Carmelo Cascone255125d2018-04-11 14:03:22 -0700109 .addBehaviour(PiPipelineInterpreter.class, BasicInterpreterImpl.class)
Carmelo Cascone776be382018-12-12 19:03:57 -0800110 .addBehaviour(Pipeliner.class, BasicPipelinerImpl.class)
Jonghwan Hyunb9358822017-12-06 17:55:27 -0800111 .addBehaviour(PortStatisticsDiscovery.class, PortStatisticsDiscoveryImpl.class)
Jonghwan Hyun722275f2018-05-14 15:44:56 -0700112 .addBehaviour(IntProgrammable.class, IntProgrammableImpl.class)
Jonghwan Hyunb9358822017-12-06 17:55:27 -0800113 .addExtension(P4_INFO_TEXT, p4InfoUrl)
114 .addExtension(BMV2_JSON, jsonUrl)
115 .build();
116 }
117
Carmelo Cascone87892e22017-11-13 16:01:29 -0800118 private static PiPipelineModel parseP4Info(URL p4InfoUrl) {
119 try {
120 return P4InfoParser.parse(p4InfoUrl);
121 } catch (P4InfoParserException e) {
Ray Milkey986a47a2018-01-25 11:38:51 -0800122 throw new IllegalStateException(e);
Carmelo Cascone87892e22017-11-13 16:01:29 -0800123 }
124 }
Carmelo Casconeca94bcf2017-10-27 14:16:59 -0700125}