blob: 92af6887b47c9564d8da187ff8f8e70db914b26a [file] [log] [blame]
Jian Licc01e452020-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";
32 private static final String STATE = "state";
33 private static final String PHYSICAL_INTERFACES = "phyIntfs";
34
35 private KubevirtNodeJsonMatcher(KubevirtNode node) {
36 this.node = node;
37 }
38
39 @Override
40 protected boolean matchesSafely(JsonNode jsonNode, Description description) {
41 // check hostname
42 String jsonHostname = jsonNode.get(Constants.HOST_NAME).asText();
43 String hostname = node.hostname();
44 if (!jsonHostname.equals(hostname)) {
45 description.appendText("hostname was " + jsonHostname);
46 return false;
47 }
48
49 // check type
50 String jsonType = jsonNode.get(Constants.TYPE).asText();
51 String type = node.type().name();
52 if (!jsonType.equals(type)) {
53 description.appendText("type was " + jsonType);
54 return false;
55 }
56
57 // check management IP
58 String jsonMgmtIp = jsonNode.get(Constants.MANAGEMENT_IP).asText();
59 String mgmtIp = node.managementIp().toString();
60 if (!jsonMgmtIp.equals(mgmtIp)) {
61 description.appendText("management IP was " + jsonMgmtIp);
62 return false;
63 }
64
65 // check integration bridge
66 JsonNode jsonIntgBridge = jsonNode.get(INTEGRATION_BRIDGE);
67 if (jsonIntgBridge != null) {
68 String intgBridge = node.intgBridge().toString();
69 if (!jsonIntgBridge.asText().equals(intgBridge)) {
70 description.appendText("integration bridge was " + jsonIntgBridge);
71 return false;
72 }
73 }
74
75 // check state
76 String jsonState = jsonNode.get(STATE).asText();
77 String state = node.state().name();
78 if (!jsonState.equals(state)) {
79 description.appendText("state was " + jsonState);
80 return false;
81 }
82
83 // check data IP
84 JsonNode jsonDataIp = jsonNode.get(Constants.DATA_IP);
85 if (jsonDataIp != null) {
86 String dataIp = node.dataIp().toString();
87 if (!jsonDataIp.asText().equals(dataIp)) {
88 description.appendText("Data IP was " + jsonDataIp.asText());
89 return false;
90 }
91 }
92
93 // check physical interfaces
94 JsonNode jsonPhyIntfs = jsonNode.get(PHYSICAL_INTERFACES);
95 if (jsonPhyIntfs != null) {
96 if (jsonPhyIntfs.size() != node.phyIntfs().size()) {
97 description.appendText("physical interface size was " + jsonPhyIntfs.size());
98 return false;
99 }
100
101 for (KubevirtPhyInterface phyIntf : node.phyIntfs()) {
102 boolean intfFound = false;
103 for (int intfIndex = 0; intfIndex < jsonPhyIntfs.size(); intfIndex++) {
104 KubevirtPhyInterfaceJsonMatcher intfMatcher =
105 KubevirtPhyInterfaceJsonMatcher.matchesKubevirtPhyInterface(phyIntf);
106 if (intfMatcher.matches(jsonPhyIntfs.get(intfIndex))) {
107 intfFound = true;
108 break;
109 }
110 }
111
112 if (!intfFound) {
113 description.appendText("PhyIntf not found " + phyIntf.toString());
114 return false;
115 }
116 }
117 }
118
119 return true;
120 }
121
122 @Override
123 public void describeTo(Description description) {
124 description.appendText(node.toString());
125 }
126
127 /**
128 * Factory to allocate an kubevirt node matcher.
129 *
130 * @param node kubevirt node object we are looking for
131 * @return matcher
132 */
133 public static KubevirtNodeJsonMatcher matchesKubevirtNode(KubevirtNode node) {
134 return new KubevirtNodeJsonMatcher(node);
135 }
136}