Jonathan Hart | a7a24cf | 2017-02-02 16:13:57 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Jonathan Hart | a7a24cf | 2017-02-02 16:13:57 -0800 | [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.routing.config; |
| 18 | |
| 19 | import com.fasterxml.jackson.databind.JsonNode; |
| 20 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 21 | import com.fasterxml.jackson.databind.node.ObjectNode; |
Jonathan Hart | 249b4cf | 2017-02-03 18:02:58 -0800 | [diff] [blame] | 22 | import com.google.common.collect.ImmutableSet; |
Jonathan Hart | a7a24cf | 2017-02-02 16:13:57 -0800 | [diff] [blame] | 23 | import org.onosproject.core.ApplicationId; |
| 24 | import org.onosproject.net.ConnectPoint; |
| 25 | import org.onosproject.net.config.Config; |
| 26 | |
| 27 | import java.util.HashSet; |
| 28 | import java.util.Set; |
| 29 | |
| 30 | /** |
| 31 | * Routing configuration. |
| 32 | */ |
| 33 | public class RoutersConfig extends Config<ApplicationId> { |
| 34 | |
| 35 | private static final String CP_CONNECT_POINT = "controlPlaneConnectPoint"; |
| 36 | private static final String OSPF_ENABLED = "ospfEnabled"; |
| 37 | private static final String PIM_ENABLED = "pimEnabled"; |
| 38 | private static final String INTERFACES = "interfaces"; |
| 39 | |
| 40 | /** |
| 41 | * Gets the router configurations. |
| 42 | * |
| 43 | * @return set of router configurations |
| 44 | */ |
| 45 | public Set<Router> getRouters() { |
| 46 | Set<Router> routers = new HashSet<>(); |
| 47 | |
| 48 | for (JsonNode routerNode : array) { |
| 49 | ConnectPoint connectPoint = ConnectPoint.deviceConnectPoint(routerNode.path(CP_CONNECT_POINT).asText()); |
| 50 | boolean ospfEnabled = routerNode.path(OSPF_ENABLED).asBoolean(); |
| 51 | |
| 52 | |
| 53 | JsonNode intfNode = routerNode.path(INTERFACES); |
Jonathan Hart | a7a24cf | 2017-02-02 16:13:57 -0800 | [diff] [blame] | 54 | Set<String> interfaces = new HashSet<>(array.size()); |
Jonathan Hart | 249b4cf | 2017-02-03 18:02:58 -0800 | [diff] [blame] | 55 | if (!intfNode.isMissingNode()) { |
| 56 | ArrayNode array = (ArrayNode) intfNode; |
| 57 | for (JsonNode intf : array) { |
| 58 | interfaces.add(intf.asText()); |
| 59 | } |
Jonathan Hart | a7a24cf | 2017-02-02 16:13:57 -0800 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | routers.add(new Router(connectPoint, ospfEnabled, interfaces)); |
| 63 | } |
| 64 | |
Jonathan Hart | 249b4cf | 2017-02-03 18:02:58 -0800 | [diff] [blame] | 65 | return ImmutableSet.copyOf(routers); |
Jonathan Hart | a7a24cf | 2017-02-02 16:13:57 -0800 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | @Override |
| 69 | public boolean isValid() { |
| 70 | for (JsonNode node : array) { |
| 71 | ObjectNode routerNode = (ObjectNode) node; |
| 72 | if (!hasOnlyFields(routerNode, INTERFACES, CP_CONNECT_POINT, OSPF_ENABLED, PIM_ENABLED)) { |
| 73 | return false; |
| 74 | } |
| 75 | |
| 76 | JsonNode intfNode = routerNode.path(INTERFACES); |
| 77 | if (!intfNode.isMissingNode() && !intfNode.isArray()) { |
| 78 | return false; |
| 79 | } |
| 80 | |
| 81 | boolean valid = isConnectPoint(routerNode, CP_CONNECT_POINT, FieldPresence.MANDATORY) && |
| 82 | isBoolean(routerNode, OSPF_ENABLED, FieldPresence.OPTIONAL) && |
| 83 | isBoolean(routerNode, PIM_ENABLED, FieldPresence.OPTIONAL); |
| 84 | |
| 85 | if (!valid) { |
| 86 | return false; |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | return true; |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Router configuration. |
| 95 | */ |
| 96 | public static class Router { |
| 97 | private final ConnectPoint connectPoint; |
| 98 | private final boolean ospfEnabled; |
| 99 | private final Set<String> interfaces; |
| 100 | |
| 101 | Router(ConnectPoint controlPlaneConnectPoint, boolean ospfEnabled, Set<String> interfaces) { |
| 102 | this.connectPoint = controlPlaneConnectPoint; |
| 103 | this.ospfEnabled = ospfEnabled; |
| 104 | this.interfaces = interfaces; |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Returns the routing control plane connect point. |
| 109 | * |
| 110 | * @return control plane connect point |
| 111 | */ |
| 112 | public ConnectPoint controlPlaneConnectPoint() { |
| 113 | return connectPoint; |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Returns whether OSPF is enabled on this router. |
| 118 | * |
| 119 | * @return true if OSPF is enabled, otherwise false |
| 120 | */ |
| 121 | public boolean isOspfEnabled() { |
| 122 | return ospfEnabled; |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Returns the set of interfaces enabled on this router. |
| 127 | * |
| 128 | * @return set of interface names that are enabled, or an empty list if |
| 129 | * all available interfaces should be used |
| 130 | */ |
| 131 | public Set<String> interfaces() { |
| 132 | return interfaces; |
| 133 | } |
| 134 | } |
| 135 | } |