blob: 58a3cd85bf3ab6a2c482a82f8c3589250a5308cd [file] [log] [blame]
Jonathan Harta7a24cf2017-02-02 16:13:57 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Jonathan Harta7a24cf2017-02-02 16:13:57 -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
17package org.onosproject.routing.config;
18
19import com.fasterxml.jackson.databind.JsonNode;
20import com.fasterxml.jackson.databind.node.ArrayNode;
21import com.fasterxml.jackson.databind.node.ObjectNode;
Jonathan Hart249b4cf2017-02-03 18:02:58 -080022import com.google.common.collect.ImmutableSet;
Jonathan Harta7a24cf2017-02-02 16:13:57 -080023import org.onosproject.core.ApplicationId;
24import org.onosproject.net.ConnectPoint;
25import org.onosproject.net.config.Config;
26
27import java.util.HashSet;
28import java.util.Set;
29
30/**
31 * Routing configuration.
32 */
33public 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 Harta7a24cf2017-02-02 16:13:57 -080054 Set<String> interfaces = new HashSet<>(array.size());
Jonathan Hart249b4cf2017-02-03 18:02:58 -080055 if (!intfNode.isMissingNode()) {
56 ArrayNode array = (ArrayNode) intfNode;
57 for (JsonNode intf : array) {
58 interfaces.add(intf.asText());
59 }
Jonathan Harta7a24cf2017-02-02 16:13:57 -080060 }
61
62 routers.add(new Router(connectPoint, ospfEnabled, interfaces));
63 }
64
Jonathan Hart249b4cf2017-02-03 18:02:58 -080065 return ImmutableSet.copyOf(routers);
Jonathan Harta7a24cf2017-02-02 16:13:57 -080066 }
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}