blob: 15c79d6474d4e832a4891862f24e09096ad37e9f [file] [log] [blame]
Aris C. Risdiantoa579c8f2017-04-13 19:38:46 +09001/*
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
17package org.onosproject.codec.impl;
18
19import com.fasterxml.jackson.databind.node.ArrayNode;
20import com.fasterxml.jackson.databind.node.JsonNodeFactory;
21import com.fasterxml.jackson.databind.node.ObjectNode;
22import org.junit.Before;
23import org.junit.Test;
24import org.onosproject.TestApplicationId;
25import org.onosproject.codec.JsonCodec;
26import org.onosproject.core.ApplicationId;
27import org.onosproject.core.CoreService;
28import org.onosproject.net.ConnectPoint;
29import org.onosproject.net.DeviceId;
30import org.onosproject.net.PortNumber;
31import org.onosproject.net.flow.DefaultTrafficSelector;
32import org.onosproject.net.flow.DefaultTrafficTreatment;
33import org.onosproject.net.flow.TrafficSelector;
34import org.onosproject.net.flow.TrafficTreatment;
35import org.onosproject.net.intent.AbstractIntentTest;
36import org.onosproject.net.intent.IntentService;
37import org.onosproject.net.intent.IntentServiceAdapter;
38import org.onosproject.net.intent.Key;
39import org.onosproject.net.intent.MultiPointToSinglePointIntent;
40
41import java.io.IOException;
42import java.util.Arrays;
43import java.util.HashSet;
44import java.util.Set;
45
46import static org.easymock.EasyMock.*;
47import static org.hamcrest.MatcherAssert.assertThat;
48import static org.hamcrest.Matchers.*;
Ray Milkey27cff8c2018-03-05 14:58:26 -080049import static org.onosproject.net.intent.ConnectivityIntentTest.FP2;
50import static org.onosproject.net.intent.ConnectivityIntentTest.FPS1;
Aris C. Risdiantoa579c8f2017-04-13 19:38:46 +090051
52/**
53 * Suite of tests of the multi-to-single point intent's codec for rest api.
54 */
55
56public 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 Milkey27cff8c2018-03-05 14:58:26 -080096 .filteredIngressPoints(FPS1)
97 .filteredEgressPoint(FP2)
Aris C. Risdiantoa579c8f2017-04-13 19:38:46 +090098 .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 Milkey27cff8c2018-03-05 14:58:26 -0800113
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. Risdiantoa579c8f2017-04-13 19:38:46 +0900127 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}