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 | |
Jian Li | e631216 | 2018-03-21 21:41:00 +0900 | [diff] [blame] | 18 | import com.fasterxml.jackson.databind.JsonNode; |
| 19 | import com.fasterxml.jackson.databind.node.ArrayNode; |
Jian Li | 23c8be2 | 2018-02-13 11:34:15 +0900 | [diff] [blame] | 20 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 21 | import org.onlab.packet.IpAddress; |
| 22 | import org.onosproject.codec.CodecContext; |
| 23 | import org.onosproject.codec.JsonCodec; |
| 24 | import org.onosproject.net.DeviceId; |
| 25 | import org.onosproject.openstacknode.api.NodeState; |
Jian Li | 2784166 | 2018-04-14 01:59:47 +0900 | [diff] [blame] | 26 | import org.onosproject.openstacknode.api.OpenstackAuth; |
Jian Li | 23c8be2 | 2018-02-13 11:34:15 +0900 | [diff] [blame] | 27 | import org.onosproject.openstacknode.api.OpenstackNode; |
Jian Li | e631216 | 2018-03-21 21:41:00 +0900 | [diff] [blame] | 28 | import org.onosproject.openstacknode.api.OpenstackPhyInterface; |
Jian Li | 23c8be2 | 2018-02-13 11:34:15 +0900 | [diff] [blame] | 29 | import org.onosproject.openstacknode.impl.DefaultOpenstackNode; |
| 30 | import org.slf4j.Logger; |
| 31 | |
Jian Li | e631216 | 2018-03-21 21:41:00 +0900 | [diff] [blame] | 32 | import java.util.ArrayList; |
| 33 | import java.util.List; |
| 34 | import java.util.stream.IntStream; |
| 35 | |
Jian Li | 23c8be2 | 2018-02-13 11:34:15 +0900 | [diff] [blame] | 36 | import static com.google.common.base.Preconditions.checkNotNull; |
| 37 | import static org.onlab.util.Tools.nullIsIllegal; |
Jian Li | 2784166 | 2018-04-14 01:59:47 +0900 | [diff] [blame] | 38 | import static org.onosproject.openstacknode.api.Constants.CONTROLLER; |
Jian Li | 23c8be2 | 2018-02-13 11:34:15 +0900 | [diff] [blame] | 39 | import static org.onosproject.openstacknode.api.Constants.DATA_IP; |
| 40 | import static org.onosproject.openstacknode.api.Constants.GATEWAY; |
| 41 | import static org.onosproject.openstacknode.api.Constants.HOST_NAME; |
| 42 | import static org.onosproject.openstacknode.api.Constants.MANAGEMENT_IP; |
| 43 | import static org.onosproject.openstacknode.api.Constants.UPLINK_PORT; |
| 44 | import static org.onosproject.openstacknode.api.Constants.VLAN_INTF_NAME; |
| 45 | import static org.slf4j.LoggerFactory.getLogger; |
| 46 | |
| 47 | /** |
| 48 | * Openstack node codec used for serializing and de-serializing JSON string. |
| 49 | */ |
| 50 | public final class OpenstackNodeCodec extends JsonCodec<OpenstackNode> { |
| 51 | |
| 52 | private final Logger log = getLogger(getClass()); |
| 53 | |
| 54 | private static final String TYPE = "type"; |
| 55 | private static final String INTEGRATION_BRIDGE = "integrationBridge"; |
Jian Li | 5ca0be8 | 2018-02-27 13:08:04 +0900 | [diff] [blame] | 56 | private static final String STATE = "state"; |
Jian Li | e631216 | 2018-03-21 21:41:00 +0900 | [diff] [blame] | 57 | private static final String PHYSICAL_INTERFACES = "phyIntfs"; |
Jian Li | 2784166 | 2018-04-14 01:59:47 +0900 | [diff] [blame] | 58 | private static final String AUTHENTICATION = "authentication"; |
Jian Li | 23c8be2 | 2018-02-13 11:34:15 +0900 | [diff] [blame] | 59 | |
| 60 | private static final String MISSING_MESSAGE = " is required in OpenstackNode"; |
| 61 | |
| 62 | @Override |
| 63 | public ObjectNode encode(OpenstackNode node, CodecContext context) { |
| 64 | checkNotNull(node, "Openstack node cannot be null"); |
| 65 | |
| 66 | ObjectNode result = context.mapper().createObjectNode() |
| 67 | .put(HOST_NAME, node.hostname()) |
| 68 | .put(TYPE, node.type().name()) |
| 69 | .put(MANAGEMENT_IP, node.managementIp().toString()) |
Jian Li | 5ca0be8 | 2018-02-27 13:08:04 +0900 | [diff] [blame] | 70 | .put(STATE, node.state().name()); |
Jian Li | 23c8be2 | 2018-02-13 11:34:15 +0900 | [diff] [blame] | 71 | |
| 72 | OpenstackNode.NodeType type = node.type(); |
| 73 | |
| 74 | if (type == OpenstackNode.NodeType.GATEWAY) { |
| 75 | result.put(UPLINK_PORT, node.uplinkPort()); |
| 76 | } |
| 77 | |
Jian Li | 2784166 | 2018-04-14 01:59:47 +0900 | [diff] [blame] | 78 | if (type != OpenstackNode.NodeType.CONTROLLER) { |
| 79 | result.put(INTEGRATION_BRIDGE, node.intgBridge().toString()); |
| 80 | } |
| 81 | |
Jian Li | 23c8be2 | 2018-02-13 11:34:15 +0900 | [diff] [blame] | 82 | if (node.vlanIntf() != null) { |
| 83 | result.put(VLAN_INTF_NAME, node.vlanIntf()); |
| 84 | } |
| 85 | |
| 86 | if (node.dataIp() != null) { |
| 87 | result.put(DATA_IP, node.dataIp().toString()); |
| 88 | } |
| 89 | |
Jian Li | 5b402c7 | 2018-02-27 14:27:34 +0900 | [diff] [blame] | 90 | // TODO: need to find a way to not refer to ServiceDirectory from |
| 91 | // DefaultOpenstackNode |
Jian Li | 5ca0be8 | 2018-02-27 13:08:04 +0900 | [diff] [blame] | 92 | |
Jian Li | e631216 | 2018-03-21 21:41:00 +0900 | [diff] [blame] | 93 | ArrayNode phyIntfs = context.mapper().createArrayNode(); |
| 94 | node.phyIntfs().forEach(phyIntf -> { |
| 95 | ObjectNode phyIntfJson = context.codec(OpenstackPhyInterface.class).encode(phyIntf, context); |
| 96 | phyIntfs.add(phyIntfJson); |
| 97 | }); |
| 98 | result.set(PHYSICAL_INTERFACES, phyIntfs); |
| 99 | |
Jian Li | 2784166 | 2018-04-14 01:59:47 +0900 | [diff] [blame] | 100 | if (node.authentication() != null) { |
| 101 | ObjectNode authJson = context.codec(OpenstackAuth.class) |
| 102 | .encode(node.authentication(), context); |
| 103 | result.put(AUTHENTICATION, authJson); |
| 104 | } |
| 105 | |
Jian Li | 23c8be2 | 2018-02-13 11:34:15 +0900 | [diff] [blame] | 106 | return result; |
| 107 | } |
| 108 | |
| 109 | @Override |
| 110 | public OpenstackNode decode(ObjectNode json, CodecContext context) { |
| 111 | if (json == null || !json.isObject()) { |
| 112 | return null; |
| 113 | } |
| 114 | |
| 115 | String hostname = nullIsIllegal(json.get(HOST_NAME).asText(), |
| 116 | HOST_NAME + MISSING_MESSAGE); |
| 117 | String type = nullIsIllegal(json.get(TYPE).asText(), |
| 118 | TYPE + MISSING_MESSAGE); |
| 119 | String mIp = nullIsIllegal(json.get(MANAGEMENT_IP).asText(), |
| 120 | MANAGEMENT_IP + MISSING_MESSAGE); |
Jian Li | 23c8be2 | 2018-02-13 11:34:15 +0900 | [diff] [blame] | 121 | |
| 122 | DefaultOpenstackNode.Builder nodeBuilder = DefaultOpenstackNode.builder() |
| 123 | .hostname(hostname) |
| 124 | .type(OpenstackNode.NodeType.valueOf(type)) |
| 125 | .managementIp(IpAddress.valueOf(mIp)) |
Jian Li | 23c8be2 | 2018-02-13 11:34:15 +0900 | [diff] [blame] | 126 | .state(NodeState.INIT); |
| 127 | |
| 128 | if (type.equals(GATEWAY)) { |
| 129 | nodeBuilder.uplinkPort(nullIsIllegal(json.get(UPLINK_PORT).asText(), |
| 130 | UPLINK_PORT + MISSING_MESSAGE)); |
| 131 | } |
Jian Li | 2784166 | 2018-04-14 01:59:47 +0900 | [diff] [blame] | 132 | if (!type.equals(CONTROLLER)) { |
| 133 | String iBridge = nullIsIllegal(json.get(INTEGRATION_BRIDGE).asText(), |
| 134 | INTEGRATION_BRIDGE + MISSING_MESSAGE); |
| 135 | nodeBuilder.intgBridge(DeviceId.deviceId(iBridge)); |
| 136 | } |
Jian Li | 23c8be2 | 2018-02-13 11:34:15 +0900 | [diff] [blame] | 137 | if (json.get(VLAN_INTF_NAME) != null) { |
| 138 | nodeBuilder.vlanIntf(json.get(VLAN_INTF_NAME).asText()); |
| 139 | } |
| 140 | if (json.get(DATA_IP) != null) { |
| 141 | nodeBuilder.dataIp(IpAddress.valueOf(json.get(DATA_IP).asText())); |
| 142 | } |
| 143 | |
Jian Li | e631216 | 2018-03-21 21:41:00 +0900 | [diff] [blame] | 144 | // parse physical interfaces |
| 145 | List<OpenstackPhyInterface> phyIntfs = new ArrayList<>(); |
| 146 | JsonNode phyIntfsJson = json.get(PHYSICAL_INTERFACES); |
| 147 | if (phyIntfsJson != null) { |
Jian Li | 2784166 | 2018-04-14 01:59:47 +0900 | [diff] [blame] | 148 | |
| 149 | final JsonCodec<OpenstackPhyInterface> |
| 150 | phyIntfCodec = context.codec(OpenstackPhyInterface.class); |
| 151 | |
Jian Li | e631216 | 2018-03-21 21:41:00 +0900 | [diff] [blame] | 152 | IntStream.range(0, phyIntfsJson.size()).forEach(i -> { |
| 153 | ObjectNode intfJson = get(phyIntfsJson, i); |
| 154 | phyIntfs.add(phyIntfCodec.decode(intfJson, context)); |
| 155 | }); |
| 156 | } |
| 157 | nodeBuilder.phyIntfs(phyIntfs); |
| 158 | |
Jian Li | 2784166 | 2018-04-14 01:59:47 +0900 | [diff] [blame] | 159 | // parse authentication |
| 160 | JsonNode authJson = json.get(AUTHENTICATION); |
| 161 | if (json.get(AUTHENTICATION) != null) { |
| 162 | |
| 163 | final JsonCodec<OpenstackAuth> authCodec = context.codec(OpenstackAuth.class); |
| 164 | |
| 165 | OpenstackAuth auth = authCodec.decode((ObjectNode) authJson.deepCopy(), context); |
| 166 | nodeBuilder.authentication(auth); |
| 167 | } |
| 168 | |
Jian Li | 23c8be2 | 2018-02-13 11:34:15 +0900 | [diff] [blame] | 169 | log.trace("node is {}", nodeBuilder.build().toString()); |
| 170 | |
| 171 | return nodeBuilder.build(); |
| 172 | } |
| 173 | } |