Kyuhwi Choi | dc2973b | 2016-05-13 14:54:31 +0900 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Kyuhwi Choi | dc2973b | 2016-05-13 14:54:31 +0900 | [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 | package org.onosproject.scalablegateway.api; |
| 17 | |
| 18 | |
| 19 | import com.fasterxml.jackson.databind.JsonNode; |
Kyuhwi Choi | 92d9ea4 | 2016-06-13 17:28:00 +0900 | [diff] [blame] | 20 | import com.fasterxml.jackson.databind.node.ObjectNode; |
Kyuhwi Choi | dc2973b | 2016-05-13 14:54:31 +0900 | [diff] [blame] | 21 | import com.google.common.collect.Sets; |
| 22 | import org.onlab.packet.Ip4Address; |
| 23 | import org.onosproject.core.ApplicationId; |
| 24 | import org.onosproject.net.DeviceId; |
| 25 | import org.onosproject.net.config.Config; |
| 26 | import org.slf4j.Logger; |
| 27 | |
Kyuhwi Choi | dc2973b | 2016-05-13 14:54:31 +0900 | [diff] [blame] | 28 | import java.util.Set; |
Kyuhwi Choi | 92d9ea4 | 2016-06-13 17:28:00 +0900 | [diff] [blame] | 29 | import java.util.stream.StreamSupport; |
Kyuhwi Choi | dc2973b | 2016-05-13 14:54:31 +0900 | [diff] [blame] | 30 | |
Kyuhwi Choi | dc2973b | 2016-05-13 14:54:31 +0900 | [diff] [blame] | 31 | import static org.onosproject.net.config.Config.FieldPresence.MANDATORY; |
Kyuhwi Choi | 92d9ea4 | 2016-06-13 17:28:00 +0900 | [diff] [blame] | 32 | import static org.slf4j.LoggerFactory.getLogger; |
Kyuhwi Choi | dc2973b | 2016-05-13 14:54:31 +0900 | [diff] [blame] | 33 | |
| 34 | /** |
| 35 | * Configuration object for OpensatckNode service. |
| 36 | */ |
| 37 | public class GatewayNodeConfig extends Config<ApplicationId> { |
| 38 | |
| 39 | protected final Logger log = getLogger(getClass()); |
| 40 | |
| 41 | public static final String NODES = "nodes"; |
| 42 | public static final String BRIDGE_ID = "bridgeId"; |
| 43 | public static final String DATAPLANE_IP = "dataPlaneIp"; |
Hyunsun Moon | f9a16ed | 2016-07-20 21:59:48 -0700 | [diff] [blame] | 44 | public static final String UPLINK_INTERFACE_NAME = "uplinkInterface"; |
Kyuhwi Choi | dc2973b | 2016-05-13 14:54:31 +0900 | [diff] [blame] | 45 | |
| 46 | /** |
| 47 | * Returns the set of nodes read from network config. |
| 48 | * |
daniel park | 9fc3d0e | 2017-09-04 15:15:17 +0900 | [diff] [blame] | 49 | * @return set of OpenstackNodeConfig or null |
Kyuhwi Choi | dc2973b | 2016-05-13 14:54:31 +0900 | [diff] [blame] | 50 | */ |
| 51 | public Set<GatewayNode> gatewayNodes() { |
| 52 | |
| 53 | Set<GatewayNode> nodes = Sets.newHashSet(); |
| 54 | |
| 55 | JsonNode jsonNodes = object.get(NODES); |
| 56 | if (jsonNodes == null) { |
| 57 | return null; |
| 58 | } |
| 59 | |
| 60 | jsonNodes.forEach(jsonNode -> { |
| 61 | try { |
| 62 | nodes.add(new GatewayNode.Builder() |
| 63 | .gatewayDeviceId(DeviceId.deviceId(jsonNode.path(BRIDGE_ID).asText())) |
Hyunsun Moon | f9a16ed | 2016-07-20 21:59:48 -0700 | [diff] [blame] | 64 | .uplinkIntf(jsonNode.path(UPLINK_INTERFACE_NAME).asText()) |
Kyuhwi Choi | dc2973b | 2016-05-13 14:54:31 +0900 | [diff] [blame] | 65 | .dataIpAddress(Ip4Address.valueOf(jsonNode.path(DATAPLANE_IP).asText())).build()); |
| 66 | } catch (IllegalArgumentException | NullPointerException e) { |
| 67 | log.error("Failed to read {}", e.toString()); |
| 68 | } |
| 69 | }); |
| 70 | return nodes; |
| 71 | } |
| 72 | |
Kyuhwi Choi | dc2973b | 2016-05-13 14:54:31 +0900 | [diff] [blame] | 73 | @Override |
| 74 | public boolean isValid() { |
Kyuhwi Choi | 92d9ea4 | 2016-06-13 17:28:00 +0900 | [diff] [blame] | 75 | JsonNode jsonNodes = object.get(NODES); |
| 76 | |
| 77 | if (jsonNodes == null) { |
| 78 | return false; |
| 79 | } |
| 80 | |
| 81 | return hasOnlyFields(NODES) |
| 82 | && StreamSupport.stream(jsonNodes.spliterator(), false).allMatch(this::checkValid); |
| 83 | } |
| 84 | |
| 85 | private boolean checkValid(JsonNode jsonNode) { |
| 86 | ObjectNode objectNode = (ObjectNode) jsonNode; |
| 87 | return isString(objectNode, BRIDGE_ID, MANDATORY) |
| 88 | && isIpAddress(objectNode, DATAPLANE_IP, MANDATORY) |
Hyunsun Moon | f9a16ed | 2016-07-20 21:59:48 -0700 | [diff] [blame] | 89 | && isString(objectNode, UPLINK_INTERFACE_NAME, MANDATORY); |
Kyuhwi Choi | dc2973b | 2016-05-13 14:54:31 +0900 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | } |