Charles Chan | cab494d | 2016-11-11 16:31:49 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Charles Chan | cab494d | 2016-11-11 16:31:49 -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 | |
Ray Milkey | 69ec871 | 2017-08-08 13:00:43 -0700 | [diff] [blame] | 17 | package org.onosproject.routeservice; |
Charles Chan | cab494d | 2016-11-11 16:31:49 -0800 | [diff] [blame] | 18 | |
Jonathan Hart | 79a0abd | 2017-02-23 19:13:27 -0800 | [diff] [blame] | 19 | import com.fasterxml.jackson.databind.node.ObjectNode; |
Charles Chan | cab494d | 2016-11-11 16:31:49 -0800 | [diff] [blame] | 20 | import com.google.common.collect.ImmutableSet; |
| 21 | import org.onlab.packet.IpAddress; |
| 22 | import org.onlab.packet.IpPrefix; |
| 23 | import org.onosproject.core.ApplicationId; |
| 24 | import org.onosproject.net.config.Config; |
| 25 | |
| 26 | import java.util.Set; |
| 27 | |
| 28 | /** |
| 29 | * Route configuration object for Route Service. |
| 30 | */ |
| 31 | public class RouteConfig extends Config<ApplicationId> { |
Charles Chan | cab494d | 2016-11-11 16:31:49 -0800 | [diff] [blame] | 32 | private static final String PREFIX = "prefix"; |
| 33 | private static final String NEXTHOP = "nextHop"; |
| 34 | |
| 35 | /** |
| 36 | * Returns all routes in this configuration. |
| 37 | * |
| 38 | * @return A set of route. |
| 39 | */ |
| 40 | public Set<Route> getRoutes() { |
| 41 | ImmutableSet.Builder<Route> routes = ImmutableSet.builder(); |
| 42 | array.forEach(route -> { |
| 43 | try { |
| 44 | IpPrefix prefix = IpPrefix.valueOf(route.path(PREFIX).asText()); |
| 45 | IpAddress nextHop = IpAddress.valueOf(route.path(NEXTHOP).asText()); |
| 46 | routes.add(new Route(Route.Source.STATIC, prefix, nextHop)); |
| 47 | } catch (IllegalArgumentException e) { |
| 48 | // Ignores routes that cannot be parsed correctly |
| 49 | } |
| 50 | }); |
| 51 | return routes.build(); |
| 52 | } |
Jonathan Hart | 79a0abd | 2017-02-23 19:13:27 -0800 | [diff] [blame] | 53 | |
| 54 | @Override |
| 55 | public boolean isValid() { |
| 56 | array.forEach(routeNode -> { |
| 57 | if (!routeNode.isObject()) { |
| 58 | throw new IllegalArgumentException("Not object node"); |
| 59 | } |
| 60 | ObjectNode route = (ObjectNode) routeNode; |
| 61 | isIpPrefix(route, PREFIX, FieldPresence.MANDATORY); |
| 62 | isIpAddress(route, NEXTHOP, FieldPresence.MANDATORY); |
| 63 | }); |
| 64 | return true; |
| 65 | } |
Charles Chan | cab494d | 2016-11-11 16:31:49 -0800 | [diff] [blame] | 66 | } |