Claudine Chiu | fb8b816 | 2016-04-01 23:50:51 +0000 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Claudine Chiu | fb8b816 | 2016-04-01 23:50:51 +0000 | [diff] [blame] | 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 | */ |
| 16 | package org.onosproject.codec.impl; |
| 17 | |
| 18 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 19 | import org.onosproject.codec.CodecContext; |
| 20 | import org.onosproject.codec.JsonCodec; |
| 21 | import org.onosproject.incubator.net.virtual.DefaultVirtualPort; |
| 22 | import org.onosproject.incubator.net.virtual.NetworkId; |
| 23 | import org.onosproject.incubator.net.virtual.VirtualDevice; |
| 24 | import org.onosproject.incubator.net.virtual.VirtualNetworkService; |
| 25 | import org.onosproject.incubator.net.virtual.VirtualPort; |
Yoonseon Han | 6c60389 | 2016-09-01 11:52:21 -0700 | [diff] [blame] | 26 | import org.onosproject.net.ConnectPoint; |
Claudine Chiu | fb8b816 | 2016-04-01 23:50:51 +0000 | [diff] [blame] | 27 | import org.onosproject.net.DeviceId; |
Claudine Chiu | fb8b816 | 2016-04-01 23:50:51 +0000 | [diff] [blame] | 28 | import org.onosproject.net.PortNumber; |
| 29 | |
| 30 | import java.util.Set; |
| 31 | |
| 32 | import static com.google.common.base.Preconditions.checkNotNull; |
| 33 | import static org.onlab.util.Tools.nullIsIllegal; |
| 34 | |
| 35 | /** |
| 36 | * Codec for the VirtualPort class. |
| 37 | */ |
| 38 | public 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 Han | 6c60389 | 2016-09-01 11:52:21 -0700 | [diff] [blame] | 59 | .put(PHYS_DEVICE_ID, vPort.realizedBy().deviceId().toString()) |
| 60 | .put(PHYS_PORT_NUM, vPort.realizedBy().port().toString()); |
Claudine Chiu | fb8b816 | 2016-04-01 23:50:51 +0000 | [diff] [blame] | 61 | |
| 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 Han | 6c60389 | 2016-09-01 11:52:21 -0700 | [diff] [blame] | 85 | ConnectPoint realizedBy = new ConnectPoint(physDId, physPortNum); |
Claudine Chiu | fb8b816 | 2016-04-01 23:50:51 +0000 | [diff] [blame] | 86 | 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 | } |