Kyuhwi Choi | dc2973b | 2016-05-13 14:54:31 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-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 | 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.Lists; |
| 22 | import com.google.common.collect.Sets; |
| 23 | import org.onlab.packet.Ip4Address; |
| 24 | import org.onosproject.core.ApplicationId; |
| 25 | import org.onosproject.net.DeviceId; |
| 26 | import org.onosproject.net.config.Config; |
| 27 | import org.slf4j.Logger; |
| 28 | |
| 29 | import java.util.Collections; |
| 30 | import java.util.List; |
| 31 | import java.util.Set; |
Kyuhwi Choi | 92d9ea4 | 2016-06-13 17:28:00 +0900 | [diff] [blame^] | 32 | import java.util.stream.StreamSupport; |
Kyuhwi Choi | dc2973b | 2016-05-13 14:54:31 +0900 | [diff] [blame] | 33 | |
Kyuhwi Choi | dc2973b | 2016-05-13 14:54:31 +0900 | [diff] [blame] | 34 | import static org.onosproject.net.config.Config.FieldPresence.MANDATORY; |
Kyuhwi Choi | 92d9ea4 | 2016-06-13 17:28:00 +0900 | [diff] [blame^] | 35 | import static org.slf4j.LoggerFactory.getLogger; |
Kyuhwi Choi | dc2973b | 2016-05-13 14:54:31 +0900 | [diff] [blame] | 36 | |
| 37 | /** |
| 38 | * Configuration object for OpensatckNode service. |
| 39 | */ |
| 40 | public class GatewayNodeConfig extends Config<ApplicationId> { |
| 41 | |
| 42 | protected final Logger log = getLogger(getClass()); |
| 43 | |
| 44 | public static final String NODES = "nodes"; |
| 45 | public static final String BRIDGE_ID = "bridgeId"; |
| 46 | public static final String DATAPLANE_IP = "dataPlaneIp"; |
| 47 | public static final String EXTERNAL_INTERFACE_NAME = "gatewayExternalInterfaceName"; |
| 48 | |
| 49 | /** |
| 50 | * Returns the set of nodes read from network config. |
| 51 | * |
| 52 | * @return set of OpensatckNodeConfig or null |
| 53 | */ |
| 54 | public Set<GatewayNode> gatewayNodes() { |
| 55 | |
| 56 | Set<GatewayNode> nodes = Sets.newHashSet(); |
| 57 | |
| 58 | JsonNode jsonNodes = object.get(NODES); |
| 59 | if (jsonNodes == null) { |
| 60 | return null; |
| 61 | } |
| 62 | |
| 63 | jsonNodes.forEach(jsonNode -> { |
| 64 | try { |
| 65 | nodes.add(new GatewayNode.Builder() |
| 66 | .gatewayDeviceId(DeviceId.deviceId(jsonNode.path(BRIDGE_ID).asText())) |
| 67 | .gatewayExternalInterfaceNames( |
| 68 | getExternalInterfaceName(jsonNode.path(EXTERNAL_INTERFACE_NAME).asText())) |
| 69 | .dataIpAddress(Ip4Address.valueOf(jsonNode.path(DATAPLANE_IP).asText())).build()); |
| 70 | } catch (IllegalArgumentException | NullPointerException e) { |
| 71 | log.error("Failed to read {}", e.toString()); |
| 72 | } |
| 73 | }); |
| 74 | return nodes; |
| 75 | } |
| 76 | |
| 77 | private List<String> getExternalInterfaceName(String s) { |
| 78 | List<String> list = Lists.newArrayList(); |
| 79 | return Collections.addAll(list, s.split(",")) ? list : null; |
| 80 | } |
| 81 | |
| 82 | @Override |
| 83 | public boolean isValid() { |
Kyuhwi Choi | 92d9ea4 | 2016-06-13 17:28:00 +0900 | [diff] [blame^] | 84 | JsonNode jsonNodes = object.get(NODES); |
| 85 | |
| 86 | if (jsonNodes == null) { |
| 87 | return false; |
| 88 | } |
| 89 | |
| 90 | return hasOnlyFields(NODES) |
| 91 | && StreamSupport.stream(jsonNodes.spliterator(), false).allMatch(this::checkValid); |
| 92 | } |
| 93 | |
| 94 | private boolean checkValid(JsonNode jsonNode) { |
| 95 | ObjectNode objectNode = (ObjectNode) jsonNode; |
| 96 | return isString(objectNode, BRIDGE_ID, MANDATORY) |
| 97 | && isIpAddress(objectNode, DATAPLANE_IP, MANDATORY) |
| 98 | && isString(objectNode, EXTERNAL_INTERFACE_NAME, MANDATORY); |
Kyuhwi Choi | dc2973b | 2016-05-13 14:54:31 +0900 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | } |