Carmelo Cascone | 07d7271 | 2017-07-14 15:57:47 -0400 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Carmelo Cascone | 07d7271 | 2017-07-14 15:57:47 -0400 | [diff] [blame] | 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.net.pi.model; |
| 18 | |
| 19 | import com.google.common.collect.ImmutableMap; |
ghj0504520 | ec1a420 | 2018-10-22 10:50:41 -0700 | [diff] [blame] | 20 | import org.apache.commons.io.IOUtils; |
Carmelo Cascone | 07d7271 | 2017-07-14 15:57:47 -0400 | [diff] [blame] | 21 | import org.onosproject.net.driver.Behaviour; |
| 22 | |
Carmelo Cascone | 31d3e44 | 2017-07-18 16:58:51 -0400 | [diff] [blame] | 23 | import java.io.IOException; |
Carmelo Cascone | 07d7271 | 2017-07-14 15:57:47 -0400 | [diff] [blame] | 24 | import java.io.InputStream; |
Carmelo Cascone | 31d3e44 | 2017-07-18 16:58:51 -0400 | [diff] [blame] | 25 | import java.net.URL; |
ghj0504520 | ec1a420 | 2018-10-22 10:50:41 -0700 | [diff] [blame] | 26 | import java.nio.ByteBuffer; |
| 27 | import java.util.ArrayList; |
| 28 | import java.util.Arrays; |
Carmelo Cascone | 07d7271 | 2017-07-14 15:57:47 -0400 | [diff] [blame] | 29 | import java.util.Collection; |
| 30 | import java.util.Map; |
| 31 | import java.util.Optional; |
| 32 | |
Carmelo Cascone | 31d3e44 | 2017-07-18 16:58:51 -0400 | [diff] [blame] | 33 | import static com.google.common.base.Preconditions.checkArgument; |
Carmelo Cascone | 07d7271 | 2017-07-14 15:57:47 -0400 | [diff] [blame] | 34 | import static com.google.common.base.Preconditions.checkNotNull; |
Carmelo Cascone | 31d3e44 | 2017-07-18 16:58:51 -0400 | [diff] [blame] | 35 | import static java.lang.String.format; |
Carmelo Cascone | 07d7271 | 2017-07-14 15:57:47 -0400 | [diff] [blame] | 36 | |
| 37 | /** |
| 38 | * Default pipeconf implementation. |
| 39 | */ |
| 40 | public final class DefaultPiPipeconf implements PiPipeconf { |
| 41 | |
| 42 | private final PiPipeconfId id; |
| 43 | private final PiPipelineModel pipelineModel; |
ghj0504520 | ec1a420 | 2018-10-22 10:50:41 -0700 | [diff] [blame] | 44 | private final long fingerprint; |
Carmelo Cascone | 07d7271 | 2017-07-14 15:57:47 -0400 | [diff] [blame] | 45 | private final Map<Class<? extends Behaviour>, Class<? extends Behaviour>> behaviours; |
Carmelo Cascone | 31d3e44 | 2017-07-18 16:58:51 -0400 | [diff] [blame] | 46 | private final Map<ExtensionType, URL> extensions; |
Carmelo Cascone | 07d7271 | 2017-07-14 15:57:47 -0400 | [diff] [blame] | 47 | |
ghj0504520 | ec1a420 | 2018-10-22 10:50:41 -0700 | [diff] [blame] | 48 | private DefaultPiPipeconf(PiPipeconfId id, PiPipelineModel pipelineModel, long fingerprint, |
Carmelo Cascone | 07d7271 | 2017-07-14 15:57:47 -0400 | [diff] [blame] | 49 | Map<Class<? extends Behaviour>, Class<? extends Behaviour>> behaviours, |
Carmelo Cascone | 31d3e44 | 2017-07-18 16:58:51 -0400 | [diff] [blame] | 50 | Map<ExtensionType, URL> extensions) { |
Carmelo Cascone | 07d7271 | 2017-07-14 15:57:47 -0400 | [diff] [blame] | 51 | this.id = id; |
| 52 | this.pipelineModel = pipelineModel; |
ghj0504520 | ec1a420 | 2018-10-22 10:50:41 -0700 | [diff] [blame] | 53 | this.fingerprint = fingerprint; |
Carmelo Cascone | 07d7271 | 2017-07-14 15:57:47 -0400 | [diff] [blame] | 54 | this.behaviours = behaviours; |
| 55 | this.extensions = extensions; |
| 56 | } |
| 57 | |
| 58 | @Override |
| 59 | public PiPipeconfId id() { |
| 60 | return id; |
| 61 | } |
| 62 | |
| 63 | @Override |
| 64 | public PiPipelineModel pipelineModel() { |
| 65 | return pipelineModel; |
| 66 | } |
| 67 | |
| 68 | @Override |
ghj0504520 | ec1a420 | 2018-10-22 10:50:41 -0700 | [diff] [blame] | 69 | public long fingerprint() { |
| 70 | return fingerprint; |
| 71 | } |
| 72 | |
| 73 | @Override |
Carmelo Cascone | 07d7271 | 2017-07-14 15:57:47 -0400 | [diff] [blame] | 74 | public Collection<Class<? extends Behaviour>> behaviours() { |
| 75 | return behaviours.keySet(); |
| 76 | } |
| 77 | |
| 78 | @Override |
| 79 | public Optional<Class<? extends Behaviour>> implementation(Class<? extends Behaviour> behaviour) { |
| 80 | return Optional.ofNullable(behaviours.get(behaviour)); |
| 81 | } |
| 82 | |
| 83 | @Override |
| 84 | public boolean hasBehaviour(Class<? extends Behaviour> behaviourClass) { |
| 85 | return behaviours.containsKey(behaviourClass); |
| 86 | } |
| 87 | |
| 88 | @Override |
| 89 | public Optional<InputStream> extension(ExtensionType type) { |
Carmelo Cascone | 31d3e44 | 2017-07-18 16:58:51 -0400 | [diff] [blame] | 90 | if (extensions.containsKey(type)) { |
| 91 | try { |
| 92 | return Optional.of(extensions.get(type).openStream()); |
| 93 | } catch (IOException e) { |
Ray Milkey | 986a47a | 2018-01-25 11:38:51 -0800 | [diff] [blame] | 94 | throw new IllegalStateException(e); |
Carmelo Cascone | 31d3e44 | 2017-07-18 16:58:51 -0400 | [diff] [blame] | 95 | } |
| 96 | } else { |
| 97 | return Optional.empty(); |
| 98 | } |
Carmelo Cascone | 07d7271 | 2017-07-14 15:57:47 -0400 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Returns a new pipeconf builder. |
| 103 | * |
| 104 | * @return pipeconf builder |
| 105 | */ |
| 106 | public static Builder builder() { |
| 107 | return new Builder(); |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Builder of pipeconf implementations. |
| 112 | */ |
| 113 | public static class Builder { |
| 114 | |
| 115 | private PiPipeconfId id; |
| 116 | private PiPipelineModel pipelineModel; |
| 117 | private ImmutableMap.Builder<Class<? extends Behaviour>, Class<? extends Behaviour>> behaviourMapBuilder |
| 118 | = ImmutableMap.builder(); |
Carmelo Cascone | 31d3e44 | 2017-07-18 16:58:51 -0400 | [diff] [blame] | 119 | private ImmutableMap.Builder<ExtensionType, URL> extensionMapBuilder = ImmutableMap.builder(); |
Carmelo Cascone | 07d7271 | 2017-07-14 15:57:47 -0400 | [diff] [blame] | 120 | |
| 121 | /** |
| 122 | * Sets the identifier of this pipeconf. |
| 123 | * |
| 124 | * @param id pipeconf identifier |
| 125 | * @return this |
| 126 | */ |
| 127 | public Builder withId(PiPipeconfId id) { |
| 128 | this.id = id; |
| 129 | return this; |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Sets the pipeline model of this pipeconf. |
| 134 | * |
| 135 | * @param model pipeline model |
| 136 | * @return this |
| 137 | */ |
| 138 | public Builder withPipelineModel(PiPipelineModel model) { |
| 139 | this.pipelineModel = model; |
| 140 | return this; |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * Adds a behaviour to this pipeconf. |
| 145 | * |
| 146 | * @param clazz behavior interface class |
| 147 | * @param implementation behavior implementation class |
| 148 | * @return this |
| 149 | */ |
| 150 | public Builder addBehaviour(Class<? extends Behaviour> clazz, Class<? extends Behaviour> implementation) { |
| 151 | checkNotNull(clazz); |
| 152 | checkNotNull(implementation); |
| 153 | behaviourMapBuilder.put(clazz, implementation); |
| 154 | return this; |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * Adds an extension to this pipeconf. |
| 159 | * |
Carmelo Cascone | 31d3e44 | 2017-07-18 16:58:51 -0400 | [diff] [blame] | 160 | * @param type extension type |
| 161 | * @param url url pointing at the extension file |
Carmelo Cascone | 07d7271 | 2017-07-14 15:57:47 -0400 | [diff] [blame] | 162 | * @return this |
| 163 | */ |
Carmelo Cascone | 31d3e44 | 2017-07-18 16:58:51 -0400 | [diff] [blame] | 164 | public Builder addExtension(ExtensionType type, URL url) { |
Carmelo Cascone | 07d7271 | 2017-07-14 15:57:47 -0400 | [diff] [blame] | 165 | checkNotNull(type); |
Carmelo Cascone | 31d3e44 | 2017-07-18 16:58:51 -0400 | [diff] [blame] | 166 | checkNotNull(url); |
| 167 | checkArgument(checkUrl(url), format("Extension url %s seems to be empty/non existent", url.toString())); |
| 168 | extensionMapBuilder.put(type, url); |
Carmelo Cascone | 07d7271 | 2017-07-14 15:57:47 -0400 | [diff] [blame] | 169 | return this; |
| 170 | } |
| 171 | |
Carmelo Cascone | 31d3e44 | 2017-07-18 16:58:51 -0400 | [diff] [blame] | 172 | private boolean checkUrl(URL url) { |
| 173 | try { |
| 174 | int byteCount = url.openStream().available(); |
| 175 | return byteCount > 0; |
| 176 | } catch (IOException e) { |
| 177 | return false; |
| 178 | } |
| 179 | } |
| 180 | |
Carmelo Cascone | 07d7271 | 2017-07-14 15:57:47 -0400 | [diff] [blame] | 181 | /** |
| 182 | * Creates a new pipeconf. |
| 183 | * |
| 184 | * @return pipeconf instance |
| 185 | */ |
| 186 | public PiPipeconf build() { |
| 187 | checkNotNull(id); |
| 188 | checkNotNull(pipelineModel); |
ghj0504520 | ec1a420 | 2018-10-22 10:50:41 -0700 | [diff] [blame] | 189 | |
| 190 | Map<ExtensionType, URL> extensions = extensionMapBuilder.build(); |
| 191 | return new DefaultPiPipeconf(id, pipelineModel, generateFingerprint(extensions), |
| 192 | behaviourMapBuilder.build(), extensions); |
Carmelo Cascone | 07d7271 | 2017-07-14 15:57:47 -0400 | [diff] [blame] | 193 | } |
| 194 | |
ghj0504520 | ec1a420 | 2018-10-22 10:50:41 -0700 | [diff] [blame] | 195 | private long generateFingerprint(Map<ExtensionType, URL> extensions) { |
| 196 | Collection<Integer> hashArray = new ArrayList<>(); |
| 197 | for (Map.Entry<ExtensionType, URL> pair : extensions.entrySet()) { |
| 198 | try { |
| 199 | hashArray.add(Arrays.hashCode(ByteBuffer.wrap(IOUtils.toByteArray( |
| 200 | pair.getValue().openStream())).array())); |
| 201 | } catch (IOException e) { |
| 202 | throw new IllegalArgumentException(e); |
| 203 | } |
| 204 | } |
| 205 | return Arrays.hashCode(hashArray.toArray()); |
| 206 | } |
Carmelo Cascone | 07d7271 | 2017-07-14 15:57:47 -0400 | [diff] [blame] | 207 | } |
| 208 | } |