blob: c0fdff4080452d5a51bb3837e170b34c6f69eed2 [file] [log] [blame]
Andrea Campanella2bdf2042019-01-28 13:47:11 +01001/*
2 * Copyright 2019-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 * This work was partially supported by EC H2020 project METRO-HAUL (761727).
17 */
18package org.onosproject.drivers.odtn.tapi;
19
20import com.fasterxml.jackson.databind.JsonNode;
21import com.fasterxml.jackson.databind.ObjectMapper;
22import com.google.common.collect.ImmutableSet;
23import org.junit.Before;
24import org.junit.Test;
25import org.onosproject.core.ApplicationId;
26import org.onosproject.core.DefaultApplicationId;
27import org.onosproject.net.AnnotationKeys;
28import org.onosproject.net.ChannelSpacing;
29import org.onosproject.net.DefaultAnnotations;
30import org.onosproject.net.DefaultDevice;
31import org.onosproject.net.DefaultPort;
32import org.onosproject.net.Device;
33import org.onosproject.net.DeviceId;
34import org.onosproject.net.GridType;
35import org.onosproject.net.Lambda;
36import org.onosproject.net.OchSignal;
37import org.onosproject.net.OchSignalType;
38import org.onosproject.net.Port;
39import org.onosproject.net.PortNumber;
40import org.onosproject.net.device.DeviceService;
41import org.onosproject.net.device.DeviceServiceAdapter;
42import org.onosproject.net.driver.Behaviour;
43import org.onosproject.net.driver.Driver;
44import org.onosproject.net.driver.DriverData;
45import org.onosproject.net.driver.DriverHandler;
46import org.onosproject.net.flow.DefaultFlowRule;
47import org.onosproject.net.flow.DefaultTrafficSelector;
48import org.onosproject.net.flow.DefaultTrafficTreatment;
49import org.onosproject.net.flow.FlowRule;
50import org.onosproject.net.flow.TrafficSelector;
51import org.onosproject.net.flow.TrafficTreatment;
52import org.onosproject.net.flow.criteria.Criteria;
53import org.onosproject.net.flow.instructions.Instructions;
54import org.onosproject.net.provider.ProviderId;
55
56import java.io.IOException;
57import java.util.Set;
58
59import static junit.framework.TestCase.assertEquals;
60
61/**
62 * Tests for the TAPI Flow Rule Programmable.
63 */
64public class TapiFlowRuleProgrammableTest {
65
66 private static final DeviceId DEVICE_ID = DeviceId.deviceId("rest:127.0.0.1:8080");
67 private static final ApplicationId APP_ID = new DefaultApplicationId(1, "test_app_id");
68
69 private static final ProviderId PID = new ProviderId("rest", "foo");
70 private static final DefaultAnnotations DEVICE_ANNOTATIONS =
71 DefaultAnnotations.builder().set(AnnotationKeys.DRIVER, "foo").build();
72 private static final Device DEV =
73 new DefaultDevice(PID, DEVICE_ID, Device.Type.OLS, "", "", "", "", null, DEVICE_ANNOTATIONS);
74
75 private static final DefaultAnnotations PORT_ANNOTATIONS = DefaultAnnotations.builder()
76 .set(TapiDeviceHelper.UUID, "76be95de-5769-4e5d-b65e-62cb6c39cf6b").build();
77
78 private static final DefaultAnnotations PORT_ANNOTATIONS_2 = DefaultAnnotations.builder()
79 .set(TapiDeviceHelper.UUID, "0923962e-b83f-4702-9b16-a1a0db0dc1f9").build();
80
81 private static final PortNumber ONE = PortNumber.portNumber(1);
82 private static final PortNumber TWO = PortNumber.portNumber(2);
83
84 private static final Port PORT_IN = new DefaultPort(DEV, ONE, true, PORT_ANNOTATIONS);
85 private static final Port PORT_OUT = new DefaultPort(DEV, TWO, true, PORT_ANNOTATIONS_2);
86
87
88 private static final Lambda LAMBDA = new OchSignal(GridType.DWDM, ChannelSpacing.CHL_0GHZ, 1, 1);
89
90 private static final TrafficSelector SELECTOR = DefaultTrafficSelector.builder()
91 .matchInPort(ONE)
92 .add(Criteria.matchLambda(LAMBDA))
93 .add(Criteria.matchOchSignalType(OchSignalType.FIXED_GRID))
94 .build();
95
96 private static final TrafficTreatment TREATMENT = DefaultTrafficTreatment.builder()
97 .add(Instructions.modL0Lambda(LAMBDA))
98 .setOutput(TWO)
99 .build();
100
101 private static final FlowRule FLOW_RULE = DefaultFlowRule.builder()
102 .forDevice(DEVICE_ID)
103 .withSelector(SELECTOR)
104 .withTreatment(TREATMENT)
105 .withPriority(1)
106 .fromApp(APP_ID)
107 .makePermanent()
108 .build();
109
110 private static final String CONNECTION_UUID = "30c3e74c-0e3c-40b3-9e26-221c56e995c2";
111 private static final String END_POINT_1_UUID = "76be95de-5769-4e5d-b65e-62cb6c39cf6b";
112 private static final String END_POINT_2_UUID = "0923962e-b83f-4702-9b16-a1a0db0dc1f9";
113
114 private static final String CONNECTIVITY_REQUEST = "{\"tapi-connectivity:connectivity-service\":" +
115 "[{\"uuid\":\"" + CONNECTION_UUID + "\",\"service-layer\":\"PHOTONIC_MEDIA\",\"service-type\"" +
Andrea Campanellae4b2c682019-08-23 15:42:55 +0200116 ":\"POINT_TO_POINT_CONNECTIVITY\",\"end-point\":[{\"local-id\":\"" + 1 + "\"," +
Andrea Campanella2bdf2042019-01-28 13:47:11 +0100117 "\"layer-protocol-name\":" + "\"PHOTONIC_MEDIA\",\"layer-protocol-qualifier\":" +
118 "\"tapi-photonic-media:PHOTONIC_LAYER_QUALIFIER_NMC\"," + "\"service-interface-point\":" +
119 "{\"service-interface-point-uuid\":\"" + END_POINT_1_UUID + "\"}}," +
Andrea Campanellae4b2c682019-08-23 15:42:55 +0200120 "{\"local-id\":\"" + 2 + "\",\"layer-protocol-name\":\"PHOTONIC_MEDIA\"," +
Andrea Campanella2bdf2042019-01-28 13:47:11 +0100121 "\"layer-protocol-qualifier\":" + "\"tapi-photonic-media:PHOTONIC_LAYER_QUALIFIER_NMC\"," +
122 "\"service-interface-point\":{" + "\"service-interface-point-uuid\":\"" + END_POINT_2_UUID + "\"}}]}]}";
123
Andrea Campanellae4b2c682019-08-23 15:42:55 +0200124 private static final String GET_CONNECTIVITY_REQUEST_REPLY = "{\"tapi-connectivity:connectivity-context\": " +
125 "{\"connectivity-service\" : [{\"uuid\" : \"" + CONNECTION_UUID + "\"}]}}}";
Andrea Campanella2bdf2042019-01-28 13:47:11 +0100126
127 private static final Set<String> CONNECTION_UUIDS = ImmutableSet.of(CONNECTION_UUID);
128
129 private TapiFlowRuleProgrammable tapiFrp;
130
131 @Before
132 public void setUp() throws Exception {
133 tapiFrp = new TapiFlowRuleProgrammable();
134 DriverHandler mockHandler = new InternalDriverHandler();
135 tapiFrp.setHandler(mockHandler);
136 }
137
138 @Test
139 public void createConnRequest() {
140 String output = tapiFrp.createConnectivityRequest(CONNECTION_UUID, FLOW_RULE).toString();
141 System.out.println(output);
142 assertEquals("Json to create network connectivity is wrong", CONNECTIVITY_REQUEST, output);
143 }
144
145 @Test
146 public void parseConnReply() throws IOException {
Andrea Campanella2bdf2042019-01-28 13:47:11 +0100147 ObjectMapper mapper = new ObjectMapper();
148 JsonNode jsonReply = mapper.readTree(GET_CONNECTIVITY_REQUEST_REPLY);
Andrea Campanellae4b2c682019-08-23 15:42:55 +0200149 Set<String> output = TapiDeviceHelper.parseTapiGetConnectivityRequest(jsonReply);
Andrea Campanella2bdf2042019-01-28 13:47:11 +0100150 assertEquals("Wrong Tapi UUIDS", CONNECTION_UUIDS, output);
151 }
152
153 private class InternalDriverHandler implements DriverHandler {
154
155 @Override
156 public Driver driver() {
157 return null;
158 }
159
160 @Override
161 public DriverData data() {
162 return null;
163 }
164
165 @Override
166 public <T extends Behaviour> T behaviour(Class<T> behaviourClass) {
167 return null;
168 }
169
170 @Override
171 public boolean hasBehaviour(Class<? extends Behaviour> behaviourClass) {
172 return false;
173 }
174
175 @Override
176 public <T> T get(Class<T> serviceClass) {
177 if (serviceClass.equals(DeviceService.class)) {
178 return (T) new InternalDeviceService();
179 }
180 return null;
181 }
182 }
183
184 private class InternalDeviceService extends DeviceServiceAdapter {
185 @Override
186 public Port getPort(DeviceId deviceId, PortNumber portNumber) {
187 if (portNumber.equals(ONE)) {
188 return PORT_IN;
189 } else {
190 return PORT_OUT;
191 }
192 }
193 }
194}