blob: 5d5dd427d271a8c90943fbf50ceac61a4d058bc3 [file] [log] [blame]
Jian Li171582e2020-12-21 01:44:05 +09001/*
2 * Copyright 2020-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.kubevirtnode.codec;
17
18import com.fasterxml.jackson.databind.JsonNode;
19import org.hamcrest.Description;
20import org.hamcrest.TypeSafeDiagnosingMatcher;
21import org.onosproject.kubevirtnode.api.KubevirtNode;
22import org.onosproject.kubevirtnode.api.KubevirtPhyInterface;
23import org.onosproject.kubevirtnode.api.Constants;
24
25/**
26 * Hamcrest matcher for kubevirt node.
27 */
28public final class KubevirtNodeJsonMatcher extends TypeSafeDiagnosingMatcher<JsonNode> {
29
30 private final KubevirtNode node;
31 private static final String INTEGRATION_BRIDGE = "integrationBridge";
Jian Li138f51f2021-01-06 03:29:58 +090032 private static final String TUNNEL_BRIDGE = "tunnelBridge";
Jian Li171582e2020-12-21 01:44:05 +090033 private static final String STATE = "state";
34 private static final String PHYSICAL_INTERFACES = "phyIntfs";
35
36 private KubevirtNodeJsonMatcher(KubevirtNode node) {
37 this.node = node;
38 }
39
40 @Override
41 protected boolean matchesSafely(JsonNode jsonNode, Description description) {
42 // check hostname
43 String jsonHostname = jsonNode.get(Constants.HOST_NAME).asText();
44 String hostname = node.hostname();
45 if (!jsonHostname.equals(hostname)) {
46 description.appendText("hostname was " + jsonHostname);
47 return false;
48 }
49
50 // check type
51 String jsonType = jsonNode.get(Constants.TYPE).asText();
52 String type = node.type().name();
53 if (!jsonType.equals(type)) {
54 description.appendText("type was " + jsonType);
55 return false;
56 }
57
58 // check management IP
59 String jsonMgmtIp = jsonNode.get(Constants.MANAGEMENT_IP).asText();
60 String mgmtIp = node.managementIp().toString();
61 if (!jsonMgmtIp.equals(mgmtIp)) {
62 description.appendText("management IP was " + jsonMgmtIp);
63 return false;
64 }
65
66 // check integration bridge
67 JsonNode jsonIntgBridge = jsonNode.get(INTEGRATION_BRIDGE);
68 if (jsonIntgBridge != null) {
69 String intgBridge = node.intgBridge().toString();
70 if (!jsonIntgBridge.asText().equals(intgBridge)) {
71 description.appendText("integration bridge was " + jsonIntgBridge);
72 return false;
73 }
74 }
75
Jian Li138f51f2021-01-06 03:29:58 +090076 // check tunnel bridge
77 JsonNode jsonTunBridge = jsonNode.get(TUNNEL_BRIDGE);
78 if (jsonTunBridge != null) {
79 String tunBridge = node.tunBridge().toString();
80 if (!jsonTunBridge.asText().equals(tunBridge)) {
81 description.appendText("tunnel bridge was " + jsonTunBridge);
82 return false;
83 }
84 }
85
Jian Li171582e2020-12-21 01:44:05 +090086 // check state
87 String jsonState = jsonNode.get(STATE).asText();
88 String state = node.state().name();
89 if (!jsonState.equals(state)) {
90 description.appendText("state was " + jsonState);
91 return false;
92 }
93
94 // check data IP
95 JsonNode jsonDataIp = jsonNode.get(Constants.DATA_IP);
96 if (jsonDataIp != null) {
97 String dataIp = node.dataIp().toString();
98 if (!jsonDataIp.asText().equals(dataIp)) {
99 description.appendText("Data IP was " + jsonDataIp.asText());
100 return false;
101 }
102 }
103
104 // check physical interfaces
105 JsonNode jsonPhyIntfs = jsonNode.get(PHYSICAL_INTERFACES);
106 if (jsonPhyIntfs != null) {
107 if (jsonPhyIntfs.size() != node.phyIntfs().size()) {
108 description.appendText("physical interface size was " + jsonPhyIntfs.size());
109 return false;
110 }
111
112 for (KubevirtPhyInterface phyIntf : node.phyIntfs()) {
113 boolean intfFound = false;
114 for (int intfIndex = 0; intfIndex < jsonPhyIntfs.size(); intfIndex++) {
115 KubevirtPhyInterfaceJsonMatcher intfMatcher =
116 KubevirtPhyInterfaceJsonMatcher.matchesKubevirtPhyInterface(phyIntf);
117 if (intfMatcher.matches(jsonPhyIntfs.get(intfIndex))) {
118 intfFound = true;
119 break;
120 }
121 }
122
123 if (!intfFound) {
124 description.appendText("PhyIntf not found " + phyIntf.toString());
125 return false;
126 }
127 }
128 }
129
130 return true;
131 }
132
133 @Override
134 public void describeTo(Description description) {
135 description.appendText(node.toString());
136 }
137
138 /**
139 * Factory to allocate an kubevirt node matcher.
140 *
141 * @param node kubevirt node object we are looking for
142 * @return matcher
143 */
144 public static KubevirtNodeJsonMatcher matchesKubevirtNode(KubevirtNode node) {
145 return new KubevirtNodeJsonMatcher(node);
146 }
147}