blob: e17b48e5099126f06cb9778c59134177910a019f [file] [log] [blame]
Carmelo Cascone356ab8b2019-09-25 01:02:53 -07001/*
2 * Copyright 2019-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.fabric.impl;
18
Carmelo Casconeb330fc72020-07-17 15:27:02 -070019import org.onosproject.net.behaviour.inbandtelemetry.IntProgrammable;
Daniele Moro464e5ed2019-07-25 14:45:01 -070020
21import org.onosproject.net.behaviour.BngProgrammable;
Carmelo Cascone356ab8b2019-09-25 01:02:53 -070022import org.onosproject.net.behaviour.Pipeliner;
23import org.onosproject.net.pi.model.DefaultPiPipeconf;
24import org.onosproject.net.pi.model.PiPipeconf;
25import org.onosproject.net.pi.model.PiPipelineInterpreter;
26import org.onosproject.net.pi.model.PiPipelineModel;
27import org.onosproject.p4runtime.model.P4InfoParser;
28import org.onosproject.p4runtime.model.P4InfoParserException;
29import org.onosproject.pipelines.fabric.FabricPipeconfService;
30import org.onosproject.pipelines.fabric.impl.behaviour.FabricIntProgrammable;
Daniele Moro464e5ed2019-07-25 14:45:01 -070031import org.onosproject.pipelines.fabric.impl.behaviour.bng.FabricBngProgrammable;
Carmelo Cascone356ab8b2019-09-25 01:02:53 -070032import org.onosproject.pipelines.fabric.impl.behaviour.FabricInterpreter;
33import org.onosproject.pipelines.fabric.impl.behaviour.pipeliner.FabricPipeliner;
34import org.osgi.service.component.annotations.Activate;
35import org.osgi.service.component.annotations.Component;
36import org.osgi.service.component.annotations.Deactivate;
37import org.slf4j.Logger;
38
39import java.net.URL;
40
41import static com.google.common.base.Preconditions.checkArgument;
42import static com.google.common.base.Preconditions.checkNotNull;
43import static org.slf4j.LoggerFactory.getLogger;
44
45/**
46 * Implementation of the FabricPipeconfService.
47 */
48@Component(immediate = true, service = FabricPipeconfService.class)
49public final class FabricPipeconfManager implements FabricPipeconfService {
50
51 private static final String INT_PROFILE_SUFFIX = "-int";
52 private static final String FULL_PROFILE_SUFFIX = "-full";
Daniele Moro464e5ed2019-07-25 14:45:01 -070053 private static final String BNG_PROFILE_SUFFIX = "-bng";
Daniele Moro8d630f12021-06-15 20:53:22 +020054 private static final String UPF_PROFILE_SUFFIX = "-spgw";
Carmelo Cascone356ab8b2019-09-25 01:02:53 -070055
56 private static Logger log = getLogger(FabricPipeconfLoader.class);
57
58 @Activate
59 public void activate() {
60 log.info("Started");
61 }
62
63 @Deactivate
64 public void deactivate() {
65 log.info("Stopped");
66 }
67
68 @Override
69 public PiPipeconf buildFabricPipeconf(
70 DefaultPiPipeconf.Builder builder, String profile, URL p4InfoUrl, URL cpuPortUrl) {
71 return build(builder, profile, p4InfoUrl, cpuPortUrl);
72 }
73
74 static PiPipeconf build(
75 DefaultPiPipeconf.Builder pipeconfBuilder,
76 String profileName, URL p4InfoUrl, URL cpuPortUrl) {
77 checkNotNull(pipeconfBuilder,
78 "pipeconfBuilder cannot be null");
79 checkArgument(profileName != null && !profileName.isEmpty(),
80 "profileName cannot be null or empty");
81 checkNotNull(p4InfoUrl,
82 "p4InfoUrl cannot be null (check if file exists)");
83 checkNotNull(cpuPortUrl,
84 "cpuPortUrl cannot be null (check if file exists)");
85
86 pipeconfBuilder
87 .withPipelineModel(parseP4Info(p4InfoUrl))
88 .addBehaviour(PiPipelineInterpreter.class, FabricInterpreter.class)
89 .addBehaviour(Pipeliner.class, FabricPipeliner.class)
90 .addExtension(PiPipeconf.ExtensionType.P4_INFO_TEXT, p4InfoUrl)
91 .addExtension(PiPipeconf.ExtensionType.CPU_PORT_TXT, cpuPortUrl);
92
93 // Add IntProgrammable behaviour for INT-enabled profiles.
94 if (profileName.endsWith(INT_PROFILE_SUFFIX) ||
95 profileName.endsWith(FULL_PROFILE_SUFFIX)) {
96 pipeconfBuilder.addBehaviour(IntProgrammable.class, FabricIntProgrammable.class);
97 }
Daniele Moro464e5ed2019-07-25 14:45:01 -070098 // Add BngProgrammable behavior for BNG-enabled pipelines.
99 if (profileName.endsWith(BNG_PROFILE_SUFFIX)) {
100 pipeconfBuilder.addBehaviour(BngProgrammable.class, FabricBngProgrammable.class);
101 }
Carmelo Cascone356ab8b2019-09-25 01:02:53 -0700102 return pipeconfBuilder.build();
103 }
104
105 private static PiPipelineModel parseP4Info(URL p4InfoUrl) {
106 try {
107 return P4InfoParser.parse(p4InfoUrl);
108 } catch (P4InfoParserException e) {
109 // FIXME: propagate exception that can be handled by whoever is
110 // trying to build pipeconfs.
111 throw new IllegalStateException(e);
112 }
113 }
114}