blob: 474196e77039d6d501719bab23abb5bfe9ef3148 [file] [log] [blame]
Claudine Chiufb8b8162016-04-01 23:50:51 +00001/*
Thomas Vachuska52f2cd12018-11-08 21:20:04 -08002 * Copyright 2018-present Open Networking Foundation
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 */
Thomas Vachuska52f2cd12018-11-08 21:20:04 -080016package org.onosproject.incubator.net.virtual.codec;
Claudine Chiufb8b8162016-04-01 23:50:51 +000017
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;
Yoonseon Han6c603892016-09-01 11:52:21 -070026import org.onosproject.net.ConnectPoint;
Claudine Chiufb8b8162016-04-01 23:50:51 +000027import org.onosproject.net.DeviceId;
Claudine Chiufb8b8162016-04-01 23:50:51 +000028import org.onosproject.net.PortNumber;
29
30import java.util.Set;
31
32import static com.google.common.base.Preconditions.checkNotNull;
33import static org.onlab.util.Tools.nullIsIllegal;
34
35/**
36 * Codec for the VirtualPort class.
37 */
38public class VirtualPortCodec extends JsonCodec<VirtualPort> {
39
40 // JSON field names
41 private static final String NETWORK_ID = "networkId";
42 private static final String DEVICE_ID = "deviceId";
43 private static final String PORT_NUM = "portNum";
44 private static final String PHYS_DEVICE_ID = "physDeviceId";
45 private static final String PHYS_PORT_NUM = "physPortNum";
46
47 private static final String NULL_OBJECT_MSG = "VirtualPort cannot be null";
48 private static final String MISSING_MEMBER_MSG = " member is required in VirtualPort";
49 private static final String INVALID_VIRTUAL_DEVICE = " is not a valid VirtualDevice";
50
51 @Override
52 public ObjectNode encode(VirtualPort vPort, CodecContext context) {
53 checkNotNull(vPort, NULL_OBJECT_MSG);
54
55 ObjectNode result = context.mapper().createObjectNode()
56 .put(NETWORK_ID, vPort.networkId().toString())
57 .put(DEVICE_ID, vPort.element().id().toString())
58 .put(PORT_NUM, vPort.number().toString())
Yoonseon Han6c603892016-09-01 11:52:21 -070059 .put(PHYS_DEVICE_ID, vPort.realizedBy().deviceId().toString())
60 .put(PHYS_PORT_NUM, vPort.realizedBy().port().toString());
Claudine Chiufb8b8162016-04-01 23:50:51 +000061
62 return result;
63 }
64
65 @Override
66 public VirtualPort decode(ObjectNode json, CodecContext context) {
67 if (json == null || !json.isObject()) {
68 return null;
69 }
70
71 NetworkId nId = NetworkId.networkId(Long.parseLong(extractMember(NETWORK_ID, json)));
72 DeviceId dId = DeviceId.deviceId(extractMember(DEVICE_ID, json));
73
74 VirtualNetworkService vnetService = context.getService(VirtualNetworkService.class);
75 Set<VirtualDevice> vDevs = vnetService.getVirtualDevices(nId);
76 VirtualDevice vDev = vDevs.stream()
77 .filter(virtualDevice -> virtualDevice.id().equals(dId))
78 .findFirst().orElse(null);
79 nullIsIllegal(vDev, dId.toString() + INVALID_VIRTUAL_DEVICE);
80
81 PortNumber portNum = PortNumber.portNumber(extractMember(PORT_NUM, json));
82 DeviceId physDId = DeviceId.deviceId(extractMember(PHYS_DEVICE_ID, json));
83 PortNumber physPortNum = PortNumber.portNumber(extractMember(PHYS_PORT_NUM, json));
84
Yoonseon Han6c603892016-09-01 11:52:21 -070085 ConnectPoint realizedBy = new ConnectPoint(physDId, physPortNum);
Claudine Chiufb8b8162016-04-01 23:50:51 +000086 return new DefaultVirtualPort(nId, vDev, portNum, realizedBy);
87 }
88
89 private String extractMember(String key, ObjectNode json) {
90 return nullIsIllegal(json.get(key), key + MISSING_MEMBER_MSG).asText();
91 }
92}