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