blob: cc77b59823b59d93a653f23d28de37fb0e45cb27 [file] [log] [blame]
Charles Chancab494d2016-11-11 16:31:49 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Charles Chancab494d2016-11-11 16:31:49 -08003 *
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 Milkey69ec8712017-08-08 13:00:43 -070017package org.onosproject.routeservice;
Charles Chancab494d2016-11-11 16:31:49 -080018
Jonathan Hart79a0abd2017-02-23 19:13:27 -080019import com.fasterxml.jackson.databind.node.ObjectNode;
Charles Chancab494d2016-11-11 16:31:49 -080020import com.google.common.collect.ImmutableSet;
21import org.onlab.packet.IpAddress;
22import org.onlab.packet.IpPrefix;
23import org.onosproject.core.ApplicationId;
24import org.onosproject.net.config.Config;
25
26import java.util.Set;
27
28/**
29 * Route configuration object for Route Service.
30 */
31public class RouteConfig extends Config<ApplicationId> {
Charles Chancab494d2016-11-11 16:31:49 -080032 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 Hart79a0abd2017-02-23 19:13:27 -080053
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 Chancab494d2016-11-11 16:31:49 -080066}