Jian Li | 8bcef8b | 2016-01-06 11:35:53 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Jian Li | 8bcef8b | 2016-01-06 11:35:53 -0800 | [diff] [blame] | 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.codec.impl; |
| 17 | |
| 18 | import com.fasterxml.jackson.databind.JsonNode; |
Niloofar Toorchi | 8bbe9ca | 2021-07-30 14:08:04 -0700 | [diff] [blame] | 19 | import com.fasterxml.jackson.databind.node.ObjectNode; |
Jian Li | 8bcef8b | 2016-01-06 11:35:53 -0800 | [diff] [blame] | 20 | import org.hamcrest.Description; |
| 21 | import org.hamcrest.TypeSafeDiagnosingMatcher; |
| 22 | import org.onosproject.net.flowobjective.NextObjective; |
Niloofar Toorchi | 8bbe9ca | 2021-07-30 14:08:04 -0700 | [diff] [blame] | 23 | import org.onosproject.net.flowobjective.NextTreatment; |
| 24 | import java.util.ArrayList; |
| 25 | import java.util.List; |
Jian Li | 8bcef8b | 2016-01-06 11:35:53 -0800 | [diff] [blame] | 26 | |
| 27 | /** |
| 28 | * Hamcrest matcher for nextObjective. |
| 29 | */ |
| 30 | public final class NextObjectiveJsonMatcher extends TypeSafeDiagnosingMatcher<JsonNode> { |
| 31 | |
| 32 | final NextObjective nextObjective; |
| 33 | |
| 34 | private NextObjectiveJsonMatcher(NextObjective nextObjective) { |
| 35 | this.nextObjective = nextObjective; |
| 36 | } |
| 37 | |
| 38 | @Override |
| 39 | protected boolean matchesSafely(JsonNode jsonNextObj, Description description) { |
| 40 | ObjectiveJsonMatcher.matchesObjective(nextObjective).matchesSafely(jsonNextObj); |
| 41 | |
| 42 | // check id |
| 43 | int jsonId = jsonNextObj.get("id").asInt(); |
| 44 | int id = nextObjective.id(); |
| 45 | if (jsonId != id) { |
| 46 | description.appendText("id was " + jsonId); |
| 47 | return false; |
| 48 | } |
| 49 | |
| 50 | // check type |
| 51 | String jsonType = jsonNextObj.get("type").asText(); |
| 52 | String type = nextObjective.type().toString(); |
| 53 | if (!jsonType.equals(type)) { |
| 54 | description.appendText("type was " + jsonType); |
| 55 | return false; |
| 56 | } |
| 57 | |
| 58 | // check size of treatment array |
| 59 | JsonNode jsonTreatments = jsonNextObj.get("treatments"); |
| 60 | if (jsonTreatments.size() != nextObjective.next().size()) { |
| 61 | description.appendText("treatments size was " + jsonTreatments.size()); |
| 62 | return false; |
| 63 | } |
| 64 | |
Niloofar Toorchi | 8bbe9ca | 2021-07-30 14:08:04 -0700 | [diff] [blame] | 65 | // check the weight |
| 66 | boolean result = true; |
| 67 | List<NextTreatment> nt = new ArrayList(nextObjective.nextTreatments()); |
| 68 | for (int i = 0; i < jsonTreatments.size(); i++) { |
| 69 | ObjectNode jsonTreatment = jsonTreatments.path(i).isObject() && |
| 70 | !jsonTreatments.path(i).isNull() ? (ObjectNode) jsonTreatments.path(i) : null; |
| 71 | int jsonWeight = jsonTreatment.get("weight").asInt(); |
| 72 | if (jsonWeight != nt.get(i).weight()) { |
| 73 | description.appendText("weight of NextTreatment with index " + i + " was " + jsonWeight); |
| 74 | result = false; |
| 75 | } |
| 76 | } |
| 77 | if (!result) { |
| 78 | return false; |
| 79 | } |
| 80 | |
Jian Li | 8bcef8b | 2016-01-06 11:35:53 -0800 | [diff] [blame] | 81 | // TODO: need to check the content of treatment collection |
| 82 | |
| 83 | // TODO: need to check the content of selector instance |
| 84 | |
| 85 | return true; |
| 86 | } |
| 87 | |
| 88 | @Override |
| 89 | public void describeTo(Description description) { |
| 90 | description.appendText(nextObjective.toString()); |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Factory to allocate a nextObjective matcher. |
| 95 | * |
| 96 | * @param nextObjective nextObjective object we are looking for |
| 97 | * @return matcher |
| 98 | */ |
| 99 | public static NextObjectiveJsonMatcher matchesNextObjective(NextObjective nextObjective) { |
| 100 | return new NextObjectiveJsonMatcher(nextObjective); |
| 101 | } |
| 102 | } |