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