Jian Li | 5b402c7 | 2018-02-27 14:27:34 +0900 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.openstacknode.codec; |
| 17 | |
| 18 | import com.fasterxml.jackson.databind.JsonNode; |
| 19 | import org.hamcrest.Description; |
| 20 | import org.hamcrest.TypeSafeDiagnosingMatcher; |
Jian Li | 789fadb | 2018-07-10 13:59:47 +0900 | [diff] [blame] | 21 | import org.onosproject.net.behaviour.ControllerInfo; |
Jian Li | 5b402c7 | 2018-02-27 14:27:34 +0900 | [diff] [blame] | 22 | import org.onosproject.openstacknode.api.Constants; |
Daniel Park | d02d7bd | 2018-08-23 23:04:31 +0900 | [diff] [blame^] | 23 | import org.onosproject.openstacknode.api.DpdkConfig; |
Jian Li | 2784166 | 2018-04-14 01:59:47 +0900 | [diff] [blame] | 24 | import org.onosproject.openstacknode.api.OpenstackAuth; |
Jian Li | 5b402c7 | 2018-02-27 14:27:34 +0900 | [diff] [blame] | 25 | import org.onosproject.openstacknode.api.OpenstackNode; |
Jian Li | e631216 | 2018-03-21 21:41:00 +0900 | [diff] [blame] | 26 | import org.onosproject.openstacknode.api.OpenstackPhyInterface; |
Daniel Park | deefa70 | 2018-07-17 17:55:51 +0900 | [diff] [blame] | 27 | import org.onosproject.openstacknode.api.OpenstackSshAuth; |
Jian Li | 5b402c7 | 2018-02-27 14:27:34 +0900 | [diff] [blame] | 28 | |
| 29 | import static org.onosproject.openstacknode.api.Constants.DATA_IP; |
| 30 | import static org.onosproject.openstacknode.api.Constants.MANAGEMENT_IP; |
| 31 | import static org.onosproject.openstacknode.api.Constants.VLAN_INTF_NAME; |
| 32 | |
| 33 | /** |
Jian Li | 2784166 | 2018-04-14 01:59:47 +0900 | [diff] [blame] | 34 | * Hamcrest matcher for openstack node. |
Jian Li | 5b402c7 | 2018-02-27 14:27:34 +0900 | [diff] [blame] | 35 | */ |
| 36 | public 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 Li | e631216 | 2018-03-21 21:41:00 +0900 | [diff] [blame] | 41 | private static final String PHYSICAL_INTERFACES = "phyIntfs"; |
Daniel Park | d02d7bd | 2018-08-23 23:04:31 +0900 | [diff] [blame^] | 42 | private static final String DPDK_CONFIG = "dpdkConfig"; |
Jian Li | 789fadb | 2018-07-10 13:59:47 +0900 | [diff] [blame] | 43 | private static final String CONTROLLERS = "controllers"; |
Jian Li | 2784166 | 2018-04-14 01:59:47 +0900 | [diff] [blame] | 44 | private static final String AUTHENTICATION = "authentication"; |
Jian Li | 6d41036 | 2018-08-17 09:41:08 +0900 | [diff] [blame] | 45 | private static final String END_POINT = "endpoint"; |
Daniel Park | deefa70 | 2018-07-17 17:55:51 +0900 | [diff] [blame] | 46 | private static final String SSH_AUTH = "sshAuth"; |
Jian Li | 5b402c7 | 2018-02-27 14:27:34 +0900 | [diff] [blame] | 47 | |
| 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 Li | 2784166 | 2018-04-14 01:59:47 +0900 | [diff] [blame] | 79 | 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 Li | 5b402c7 | 2018-02-27 14:27:34 +0900 | [diff] [blame] | 86 | } |
| 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 Li | 2784166 | 2018-04-14 01:59:47 +0900 | [diff] [blame] | 116 | // 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 Park | deefa70 | 2018-07-17 17:55:51 +0900 | [diff] [blame] | 127 | // 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 Park | d02d7bd | 2018-08-23 23:04:31 +0900 | [diff] [blame^] | 138 | // check dpdk config |
| 139 | JsonNode jsonDpdkConfig = jsonNode.get(DPDK_CONFIG); |
| 140 | if (jsonDpdkConfig != null) { |
| 141 | DpdkConfig dpdkConfig = node.dpdkConfig(); |
| 142 | |
| 143 | } |
| 144 | |
Jian Li | 92d42fc | 2018-05-25 16:23:49 +0900 | [diff] [blame] | 145 | // check endpoint URL |
Jian Li | 6d41036 | 2018-08-17 09:41:08 +0900 | [diff] [blame] | 146 | 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 Li | 92d42fc | 2018-05-25 16:23:49 +0900 | [diff] [blame] | 151 | return false; |
| 152 | } |
| 153 | } |
| 154 | |
Jian Li | e631216 | 2018-03-21 21:41:00 +0900 | [diff] [blame] | 155 | // 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 Li | 789fadb | 2018-07-10 13:59:47 +0900 | [diff] [blame] | 181 | // 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 Li | 5b402c7 | 2018-02-27 14:27:34 +0900 | [diff] [blame] | 207 | 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 | } |