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.*; |
| 49 | |
| 50 | /** |
| 51 | * Suite of tests of the multi-to-single point intent's codec for rest api. |
| 52 | */ |
| 53 | |
| 54 | public class MultiPointToSinglePointIntentCodecTest extends AbstractIntentTest { |
| 55 | |
| 56 | public static final ApplicationId APPID = new TestApplicationId("foo"); |
| 57 | public static final Key KEY = Key.of(1L, APPID); |
| 58 | public static final TrafficSelector MATCH = DefaultTrafficSelector.emptySelector(); |
| 59 | public static final TrafficTreatment NOP = DefaultTrafficTreatment.emptyTreatment(); |
| 60 | public static final ConnectPoint P1 = new ConnectPoint(DeviceId.deviceId("111"), PortNumber.portNumber(0x1)); |
| 61 | public static final ConnectPoint P2 = new ConnectPoint(DeviceId.deviceId("222"), PortNumber.portNumber(0x2)); |
| 62 | public static final ConnectPoint P3 = new ConnectPoint(DeviceId.deviceId("333"), PortNumber.portNumber(0x3)); |
| 63 | |
| 64 | public static final Set<ConnectPoint> PS1 = new HashSet<>(Arrays.asList(new ConnectPoint[]{P1, P3})); |
| 65 | public static final Set<ConnectPoint> PS2 = new HashSet<>(Arrays.asList(new ConnectPoint[]{P2, P3})); |
| 66 | |
| 67 | private final MockCodecContext context = new MockCodecContext(); |
| 68 | final CoreService mockCoreService = createMock(CoreService.class); |
| 69 | |
| 70 | private static final String INGRESS_POINT = "ingressPoint"; |
| 71 | private static final String EGRESS_POINT = "egressPoint"; |
| 72 | private static final String CP_POINTS = "connectPoints"; |
| 73 | |
| 74 | @Before |
| 75 | public void setUpIntentService() { |
| 76 | final IntentService mockIntentService = new IntentServiceAdapter(); |
| 77 | context.registerService(IntentService.class, mockIntentService); |
| 78 | context.registerService(CoreService.class, mockCoreService); |
| 79 | expect(mockCoreService.getAppId(APPID.name())) |
| 80 | .andReturn(APPID); |
| 81 | replay(mockCoreService); |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Tests the encoding of a multi point to single point intent using Intent Codec. |
| 86 | */ |
| 87 | @Test |
| 88 | public void encodeMultiPointToSinglePointIntent() { |
| 89 | |
| 90 | final MultiPointToSinglePointIntent intent = MultiPointToSinglePointIntent.builder() |
| 91 | .appId(APPID) |
| 92 | .selector(MATCH) |
| 93 | .treatment(NOP) |
| 94 | .ingressPoints(PS1) |
| 95 | .egressPoint(P2) |
| 96 | .build(); |
| 97 | |
| 98 | assertThat(intent, notNullValue()); |
| 99 | assertThat(intent, instanceOf(MultiPointToSinglePointIntent.class)); |
| 100 | |
| 101 | final JsonCodec<MultiPointToSinglePointIntent> intentCodec = |
| 102 | context.codec(MultiPointToSinglePointIntent.class); |
| 103 | assertThat(intentCodec, notNullValue()); |
| 104 | |
| 105 | final ObjectNode result = intentCodec.encode(intent, context); |
| 106 | |
| 107 | assertThat(result.get("type").textValue(), is("MultiPointToSinglePointIntent")); |
| 108 | assertThat(result.get("id").textValue(), is("0x0")); |
| 109 | assertThat(result.get("appId").textValue(), is("foo")); |
| 110 | assertThat(result.get("priority").asInt(), is(100)); |
| 111 | assertThat(result.get("ingressPoint").get(0).get("port").textValue(), is("3")); |
| 112 | assertThat(result.get("ingressPoint").get(1).get("port").textValue(), is("1")); |
| 113 | assertThat(result.get("egressPoint").get("port").textValue(), is("2")); |
| 114 | assertThat(result.get("egressPoint").get("device").textValue(), is("222")); |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Tests the multi point to single point intent decoding with JSON codec. |
| 119 | * |
| 120 | * @throws IOException if JSON processing fails |
| 121 | */ |
| 122 | @Test |
| 123 | public void decodeMultiPointToSinglePointIntent() throws IOException { |
| 124 | |
| 125 | final JsonNodeFactory nodeFactory = JsonNodeFactory.instance; |
| 126 | ObjectNode json = nodeFactory.objectNode(); |
| 127 | json.put("type", "MultiPointToSinglePointIntent"); |
| 128 | json.put("id", "0x0"); |
| 129 | json.put("appId", "foo"); |
| 130 | json.put("priority", 100); |
| 131 | ArrayNode ingress = nodeFactory.arrayNode(); |
| 132 | ObjectNode ingressPoint = nodeFactory.objectNode(); |
| 133 | ingressPoint.put("port", "3"); |
| 134 | ingressPoint.put("device", "333"); |
| 135 | ingress.add(ingressPoint); |
| 136 | ObjectNode ingressPoint2 = nodeFactory.objectNode(); |
| 137 | ingressPoint2.put("port", "1"); |
| 138 | ingressPoint2.put("device", "111"); |
| 139 | ingress.add(ingressPoint2); |
| 140 | json.set("ingressPoint", ingress); |
| 141 | ObjectNode egressPoint = nodeFactory.objectNode(); |
| 142 | egressPoint.put("port", "2"); |
| 143 | egressPoint.put("device", "222"); |
| 144 | json.set("egressPoint", egressPoint); |
| 145 | assertThat(json, notNullValue()); |
| 146 | |
| 147 | JsonCodec<MultiPointToSinglePointIntent> intentCodec = context.codec(MultiPointToSinglePointIntent.class); |
| 148 | assertThat(intentCodec, notNullValue()); |
| 149 | |
| 150 | final MultiPointToSinglePointIntent intent = intentCodec.decode(json, context); |
| 151 | |
| 152 | assertThat(intent.toString(), notNullValue()); |
| 153 | assertThat(intent, instanceOf(MultiPointToSinglePointIntent.class)); |
| 154 | assertThat(intent.priority(), is(100)); |
| 155 | assertThat(intent.ingressPoints().toString(), is("[333/3, 111/1]")); |
| 156 | assertThat(intent.egressPoint().toString(), is("222/2")); |
| 157 | |
| 158 | } |
| 159 | |
| 160 | } |