blob: 940a727d092022ce58b9bfe8373caa10c78290ce [file] [log] [blame]
Jonathan Harta7a24cf2017-02-02 16:13:57 -08001/*
2 * Copyright 2017-present Open Networking Laboratory
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
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;
22import org.onosproject.core.ApplicationId;
23import org.onosproject.net.ConnectPoint;
24import org.onosproject.net.config.Config;
25
26import java.util.HashSet;
27import java.util.Set;
28
29/**
30 * Routing configuration.
31 */
32public class RoutersConfig extends Config<ApplicationId> {
33
34 private static final String CP_CONNECT_POINT = "controlPlaneConnectPoint";
35 private static final String OSPF_ENABLED = "ospfEnabled";
36 private static final String PIM_ENABLED = "pimEnabled";
37 private static final String INTERFACES = "interfaces";
38
39 /**
40 * Gets the router configurations.
41 *
42 * @return set of router configurations
43 */
44 public Set<Router> getRouters() {
45 Set<Router> routers = new HashSet<>();
46
47 for (JsonNode routerNode : array) {
48 ConnectPoint connectPoint = ConnectPoint.deviceConnectPoint(routerNode.path(CP_CONNECT_POINT).asText());
49 boolean ospfEnabled = routerNode.path(OSPF_ENABLED).asBoolean();
50
51
52 JsonNode intfNode = routerNode.path(INTERFACES);
53 if (intfNode.isMissingNode() || !intfNode.isArray()) {
54 continue;
55 }
56 ArrayNode array = (ArrayNode) intfNode;
57 Set<String> interfaces = new HashSet<>(array.size());
58 for (JsonNode intf : array) {
59 interfaces.add(intf.asText());
60 }
61
62 routers.add(new Router(connectPoint, ospfEnabled, interfaces));
63 }
64
65 return routers;
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}