Anton Chigrin | 4af4f87 | 2019-01-14 17:29:56 +0200 | [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.openflow.controller; |
| 18 | |
| 19 | import com.fasterxml.jackson.databind.JsonNode; |
| 20 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 21 | import com.google.common.collect.Sets; |
| 22 | import org.onosproject.net.DeviceId; |
| 23 | import org.onosproject.net.config.Config; |
| 24 | import org.onlab.packet.EthType; |
| 25 | import org.slf4j.Logger; |
| 26 | import org.slf4j.LoggerFactory; |
| 27 | |
| 28 | import java.util.Iterator; |
| 29 | import java.util.Set; |
| 30 | |
| 31 | import static com.google.common.base.Preconditions.checkArgument; |
| 32 | import static com.google.common.base.Preconditions.checkNotNull; |
| 33 | |
| 34 | /** |
| 35 | * Configuration for classifiers. |
| 36 | */ |
| 37 | public class OpenFlowClassifierConfig extends Config<DeviceId> { |
| 38 | private static Logger log = LoggerFactory.getLogger(OpenFlowClassifierConfig.class); |
| 39 | |
| 40 | public static final String TARGET_QUEUE = "target-queue"; |
| 41 | public static final String ETHER_TYPE = "ethernet-type"; |
| 42 | |
| 43 | private static final String CONFIG_VALUE_ERROR = "Error parsing config value"; |
| 44 | private static final String CLASSF_NULL_ERROR = "Classifier cannot be null"; |
| 45 | |
| 46 | private short etherValue(String etherType) throws IllegalArgumentException { |
| 47 | short etherTypeValue; |
| 48 | try { |
| 49 | if (etherType.startsWith("0x")) { |
| 50 | Integer e = Integer.valueOf(etherType.substring(2), 16); |
| 51 | if (e < 0 || e > 0xFFFF) { |
| 52 | throw new IllegalArgumentException("EtherType value out of range"); |
| 53 | } |
| 54 | etherTypeValue = e.shortValue(); |
| 55 | } else { |
| 56 | etherTypeValue = EthType.EtherType.valueOf(etherType).ethType().toShort(); |
| 57 | } |
| 58 | } catch (IllegalArgumentException e) { |
| 59 | throw e; |
| 60 | } catch (Exception e) { |
| 61 | throw new IllegalArgumentException("Failed to parse ethernet type string"); |
| 62 | } |
| 63 | return etherTypeValue; |
| 64 | } |
| 65 | |
| 66 | @Override |
| 67 | public boolean isValid() { |
| 68 | for (JsonNode node : array) { |
| 69 | if (!hasOnlyFields((ObjectNode) node, TARGET_QUEUE, ETHER_TYPE)) { |
| 70 | return false; |
| 71 | } |
| 72 | |
| 73 | ObjectNode obj = (ObjectNode) node; |
| 74 | |
| 75 | if (!(isString(obj, ETHER_TYPE, FieldPresence.MANDATORY) && |
| 76 | isIntegralNumber(obj, TARGET_QUEUE, FieldPresence.MANDATORY, 0, 7))) { |
| 77 | return false; |
| 78 | } |
| 79 | |
| 80 | try { |
| 81 | etherValue(node.path(ETHER_TYPE).asText()); |
| 82 | } catch (Exception e) { |
| 83 | return false; |
| 84 | } |
| 85 | } |
| 86 | return true; |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Retrieves all classifiers configured on this port. |
| 91 | * |
| 92 | * @return set of classifiers |
| 93 | */ |
| 94 | public Set<OpenFlowClassifier> getClassifiers() { |
| 95 | Set<OpenFlowClassifier> classifiers = Sets.newHashSet(); |
| 96 | |
| 97 | for (JsonNode classfNode : array) { |
| 98 | DeviceId deviceId = this.subject(); |
| 99 | short ethernetType = etherValue(classfNode.path(ETHER_TYPE).asText()); |
| 100 | int idQueue = Integer.valueOf(classfNode.path(TARGET_QUEUE).asText()); |
| 101 | |
| 102 | OpenFlowClassifier classf = |
| 103 | new OpenFlowClassifier.Builder(deviceId, idQueue).ethernetType(ethernetType).build(); |
| 104 | classifiers.add(classf); |
| 105 | } |
| 106 | |
| 107 | return classifiers; |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Adds a classifier to the config. |
| 112 | * |
| 113 | * @param classf classifier to add |
| 114 | */ |
| 115 | public void addClassifier(OpenFlowClassifier classf) { |
| 116 | checkNotNull(classf, CLASSF_NULL_ERROR); |
| 117 | checkArgument(classf.deviceId().equals(this.subject())); |
| 118 | |
| 119 | ObjectNode classfNode = array.addObject(); |
| 120 | |
| 121 | EthType.EtherType e = EthType.EtherType.lookup(classf.ethernetType()); |
| 122 | if (e.equals(EthType.EtherType.UNKNOWN)) { |
| 123 | classfNode.put(ETHER_TYPE, String.format("0x%04x", classf.ethernetType())); |
| 124 | } else { |
| 125 | classfNode.put(ETHER_TYPE, e.name()); |
| 126 | } |
| 127 | classfNode.put(TARGET_QUEUE, classf.idQueue()); |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Removes a classifier from the config. |
| 132 | * |
| 133 | * @param classf classifier to remove |
| 134 | */ |
| 135 | public void removeClassifier(OpenFlowClassifier classf) { |
| 136 | checkNotNull(classf, CLASSF_NULL_ERROR); |
| 137 | checkArgument(classf.deviceId().equals(this.subject())); |
| 138 | |
| 139 | Iterator<JsonNode> it = array.iterator(); |
| 140 | while (it.hasNext()) { |
| 141 | JsonNode node = it.next(); |
| 142 | if (etherValue(node.path(ETHER_TYPE).asText()) == classf.ethernetType() |
| 143 | && Integer.valueOf(node.path(TARGET_QUEUE).asText()) == classf.idQueue()) { |
| 144 | it.remove(); |
| 145 | break; |
| 146 | } |
| 147 | } |
| 148 | } |
| 149 | } |