blob: 41d8cfebfa9c9633f8a38fb11c9f96a7e51af83e [file] [log] [blame]
Jian Li073f1ba2021-02-28 03:50:15 +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.onosproject.kubevirtnetworking.api.KubevirtFloatingIp;
22
23/**
24 * Hamcrest matcher for kubevirt router interface.
25 */
26public final class KubevirtFloatingIpJsonMatcher extends TypeSafeDiagnosingMatcher<JsonNode> {
27
28 private final KubevirtFloatingIp floatingIp;
29 private static final String ID = "id";
30 private static final String ROUTER_NAME = "routerName";
Jian Lib636f702021-03-03 14:46:50 +090031 private static final String NETWORK_NAME = "networkName";
Jian Li073f1ba2021-02-28 03:50:15 +090032 private static final String POD_NAME = "podName";
Jian Licb580422022-02-03 15:08:31 +090033 private static final String VM_NAME = "vmName";
Jian Li073f1ba2021-02-28 03:50:15 +090034 private static final String FLOATING_IP = "floatingIp";
35 private static final String FIXED_IP = "fixedIp";
36
37 private KubevirtFloatingIpJsonMatcher(KubevirtFloatingIp floatingIp) {
38 this.floatingIp = floatingIp;
39 }
40
41 @Override
42 protected boolean matchesSafely(JsonNode jsonNode, Description description) {
43
44 // check ID
45 String jsonId = jsonNode.get(ID).asText();
46 String id = floatingIp.id();
47 if (!jsonId.equals(id)) {
48 description.appendText("ID was " + jsonId);
49 return false;
50 }
51
52 // check router name
53 String jsonRouterName = jsonNode.get(ROUTER_NAME).asText();
54 String routerName = floatingIp.routerName();
55 if (!jsonRouterName.equals(routerName)) {
56 description.appendText("Router name was " + jsonRouterName);
57 return false;
58 }
59
Jian Lib636f702021-03-03 14:46:50 +090060 // check network name
61 String jsonNetworkName = jsonNode.get(NETWORK_NAME).asText();
62 String networkName = floatingIp.networkName();
63 if (!jsonNetworkName.equals(networkName)) {
64 description.appendText("Network name was " + jsonNetworkName);
65 return false;
66 }
67
Jian Li073f1ba2021-02-28 03:50:15 +090068 // check floating IP
69 String jsonFip = jsonNode.get(FLOATING_IP).asText();
70 String fip = floatingIp.floatingIp().toString();
71 if (!jsonFip.equals(fip)) {
72 description.appendText("Floating IP was " + jsonFip);
73 return false;
74 }
75
76 // check POD name
77 JsonNode jsonPodName = jsonNode.get(POD_NAME);
78 if (jsonPodName != null) {
79 if (!floatingIp.podName().equals(jsonPodName.asText())) {
80 description.appendText("POD name was " + jsonPodName);
81 return false;
82 }
83 }
84
Jian Licb580422022-02-03 15:08:31 +090085 JsonNode jsonVmName = jsonNode.get(VM_NAME);
86 if (jsonVmName != null) {
87 if (!floatingIp.vmName().equals(jsonVmName.asText())) {
88 description.appendText("VM name was " + jsonVmName);
89 return false;
90 }
91 }
92
93
Jian Li073f1ba2021-02-28 03:50:15 +090094 // check fixed IP
95 JsonNode jsonFixedIp = jsonNode.get(FIXED_IP);
96 if (jsonFixedIp != null) {
97 if (!floatingIp.fixedIp().toString().equals(jsonFixedIp.asText())) {
98 description.appendText("Fixed IP was " + jsonFixedIp);
99 return false;
100 }
101 }
102
103 return true;
104 }
105
106 @Override
107 public void describeTo(Description description) {
108 description.appendText(floatingIp.toString());
109 }
110
111 /**
112 * Factory to allocate a kubevirt floating IP matcher.
113 *
114 * @param fip kubevirt floating IP object we are looking for
115 * @return matcher
116 */
117 public static KubevirtFloatingIpJsonMatcher matchesKubevirtFloatingIp(KubevirtFloatingIp fip) {
118 return new KubevirtFloatingIpJsonMatcher(fip);
119 }
120}