Yuta HIGUCHI | 44f18e9 | 2017-03-02 22:05:41 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-present 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.driver.optical.config; |
| 17 | |
| 18 | import static org.hamcrest.Matchers.*; |
| 19 | import static org.junit.Assert.*; |
| 20 | import static org.onosproject.net.PortNumber.portNumber; |
| 21 | import static org.onosproject.net.flow.instructions.Instructions.modL0Lambda; |
| 22 | |
| 23 | import java.io.IOException; |
| 24 | import java.util.Set; |
| 25 | |
| 26 | import org.junit.After; |
| 27 | import org.junit.Before; |
| 28 | import org.junit.Test; |
| 29 | import org.onosproject.core.ApplicationId; |
| 30 | import org.onosproject.core.CoreService; |
| 31 | import org.onosproject.core.CoreServiceAdapter; |
| 32 | import org.onosproject.core.DefaultApplicationId; |
| 33 | import org.onosproject.net.DeviceId; |
| 34 | import org.onosproject.net.OchSignal; |
| 35 | import org.onosproject.net.PortNumber; |
| 36 | import org.onosproject.net.flow.DefaultFlowRule; |
| 37 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 38 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 39 | import org.onosproject.net.flow.FlowRule; |
| 40 | |
| 41 | import com.fasterxml.jackson.core.JsonProcessingException; |
| 42 | import com.fasterxml.jackson.databind.JsonNode; |
| 43 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 44 | import com.google.common.collect.ImmutableSet; |
| 45 | |
| 46 | public class FlowTableConfigTest extends BaseConfigTestHelper { |
| 47 | |
| 48 | private static final String SAMPLE = "flow_table_config.json"; |
| 49 | |
| 50 | |
| 51 | private static final DeviceId DID = DeviceId.deviceId("of:0000000000000001"); |
| 52 | private static final PortNumber PN_1 = portNumber(1); |
| 53 | private static final PortNumber PN_2 = portNumber(2); |
| 54 | private static final int FLOW_ID_3 = 3; |
| 55 | private static final int PRIO_4 = 4; |
| 56 | |
| 57 | private static final DefaultApplicationId APP_ID = |
| 58 | new DefaultApplicationId(FLOW_ID_3 >>> 48, "test"); |
| 59 | |
| 60 | private static final OchSignal LAMBDA_42 = OchSignal.newFlexGridSlot(42); |
| 61 | |
| 62 | private static final FlowRule FLOW_RULE = DefaultFlowRule.builder() |
| 63 | .forDevice(DID) |
| 64 | .withCookie(FLOW_ID_3) |
| 65 | .makePermanent() |
| 66 | .withPriority(PRIO_4) |
| 67 | .withSelector(DefaultTrafficSelector.builder() |
| 68 | .matchInPort(PN_1) |
| 69 | .build()) |
| 70 | .withTreatment(DefaultTrafficTreatment.builder() |
| 71 | .setOutput(PN_2) |
| 72 | .add(modL0Lambda(LAMBDA_42)) |
| 73 | .build()) |
| 74 | .build(); |
| 75 | |
| 76 | |
| 77 | private ObjectMapper mapper; |
| 78 | |
| 79 | |
| 80 | private JsonNode cfgnode; |
| 81 | |
| 82 | @Before |
| 83 | public void setUp() throws Exception { |
| 84 | |
| 85 | directory.add(CoreService.class, new CoreServiceAdapter() { |
| 86 | @Override |
| 87 | public ApplicationId getAppId(Short id) { |
| 88 | return APP_ID; |
| 89 | } |
| 90 | |
| 91 | @Override |
| 92 | public ApplicationId registerApplication(String name) { |
| 93 | return APP_ID; |
| 94 | } |
| 95 | }); |
| 96 | |
| 97 | mapper = testFriendlyMapper(); |
| 98 | JsonNode sample = loadJsonFromResource(SAMPLE, mapper); |
| 99 | |
| 100 | cfgnode = sample.path("devices") |
| 101 | .path(DID.toString()) |
| 102 | .path(FlowTableConfig.CONFIG_KEY); |
| 103 | |
| 104 | } |
| 105 | |
| 106 | @After |
| 107 | public void tearDown() throws Exception { |
| 108 | } |
| 109 | |
| 110 | @Test |
| 111 | public void readTest() throws JsonProcessingException, IOException { |
| 112 | |
| 113 | FlowTableConfig sut = new FlowTableConfig(); |
| 114 | sut.init(DID, FlowTableConfig.CONFIG_KEY, cfgnode, mapper, noopDelegate); |
| 115 | |
| 116 | assertThat(sut.flowtable(), is(equalTo(ImmutableSet.of(FLOW_RULE)))); |
| 117 | } |
| 118 | |
| 119 | @Test |
| 120 | public void writeTest() throws JsonProcessingException, IOException { |
| 121 | |
| 122 | FlowTableConfig w = new FlowTableConfig(); |
| 123 | w.init(DID, FlowTableConfig.CONFIG_KEY, mapper.createObjectNode(), mapper, noopDelegate); |
| 124 | |
| 125 | Set<FlowRule> table = ImmutableSet.of(FLOW_RULE); |
| 126 | w.flowtable(table); |
| 127 | |
| 128 | assertEquals(cfgnode, w.node()); |
| 129 | } |
| 130 | |
| 131 | } |