blob: 71c5ca928ed20e9003cf1d0364b880eff34445f0 [file] [log] [blame]
Jian Li49109b52019-01-22 00:17:28 +09001/*
2 * Copyright 2019-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.k8snode.codec;
17
18import com.fasterxml.jackson.databind.JsonNode;
19import org.hamcrest.Description;
20import org.hamcrest.TypeSafeDiagnosingMatcher;
Jian Li0c632722019-05-08 15:58:04 +090021import org.onlab.packet.IpAddress;
Jian Lic2242bd2020-09-03 13:12:14 +090022import org.onlab.packet.MacAddress;
Jian Li49109b52019-01-22 00:17:28 +090023import org.onosproject.k8snode.api.K8sNode;
24
25/**
26 * Hamcrest matcher for kubernetes node.
27 */
28public final class K8sNodeJsonMatcher extends TypeSafeDiagnosingMatcher<JsonNode> {
29
30 private final K8sNode node;
31
Jian Lie2a04ce2020-07-01 19:07:02 +090032 private static final String CLUSTER_NAME = "clusterName";
Jian Li49109b52019-01-22 00:17:28 +090033 private static final String HOSTNAME = "hostname";
34 private static final String TYPE = "type";
Jian Lie2a04ce2020-07-01 19:07:02 +090035 private static final String SEGMENT_ID = "segmentId";
Jian Li49109b52019-01-22 00:17:28 +090036 private static final String MANAGEMENT_IP = "managementIp";
37 private static final String DATA_IP = "dataIp";
38 private static final String INTEGRATION_BRIDGE = "integrationBridge";
39 private static final String STATE = "state";
Jian Li0c632722019-05-08 15:58:04 +090040 private static final String EXTERNAL_INTF = "externalInterface";
41 private static final String EXTERNAL_BRIDGE_IP = "externalBridgeIp";
42 private static final String EXTERNAL_GATEWAY_IP = "externalGatewayIp";
Jian Lic2242bd2020-09-03 13:12:14 +090043 private static final String EXTERNAL_GATEWAY_MAC = "externalGatewayMac";
Jian Li49109b52019-01-22 00:17:28 +090044
45 private K8sNodeJsonMatcher(K8sNode node) {
46 this.node = node;
47 }
48
49 @Override
50 protected boolean matchesSafely(JsonNode jsonNode, Description description) {
51
Jian Lie2a04ce2020-07-01 19:07:02 +090052 // check cluster name
53 String jsonClusterName = jsonNode.get(CLUSTER_NAME).asText();
54 String clusterName = node.clusterName();
55 if (!jsonClusterName.equals(clusterName)) {
56 description.appendText("cluster name was " + jsonClusterName);
57 return false;
58 }
59
Jian Li49109b52019-01-22 00:17:28 +090060 // check hostname
61 String jsonHostname = jsonNode.get(HOSTNAME).asText();
62 String hostname = node.hostname();
63 if (!jsonHostname.equals(hostname)) {
64 description.appendText("hostname was " + jsonHostname);
65 return false;
66 }
67
68 // check type
69 String jsonType = jsonNode.get(TYPE).asText();
70 String type = node.type().name();
71 if (!jsonType.equals(type)) {
72 description.appendText("type was " + jsonType);
73 return false;
74 }
75
Jian Lie2a04ce2020-07-01 19:07:02 +090076 // check segment ID
77 JsonNode jsonSegmentId = jsonNode.get(SEGMENT_ID);
78 if (jsonSegmentId != null) {
79 int segmentId = jsonSegmentId.asInt();
80 if (segmentId != node.segmentId()) {
81 description.appendText("segment ID was " + segmentId);
82 return false;
83 }
84 }
85
Jian Li49109b52019-01-22 00:17:28 +090086 // check management IP
87 String jsonMgmtIp = jsonNode.get(MANAGEMENT_IP).asText();
88 String mgmtIp = node.managementIp().toString();
89 if (!jsonMgmtIp.equals(mgmtIp)) {
90 description.appendText("management IP was " + jsonMgmtIp);
91 return false;
92 }
93
94 // check integration bridge
95 JsonNode jsonIntgBridge = jsonNode.get(INTEGRATION_BRIDGE);
96 if (jsonIntgBridge != null) {
97 String intgBridge = node.intgBridge().toString();
98 if (!jsonIntgBridge.asText().equals(intgBridge)) {
99 description.appendText("integration bridge was " + jsonIntgBridge);
100 return false;
101 }
102 }
103
104 // check state
105 String jsonState = jsonNode.get(STATE).asText();
106 String state = node.state().name();
107 if (!jsonState.equals(state)) {
108 description.appendText("state was " + jsonState);
109 return false;
110 }
111
112 // check data IP
113 JsonNode jsonDataIp = jsonNode.get(DATA_IP);
114 if (jsonDataIp != null) {
115 String dataIp = node.dataIp().toString();
116 if (!jsonDataIp.asText().equals(dataIp)) {
117 description.appendText("Data IP was " + jsonDataIp.asText());
118 return false;
119 }
120 }
121
Jian Li0c632722019-05-08 15:58:04 +0900122 // check external interface
123 JsonNode jsonExtIntf = jsonNode.get(EXTERNAL_INTF);
124 if (jsonExtIntf != null) {
125 String extIntf = node.extIntf();
126 if (!jsonExtIntf.asText().equals(extIntf)) {
127 description.appendText("External interface was " + jsonExtIntf.asText());
128 return false;
129 }
130 }
131
132 // check external bridge IP
133 JsonNode jsonExtBridgeIp = jsonNode.get(EXTERNAL_BRIDGE_IP);
134 if (jsonExtBridgeIp != null) {
135 IpAddress extBridgeIp = node.extBridgeIp();
136 if (!jsonExtBridgeIp.asText().equals(extBridgeIp.toString())) {
137 description.appendText("External bridge IP was " + jsonExtBridgeIp.asText());
138 return false;
139 }
140 }
141
142 // check external gateway IP
143 JsonNode jsonExtGatewayIp = jsonNode.get(EXTERNAL_GATEWAY_IP);
144 if (jsonExtGatewayIp != null) {
145 IpAddress extGatewayIp = node.extGatewayIp();
146 if (!jsonExtGatewayIp.asText().equals(extGatewayIp.toString())) {
147 description.appendText("External gateway IP was " + jsonExtGatewayIp.asText());
148 return false;
149 }
150 }
151
Jian Lic2242bd2020-09-03 13:12:14 +0900152 // check external gateway MAC
153 JsonNode jsonExtGatewayMac = jsonNode.get(EXTERNAL_GATEWAY_MAC);
154 if (jsonExtGatewayMac != null) {
155 MacAddress extGatewayMac = node.extGatewayMac();
156 if (!jsonExtGatewayMac.asText().equals(extGatewayMac.toString())) {
157 description.appendText("External gateway MAC was " + jsonExtGatewayMac.asText());
158 return false;
159 }
160 }
161
Jian Li49109b52019-01-22 00:17:28 +0900162 return true;
163 }
164
165 @Override
166 public void describeTo(Description description) {
167 description.appendText(node.toString());
168 }
169
170 /**
171 * Factory to allocate an kubernetes node matcher.
172 *
173 * @param node kubernetes node object we are looking for
174 * @return matcher
175 */
176 public static K8sNodeJsonMatcher matchesK8sNode(K8sNode node) {
177 return new K8sNodeJsonMatcher(node);
178 }
179}