blob: 7ec38664cb9d6736b9a508f43579987abc0a02ef [file] [log] [blame]
Jian Li3defa842019-02-12 00:31:35 +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 Lie2a04ce2020-07-01 19:07:02 +090021import org.onosproject.k8snode.api.HostNodesInfo;
Jian Li3defa842019-02-12 00:31:35 +090022import org.onosproject.k8snode.api.K8sApiConfig;
23
24/**
25 * Hamcrest matcher for kubernetes API config.
26 */
27public final class K8sApiConfigJsonMatcher extends TypeSafeDiagnosingMatcher<JsonNode> {
28
29 private final K8sApiConfig k8sApiConfig;
30
Jian Lie2a04ce2020-07-01 19:07:02 +090031 private static final String CLUSTER_NAME = "clusterName";
32 private static final String SEGMENT_ID = "segmentId";
33 private static final String EXT_NETWORK_CIDR = "extNetworkCidr";
34 private static final String MODE = "mode";
Jian Li3defa842019-02-12 00:31:35 +090035 private static final String SCHEME = "scheme";
36 private static final String IP_ADDRESS = "ipAddress";
37 private static final String PORT = "port";
Jian Li1cee9882019-02-13 11:25:25 +090038 private static final String STATE = "state";
Jian Li3defa842019-02-12 00:31:35 +090039 private static final String TOKEN = "token";
40 private static final String CA_CERT_DATA = "caCertData";
41 private static final String CLIENT_CERT_DATA = "clientCertData";
42 private static final String CLIENT_KEY_DATA = "clientKeyData";
Jian Lie2a04ce2020-07-01 19:07:02 +090043 private static final String HOST_NODES_INFO = "hostNodesInfo";
Jian Li3defa842019-02-12 00:31:35 +090044
45 private K8sApiConfigJsonMatcher(K8sApiConfig k8sApiConfig) {
46 this.k8sApiConfig = k8sApiConfig;
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 = k8sApiConfig.clusterName();
55 if (!jsonClusterName.equals(clusterName)) {
56 description.appendText("cluster name was " + jsonClusterName);
57 return false;
58 }
59
60 // check segment ID
61 int jsonSegmentId = jsonNode.get(SEGMENT_ID).asInt();
62 int segmentId = k8sApiConfig.segmentId();
63 if (jsonSegmentId != segmentId) {
64 description.appendText("Segment ID was " + jsonSegmentId);
65 return false;
66 }
67
68 // check mode
69 String jsonMode = jsonNode.get(MODE).asText();
70 String mode = k8sApiConfig.mode().name();
71 if (!jsonMode.equals(mode)) {
72 description.appendText("mode was " + jsonMode);
73 return false;
74 }
75
76 // check external network CIDR
77 JsonNode jsonCidr = jsonNode.get(EXT_NETWORK_CIDR);
78 String cidr = k8sApiConfig.extNetworkCidr().toString();
79 if (jsonCidr != null) {
80 if (!jsonCidr.asText().equals(cidr)) {
81 description.appendText("External network CIDR was " + jsonCidr);
82 return false;
83 }
84 }
85
Jian Li3defa842019-02-12 00:31:35 +090086 // check scheme
87 String jsonScheme = jsonNode.get(SCHEME).asText();
88 String scheme = k8sApiConfig.scheme().name();
89 if (!jsonScheme.equals(scheme)) {
90 description.appendText("scheme was " + jsonScheme);
91 return false;
92 }
93
94 // check IP address
95 String jsonIpAddress = jsonNode.get(IP_ADDRESS).asText();
96 String ipAddress = k8sApiConfig.ipAddress().toString();
97 if (!jsonIpAddress.equals(ipAddress)) {
98 description.appendText("ipAddress was " + jsonIpAddress);
99 return false;
100 }
101
102 // check port
103 int jsonPort = jsonNode.get(PORT).asInt();
104 int port = k8sApiConfig.port();
105 if (jsonPort != port) {
106 description.appendText("port was " + jsonPort);
107 return false;
108 }
109
Jian Li1cee9882019-02-13 11:25:25 +0900110 // check state
111 JsonNode jsonState = jsonNode.get(STATE);
112 String state = k8sApiConfig.state().name();
113 if (jsonState != null) {
114 if (!jsonState.asText().equals(state)) {
115 description.appendText("state was " + jsonState);
116 return false;
117 }
118 }
119
Jian Li3defa842019-02-12 00:31:35 +0900120 // check token
121 JsonNode jsonToken = jsonNode.get(TOKEN);
122 String token = k8sApiConfig.token();
123 if (jsonToken != null) {
124 if (!jsonToken.asText().equals(token)) {
125 description.appendText("token was " + jsonToken);
126 return false;
127 }
128 }
129
130 // check caCertData
131 JsonNode jsonCaCertData = jsonNode.get(CA_CERT_DATA);
132 String caCertData = k8sApiConfig.caCertData();
133 if (jsonCaCertData != null) {
134 if (!jsonCaCertData.asText().equals(caCertData)) {
135 description.appendText("caCertData was " + jsonCaCertData);
136 return false;
137 }
138 }
139
140 // check clientCertData
141 JsonNode jsonClientCertData = jsonNode.get(CLIENT_CERT_DATA);
142 String clientCertData = k8sApiConfig.clientCertData();
143
144 if (jsonClientCertData != null) {
145 if (!jsonClientCertData.asText().equals(clientCertData)) {
146 description.appendText("clientCertData was " + jsonClientCertData);
147 return false;
148 }
149 }
150
151 // check clientKeyData
152 JsonNode jsonClientKeyData = jsonNode.get(CLIENT_KEY_DATA);
153 String clientKeyData = k8sApiConfig.clientKeyData();
154
155 if (jsonClientKeyData != null) {
156 if (!jsonClientKeyData.asText().equals(clientKeyData)) {
157 description.appendText("clientKeyData was " + jsonClientKeyData);
158 return false;
159 }
160 }
161
Jian Lie2a04ce2020-07-01 19:07:02 +0900162 // check hostNodesInfo size
163 JsonNode jsonInfos = jsonNode.get(HOST_NODES_INFO);
164 if (jsonInfos.size() != k8sApiConfig.infos().size()) {
165 description.appendText("Info size was " + jsonInfos.size());
166 return false;
167 }
168
169 // check info
170 for (HostNodesInfo info : k8sApiConfig.infos()) {
171 boolean infoFound = false;
172 for (int infoIndex = 0; infoIndex < jsonInfos.size(); infoIndex++) {
173 HostNodesInfoJsonMatcher infoMatcher = HostNodesInfoJsonMatcher.matchesHostNodesInfo(info);
174 if (infoMatcher.matches(jsonInfos.get(infoIndex))) {
175 infoFound = true;
176 break;
177 }
178 }
179 if (!infoFound) {
180 description.appendText("Info not found " + info.toString());
181 return false;
182 }
183 }
184
Jian Li3defa842019-02-12 00:31:35 +0900185 return true;
186 }
187
188 @Override
189 public void describeTo(Description description) {
190 description.appendText(k8sApiConfig.toString());
191 }
192
193 /**
194 * Factory to allocate an k8sApiConfig matcher.
195 *
196 * @param config k8sApiConfig object we are looking for
197 * @return matcher
198 */
199 public static K8sApiConfigJsonMatcher matchesK8sApiConfig(K8sApiConfig config) {
200 return new K8sApiConfigJsonMatcher(config);
201 }
202}