Jian Li | cc01e45 | 2020-12-21 01:44:05 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2020-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.kubevirtnode.codec; |
| 17 | |
| 18 | import com.fasterxml.jackson.databind.JsonNode; |
| 19 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 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.kubevirtnode.api.DefaultKubevirtNode; |
| 25 | import org.onosproject.kubevirtnode.api.KubevirtNode; |
| 26 | import org.onosproject.kubevirtnode.api.KubevirtNodeState; |
| 27 | import org.onosproject.kubevirtnode.api.KubevirtPhyInterface; |
| 28 | import org.onosproject.net.DeviceId; |
| 29 | import org.slf4j.Logger; |
| 30 | |
| 31 | import java.util.ArrayList; |
| 32 | import java.util.List; |
| 33 | import java.util.stream.IntStream; |
| 34 | |
| 35 | import static com.google.common.base.Preconditions.checkNotNull; |
| 36 | import static org.onlab.util.Tools.nullIsIllegal; |
| 37 | import static org.onosproject.kubevirtnode.api.Constants.DATA_IP; |
| 38 | import static org.onosproject.kubevirtnode.api.Constants.HOST_NAME; |
| 39 | import static org.onosproject.kubevirtnode.api.Constants.MANAGEMENT_IP; |
| 40 | import static org.slf4j.LoggerFactory.getLogger; |
| 41 | |
| 42 | /** |
| 43 | * Kubevirt node codec used for serializing and de-serializing JSON string. |
| 44 | */ |
| 45 | public final class KubevirtNodeCodec extends JsonCodec<KubevirtNode> { |
| 46 | |
| 47 | private final Logger log = getLogger(getClass()); |
| 48 | |
| 49 | private static final String TYPE = "type"; |
| 50 | private static final String INTEGRATION_BRIDGE = "integrationBridge"; |
| 51 | private static final String STATE = "state"; |
| 52 | private static final String PHYSICAL_INTERFACES = "phyIntfs"; |
| 53 | |
| 54 | private static final String MISSING_MESSAGE = " is required in OpenstackNode"; |
| 55 | |
| 56 | @Override |
| 57 | public ObjectNode encode(KubevirtNode node, CodecContext context) { |
| 58 | checkNotNull(node, "Kubevirt node cannot be null"); |
| 59 | |
| 60 | ObjectNode result = context.mapper().createObjectNode() |
| 61 | .put(HOST_NAME, node.hostname()) |
| 62 | .put(TYPE, node.type().name()) |
| 63 | .put(STATE, node.state().name()) |
| 64 | .put(MANAGEMENT_IP, node.managementIp().toString()); |
| 65 | |
| 66 | // serialize integration bridge config |
| 67 | if (node.intgBridge() != null) { |
| 68 | result.put(INTEGRATION_BRIDGE, node.intgBridge().toString()); |
| 69 | } |
| 70 | |
| 71 | // serialize data IP only if it presents |
| 72 | if (node.dataIp() != null) { |
| 73 | result.put(DATA_IP, node.dataIp().toString()); |
| 74 | } |
| 75 | |
| 76 | // serialize physical interfaces, it is valid only if any of physical interface presents |
| 77 | if (node.phyIntfs() != null && !node.phyIntfs().isEmpty()) { |
| 78 | ArrayNode phyIntfs = context.mapper().createArrayNode(); |
| 79 | node.phyIntfs().forEach(phyIntf -> { |
| 80 | ObjectNode phyIntfJson = |
| 81 | context.codec(KubevirtPhyInterface.class).encode(phyIntf, context); |
| 82 | phyIntfs.add(phyIntfJson); |
| 83 | }); |
| 84 | result.set(PHYSICAL_INTERFACES, phyIntfs); |
| 85 | } |
| 86 | |
| 87 | return result; |
| 88 | } |
| 89 | |
| 90 | @Override |
| 91 | public KubevirtNode decode(ObjectNode json, CodecContext context) { |
| 92 | if (json == null || !json.isObject()) { |
| 93 | return null; |
| 94 | } |
| 95 | |
| 96 | String hostname = nullIsIllegal(json.get(HOST_NAME).asText(), |
| 97 | HOST_NAME + MISSING_MESSAGE); |
| 98 | String type = nullIsIllegal(json.get(TYPE).asText(), |
| 99 | TYPE + MISSING_MESSAGE); |
| 100 | String mIp = nullIsIllegal(json.get(MANAGEMENT_IP).asText(), |
| 101 | MANAGEMENT_IP + MISSING_MESSAGE); |
| 102 | |
| 103 | KubevirtNode.Builder nodeBuilder = DefaultKubevirtNode.builder() |
| 104 | .hostname(hostname) |
| 105 | .type(KubevirtNode.Type.valueOf(type)) |
| 106 | .state(KubevirtNodeState.INIT) |
| 107 | .managementIp(IpAddress.valueOf(mIp)); |
| 108 | |
| 109 | if (json.get(DATA_IP) != null) { |
| 110 | nodeBuilder.dataIp(IpAddress.valueOf(json.get(DATA_IP).asText())); |
| 111 | } |
| 112 | |
| 113 | JsonNode intBridgeJson = json.get(INTEGRATION_BRIDGE); |
| 114 | if (intBridgeJson != null) { |
| 115 | nodeBuilder.intgBridge(DeviceId.deviceId(intBridgeJson.asText())); |
| 116 | } |
| 117 | |
| 118 | // parse physical interfaces |
| 119 | List<KubevirtPhyInterface> phyIntfs = new ArrayList<>(); |
| 120 | JsonNode phyIntfsJson = json.get(PHYSICAL_INTERFACES); |
| 121 | if (phyIntfsJson != null) { |
| 122 | final JsonCodec<KubevirtPhyInterface> |
| 123 | phyIntfCodec = context.codec(KubevirtPhyInterface.class); |
| 124 | |
| 125 | IntStream.range(0, phyIntfsJson.size()).forEach(i -> { |
| 126 | ObjectNode intfJson = get(phyIntfsJson, i); |
| 127 | phyIntfs.add(phyIntfCodec.decode(intfJson, context)); |
| 128 | }); |
| 129 | } |
| 130 | nodeBuilder.phyIntfs(phyIntfs); |
| 131 | |
| 132 | log.trace("node is {}", nodeBuilder.build().toString()); |
| 133 | |
| 134 | return nodeBuilder.build(); |
| 135 | } |
| 136 | } |