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; |
Jian Li | 2784166 | 2018-04-14 01:59:47 +0900 | [diff] [blame] | 23 | import org.onosproject.openstacknode.api.OpenstackAuth; |
Jian Li | 5b402c7 | 2018-02-27 14:27:34 +0900 | [diff] [blame] | 24 | import org.onosproject.openstacknode.api.OpenstackNode; |
Jian Li | e631216 | 2018-03-21 21:41:00 +0900 | [diff] [blame] | 25 | import org.onosproject.openstacknode.api.OpenstackPhyInterface; |
Daniel Park | deefa70 | 2018-07-17 17:55:51 +0900 | [diff] [blame^] | 26 | import org.onosproject.openstacknode.api.OpenstackSshAuth; |
Jian Li | 5b402c7 | 2018-02-27 14:27:34 +0900 | [diff] [blame] | 27 | |
| 28 | import static org.onosproject.openstacknode.api.Constants.DATA_IP; |
| 29 | import static org.onosproject.openstacknode.api.Constants.MANAGEMENT_IP; |
| 30 | import static org.onosproject.openstacknode.api.Constants.VLAN_INTF_NAME; |
| 31 | |
| 32 | /** |
Jian Li | 2784166 | 2018-04-14 01:59:47 +0900 | [diff] [blame] | 33 | * Hamcrest matcher for openstack node. |
Jian Li | 5b402c7 | 2018-02-27 14:27:34 +0900 | [diff] [blame] | 34 | */ |
| 35 | public final class OpenstackNodeJsonMatcher extends TypeSafeDiagnosingMatcher<JsonNode> { |
| 36 | |
| 37 | private final OpenstackNode node; |
| 38 | private static final String INTEGRATION_BRIDGE = "integrationBridge"; |
| 39 | private static final String STATE = "state"; |
Jian Li | e631216 | 2018-03-21 21:41:00 +0900 | [diff] [blame] | 40 | private static final String PHYSICAL_INTERFACES = "phyIntfs"; |
Jian Li | 789fadb | 2018-07-10 13:59:47 +0900 | [diff] [blame] | 41 | private static final String CONTROLLERS = "controllers"; |
Jian Li | 2784166 | 2018-04-14 01:59:47 +0900 | [diff] [blame] | 42 | private static final String AUTHENTICATION = "authentication"; |
Jian Li | 92d42fc | 2018-05-25 16:23:49 +0900 | [diff] [blame] | 43 | private static final String END_POINT = "endPoint"; |
Daniel Park | deefa70 | 2018-07-17 17:55:51 +0900 | [diff] [blame^] | 44 | private static final String SSH_AUTH = "sshAuth"; |
Jian Li | 5b402c7 | 2018-02-27 14:27:34 +0900 | [diff] [blame] | 45 | |
| 46 | private OpenstackNodeJsonMatcher(OpenstackNode node) { |
| 47 | this.node = node; |
| 48 | } |
| 49 | |
| 50 | @Override |
| 51 | protected boolean matchesSafely(JsonNode jsonNode, Description description) { |
| 52 | // check hostname |
| 53 | String jsonHostname = jsonNode.get(Constants.HOST_NAME).asText(); |
| 54 | String hostname = node.hostname(); |
| 55 | if (!jsonHostname.equals(hostname)) { |
| 56 | description.appendText("hostname was " + jsonHostname); |
| 57 | return false; |
| 58 | } |
| 59 | |
| 60 | // check type |
| 61 | String jsonType = jsonNode.get(Constants.TYPE).asText(); |
| 62 | String type = node.type().name(); |
| 63 | if (!jsonType.equals(type)) { |
| 64 | description.appendText("type was " + jsonType); |
| 65 | return false; |
| 66 | } |
| 67 | |
| 68 | // check management IP |
| 69 | String jsonMgmtIp = jsonNode.get(MANAGEMENT_IP).asText(); |
| 70 | String mgmtIp = node.managementIp().toString(); |
| 71 | if (!jsonMgmtIp.equals(mgmtIp)) { |
| 72 | description.appendText("management IP was " + jsonMgmtIp); |
| 73 | return false; |
| 74 | } |
| 75 | |
| 76 | // check integration bridge |
Jian Li | 2784166 | 2018-04-14 01:59:47 +0900 | [diff] [blame] | 77 | JsonNode jsonIntgBridge = jsonNode.get(INTEGRATION_BRIDGE); |
| 78 | if (jsonIntgBridge != null) { |
| 79 | String intgBridge = node.intgBridge().toString(); |
| 80 | if (!jsonIntgBridge.asText().equals(intgBridge)) { |
| 81 | description.appendText("integration bridge was " + jsonIntgBridge); |
| 82 | return false; |
| 83 | } |
Jian Li | 5b402c7 | 2018-02-27 14:27:34 +0900 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | // check state |
| 87 | String jsonState = jsonNode.get(STATE).asText(); |
| 88 | String state = node.state().name(); |
| 89 | if (!jsonState.equals(state)) { |
| 90 | description.appendText("state was " + jsonState); |
| 91 | return false; |
| 92 | } |
| 93 | |
| 94 | // check VLAN interface |
| 95 | JsonNode jsonVlanIntf = jsonNode.get(VLAN_INTF_NAME); |
| 96 | if (jsonVlanIntf != null) { |
| 97 | String vlanIntf = node.vlanIntf(); |
| 98 | if (!jsonVlanIntf.asText().equals(vlanIntf)) { |
| 99 | description.appendText("VLAN interface was " + jsonVlanIntf.asText()); |
| 100 | return false; |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | // check data IP |
| 105 | JsonNode jsonDataIp = jsonNode.get(DATA_IP); |
| 106 | if (jsonDataIp != null) { |
| 107 | String dataIp = node.dataIp().toString(); |
| 108 | if (!jsonDataIp.asText().equals(dataIp)) { |
| 109 | description.appendText("Data IP was " + jsonDataIp.asText()); |
| 110 | return false; |
| 111 | } |
| 112 | } |
| 113 | |
Jian Li | 2784166 | 2018-04-14 01:59:47 +0900 | [diff] [blame] | 114 | // check openstack auth |
| 115 | JsonNode jsonAuth = jsonNode.get(AUTHENTICATION); |
| 116 | if (jsonAuth != null) { |
| 117 | OpenstackAuth auth = node.authentication(); |
| 118 | OpenstackAuthJsonMatcher authMatcher = |
| 119 | OpenstackAuthJsonMatcher.matchOpenstackAuth(auth); |
| 120 | if (!authMatcher.matches(jsonAuth)) { |
| 121 | return false; |
| 122 | } |
| 123 | } |
| 124 | |
Daniel Park | deefa70 | 2018-07-17 17:55:51 +0900 | [diff] [blame^] | 125 | // check openstack ssh auth |
| 126 | JsonNode jsonSshAuth = jsonNode.get(SSH_AUTH); |
| 127 | if (jsonSshAuth != null) { |
| 128 | OpenstackSshAuth sshAuth = node.sshAuthInfo(); |
| 129 | OpenstackSshAuthJsonMatcher sshAuthJsonMatcher = |
| 130 | OpenstackSshAuthJsonMatcher.matchOpenstackSshAuth(sshAuth); |
| 131 | if (!sshAuthJsonMatcher.matches(jsonSshAuth)) { |
| 132 | return false; |
| 133 | } |
| 134 | } |
| 135 | |
Jian Li | 92d42fc | 2018-05-25 16:23:49 +0900 | [diff] [blame] | 136 | // check endpoint URL |
| 137 | JsonNode jsonEndPoint = jsonNode.get(END_POINT); |
| 138 | if (jsonEndPoint != null) { |
| 139 | String endPoint = node.endPoint(); |
| 140 | if (!jsonEndPoint.asText().equals(endPoint)) { |
| 141 | description.appendText("endpoint URL was " + jsonEndPoint); |
| 142 | return false; |
| 143 | } |
| 144 | } |
| 145 | |
Jian Li | e631216 | 2018-03-21 21:41:00 +0900 | [diff] [blame] | 146 | // check physical interfaces |
| 147 | JsonNode jsonPhyIntfs = jsonNode.get(PHYSICAL_INTERFACES); |
| 148 | if (jsonPhyIntfs != null) { |
| 149 | if (jsonPhyIntfs.size() != node.phyIntfs().size()) { |
| 150 | description.appendText("physical interface size was " + jsonPhyIntfs.size()); |
| 151 | return false; |
| 152 | } |
| 153 | |
| 154 | for (OpenstackPhyInterface phyIntf : node.phyIntfs()) { |
| 155 | boolean intfFound = false; |
| 156 | for (int intfIndex = 0; intfIndex < jsonPhyIntfs.size(); intfIndex++) { |
| 157 | OpenstackPhyInterfaceJsonMatcher intfMatcher = |
| 158 | OpenstackPhyInterfaceJsonMatcher.matchesOpenstackPhyInterface(phyIntf); |
| 159 | if (intfMatcher.matches(jsonPhyIntfs.get(intfIndex))) { |
| 160 | intfFound = true; |
| 161 | break; |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | if (!intfFound) { |
| 166 | description.appendText("PhyIntf not found " + phyIntf.toString()); |
| 167 | return false; |
| 168 | } |
| 169 | } |
| 170 | } |
| 171 | |
Jian Li | 789fadb | 2018-07-10 13:59:47 +0900 | [diff] [blame] | 172 | // check controllers |
| 173 | JsonNode jsonControllers = jsonNode.get(CONTROLLERS); |
| 174 | if (jsonControllers != null) { |
| 175 | if (jsonControllers.size() != node.controllers().size()) { |
| 176 | description.appendText("controllers size was " + jsonControllers.size()); |
| 177 | return false; |
| 178 | } |
| 179 | |
| 180 | for (ControllerInfo controller : node.controllers()) { |
| 181 | boolean ctrlFound = false; |
| 182 | for (int ctrlIndex = 0; ctrlIndex < jsonControllers.size(); ctrlIndex++) { |
| 183 | OpenstackControllerJsonMatcher ctrlMatcher = |
| 184 | OpenstackControllerJsonMatcher.matchesOpenstackController(controller); |
| 185 | if (ctrlMatcher.matches(jsonControllers.get(ctrlIndex))) { |
| 186 | ctrlFound = true; |
| 187 | break; |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | if (!ctrlFound) { |
| 192 | description.appendText("Controller not found " + controller.toString()); |
| 193 | return false; |
| 194 | } |
| 195 | } |
| 196 | } |
| 197 | |
Jian Li | 5b402c7 | 2018-02-27 14:27:34 +0900 | [diff] [blame] | 198 | return true; |
| 199 | } |
| 200 | |
| 201 | @Override |
| 202 | public void describeTo(Description description) { |
| 203 | description.appendText(node.toString()); |
| 204 | } |
| 205 | |
| 206 | /** |
| 207 | * Factory to allocate an openstack node matcher. |
| 208 | * |
| 209 | * @param node openstack node object we are looking for |
| 210 | * @return matcher |
| 211 | */ |
| 212 | public static OpenstackNodeJsonMatcher matchesOpenstackNode(OpenstackNode node) { |
| 213 | return new OpenstackNodeJsonMatcher(node); |
| 214 | } |
| 215 | } |