blob: 0f264e95a4b5c9443de613ec814975672db9555b [file] [log] [blame]
Daniel Parka7d6e9f2016-01-18 17:54:14 +09001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Daniel Parka7d6e9f2016-01-18 17:54:14 +09003 *
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 */
16package org.onosproject.openstacknode;
17
18
19import com.fasterxml.jackson.databind.JsonNode;
Hyunsun Moon34bbe172016-06-28 19:18:40 -070020import com.fasterxml.jackson.databind.node.ObjectNode;
Daniel Parka7d6e9f2016-01-18 17:54:14 +090021import com.google.common.collect.Sets;
Hyunsun Moon34bbe172016-06-28 19:18:40 -070022import org.onlab.packet.IpAddress;
Daniel Parka7d6e9f2016-01-18 17:54:14 +090023import org.onosproject.core.ApplicationId;
24import org.onosproject.net.DeviceId;
Hyunsun Moon34bbe172016-06-28 19:18:40 -070025import org.onosproject.openstacknode.OpenstackNodeService.NodeType;
Daniel Parka7d6e9f2016-01-18 17:54:14 +090026import java.util.Set;
27import org.onosproject.net.config.Config;
Hyunsun Moon34bbe172016-06-28 19:18:40 -070028
29import static org.onosproject.net.config.Config.FieldPresence.MANDATORY;
daniel park917beb42017-03-16 18:07:15 +090030import static org.onosproject.net.config.Config.FieldPresence.OPTIONAL;
Hyunsun Moon34bbe172016-06-28 19:18:40 -070031import static org.onosproject.openstacknode.OpenstackNodeService.NodeType.GATEWAY;
Daniel Parka7d6e9f2016-01-18 17:54:14 +090032
33/**
34 * Configuration object for OpensatckNode service.
35 */
Hyunsun Moon34bbe172016-06-28 19:18:40 -070036public final class OpenstackNodeConfig extends Config<ApplicationId> {
Daniel Parka7d6e9f2016-01-18 17:54:14 +090037
Hyunsun Moon34bbe172016-06-28 19:18:40 -070038 private static final String NODES = "nodes";
39 private static final String HOST_NAME = "hostname";
40 private static final String TYPE = "type";
41 private static final String MANAGEMENT_IP = "managementIp";
42 private static final String DATA_IP = "dataIp";
43 private static final String INTEGRATION_BRIDGE = "integrationBridge";
Hyunsun Moon052c71f2016-07-11 18:56:18 -070044
45 // GATEWAY node specific fields
Hyunsun Moon34bbe172016-06-28 19:18:40 -070046 private static final String ROUTER_BRIDGE = "routerBridge";
Hyunsun Moon052c71f2016-07-11 18:56:18 -070047 private static final String UPLINK_PORT_NAME = "uplinkPort";
48 // TODO remove this when vRouter supports multiple switches
49 private static final String ROUTER_CONTROLLER = "routerController";
daniel park917beb42017-03-16 18:07:15 +090050 private static final String VLAN_PORT_NAME = "vlanPort";
Daniel Parka7d6e9f2016-01-18 17:54:14 +090051
Hyunsun Moon34bbe172016-06-28 19:18:40 -070052 @Override
53 public boolean isValid() {
54 boolean result = hasOnlyFields(NODES);
Daniel Parka7d6e9f2016-01-18 17:54:14 +090055
Hyunsun Moon34bbe172016-06-28 19:18:40 -070056 if (object.get(NODES) == null || object.get(NODES).size() < 1) {
57 final String msg = "No node is present";
58 throw new IllegalArgumentException(msg);
59 }
60
61 for (JsonNode node : object.get(NODES)) {
daniel park917beb42017-03-16 18:07:15 +090062 if (get(node, DATA_IP) == null && get(node, VLAN_PORT_NAME) == null) {
63 final String msg = "There is neither tunnel interface nor vlan port";
64 throw new IllegalArgumentException(msg);
65 }
Hyunsun Moon34bbe172016-06-28 19:18:40 -070066 ObjectNode osNode = (ObjectNode) node;
67 result &= hasOnlyFields(osNode,
68 HOST_NAME,
69 TYPE,
70 MANAGEMENT_IP,
71 DATA_IP,
72 INTEGRATION_BRIDGE,
Hyunsun Moon052c71f2016-07-11 18:56:18 -070073 ROUTER_BRIDGE,
74 UPLINK_PORT_NAME,
daniel park917beb42017-03-16 18:07:15 +090075 ROUTER_CONTROLLER,
76 VLAN_PORT_NAME
Hyunsun Moon34bbe172016-06-28 19:18:40 -070077 );
78
79 result &= isString(osNode, HOST_NAME, MANDATORY);
80 result &= isString(osNode, TYPE, MANDATORY);
81 result &= isIpAddress(osNode, MANAGEMENT_IP, MANDATORY);
Hyunsun Moon34bbe172016-06-28 19:18:40 -070082 result &= isString(osNode, INTEGRATION_BRIDGE, MANDATORY);
daniel park917beb42017-03-16 18:07:15 +090083 result &= isString(osNode, VLAN_PORT_NAME, OPTIONAL);
84 result &= isIpAddress(osNode, DATA_IP, OPTIONAL);
Hyunsun Moon34bbe172016-06-28 19:18:40 -070085
86 DeviceId.deviceId(osNode.get(INTEGRATION_BRIDGE).asText());
87 NodeType.valueOf(osNode.get(TYPE).asText());
88
89 if (osNode.get(TYPE).asText().equals(GATEWAY.name())) {
90 result &= isString(osNode, ROUTER_BRIDGE, MANDATORY);
91 DeviceId.deviceId(osNode.get(ROUTER_BRIDGE).asText());
Hyunsun Moon052c71f2016-07-11 18:56:18 -070092 result &= isString(osNode, UPLINK_PORT_NAME, MANDATORY);
93 result &= isIpAddress(osNode, ROUTER_CONTROLLER, MANDATORY);
Hyunsun Moon34bbe172016-06-28 19:18:40 -070094 }
95 }
96 return result;
97 }
Daniel Parka7d6e9f2016-01-18 17:54:14 +090098
99 /**
100 * Returns the set of nodes read from network config.
101 *
Hyunsun Moon34bbe172016-06-28 19:18:40 -0700102 * @return set of openstack nodes
Daniel Parka7d6e9f2016-01-18 17:54:14 +0900103 */
104 public Set<OpenstackNode> openstackNodes() {
Daniel Parka7d6e9f2016-01-18 17:54:14 +0900105 Set<OpenstackNode> nodes = Sets.newHashSet();
Hyunsun Moon34bbe172016-06-28 19:18:40 -0700106 for (JsonNode node : object.get(NODES)) {
107 NodeType type = NodeType.valueOf(get(node, TYPE));
108 OpenstackNode.Builder nodeBuilder = OpenstackNode.builder()
109 .integrationBridge(DeviceId.deviceId(get(node, INTEGRATION_BRIDGE)))
Hyunsun Moon34bbe172016-06-28 19:18:40 -0700110 .managementIp(IpAddress.valueOf(get(node, MANAGEMENT_IP)))
111 .type(type)
112 .hostname(get(node, HOST_NAME));
Daniel Parka7d6e9f2016-01-18 17:54:14 +0900113
daniel park917beb42017-03-16 18:07:15 +0900114 if (get(node, DATA_IP) != null) {
115 nodeBuilder.dataIp(IpAddress.valueOf(get(node, DATA_IP)));
116 }
117
118 if (get(node, VLAN_PORT_NAME) != null) {
119 nodeBuilder.vlanPort(get(node, VLAN_PORT_NAME));
120 }
121
Hyunsun Moon34bbe172016-06-28 19:18:40 -0700122 if (type.equals(GATEWAY)) {
Hyunsun Moon052c71f2016-07-11 18:56:18 -0700123 nodeBuilder.routerBridge(DeviceId.deviceId(get(node, ROUTER_BRIDGE)))
124 .uplink(get(node, UPLINK_PORT_NAME))
125 .routerController(IpAddress.valueOf(get(node, ROUTER_CONTROLLER)));
Daniel Parka7d6e9f2016-01-18 17:54:14 +0900126 }
Hyunsun Moon34bbe172016-06-28 19:18:40 -0700127 nodes.add(nodeBuilder.build());
128 }
Daniel Parka7d6e9f2016-01-18 17:54:14 +0900129 return nodes;
130 }
Hyunsun Moon34bbe172016-06-28 19:18:40 -0700131
132 private String get(JsonNode jsonNode, String path) {
daniel park917beb42017-03-16 18:07:15 +0900133 JsonNode jNode = jsonNode.get(path);
134 if (jNode == null || jNode.isMissingNode()) {
135 return null;
136 }
137 return jNode.asText();
Hyunsun Moon34bbe172016-06-28 19:18:40 -0700138 }
Daniel Parka7d6e9f2016-01-18 17:54:14 +0900139}