Jian Li | 23c8be2 | 2018-02-13 11:34:15 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-present Open Networking Foundation |
| 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.codec; |
| 17 | |
| 18 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 19 | import org.onlab.packet.IpAddress; |
| 20 | import org.onosproject.codec.CodecContext; |
| 21 | import org.onosproject.codec.JsonCodec; |
| 22 | import org.onosproject.net.DeviceId; |
| 23 | import org.onosproject.openstacknode.api.NodeState; |
| 24 | import org.onosproject.openstacknode.api.OpenstackNode; |
| 25 | import org.onosproject.openstacknode.impl.DefaultOpenstackNode; |
| 26 | import org.slf4j.Logger; |
| 27 | |
| 28 | import static com.google.common.base.Preconditions.checkNotNull; |
| 29 | import static org.onlab.util.Tools.nullIsIllegal; |
| 30 | import static org.onosproject.openstacknode.api.Constants.DATA_IP; |
| 31 | import static org.onosproject.openstacknode.api.Constants.GATEWAY; |
| 32 | import static org.onosproject.openstacknode.api.Constants.HOST_NAME; |
| 33 | import static org.onosproject.openstacknode.api.Constants.MANAGEMENT_IP; |
| 34 | import static org.onosproject.openstacknode.api.Constants.UPLINK_PORT; |
| 35 | import static org.onosproject.openstacknode.api.Constants.VLAN_INTF_NAME; |
| 36 | import static org.slf4j.LoggerFactory.getLogger; |
| 37 | |
| 38 | /** |
| 39 | * Openstack node codec used for serializing and de-serializing JSON string. |
| 40 | */ |
| 41 | public final class OpenstackNodeCodec extends JsonCodec<OpenstackNode> { |
| 42 | |
| 43 | private final Logger log = getLogger(getClass()); |
| 44 | |
| 45 | private static final String TYPE = "type"; |
| 46 | private static final String INTEGRATION_BRIDGE = "integrationBridge"; |
Jian Li | 5ca0be8 | 2018-02-27 13:08:04 +0900 | [diff] [blame] | 47 | private static final String TUNNEL_PORT_NUM = "tunnelPortNum"; |
| 48 | private static final String VLAN_PORT_NUM = "vlanPortNum"; |
| 49 | private static final String STATE = "state"; |
Jian Li | 23c8be2 | 2018-02-13 11:34:15 +0900 | [diff] [blame] | 50 | |
| 51 | private static final String MISSING_MESSAGE = " is required in OpenstackNode"; |
| 52 | |
| 53 | @Override |
| 54 | public ObjectNode encode(OpenstackNode node, CodecContext context) { |
| 55 | checkNotNull(node, "Openstack node cannot be null"); |
| 56 | |
| 57 | ObjectNode result = context.mapper().createObjectNode() |
| 58 | .put(HOST_NAME, node.hostname()) |
| 59 | .put(TYPE, node.type().name()) |
| 60 | .put(MANAGEMENT_IP, node.managementIp().toString()) |
Jian Li | 5ca0be8 | 2018-02-27 13:08:04 +0900 | [diff] [blame] | 61 | .put(INTEGRATION_BRIDGE, node.intgBridge().toString()) |
| 62 | .put(STATE, node.state().name()); |
Jian Li | 23c8be2 | 2018-02-13 11:34:15 +0900 | [diff] [blame] | 63 | |
| 64 | OpenstackNode.NodeType type = node.type(); |
| 65 | |
| 66 | if (type == OpenstackNode.NodeType.GATEWAY) { |
| 67 | result.put(UPLINK_PORT, node.uplinkPort()); |
| 68 | } |
| 69 | |
| 70 | if (node.vlanIntf() != null) { |
| 71 | result.put(VLAN_INTF_NAME, node.vlanIntf()); |
| 72 | } |
| 73 | |
| 74 | if (node.dataIp() != null) { |
| 75 | result.put(DATA_IP, node.dataIp().toString()); |
| 76 | } |
| 77 | |
Jian Li | 5ca0be8 | 2018-02-27 13:08:04 +0900 | [diff] [blame] | 78 | if (node.tunnelPortNum() != null) { |
| 79 | result.put(TUNNEL_PORT_NUM, node.tunnelPortNum().toString()); |
| 80 | } |
| 81 | |
| 82 | if (node.vlanPortNum() != null) { |
| 83 | result.put(VLAN_PORT_NUM, node.vlanPortNum().toString()); |
| 84 | } |
| 85 | |
Jian Li | 23c8be2 | 2018-02-13 11:34:15 +0900 | [diff] [blame] | 86 | return result; |
| 87 | } |
| 88 | |
| 89 | @Override |
| 90 | public OpenstackNode decode(ObjectNode json, CodecContext context) { |
| 91 | if (json == null || !json.isObject()) { |
| 92 | return null; |
| 93 | } |
| 94 | |
| 95 | String hostname = nullIsIllegal(json.get(HOST_NAME).asText(), |
| 96 | HOST_NAME + MISSING_MESSAGE); |
| 97 | String type = nullIsIllegal(json.get(TYPE).asText(), |
| 98 | TYPE + MISSING_MESSAGE); |
| 99 | String mIp = nullIsIllegal(json.get(MANAGEMENT_IP).asText(), |
| 100 | MANAGEMENT_IP + MISSING_MESSAGE); |
| 101 | String iBridge = nullIsIllegal(json.get(INTEGRATION_BRIDGE).asText(), |
| 102 | INTEGRATION_BRIDGE + MISSING_MESSAGE); |
| 103 | |
| 104 | DefaultOpenstackNode.Builder nodeBuilder = DefaultOpenstackNode.builder() |
| 105 | .hostname(hostname) |
| 106 | .type(OpenstackNode.NodeType.valueOf(type)) |
| 107 | .managementIp(IpAddress.valueOf(mIp)) |
| 108 | .intgBridge(DeviceId.deviceId(iBridge)) |
| 109 | .state(NodeState.INIT); |
| 110 | |
| 111 | if (type.equals(GATEWAY)) { |
| 112 | nodeBuilder.uplinkPort(nullIsIllegal(json.get(UPLINK_PORT).asText(), |
| 113 | UPLINK_PORT + MISSING_MESSAGE)); |
| 114 | } |
| 115 | if (json.get(VLAN_INTF_NAME) != null) { |
| 116 | nodeBuilder.vlanIntf(json.get(VLAN_INTF_NAME).asText()); |
| 117 | } |
| 118 | if (json.get(DATA_IP) != null) { |
| 119 | nodeBuilder.dataIp(IpAddress.valueOf(json.get(DATA_IP).asText())); |
| 120 | } |
| 121 | |
| 122 | log.trace("node is {}", nodeBuilder.build().toString()); |
| 123 | |
| 124 | return nodeBuilder.build(); |
| 125 | } |
| 126 | } |