Ray Milkey | db35808 | 2015-01-13 16:34:38 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Open Networking Laboratory |
| 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 | |
Ray Milkey | b9a8a18 | 2015-01-20 14:15:35 -0800 | [diff] [blame] | 18 | import java.time.Duration; |
Ray Milkey | db35808 | 2015-01-13 16:34:38 -0800 | [diff] [blame] | 19 | import java.util.List; |
| 20 | |
| 21 | import org.junit.Test; |
| 22 | import org.onlab.packet.IpPrefix; |
| 23 | import org.onlab.packet.MacAddress; |
| 24 | import org.onosproject.codec.CodecContext; |
| 25 | import org.onosproject.codec.JsonCodec; |
| 26 | import org.onosproject.core.ApplicationId; |
| 27 | import org.onosproject.core.DefaultApplicationId; |
| 28 | import org.onosproject.net.ConnectPoint; |
Ray Milkey | b9a8a18 | 2015-01-20 14:15:35 -0800 | [diff] [blame] | 29 | import org.onosproject.net.DeviceId; |
Ray Milkey | db35808 | 2015-01-13 16:34:38 -0800 | [diff] [blame] | 30 | import org.onosproject.net.HostId; |
| 31 | import org.onosproject.net.NetTestTools; |
| 32 | import org.onosproject.net.PortNumber; |
| 33 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 34 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 35 | import org.onosproject.net.flow.TrafficSelector; |
| 36 | import org.onosproject.net.flow.TrafficTreatment; |
| 37 | import org.onosproject.net.intent.Constraint; |
| 38 | import org.onosproject.net.intent.HostToHostIntent; |
| 39 | import org.onosproject.net.intent.AbstractIntentTest; |
| 40 | import org.onosproject.net.intent.PointToPointIntent; |
Ray Milkey | b9a8a18 | 2015-01-20 14:15:35 -0800 | [diff] [blame] | 41 | import org.onosproject.net.intent.constraint.AnnotationConstraint; |
| 42 | import org.onosproject.net.intent.constraint.AsymmetricPathConstraint; |
Ray Milkey | db35808 | 2015-01-13 16:34:38 -0800 | [diff] [blame] | 43 | import org.onosproject.net.intent.constraint.BandwidthConstraint; |
| 44 | import org.onosproject.net.intent.constraint.LambdaConstraint; |
Ray Milkey | b9a8a18 | 2015-01-20 14:15:35 -0800 | [diff] [blame] | 45 | import org.onosproject.net.intent.constraint.LatencyConstraint; |
| 46 | import org.onosproject.net.intent.constraint.ObstacleConstraint; |
| 47 | import org.onosproject.net.intent.constraint.WaypointConstraint; |
Ray Milkey | db35808 | 2015-01-13 16:34:38 -0800 | [diff] [blame] | 48 | import org.onosproject.net.resource.Bandwidth; |
| 49 | import org.onosproject.net.resource.Lambda; |
| 50 | |
| 51 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 52 | import com.google.common.collect.ImmutableList; |
| 53 | |
| 54 | import static org.onosproject.codec.impl.IntentJsonMatcher.matchesIntent; |
Ray Milkey | b9a8a18 | 2015-01-20 14:15:35 -0800 | [diff] [blame] | 55 | import static org.onosproject.net.NetTestTools.did; |
Ray Milkey | db35808 | 2015-01-13 16:34:38 -0800 | [diff] [blame] | 56 | import static org.onosproject.net.NetTestTools.hid; |
| 57 | |
| 58 | |
| 59 | import static org.hamcrest.MatcherAssert.assertThat; |
| 60 | import static org.hamcrest.Matchers.notNullValue; |
| 61 | |
| 62 | /** |
| 63 | * Unit tests for the host to host intent class codec. |
| 64 | */ |
| 65 | public class IntentCodecTest extends AbstractIntentTest { |
| 66 | |
| 67 | private final HostId id1 = hid("12:34:56:78:91:ab/1"); |
| 68 | private final HostId id2 = hid("12:34:56:78:92:ab/1"); |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 69 | private final ApplicationId appId = new DefaultApplicationId(3, "test"); |
Ray Milkey | db35808 | 2015-01-13 16:34:38 -0800 | [diff] [blame] | 70 | final TrafficSelector emptySelector = |
| 71 | DefaultTrafficSelector.builder().build(); |
| 72 | final TrafficTreatment emptyTreatment = |
| 73 | DefaultTrafficTreatment.builder().build(); |
| 74 | private final CodecContext context = new MockCodecContext(); |
| 75 | |
| 76 | /** |
| 77 | * Tests the encoding of a host to host intent. |
| 78 | */ |
| 79 | @Test |
| 80 | public void hostToHostIntent() { |
| 81 | final HostToHostIntent intent = |
| 82 | new HostToHostIntent(appId, id1, id2); |
| 83 | |
| 84 | final JsonCodec<HostToHostIntent> intentCodec = |
| 85 | context.codec(HostToHostIntent.class); |
| 86 | assertThat(intentCodec, notNullValue()); |
| 87 | |
| 88 | final ObjectNode intentJson = intentCodec.encode(intent, context); |
| 89 | assertThat(intentJson, matchesIntent(intent)); |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Tests the encoding of a point to point intent. |
| 94 | */ |
| 95 | @Test |
| 96 | public void pointToPointIntent() { |
| 97 | ConnectPoint ingress = NetTestTools.connectPoint("ingress", 1); |
| 98 | ConnectPoint egress = NetTestTools.connectPoint("egress", 2); |
| 99 | |
| 100 | final PointToPointIntent intent = |
| 101 | new PointToPointIntent(appId, emptySelector, |
| 102 | emptyTreatment, ingress, egress); |
| 103 | |
| 104 | final CodecContext context = new MockCodecContext(); |
| 105 | final JsonCodec<PointToPointIntent> intentCodec = |
| 106 | context.codec(PointToPointIntent.class); |
| 107 | assertThat(intentCodec, notNullValue()); |
| 108 | |
| 109 | final ObjectNode intentJson = intentCodec.encode(intent, context); |
| 110 | assertThat(intentJson, matchesIntent(intent)); |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Tests the encoding of an intent with treatment, selector and constraints |
| 115 | * specified. |
| 116 | */ |
| 117 | @Test |
| 118 | public void intentWithTreatmentSelectorAndConstraints() { |
| 119 | ConnectPoint ingress = NetTestTools.connectPoint("ingress", 1); |
| 120 | ConnectPoint egress = NetTestTools.connectPoint("egress", 2); |
Ray Milkey | b9a8a18 | 2015-01-20 14:15:35 -0800 | [diff] [blame] | 121 | DeviceId did1 = did("device1"); |
| 122 | DeviceId did2 = did("device2"); |
| 123 | DeviceId did3 = did("device3"); |
Ray Milkey | db35808 | 2015-01-13 16:34:38 -0800 | [diff] [blame] | 124 | final TrafficSelector selector = DefaultTrafficSelector.builder() |
| 125 | .matchIPProtocol((byte) 3) |
| 126 | .matchMplsLabel(4) |
| 127 | .matchOpticalSignalType((short) 5) |
| 128 | .matchLambda((short) 6) |
| 129 | .matchEthDst(MacAddress.BROADCAST) |
| 130 | .matchIPDst(IpPrefix.valueOf("1.2.3.4/24")) |
| 131 | .build(); |
| 132 | final TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
| 133 | .setLambda((short) 33) |
| 134 | .setMpls(44) |
| 135 | .setOutput(PortNumber.CONTROLLER) |
| 136 | .setEthDst(MacAddress.BROADCAST) |
| 137 | .build(); |
Ray Milkey | b9a8a18 | 2015-01-20 14:15:35 -0800 | [diff] [blame] | 138 | |
| 139 | final List<Constraint> constraints = |
| 140 | ImmutableList.of( |
Sho SHIMIZU | 0ce220a | 2015-01-23 15:54:47 -0800 | [diff] [blame] | 141 | new BandwidthConstraint(Bandwidth.bps(1.0)), |
Ray Milkey | b9a8a18 | 2015-01-20 14:15:35 -0800 | [diff] [blame] | 142 | new LambdaConstraint(Lambda.valueOf(3)), |
| 143 | new AnnotationConstraint("key", 33.0), |
| 144 | new AsymmetricPathConstraint(), |
| 145 | new LatencyConstraint(Duration.ofSeconds(2)), |
| 146 | new ObstacleConstraint(did1, did2), |
| 147 | new WaypointConstraint(did3)); |
Ray Milkey | db35808 | 2015-01-13 16:34:38 -0800 | [diff] [blame] | 148 | |
| 149 | final PointToPointIntent intent = |
| 150 | new PointToPointIntent(appId, selector, treatment, |
| 151 | ingress, egress, constraints); |
| 152 | |
| 153 | final CodecContext context = new MockCodecContext(); |
| 154 | final JsonCodec<PointToPointIntent> intentCodec = |
| 155 | context.codec(PointToPointIntent.class); |
| 156 | assertThat(intentCodec, notNullValue()); |
| 157 | |
| 158 | final ObjectNode intentJson = intentCodec.encode(intent, context); |
| 159 | assertThat(intentJson, matchesIntent(intent)); |
| 160 | |
| 161 | } |
| 162 | } |