Aris C. Risdianto | a579c8f | 2017-04-13 19:38:46 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015-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 | |
| 17 | package org.onosproject.codec.impl; |
| 18 | |
| 19 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 20 | import com.fasterxml.jackson.databind.node.JsonNodeFactory; |
| 21 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 22 | import org.junit.Before; |
| 23 | import org.junit.Test; |
| 24 | import org.onosproject.TestApplicationId; |
| 25 | import org.onosproject.codec.JsonCodec; |
| 26 | import org.onosproject.core.ApplicationId; |
| 27 | import org.onosproject.core.CoreService; |
| 28 | import org.onosproject.net.ConnectPoint; |
| 29 | import org.onosproject.net.DeviceId; |
| 30 | import org.onosproject.net.PortNumber; |
| 31 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 32 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 33 | import org.onosproject.net.flow.TrafficSelector; |
| 34 | import org.onosproject.net.flow.TrafficTreatment; |
| 35 | import org.onosproject.net.intent.AbstractIntentTest; |
| 36 | import org.onosproject.net.intent.IntentService; |
| 37 | import org.onosproject.net.intent.IntentServiceAdapter; |
| 38 | import org.onosproject.net.intent.Key; |
| 39 | import org.onosproject.net.intent.MultiPointToSinglePointIntent; |
| 40 | |
| 41 | import java.io.IOException; |
| 42 | import java.util.Arrays; |
| 43 | import java.util.HashSet; |
| 44 | import java.util.Set; |
| 45 | |
| 46 | import static org.easymock.EasyMock.*; |
| 47 | import static org.hamcrest.MatcherAssert.assertThat; |
| 48 | import static org.hamcrest.Matchers.*; |
Ray Milkey | 27cff8c | 2018-03-05 14:58:26 -0800 | [diff] [blame] | 49 | import static org.onosproject.net.intent.ConnectivityIntentTest.FP2; |
| 50 | import static org.onosproject.net.intent.ConnectivityIntentTest.FPS1; |
Aris C. Risdianto | a579c8f | 2017-04-13 19:38:46 +0900 | [diff] [blame] | 51 | |
| 52 | /** |
| 53 | * Suite of tests of the multi-to-single point intent's codec for rest api. |
| 54 | */ |
| 55 | |
| 56 | public class MultiPointToSinglePointIntentCodecTest extends AbstractIntentTest { |
| 57 | |
| 58 | public static final ApplicationId APPID = new TestApplicationId("foo"); |
| 59 | public static final Key KEY = Key.of(1L, APPID); |
| 60 | public static final TrafficSelector MATCH = DefaultTrafficSelector.emptySelector(); |
| 61 | public static final TrafficTreatment NOP = DefaultTrafficTreatment.emptyTreatment(); |
| 62 | public static final ConnectPoint P1 = new ConnectPoint(DeviceId.deviceId("111"), PortNumber.portNumber(0x1)); |
| 63 | public static final ConnectPoint P2 = new ConnectPoint(DeviceId.deviceId("222"), PortNumber.portNumber(0x2)); |
| 64 | public static final ConnectPoint P3 = new ConnectPoint(DeviceId.deviceId("333"), PortNumber.portNumber(0x3)); |
| 65 | |
| 66 | public static final Set<ConnectPoint> PS1 = new HashSet<>(Arrays.asList(new ConnectPoint[]{P1, P3})); |
| 67 | public static final Set<ConnectPoint> PS2 = new HashSet<>(Arrays.asList(new ConnectPoint[]{P2, P3})); |
| 68 | |
| 69 | private final MockCodecContext context = new MockCodecContext(); |
| 70 | final CoreService mockCoreService = createMock(CoreService.class); |
| 71 | |
| 72 | private static final String INGRESS_POINT = "ingressPoint"; |
| 73 | private static final String EGRESS_POINT = "egressPoint"; |
| 74 | private static final String CP_POINTS = "connectPoints"; |
| 75 | |
| 76 | @Before |
| 77 | public void setUpIntentService() { |
| 78 | final IntentService mockIntentService = new IntentServiceAdapter(); |
| 79 | context.registerService(IntentService.class, mockIntentService); |
| 80 | context.registerService(CoreService.class, mockCoreService); |
| 81 | expect(mockCoreService.getAppId(APPID.name())) |
| 82 | .andReturn(APPID); |
| 83 | replay(mockCoreService); |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Tests the encoding of a multi point to single point intent using Intent Codec. |
| 88 | */ |
| 89 | @Test |
| 90 | public void encodeMultiPointToSinglePointIntent() { |
| 91 | |
| 92 | final MultiPointToSinglePointIntent intent = MultiPointToSinglePointIntent.builder() |
| 93 | .appId(APPID) |
| 94 | .selector(MATCH) |
| 95 | .treatment(NOP) |
Ray Milkey | 27cff8c | 2018-03-05 14:58:26 -0800 | [diff] [blame] | 96 | .filteredIngressPoints(FPS1) |
| 97 | .filteredEgressPoint(FP2) |
Aris C. Risdianto | a579c8f | 2017-04-13 19:38:46 +0900 | [diff] [blame] | 98 | .build(); |
| 99 | |
| 100 | assertThat(intent, notNullValue()); |
| 101 | assertThat(intent, instanceOf(MultiPointToSinglePointIntent.class)); |
| 102 | |
| 103 | final JsonCodec<MultiPointToSinglePointIntent> intentCodec = |
| 104 | context.codec(MultiPointToSinglePointIntent.class); |
| 105 | assertThat(intentCodec, notNullValue()); |
| 106 | |
| 107 | final ObjectNode result = intentCodec.encode(intent, context); |
| 108 | |
| 109 | assertThat(result.get("type").textValue(), is("MultiPointToSinglePointIntent")); |
| 110 | assertThat(result.get("id").textValue(), is("0x0")); |
| 111 | assertThat(result.get("appId").textValue(), is("foo")); |
| 112 | assertThat(result.get("priority").asInt(), is(100)); |
Ray Milkey | 27cff8c | 2018-03-05 14:58:26 -0800 | [diff] [blame] | 113 | |
| 114 | boolean found1 = false; |
| 115 | boolean found3 = false; |
| 116 | for (int i = 0; i < FPS1.size(); i++) { |
| 117 | String portString = result.get("ingressPoint").get(i).get("port").textValue(); |
| 118 | if (portString.equals("1")) { |
| 119 | found1 = true; |
| 120 | } else if (portString.equals("3")) { |
| 121 | found3 = true; |
| 122 | } |
| 123 | } |
| 124 | assertThat("Port 1 was not found", found1, is(true)); |
| 125 | assertThat("Port 3 was not found", found3, is(true)); |
| 126 | |
Aris C. Risdianto | a579c8f | 2017-04-13 19:38:46 +0900 | [diff] [blame] | 127 | assertThat(result.get("egressPoint").get("port").textValue(), is("2")); |
| 128 | assertThat(result.get("egressPoint").get("device").textValue(), is("222")); |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Tests the multi point to single point intent decoding with JSON codec. |
| 133 | * |
| 134 | * @throws IOException if JSON processing fails |
| 135 | */ |
| 136 | @Test |
| 137 | public void decodeMultiPointToSinglePointIntent() throws IOException { |
| 138 | |
| 139 | final JsonNodeFactory nodeFactory = JsonNodeFactory.instance; |
| 140 | ObjectNode json = nodeFactory.objectNode(); |
| 141 | json.put("type", "MultiPointToSinglePointIntent"); |
| 142 | json.put("id", "0x0"); |
| 143 | json.put("appId", "foo"); |
| 144 | json.put("priority", 100); |
| 145 | ArrayNode ingress = nodeFactory.arrayNode(); |
| 146 | ObjectNode ingressPoint = nodeFactory.objectNode(); |
| 147 | ingressPoint.put("port", "3"); |
| 148 | ingressPoint.put("device", "333"); |
| 149 | ingress.add(ingressPoint); |
| 150 | ObjectNode ingressPoint2 = nodeFactory.objectNode(); |
| 151 | ingressPoint2.put("port", "1"); |
| 152 | ingressPoint2.put("device", "111"); |
| 153 | ingress.add(ingressPoint2); |
| 154 | json.set("ingressPoint", ingress); |
| 155 | ObjectNode egressPoint = nodeFactory.objectNode(); |
| 156 | egressPoint.put("port", "2"); |
| 157 | egressPoint.put("device", "222"); |
| 158 | json.set("egressPoint", egressPoint); |
| 159 | assertThat(json, notNullValue()); |
| 160 | |
| 161 | JsonCodec<MultiPointToSinglePointIntent> intentCodec = context.codec(MultiPointToSinglePointIntent.class); |
| 162 | assertThat(intentCodec, notNullValue()); |
| 163 | |
| 164 | final MultiPointToSinglePointIntent intent = intentCodec.decode(json, context); |
| 165 | |
| 166 | assertThat(intent.toString(), notNullValue()); |
| 167 | assertThat(intent, instanceOf(MultiPointToSinglePointIntent.class)); |
| 168 | assertThat(intent.priority(), is(100)); |
| 169 | assertThat(intent.ingressPoints().toString(), is("[333/3, 111/1]")); |
| 170 | assertThat(intent.egressPoint().toString(), is("222/2")); |
| 171 | |
| 172 | } |
| 173 | |
| 174 | } |