blob: 1fc6207fde427e41290bbc1a1e486cde87174549 [file] [log] [blame]
Jian Lica20b712021-01-18 00:19:31 +09001/*
2 * Copyright 2021-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.kubevirtnetworking.codec;
17
18import com.fasterxml.jackson.databind.JsonNode;
19import org.hamcrest.Description;
20import org.hamcrest.TypeSafeDiagnosingMatcher;
21import org.onlab.packet.IpAddress;
22import org.onosproject.kubevirtnetworking.api.KubevirtPort;
23import org.onosproject.net.DeviceId;
24import org.onosproject.net.PortNumber;
25
26/**
27 * Hamcrest matcher for kubevirt port.
28 */
29public final class KubevirtPortJsonMatcher extends TypeSafeDiagnosingMatcher<JsonNode> {
30
31 private final KubevirtPort port;
32
Jian Li9557e902021-06-08 10:12:52 +090033 private static final String VM_NAME = "vmName";
Jian Lica20b712021-01-18 00:19:31 +090034 private static final String NETWORK_ID = "networkId";
35 private static final String MAC_ADDRESS = "macAddress";
36 private static final String IP_ADDRESS = "ipAddress";
37 private static final String DEVICE_ID = "deviceId";
38 private static final String PORT_NUMBER = "portNumber";
39
40 private KubevirtPortJsonMatcher(KubevirtPort port) {
41 this.port = port;
42 }
43
44 @Override
45 protected boolean matchesSafely(JsonNode jsonNode, Description description) {
Jian Li9557e902021-06-08 10:12:52 +090046 // check VM name
47 String jsonVmName = jsonNode.get(VM_NAME).asText();
48 String vmName = port.vmName();
49 if (!jsonVmName.equals(vmName)) {
50 description.appendText("VM name was " + jsonVmName);
51 return false;
52 }
53
Jian Lica20b712021-01-18 00:19:31 +090054 // check network ID
55 String jsonNetworkId = jsonNode.get(NETWORK_ID).asText();
56 String networkId = port.networkId();
57 if (!jsonNetworkId.equals(networkId)) {
58 description.appendText("network ID was " + jsonNetworkId);
59 return false;
60 }
61
62 // check MAC address
63 String jsonMacAddress = jsonNode.get(MAC_ADDRESS).asText();
64 String macAddress = port.macAddress().toString();
65 if (!jsonMacAddress.equals(macAddress)) {
66 description.appendText("MAC address was " + jsonMacAddress);
67 return false;
68 }
69
70 // check IP address
71 JsonNode jsonIpAddress = jsonNode.get(IP_ADDRESS);
72 if (jsonIpAddress != null) {
73 IpAddress ipAddress = port.ipAddress();
74 if (!jsonIpAddress.asText().equals(ipAddress.toString())) {
75 description.appendText("IP address was " + jsonIpAddress.asText());
76 return false;
77 }
78 }
79
80 // check device ID
81 JsonNode jsonDeviceId = jsonNode.get(DEVICE_ID);
82 if (jsonDeviceId != null) {
83 DeviceId deviceId = port.deviceId();
84 if (!jsonDeviceId.asText().equals(deviceId.toString())) {
85 description.appendText("Device ID was " + jsonDeviceId.asText());
86 return false;
87 }
88 }
89
90 // check port number
91 JsonNode jsonPortNumber = jsonNode.get(PORT_NUMBER);
92 if (jsonPortNumber != null) {
93 PortNumber portNUmber = port.portNumber();
94 if (!jsonPortNumber.asText().equals(portNUmber.toString())) {
95 description.appendText("Port number was " + jsonPortNumber.asText());
96 return false;
97 }
98 }
99
100 return true;
101 }
102
103 @Override
104 public void describeTo(Description description) {
105 description.appendText(port.toString());
106 }
107 /**
108 * Factory to allocate an kubevirt port matcher.
109 *
110 * @param port kubevirt port object we are looking for
111 * @return matcher
112 */
113 public static KubevirtPortJsonMatcher matchesKubevirtPort(KubevirtPort port) {
114 return new KubevirtPortJsonMatcher(port);
115 }
116}