Andrea Campanella | 75ef9f5 | 2017-07-27 20:14:32 +0200 | [diff] [blame] | 1 | /* |
Thomas Vachuska | 52f2cd1 | 2018-11-08 21:20:04 -0800 | [diff] [blame] | 2 | * Copyright 2018-present Open Networking Foundation |
Andrea Campanella | 75ef9f5 | 2017-07-27 20:14:32 +0200 | [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 | |
Thomas Vachuska | 52f2cd1 | 2018-11-08 21:20:04 -0800 | [diff] [blame] | 17 | package org.onosproject.net.config.basics; |
Andrea Campanella | 75ef9f5 | 2017-07-27 20:14:32 +0200 | [diff] [blame] | 18 | |
| 19 | import com.fasterxml.jackson.databind.JsonNode; |
| 20 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 21 | import com.google.common.annotations.Beta; |
| 22 | import com.google.common.collect.ImmutableList; |
| 23 | import org.onosproject.net.DefaultAnnotations; |
| 24 | import org.onosproject.net.DeviceId; |
| 25 | import org.onosproject.net.Port; |
| 26 | import org.onosproject.net.PortNumber; |
| 27 | import org.onosproject.net.config.Config; |
| 28 | import org.onosproject.net.device.DefaultPortDescription; |
| 29 | import org.onosproject.net.device.PortDescription; |
| 30 | import org.slf4j.Logger; |
| 31 | import org.slf4j.LoggerFactory; |
| 32 | |
| 33 | import java.util.Iterator; |
| 34 | import java.util.List; |
| 35 | import java.util.Map; |
| 36 | |
| 37 | /** |
| 38 | * Configuration for Ports. Creates a list of PortDescription based on the given Json. |
| 39 | */ |
| 40 | @Beta |
| 41 | public class PortDescriptionsConfig extends Config<DeviceId> { |
| 42 | private static Logger log = LoggerFactory.getLogger(PortDescriptionsConfig.class); |
| 43 | |
| 44 | private static final String NUMBER = "number"; |
| 45 | private static final String NAME = "name"; |
| 46 | private static final String ENABLED = "enabled"; |
| 47 | private static final String REMOVED = "removed"; |
| 48 | private static final String TYPE = "type"; |
| 49 | private static final String SPEED = "speed"; |
| 50 | private static final String ANNOTATIONS = "annotations"; |
| 51 | |
| 52 | private static final String CONFIG_VALUE_ERROR = "Error parsing config value"; |
| 53 | |
| 54 | @Override |
| 55 | public boolean isValid() { |
| 56 | for (Iterator<Map.Entry<String, JsonNode>> it = node.fields(); it.hasNext();) { |
| 57 | JsonNode nodePort = it.next().getValue(); |
| 58 | if (!hasOnlyFields((ObjectNode) nodePort, NUMBER, NAME, ENABLED, REMOVED, TYPE, |
| 59 | SPEED, ANNOTATIONS)) { |
| 60 | return false; |
| 61 | } |
| 62 | ObjectNode obj = (ObjectNode) nodePort; |
| 63 | |
| 64 | if (!(isNumber(obj, NUMBER, FieldPresence.MANDATORY) && |
| 65 | isString(obj, NAME, FieldPresence.OPTIONAL) && |
| 66 | isBoolean(obj, ENABLED, FieldPresence.OPTIONAL) && |
| 67 | isBoolean(obj, REMOVED, FieldPresence.OPTIONAL) && |
| 68 | isString(obj, TYPE, FieldPresence.OPTIONAL) && |
| 69 | isIntegralNumber(obj, SPEED, FieldPresence.OPTIONAL))) { |
| 70 | return false; |
| 71 | } |
| 72 | |
| 73 | if (node.has(ANNOTATIONS) && !node.get(ANNOTATIONS).isObject()) { |
| 74 | log.error("Annotations must be an inner json node"); |
| 75 | return false; |
| 76 | } |
| 77 | |
| 78 | } |
| 79 | return true; |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Retrieves all port descriptions. |
| 84 | * |
| 85 | * @return set of port descriptions |
| 86 | */ |
| 87 | public List<PortDescription> portDescriptions() { |
| 88 | |
| 89 | try { |
| 90 | ImmutableList.Builder<PortDescription> portDescriptions = ImmutableList.builder(); |
| 91 | for (Iterator<Map.Entry<String, JsonNode>> it = node.fields(); it.hasNext();) { |
| 92 | JsonNode portNode = it.next().getValue(); |
| 93 | long number = portNode.path(NUMBER).asLong(); |
| 94 | |
| 95 | String name = portNode.path(NAME).asText(null); |
| 96 | |
| 97 | PortNumber portNumber = createPortNumber(number, name); |
| 98 | |
| 99 | DefaultPortDescription.Builder builder = DefaultPortDescription.builder() |
Yuta HIGUCHI | 53e4796 | 2018-03-01 23:50:48 -0800 | [diff] [blame] | 100 | .withPortNumber(portNumber); |
Andrea Campanella | 75ef9f5 | 2017-07-27 20:14:32 +0200 | [diff] [blame] | 101 | if (portNode.has(ENABLED)) { |
| 102 | builder.isEnabled(portNode.path(ENABLED).asBoolean()); |
| 103 | } |
| 104 | |
| 105 | if (portNode.has(REMOVED)) { |
| 106 | builder.isRemoved(portNode.path(REMOVED).asBoolean()); |
| 107 | } |
| 108 | |
| 109 | if (portNode.has(TYPE)) { |
| 110 | builder.type(Port.Type.valueOf(portNode.path(TYPE).asText().toUpperCase())); |
| 111 | } |
| 112 | |
| 113 | if (portNode.has(SPEED)) { |
| 114 | builder.portSpeed(portNode.path(SPEED).asLong()); |
| 115 | } |
| 116 | |
| 117 | if (portNode.has(ANNOTATIONS)) { |
| 118 | DefaultAnnotations.Builder annotationsBuilder = DefaultAnnotations.builder(); |
| 119 | Iterator<Map.Entry<String, JsonNode>> annotationsIt = portNode.get(ANNOTATIONS).fields(); |
Carmelo Cascone | 693c38c | 2018-01-31 14:46:29 -0800 | [diff] [blame] | 120 | while (annotationsIt.hasNext()) { |
Andrea Campanella | 75ef9f5 | 2017-07-27 20:14:32 +0200 | [diff] [blame] | 121 | Map.Entry<String, JsonNode> entry = annotationsIt.next(); |
| 122 | annotationsBuilder.set(entry.getKey(), entry.getValue().asText()); |
| 123 | } |
| 124 | builder.annotations(annotationsBuilder.build()); |
| 125 | } |
| 126 | |
| 127 | portDescriptions.add(builder.build()); |
| 128 | } |
| 129 | |
| 130 | return portDescriptions.build(); |
| 131 | |
| 132 | } catch (IllegalArgumentException e) { |
| 133 | log.error(CONFIG_VALUE_ERROR, e); |
| 134 | return ImmutableList.of(); |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | private PortNumber createPortNumber(long number, String name) { |
| 139 | if (name == null) { |
| 140 | return PortNumber.portNumber(number); |
| 141 | } |
| 142 | return PortNumber.portNumber(number, name); |
| 143 | } |
| 144 | |
| 145 | |
| 146 | } |