blob: c4b3cdc55fadddf2ecc851be61d06423e1cbc681 [file] [log] [blame]
Jian Li2c63bd22018-07-15 23:35:34 +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.openstacknetworking.codec;
17
18import com.fasterxml.jackson.databind.node.ObjectNode;
19import org.onlab.packet.IpAddress;
20import org.onlab.packet.MacAddress;
21import org.onosproject.codec.CodecContext;
22import org.onosproject.codec.JsonCodec;
23import org.onosproject.net.DeviceId;
24import org.onosproject.net.PortNumber;
25import org.onosproject.openstacknetworking.api.InstancePort;
26import org.onosproject.openstacknetworking.impl.DefaultInstancePort;
27import org.slf4j.Logger;
28
29import static com.google.common.base.Preconditions.checkNotNull;
30import static org.onlab.util.Tools.nullIsIllegal;
31import static org.slf4j.LoggerFactory.getLogger;
32
33/**
34 * Openstack instance port codec used for serializing and de-serializing JSON string.
35 */
36public class InstancePortCodec extends JsonCodec<InstancePort> {
37
38 private final Logger log = getLogger(getClass());
39
40 private static final String NETWORK_ID = "networkId";
41 private static final String PORT_ID = "portId";
42 private static final String MAC_ADDRESS = "macAddress";
43 private static final String IP_ADDRESS = "ipAddress";
44 private static final String DEVICE_ID = "deviceId";
45 private static final String PORT_NUMBER = "portNumber";
46 private static final String STATE = "state";
47
48 private static final String MISSING_MESSAGE = " is required in InstancePort";
49
50 @Override
51 public ObjectNode encode(InstancePort port, CodecContext context) {
52 checkNotNull(port, "Instance port cannot be null");
53
54 return context.mapper().createObjectNode()
55 .put(NETWORK_ID, port.networkId())
56 .put(PORT_ID, port.portId())
57 .put(MAC_ADDRESS, port.macAddress().toString())
58 .put(IP_ADDRESS, port.ipAddress().toString())
59 .put(DEVICE_ID, port.deviceId().toString())
60 .put(PORT_NUMBER, port.portNumber().toString())
61 .put(STATE, port.state().name());
62 }
63
64 @Override
65 public InstancePort decode(ObjectNode json, CodecContext context) {
66 if (json == null || !json.isObject()) {
67 return null;
68 }
69
70 String networkId = nullIsIllegal(json.get(NETWORK_ID).asText(),
71 NETWORK_ID + MISSING_MESSAGE);
72 String portId = nullIsIllegal(json.get(PORT_ID).asText(),
73 PORT_ID + MISSING_MESSAGE);
74 String macAddress = nullIsIllegal(json.get(MAC_ADDRESS).asText(),
75 MAC_ADDRESS + MISSING_MESSAGE);
76 String ipAddress = nullIsIllegal(json.get(IP_ADDRESS).asText(),
77 IP_ADDRESS + MISSING_MESSAGE);
78 String deviceId = nullIsIllegal(json.get(DEVICE_ID).asText(),
79 DEVICE_ID + MISSING_MESSAGE);
80 String portNumber = nullIsIllegal(json.get(PORT_NUMBER).asText(),
81 PORT_NUMBER + MISSING_MESSAGE);
82 String state = nullIsIllegal(json.get(STATE).asText(),
83 STATE + MISSING_MESSAGE);
84
85 return DefaultInstancePort.builder()
86 .networkId(networkId)
87 .portId(portId)
88 .macAddress(MacAddress.valueOf(macAddress))
89 .ipAddress(IpAddress.valueOf(ipAddress))
90 .deviceId(DeviceId.deviceId(deviceId))
91 .portNumber(PortNumber.fromString(portNumber))
92 .state(InstancePort.State.valueOf(state)).build();
93 }
94}