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