blob: ccfb76c50930af734b2ee82c6705312abf9181e3 [file] [log] [blame]
Jian Lie48a6172021-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 Li4acd4542021-03-03 14:46:50 +090031 private static final String NETWORK_NAME = "networkName";
Jian Lie48a6172021-02-28 03:50:15 +090032 private static final String POD_NAME = "podName";
33 private static final String FLOATING_IP = "floatingIp";
34 private static final String FIXED_IP = "fixedIp";
35
36 private KubevirtFloatingIpJsonMatcher(KubevirtFloatingIp floatingIp) {
37 this.floatingIp = floatingIp;
38 }
39
40 @Override
41 protected boolean matchesSafely(JsonNode jsonNode, Description description) {
42
43 // check ID
44 String jsonId = jsonNode.get(ID).asText();
45 String id = floatingIp.id();
46 if (!jsonId.equals(id)) {
47 description.appendText("ID was " + jsonId);
48 return false;
49 }
50
51 // check router name
52 String jsonRouterName = jsonNode.get(ROUTER_NAME).asText();
53 String routerName = floatingIp.routerName();
54 if (!jsonRouterName.equals(routerName)) {
55 description.appendText("Router name was " + jsonRouterName);
56 return false;
57 }
58
Jian Li4acd4542021-03-03 14:46:50 +090059 // check network name
60 String jsonNetworkName = jsonNode.get(NETWORK_NAME).asText();
61 String networkName = floatingIp.networkName();
62 if (!jsonNetworkName.equals(networkName)) {
63 description.appendText("Network name was " + jsonNetworkName);
64 return false;
65 }
66
Jian Lie48a6172021-02-28 03:50:15 +090067 // check floating IP
68 String jsonFip = jsonNode.get(FLOATING_IP).asText();
69 String fip = floatingIp.floatingIp().toString();
70 if (!jsonFip.equals(fip)) {
71 description.appendText("Floating IP was " + jsonFip);
72 return false;
73 }
74
75 // check POD name
76 JsonNode jsonPodName = jsonNode.get(POD_NAME);
77 if (jsonPodName != null) {
78 if (!floatingIp.podName().equals(jsonPodName.asText())) {
79 description.appendText("POD name was " + jsonPodName);
80 return false;
81 }
82 }
83
84 // check fixed IP
85 JsonNode jsonFixedIp = jsonNode.get(FIXED_IP);
86 if (jsonFixedIp != null) {
87 if (!floatingIp.fixedIp().toString().equals(jsonFixedIp.asText())) {
88 description.appendText("Fixed IP was " + jsonFixedIp);
89 return false;
90 }
91 }
92
93 return true;
94 }
95
96 @Override
97 public void describeTo(Description description) {
98 description.appendText(floatingIp.toString());
99 }
100
101 /**
102 * Factory to allocate a kubevirt floating IP matcher.
103 *
104 * @param fip kubevirt floating IP object we are looking for
105 * @return matcher
106 */
107 public static KubevirtFloatingIpJsonMatcher matchesKubevirtFloatingIp(KubevirtFloatingIp fip) {
108 return new KubevirtFloatingIpJsonMatcher(fip);
109 }
110}