blob: d5f170ca610dfd52e5ebb412d18a69d18a42038d [file] [log] [blame]
Claudine Chiufb8b8162016-04-01 23:50:51 +00001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Claudine Chiufb8b8162016-04-01 23:50:51 +00003 *
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.codec.impl;
17
18import com.fasterxml.jackson.databind.node.ObjectNode;
19import org.onosproject.codec.CodecContext;
20import org.onosproject.codec.JsonCodec;
21import org.onosproject.incubator.net.virtual.DefaultVirtualPort;
22import org.onosproject.incubator.net.virtual.NetworkId;
23import org.onosproject.incubator.net.virtual.VirtualDevice;
24import org.onosproject.incubator.net.virtual.VirtualNetworkService;
25import org.onosproject.incubator.net.virtual.VirtualPort;
26import org.onosproject.net.DefaultAnnotations;
27import org.onosproject.net.DefaultDevice;
28import org.onosproject.net.DefaultPort;
29import org.onosproject.net.Device;
30import org.onosproject.net.DeviceId;
31import org.onosproject.net.Port;
32import org.onosproject.net.PortNumber;
33
34import java.util.Set;
35
36import static com.google.common.base.Preconditions.checkNotNull;
37import static org.onlab.util.Tools.nullIsIllegal;
38
39/**
40 * Codec for the VirtualPort class.
41 */
42public class VirtualPortCodec extends JsonCodec<VirtualPort> {
43
44 // JSON field names
45 private static final String NETWORK_ID = "networkId";
46 private static final String DEVICE_ID = "deviceId";
47 private static final String PORT_NUM = "portNum";
48 private static final String PHYS_DEVICE_ID = "physDeviceId";
49 private static final String PHYS_PORT_NUM = "physPortNum";
50
51 private static final String NULL_OBJECT_MSG = "VirtualPort cannot be null";
52 private static final String MISSING_MEMBER_MSG = " member is required in VirtualPort";
53 private static final String INVALID_VIRTUAL_DEVICE = " is not a valid VirtualDevice";
54
55 @Override
56 public ObjectNode encode(VirtualPort vPort, CodecContext context) {
57 checkNotNull(vPort, NULL_OBJECT_MSG);
58
59 ObjectNode result = context.mapper().createObjectNode()
60 .put(NETWORK_ID, vPort.networkId().toString())
61 .put(DEVICE_ID, vPort.element().id().toString())
62 .put(PORT_NUM, vPort.number().toString())
63 .put(PHYS_DEVICE_ID, vPort.realizedBy().element().id().toString())
64 .put(PHYS_PORT_NUM, vPort.realizedBy().number().toString());
65
66 return result;
67 }
68
69 @Override
70 public VirtualPort decode(ObjectNode json, CodecContext context) {
71 if (json == null || !json.isObject()) {
72 return null;
73 }
74
75 NetworkId nId = NetworkId.networkId(Long.parseLong(extractMember(NETWORK_ID, json)));
76 DeviceId dId = DeviceId.deviceId(extractMember(DEVICE_ID, json));
77
78 VirtualNetworkService vnetService = context.getService(VirtualNetworkService.class);
79 Set<VirtualDevice> vDevs = vnetService.getVirtualDevices(nId);
80 VirtualDevice vDev = vDevs.stream()
81 .filter(virtualDevice -> virtualDevice.id().equals(dId))
82 .findFirst().orElse(null);
83 nullIsIllegal(vDev, dId.toString() + INVALID_VIRTUAL_DEVICE);
84
85 PortNumber portNum = PortNumber.portNumber(extractMember(PORT_NUM, json));
86 DeviceId physDId = DeviceId.deviceId(extractMember(PHYS_DEVICE_ID, json));
87 PortNumber physPortNum = PortNumber.portNumber(extractMember(PHYS_PORT_NUM, json));
88
89 DefaultAnnotations annotations = DefaultAnnotations.builder().build();
90 Device physDevice = new DefaultDevice(null, physDId,
91 null, null, null, null, null, null, annotations);
92 Port realizedBy = new DefaultPort(physDevice, physPortNum, true);
93 return new DefaultVirtualPort(nId, vDev, portNum, realizedBy);
94 }
95
96 private String extractMember(String key, ObjectNode json) {
97 return nullIsIllegal(json.get(key), key + MISSING_MEMBER_MSG).asText();
98 }
99}