blob: 97dace9199a8efec329fc4c2c4e06da8aea90d36 [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
19import org.onosproject.core.CoreService;
20import org.onosproject.net.device.PortStatisticsDiscovery;
21import org.onosproject.net.pi.model.DefaultPiPipeconf;
22import org.onosproject.net.pi.model.PiPipeconf;
23import org.onosproject.net.pi.model.PiPipeconfId;
24import org.onosproject.net.pi.service.PiPipeconfService;
25import org.onosproject.pipelines.fabric.impl.behaviour.FabricPortStatisticsDiscovery;
26import org.osgi.framework.FrameworkUtil;
27import org.osgi.framework.wiring.BundleWiring;
28import org.osgi.service.component.annotations.Activate;
29import org.osgi.service.component.annotations.Component;
30import org.osgi.service.component.annotations.Deactivate;
31import org.osgi.service.component.annotations.Reference;
32import org.osgi.service.component.annotations.ReferenceCardinality;
33import org.slf4j.Logger;
34
35import java.io.File;
36import java.io.FileNotFoundException;
37import java.net.URL;
38import java.util.Collection;
39import java.util.Objects;
40import java.util.stream.Collectors;
41
42import static java.lang.String.format;
43import static org.onosproject.pipelines.fabric.impl.FabricPipeconfManager.build;
44import static org.osgi.framework.wiring.BundleWiring.LISTRESOURCES_RECURSE;
45import static org.slf4j.LoggerFactory.getLogger;
46
47/**
48 * Component responsible of building and registering fabric pipeconfs at app
49 * activation.
50 * <p>
51 * This implementation looks at the content of the resource path for p4c output,
52 * automatically building different pipeconfs for different profiles, target and
53 * platforms.
54 */
55@Component(immediate = true)
56public final class FabricPipeconfLoader {
57
58 public static final String PIPELINE_APP_NAME = "org.onosproject.pipelines.fabric";
Daniele Moro668b3d92021-07-05 23:37:36 +020059 public static final String PIPELINE_APP_NAME_UPF = "org.onosproject.pipelines.fabric.upf";
Carmelo Cascone356ab8b2019-09-25 01:02:53 -070060
61 private static Logger log = getLogger(FabricPipeconfLoader.class);
62
63 private static final String SEP = File.separator;
64 private static final String SPECTRUM = "spectrum";
65 private static final String BMV2 = "bmv2";
66 private static final String DEFAULT_PLATFORM = "default";
67 private static final String BMV2_JSON = "bmv2.json";
68 private static final String P4INFO_TXT = "p4info.txt";
69 private static final String CPU_PORT_TXT = "cpu_port.txt";
70 private static final String SPECTRUM_BIN = "spectrum.bin";
71
72 private static final String BASE_PIPECONF_ID = "org.onosproject.pipelines";
73 private static final String P4C_OUT_PATH = "/p4c-out";
74 // p4c-out/<profile>/<target>/<platform>
75 private static final String P4C_RES_BASE_PATH = P4C_OUT_PATH + "/%s/%s/%s/";
76
77 @Reference(cardinality = ReferenceCardinality.MANDATORY)
78 private PiPipeconfService piPipeconfService;
79
80 @Reference(cardinality = ReferenceCardinality.MANDATORY)
81 private CoreService coreService;
82
83 private Collection<PiPipeconf> pipeconfs;
84
85
86 @Activate
87 public void activate() {
88 coreService.registerApplication(PIPELINE_APP_NAME);
Daniele Moro668b3d92021-07-05 23:37:36 +020089 coreService.registerApplication(PIPELINE_APP_NAME_UPF);
Carmelo Cascone356ab8b2019-09-25 01:02:53 -070090 // Registers all pipeconf at component activation.
91 pipeconfs = buildAllPipeconfs();
92 pipeconfs.forEach(piPipeconfService::register);
93 log.info("Started");
94 }
95
96 @Deactivate
97 public void deactivate() {
98 pipeconfs.stream()
99 .map(PiPipeconf::id)
100 .forEach(piPipeconfService::unregister);
101 pipeconfs = null;
102 log.info("Stopped");
103 }
104
105 private Collection<PiPipeconf> buildAllPipeconfs() {
106 return FrameworkUtil
107 .getBundle(this.getClass())
108 .adapt(BundleWiring.class)
109 // List all resource files in /p4c-out
110 .listResources(P4C_OUT_PATH, "*", LISTRESOURCES_RECURSE)
111 .stream()
112 // Filter only directories
113 .filter(name -> name.endsWith(SEP))
114 // Derive profile, target, and platform and build pipeconf.
115 .map(this::buildPipeconfFromPath)
116 .filter(Objects::nonNull)
117 .collect(Collectors.toList());
118 }
119
120 private PiPipeconf buildPipeconfFromPath(String path) {
121 String[] pieces = path.split(SEP);
122 // We expect a path of 4 elements, e.g.
123 // p4c-out/<profile>/<target>/<platform>
124 if (pieces.length != 4) {
125 return null;
126 }
127 String profile = pieces[1];
128 String target = pieces[2];
129 String platform = pieces[3];
130 final PiPipeconf pipeconf;
131 try {
132 switch (target) {
133 case BMV2:
134 pipeconf = bmv2Pipeconf(profile, platform);
135 break;
136 case SPECTRUM:
137 pipeconf = spectrumPipeconf(profile, platform);
138 break;
139 default:
140 log.warn("Unknown target '{}', skipping pipeconf build...",
141 target);
142 return null;
143 }
144 } catch (FileNotFoundException e) {
145 log.warn("Unable to build pipeconf at {} because file is missing: {}",
146 path, e.getMessage());
147 return null;
148 }
149
150 return pipeconf;
151 }
152
153 private PiPipeconf bmv2Pipeconf(String profile, String platform)
154 throws FileNotFoundException {
155 final URL bmv2JsonUrl = this.getClass().getResource(format(
156 P4C_RES_BASE_PATH + BMV2_JSON, profile, BMV2, platform));
157 final URL p4InfoUrl = this.getClass().getResource(format(
158 P4C_RES_BASE_PATH + P4INFO_TXT, profile, BMV2, platform));
159 final URL cpuPortUrl = this.getClass().getResource(format(
160 P4C_RES_BASE_PATH + CPU_PORT_TXT, profile, BMV2, platform));
161
162 checkFileExists(bmv2JsonUrl, BMV2_JSON);
163 checkFileExists(p4InfoUrl, P4INFO_TXT);
164 checkFileExists(cpuPortUrl, CPU_PORT_TXT);
165
166 final DefaultPiPipeconf.Builder builder = DefaultPiPipeconf.builder()
167 .withId(makePipeconfId(platform, profile))
168 .addBehaviour(PortStatisticsDiscovery.class,
169 FabricPortStatisticsDiscovery.class)
170 .addExtension(PiPipeconf.ExtensionType.BMV2_JSON, bmv2JsonUrl);
171
172 return build(builder, profile, p4InfoUrl, cpuPortUrl);
173 }
174
175 // FIXME: this method should be removed. Instead, there should be a
176 // third-party app using the FabricPipeconfService to register a
177 // Mellanox-specific version of the fabric pipeconfs.
178 private PiPipeconf spectrumPipeconf(String profile, String platform)
179 throws FileNotFoundException {
180
181 final URL spectrumBinUrl = this.getClass().getResource(format(
182 P4C_RES_BASE_PATH + SPECTRUM_BIN, profile, SPECTRUM, platform));
183 final URL p4InfoUrl = this.getClass().getResource(format(
184 P4C_RES_BASE_PATH + P4INFO_TXT, profile, SPECTRUM, platform));
185 final URL cpuPortUrl = this.getClass().getResource(format(
186 P4C_RES_BASE_PATH + CPU_PORT_TXT, profile, SPECTRUM, platform));
187
188 checkFileExists(spectrumBinUrl, SPECTRUM_BIN);
189 checkFileExists(p4InfoUrl, P4INFO_TXT);
190 checkFileExists(cpuPortUrl, CPU_PORT_TXT);
191
192 final DefaultPiPipeconf.Builder builder = DefaultPiPipeconf.builder()
193 .withId(makePipeconfId(platform, profile))
194 .addExtension(PiPipeconf.ExtensionType.SPECTRUM_BIN, spectrumBinUrl);
195
196 return build(builder, profile, p4InfoUrl, cpuPortUrl);
197 }
198
199 private void checkFileExists(URL url, String name)
200 throws FileNotFoundException {
201 if (url == null) {
202 throw new FileNotFoundException(name);
203 }
204 }
205
206 private PiPipeconfId makePipeconfId(String platform, String profile) {
207 final String id = platform.equals(DEFAULT_PLATFORM)
208 // Omit platform if default, e.g. with BMv2 pipeconf
209 ? format("%s.%s", BASE_PIPECONF_ID, profile)
210 : format("%s.%s.%s", BASE_PIPECONF_ID, profile, platform);
211 return new PiPipeconfId(id);
212 }
213}