Daniel Park | a7d6e9f | 2016-01-18 17:54:14 +0900 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Daniel Park | a7d6e9f | 2016-01-18 17:54:14 +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.openstacknode; |
| 17 | |
| 18 | |
| 19 | import com.fasterxml.jackson.databind.JsonNode; |
| 20 | import com.google.common.collect.Sets; |
| 21 | import org.onlab.packet.Ip4Address; |
| 22 | import org.onlab.packet.MacAddress; |
| 23 | import org.onlab.packet.TpPort; |
| 24 | import org.onosproject.core.ApplicationId; |
| 25 | import org.onosproject.net.DeviceId; |
| 26 | import org.slf4j.Logger; |
| 27 | import java.util.Set; |
| 28 | import org.onosproject.net.config.Config; |
| 29 | import static org.slf4j.LoggerFactory.getLogger; |
| 30 | |
| 31 | /** |
| 32 | * Configuration object for OpensatckNode service. |
| 33 | */ |
| 34 | public class OpenstackNodeConfig extends Config<ApplicationId> { |
| 35 | |
| 36 | protected final Logger log = getLogger(getClass()); |
| 37 | |
| 38 | |
| 39 | public static final String NODES = "nodes"; |
| 40 | public static final String HOST_NAME = "hostname"; |
| 41 | public static final String OVSDB_IP = "ovsdbIp"; |
| 42 | public static final String OVSDB_PORT = "ovsdbPort"; |
| 43 | public static final String BRIDGE_ID = "bridgeId"; |
| 44 | public static final String NODE_TYPE = "openstackNodeType"; |
| 45 | public static final String GATEWAY_EXTERNAL_INTERFACE_NAME = "gatewayExternalInterfaceName"; |
| 46 | public static final String GATEWAY_EXTERNAL_INTERFACE_MAC = "gatewayExternalInterfaceMac"; |
| 47 | |
| 48 | /** |
| 49 | * Returns the set of nodes read from network config. |
| 50 | * |
| 51 | * @return set of OpensatckNodeConfig or null |
| 52 | */ |
| 53 | public Set<OpenstackNode> openstackNodes() { |
| 54 | |
| 55 | Set<OpenstackNode> nodes = Sets.newHashSet(); |
| 56 | |
| 57 | JsonNode jsonNodes = object.get(NODES); |
| 58 | if (jsonNodes == null) { |
| 59 | return null; |
| 60 | } |
| 61 | |
| 62 | jsonNodes.forEach(jsonNode -> { |
| 63 | try { |
| 64 | if (OpenstackNodeService.OpenstackNodeType.valueOf(jsonNode.path(NODE_TYPE).asText()) == |
| 65 | OpenstackNodeService.OpenstackNodeType.COMPUTENODE) { |
| 66 | nodes.add(new OpenstackNode( |
| 67 | jsonNode.path(HOST_NAME).asText(), |
| 68 | Ip4Address.valueOf(jsonNode.path(OVSDB_IP).asText()), |
| 69 | TpPort.tpPort(jsonNode.path(OVSDB_PORT).asInt()), |
| 70 | DeviceId.deviceId(jsonNode.path(BRIDGE_ID).asText()), |
| 71 | OpenstackNodeService.OpenstackNodeType.valueOf(jsonNode.path(NODE_TYPE).asText()), |
| 72 | null, MacAddress.NONE)); |
| 73 | } else { |
| 74 | nodes.add(new OpenstackNode( |
| 75 | jsonNode.path(HOST_NAME).asText(), |
| 76 | Ip4Address.valueOf(jsonNode.path(OVSDB_IP).asText()), |
| 77 | TpPort.tpPort(jsonNode.path(OVSDB_PORT).asInt()), |
| 78 | DeviceId.deviceId(jsonNode.path(BRIDGE_ID).asText()), |
| 79 | OpenstackNodeService.OpenstackNodeType.valueOf(jsonNode.path(NODE_TYPE).asText()), |
| 80 | jsonNode.path(GATEWAY_EXTERNAL_INTERFACE_NAME).asText(), |
| 81 | MacAddress.valueOf(jsonNode.path(GATEWAY_EXTERNAL_INTERFACE_MAC).asText()))); |
| 82 | } |
| 83 | } catch (IllegalArgumentException | NullPointerException e) { |
| 84 | log.error("Failed to read {}", e.toString()); |
| 85 | } |
| 86 | }); |
| 87 | return nodes; |
| 88 | } |
| 89 | } |