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