blob: bb4a82bc9428ac8e7c214d7b05fb1c77290a87b6 [file] [log] [blame]
Hyunsun Moone5a1fc32016-09-02 16:01:01 -07001/*
2 * Copyright 2016-present Open Networking Laboratory
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.opencord.cordvtn.codec;
17
18import com.fasterxml.jackson.databind.node.ArrayNode;
Hyunsun Moon4a94c2e2016-10-21 17:37:05 -070019import com.fasterxml.jackson.databind.node.NullNode;
Hyunsun Moone5a1fc32016-09-02 16:01:01 -070020import com.fasterxml.jackson.databind.node.ObjectNode;
Hyunsun Mooneaf75e62016-09-27 16:40:23 -070021import com.google.common.collect.Sets;
Hyunsun Moone5a1fc32016-09-02 16:01:01 -070022import org.onlab.packet.IpAddress;
23import org.onlab.packet.MacAddress;
24import org.onlab.packet.VlanId;
25import org.onosproject.codec.CodecContext;
26import org.onosproject.codec.JsonCodec;
Hyunsun Moonfd5a24e2016-10-19 19:15:48 -070027import org.opencord.cordvtn.api.net.AddressPair;
28import org.opencord.cordvtn.api.net.PortId;
29import org.opencord.cordvtn.api.net.ServicePort;
Hyunsun Moone5a1fc32016-09-02 16:01:01 -070030
Hyunsun Mooneaf75e62016-09-27 16:40:23 -070031import java.util.Set;
32
Hyunsun Moon4a94c2e2016-10-21 17:37:05 -070033import static com.google.common.base.Preconditions.checkArgument;
34
Hyunsun Moone5a1fc32016-09-02 16:01:01 -070035/**
36 * Service port JSON codec.
37 */
38public final class ServicePortCodec extends JsonCodec<ServicePort> {
39
40 private static final String ID = "id";
41 private static final String VLAN_ID = "vlan_id";
42 private static final String FLOATING_ADDRESS_PAIRS = "floating_address_pairs";
43 private static final String IP_ADDRESS = "ip_address";
44 private static final String MAC_ADDRESS = "mac_address";
45
Hyunsun Moon4a94c2e2016-10-21 17:37:05 -070046 private static final String ERR_JSON = "Invalid ServicePort received";
47 private static final String ERR_ID = ": port ID cannot be null";
48
Hyunsun Moone5a1fc32016-09-02 16:01:01 -070049 @Override
50 public ObjectNode encode(ServicePort sport, CodecContext context) {
51 ObjectNode result = context.mapper().createObjectNode()
52 .put(ID, sport.id().id());
53 if (sport.vlanId().isPresent()) {
54 result.put(VLAN_ID, sport.vlanId().get().id());
55 }
56
57 ArrayNode addressPairs = context.mapper().createArrayNode();
58 sport.addressPairs().forEach(pair -> {
59 ObjectNode pairJson = context.mapper().createObjectNode()
60 .put(IP_ADDRESS, pair.ip().toString())
61 .put(MAC_ADDRESS, pair.mac().toString());
62 addressPairs.add(pairJson);
63 });
64 result.set(FLOATING_ADDRESS_PAIRS, addressPairs);
65 return result;
66 }
67
68 @Override
69 public ServicePort decode(ObjectNode json, CodecContext context) {
Hyunsun Moon4a94c2e2016-10-21 17:37:05 -070070 checkArgument(json != null && json.isObject(), ERR_JSON);
71 checkArgument(json.get(ID) != null &&
72 json.get(ID) != NullNode.getInstance(),
73 ERR_JSON + ERR_ID);
Hyunsun Moone5a1fc32016-09-02 16:01:01 -070074
Hyunsun Mooneaf75e62016-09-27 16:40:23 -070075 PortId portId = PortId.of(json.get(ID).asText());
76 VlanId vlanId = null;
Hyunsun Moon4a94c2e2016-10-21 17:37:05 -070077 if (json.get(VLAN_ID) != NullNode.getInstance()) {
Hyunsun Mooneaf75e62016-09-27 16:40:23 -070078 try {
79 vlanId = VlanId.vlanId(json.get(VLAN_ID).asText());
80 } catch (Exception ignore) {
81 }
Hyunsun Moone5a1fc32016-09-02 16:01:01 -070082 }
83
Hyunsun Mooneaf75e62016-09-27 16:40:23 -070084 Set<AddressPair> addressPairs = Sets.newHashSet();
Hyunsun Moone5a1fc32016-09-02 16:01:01 -070085 if (json.get(FLOATING_ADDRESS_PAIRS) != null) {
86 json.get(FLOATING_ADDRESS_PAIRS).forEach(pair -> {
87 AddressPair addrPair = AddressPair.of(
88 IpAddress.valueOf(pair.get(IP_ADDRESS).asText()),
89 MacAddress.valueOf(pair.get(MAC_ADDRESS).asText()));
Hyunsun Mooneaf75e62016-09-27 16:40:23 -070090 addressPairs.add(addrPair);
Hyunsun Moone5a1fc32016-09-02 16:01:01 -070091 });
92 }
Hyunsun Mooneaf75e62016-09-27 16:40:23 -070093 return new ServicePort(portId, vlanId, addressPairs);
Hyunsun Moone5a1fc32016-09-02 16:01:01 -070094 }
95}