blob: 1d30bf6c50f23f84bcdcf7550e01a5432d7b777c [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
Jian Lie6312162018-03-21 21:41:00 +090018import com.fasterxml.jackson.databind.JsonNode;
19import com.fasterxml.jackson.databind.node.ArrayNode;
Jian Li23c8be22018-02-13 11:34:15 +090020import com.fasterxml.jackson.databind.node.ObjectNode;
21import org.onlab.packet.IpAddress;
22import org.onosproject.codec.CodecContext;
23import org.onosproject.codec.JsonCodec;
24import org.onosproject.net.DeviceId;
25import org.onosproject.openstacknode.api.NodeState;
Jian Li27841662018-04-14 01:59:47 +090026import org.onosproject.openstacknode.api.OpenstackAuth;
Jian Li23c8be22018-02-13 11:34:15 +090027import org.onosproject.openstacknode.api.OpenstackNode;
Jian Lie6312162018-03-21 21:41:00 +090028import org.onosproject.openstacknode.api.OpenstackPhyInterface;
Jian Li23c8be22018-02-13 11:34:15 +090029import org.onosproject.openstacknode.impl.DefaultOpenstackNode;
30import org.slf4j.Logger;
31
Jian Lie6312162018-03-21 21:41:00 +090032import java.util.ArrayList;
33import java.util.List;
34import java.util.stream.IntStream;
35
Jian Li23c8be22018-02-13 11:34:15 +090036import static com.google.common.base.Preconditions.checkNotNull;
37import static org.onlab.util.Tools.nullIsIllegal;
Jian Li27841662018-04-14 01:59:47 +090038import static org.onosproject.openstacknode.api.Constants.CONTROLLER;
Jian Li23c8be22018-02-13 11:34:15 +090039import static org.onosproject.openstacknode.api.Constants.DATA_IP;
40import static org.onosproject.openstacknode.api.Constants.GATEWAY;
41import static org.onosproject.openstacknode.api.Constants.HOST_NAME;
42import static org.onosproject.openstacknode.api.Constants.MANAGEMENT_IP;
43import static org.onosproject.openstacknode.api.Constants.UPLINK_PORT;
44import static org.onosproject.openstacknode.api.Constants.VLAN_INTF_NAME;
45import static org.slf4j.LoggerFactory.getLogger;
46
47/**
48 * Openstack node codec used for serializing and de-serializing JSON string.
49 */
50public 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 Li5ca0be82018-02-27 13:08:04 +090056 private static final String STATE = "state";
Jian Lie6312162018-03-21 21:41:00 +090057 private static final String PHYSICAL_INTERFACES = "phyIntfs";
Jian Li27841662018-04-14 01:59:47 +090058 private static final String AUTHENTICATION = "authentication";
Jian Li23c8be22018-02-13 11:34:15 +090059
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 Li5ca0be82018-02-27 13:08:04 +090070 .put(STATE, node.state().name());
Jian Li23c8be22018-02-13 11:34:15 +090071
72 OpenstackNode.NodeType type = node.type();
73
74 if (type == OpenstackNode.NodeType.GATEWAY) {
75 result.put(UPLINK_PORT, node.uplinkPort());
76 }
77
Jian Li27841662018-04-14 01:59:47 +090078 if (type != OpenstackNode.NodeType.CONTROLLER) {
79 result.put(INTEGRATION_BRIDGE, node.intgBridge().toString());
80 }
81
Jian Li23c8be22018-02-13 11:34:15 +090082 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 Li5b402c72018-02-27 14:27:34 +090090 // TODO: need to find a way to not refer to ServiceDirectory from
91 // DefaultOpenstackNode
Jian Li5ca0be82018-02-27 13:08:04 +090092
Jian Lie6312162018-03-21 21:41:00 +090093 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 Li27841662018-04-14 01:59:47 +0900100 if (node.authentication() != null) {
101 ObjectNode authJson = context.codec(OpenstackAuth.class)
102 .encode(node.authentication(), context);
103 result.put(AUTHENTICATION, authJson);
104 }
105
Jian Li23c8be22018-02-13 11:34:15 +0900106 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 Li23c8be22018-02-13 11:34:15 +0900121
122 DefaultOpenstackNode.Builder nodeBuilder = DefaultOpenstackNode.builder()
123 .hostname(hostname)
124 .type(OpenstackNode.NodeType.valueOf(type))
125 .managementIp(IpAddress.valueOf(mIp))
Jian Li23c8be22018-02-13 11:34:15 +0900126 .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 Li27841662018-04-14 01:59:47 +0900132 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 Li23c8be22018-02-13 11:34:15 +0900137 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 Lie6312162018-03-21 21:41:00 +0900144 // parse physical interfaces
145 List<OpenstackPhyInterface> phyIntfs = new ArrayList<>();
146 JsonNode phyIntfsJson = json.get(PHYSICAL_INTERFACES);
147 if (phyIntfsJson != null) {
Jian Li27841662018-04-14 01:59:47 +0900148
149 final JsonCodec<OpenstackPhyInterface>
150 phyIntfCodec = context.codec(OpenstackPhyInterface.class);
151
Jian Lie6312162018-03-21 21:41:00 +0900152 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 Li27841662018-04-14 01:59:47 +0900159 // 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 Li23c8be22018-02-13 11:34:15 +0900169 log.trace("node is {}", nodeBuilder.build().toString());
170
171 return nodeBuilder.build();
172 }
173}