blob: 6e3b4732209841978d68f66ad6656cc7a4e43cb4 [file] [log] [blame]
Jian Li5b402c72018-02-27 14:27:34 +09001/*
2 * Copyright 2018-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.openstacknode.codec;
17
18import com.fasterxml.jackson.databind.JsonNode;
19import org.hamcrest.Description;
20import org.hamcrest.TypeSafeDiagnosingMatcher;
Jian Li789fadb2018-07-10 13:59:47 +090021import org.onosproject.net.behaviour.ControllerInfo;
Jian Li5b402c72018-02-27 14:27:34 +090022import org.onosproject.openstacknode.api.Constants;
Daniel Parkd02d7bd2018-08-23 23:04:31 +090023import org.onosproject.openstacknode.api.DpdkConfig;
Jian Li27841662018-04-14 01:59:47 +090024import org.onosproject.openstacknode.api.OpenstackAuth;
Jian Li5b402c72018-02-27 14:27:34 +090025import org.onosproject.openstacknode.api.OpenstackNode;
Jian Lie6312162018-03-21 21:41:00 +090026import org.onosproject.openstacknode.api.OpenstackPhyInterface;
Daniel Parkdeefa702018-07-17 17:55:51 +090027import org.onosproject.openstacknode.api.OpenstackSshAuth;
Jian Li5b402c72018-02-27 14:27:34 +090028
29import static org.onosproject.openstacknode.api.Constants.DATA_IP;
30import static org.onosproject.openstacknode.api.Constants.MANAGEMENT_IP;
31import static org.onosproject.openstacknode.api.Constants.VLAN_INTF_NAME;
32
33/**
Jian Li27841662018-04-14 01:59:47 +090034 * Hamcrest matcher for openstack node.
Jian Li5b402c72018-02-27 14:27:34 +090035 */
36public final class OpenstackNodeJsonMatcher extends TypeSafeDiagnosingMatcher<JsonNode> {
37
38 private final OpenstackNode node;
39 private static final String INTEGRATION_BRIDGE = "integrationBridge";
40 private static final String STATE = "state";
Jian Lie6312162018-03-21 21:41:00 +090041 private static final String PHYSICAL_INTERFACES = "phyIntfs";
Daniel Parkd02d7bd2018-08-23 23:04:31 +090042 private static final String DPDK_CONFIG = "dpdkConfig";
Jian Li789fadb2018-07-10 13:59:47 +090043 private static final String CONTROLLERS = "controllers";
Jian Li27841662018-04-14 01:59:47 +090044 private static final String AUTHENTICATION = "authentication";
Jian Li6d410362018-08-17 09:41:08 +090045 private static final String END_POINT = "endpoint";
Daniel Parkdeefa702018-07-17 17:55:51 +090046 private static final String SSH_AUTH = "sshAuth";
Jian Li5b402c72018-02-27 14:27:34 +090047
48 private OpenstackNodeJsonMatcher(OpenstackNode node) {
49 this.node = node;
50 }
51
52 @Override
53 protected boolean matchesSafely(JsonNode jsonNode, Description description) {
54 // check hostname
55 String jsonHostname = jsonNode.get(Constants.HOST_NAME).asText();
56 String hostname = node.hostname();
57 if (!jsonHostname.equals(hostname)) {
58 description.appendText("hostname was " + jsonHostname);
59 return false;
60 }
61
62 // check type
63 String jsonType = jsonNode.get(Constants.TYPE).asText();
64 String type = node.type().name();
65 if (!jsonType.equals(type)) {
66 description.appendText("type was " + jsonType);
67 return false;
68 }
69
70 // check management IP
71 String jsonMgmtIp = jsonNode.get(MANAGEMENT_IP).asText();
72 String mgmtIp = node.managementIp().toString();
73 if (!jsonMgmtIp.equals(mgmtIp)) {
74 description.appendText("management IP was " + jsonMgmtIp);
75 return false;
76 }
77
78 // check integration bridge
Jian Li27841662018-04-14 01:59:47 +090079 JsonNode jsonIntgBridge = jsonNode.get(INTEGRATION_BRIDGE);
80 if (jsonIntgBridge != null) {
81 String intgBridge = node.intgBridge().toString();
82 if (!jsonIntgBridge.asText().equals(intgBridge)) {
83 description.appendText("integration bridge was " + jsonIntgBridge);
84 return false;
85 }
Jian Li5b402c72018-02-27 14:27:34 +090086 }
87
88 // check state
89 String jsonState = jsonNode.get(STATE).asText();
90 String state = node.state().name();
91 if (!jsonState.equals(state)) {
92 description.appendText("state was " + jsonState);
93 return false;
94 }
95
96 // check VLAN interface
97 JsonNode jsonVlanIntf = jsonNode.get(VLAN_INTF_NAME);
98 if (jsonVlanIntf != null) {
99 String vlanIntf = node.vlanIntf();
100 if (!jsonVlanIntf.asText().equals(vlanIntf)) {
101 description.appendText("VLAN interface was " + jsonVlanIntf.asText());
102 return false;
103 }
104 }
105
106 // check data IP
107 JsonNode jsonDataIp = jsonNode.get(DATA_IP);
108 if (jsonDataIp != null) {
109 String dataIp = node.dataIp().toString();
110 if (!jsonDataIp.asText().equals(dataIp)) {
111 description.appendText("Data IP was " + jsonDataIp.asText());
112 return false;
113 }
114 }
115
Jian Li27841662018-04-14 01:59:47 +0900116 // check openstack auth
117 JsonNode jsonAuth = jsonNode.get(AUTHENTICATION);
118 if (jsonAuth != null) {
119 OpenstackAuth auth = node.authentication();
120 OpenstackAuthJsonMatcher authMatcher =
121 OpenstackAuthJsonMatcher.matchOpenstackAuth(auth);
122 if (!authMatcher.matches(jsonAuth)) {
123 return false;
124 }
125 }
126
Daniel Parkdeefa702018-07-17 17:55:51 +0900127 // check openstack ssh auth
128 JsonNode jsonSshAuth = jsonNode.get(SSH_AUTH);
129 if (jsonSshAuth != null) {
130 OpenstackSshAuth sshAuth = node.sshAuthInfo();
131 OpenstackSshAuthJsonMatcher sshAuthJsonMatcher =
132 OpenstackSshAuthJsonMatcher.matchOpenstackSshAuth(sshAuth);
133 if (!sshAuthJsonMatcher.matches(jsonSshAuth)) {
134 return false;
135 }
136 }
137
Daniel Parkd02d7bd2018-08-23 23:04:31 +0900138 // check dpdk config
139 JsonNode jsonDpdkConfig = jsonNode.get(DPDK_CONFIG);
140 if (jsonDpdkConfig != null) {
141 DpdkConfig dpdkConfig = node.dpdkConfig();
142
143 }
144
Jian Li92d42fc2018-05-25 16:23:49 +0900145 // check endpoint URL
Jian Li6d410362018-08-17 09:41:08 +0900146 JsonNode jsonEndpoint = jsonNode.get(END_POINT);
147 if (jsonEndpoint != null) {
148 String endpoint = node.endpoint();
149 if (!jsonEndpoint.asText().equals(endpoint)) {
150 description.appendText("endpoint URL was " + jsonEndpoint);
Jian Li92d42fc2018-05-25 16:23:49 +0900151 return false;
152 }
153 }
154
Jian Lie6312162018-03-21 21:41:00 +0900155 // check physical interfaces
156 JsonNode jsonPhyIntfs = jsonNode.get(PHYSICAL_INTERFACES);
157 if (jsonPhyIntfs != null) {
158 if (jsonPhyIntfs.size() != node.phyIntfs().size()) {
159 description.appendText("physical interface size was " + jsonPhyIntfs.size());
160 return false;
161 }
162
163 for (OpenstackPhyInterface phyIntf : node.phyIntfs()) {
164 boolean intfFound = false;
165 for (int intfIndex = 0; intfIndex < jsonPhyIntfs.size(); intfIndex++) {
166 OpenstackPhyInterfaceJsonMatcher intfMatcher =
167 OpenstackPhyInterfaceJsonMatcher.matchesOpenstackPhyInterface(phyIntf);
168 if (intfMatcher.matches(jsonPhyIntfs.get(intfIndex))) {
169 intfFound = true;
170 break;
171 }
172 }
173
174 if (!intfFound) {
175 description.appendText("PhyIntf not found " + phyIntf.toString());
176 return false;
177 }
178 }
179 }
180
Jian Li789fadb2018-07-10 13:59:47 +0900181 // check controllers
182 JsonNode jsonControllers = jsonNode.get(CONTROLLERS);
183 if (jsonControllers != null) {
184 if (jsonControllers.size() != node.controllers().size()) {
185 description.appendText("controllers size was " + jsonControllers.size());
186 return false;
187 }
188
189 for (ControllerInfo controller : node.controllers()) {
190 boolean ctrlFound = false;
191 for (int ctrlIndex = 0; ctrlIndex < jsonControllers.size(); ctrlIndex++) {
192 OpenstackControllerJsonMatcher ctrlMatcher =
193 OpenstackControllerJsonMatcher.matchesOpenstackController(controller);
194 if (ctrlMatcher.matches(jsonControllers.get(ctrlIndex))) {
195 ctrlFound = true;
196 break;
197 }
198 }
199
200 if (!ctrlFound) {
201 description.appendText("Controller not found " + controller.toString());
202 return false;
203 }
204 }
205 }
206
Jian Li5b402c72018-02-27 14:27:34 +0900207 return true;
208 }
209
210 @Override
211 public void describeTo(Description description) {
212 description.appendText(node.toString());
213 }
214
215 /**
216 * Factory to allocate an openstack node matcher.
217 *
218 * @param node openstack node object we are looking for
219 * @return matcher
220 */
221 public static OpenstackNodeJsonMatcher matchesOpenstackNode(OpenstackNode node) {
222 return new OpenstackNodeJsonMatcher(node);
223 }
224}