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