Yuta HIGUCHI | 44f18e9 | 2017-03-02 22:05:41 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Yuta HIGUCHI | 44f18e9 | 2017-03-02 22:05:41 -0800 | [diff] [blame] | 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 java.util.Set; |
| 19 | |
| 20 | import org.onosproject.codec.JsonCodec; |
| 21 | import org.onosproject.net.DeviceId; |
| 22 | import org.onosproject.net.config.BaseConfig; |
| 23 | import org.onosproject.net.flow.FlowRule; |
| 24 | |
| 25 | import com.fasterxml.jackson.databind.JsonNode; |
| 26 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 27 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 28 | import com.google.common.collect.ImmutableSet; |
| 29 | import com.google.common.collect.ImmutableSet.Builder; |
| 30 | |
| 31 | /** |
| 32 | * Config to store FlowTable. |
| 33 | * |
| 34 | * Used by ConfigFlowRuleProgrammable |
| 35 | */ |
| 36 | public class FlowTableConfig extends BaseConfig<DeviceId> { |
| 37 | |
| 38 | /** |
| 39 | * Configuration key for {@link FlowTableConfig}. |
| 40 | */ |
| 41 | public static final String CONFIG_KEY = "flowtable"; |
| 42 | |
| 43 | public static final String ENTRIES = "entries"; |
| 44 | |
| 45 | |
| 46 | @Override |
| 47 | public boolean isValid() { |
| 48 | return hasField(ENTRIES); |
| 49 | } |
| 50 | |
| 51 | |
| 52 | public Set<FlowRule> flowtable() { |
| 53 | JsonNode ents = object.path(ENTRIES); |
| 54 | if (!ents.isArray()) { |
| 55 | |
| 56 | return ImmutableSet.of(); |
| 57 | } |
| 58 | ArrayNode entries = (ArrayNode) ents; |
| 59 | |
| 60 | Builder<FlowRule> builder = ImmutableSet.builder(); |
| 61 | entries.forEach(entry -> builder.add(decode(entry, FlowRule.class))); |
| 62 | return builder.build(); |
| 63 | } |
| 64 | |
| 65 | public FlowTableConfig flowtable(Set<FlowRule> table) { |
| 66 | JsonCodec<FlowRule> codec = codec(FlowRule.class); |
| 67 | ArrayNode entries = codec.encode(table, this); |
| 68 | object.set(ENTRIES, entries); |
| 69 | return this; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Create a {@link FlowTableConfig}. |
| 74 | * <p> |
| 75 | * Note: created instance needs to be initialized by #init(..) before using. |
| 76 | */ |
| 77 | public FlowTableConfig() { |
| 78 | super(); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Create a {@link FlowTableConfig} for specified Device. |
| 83 | * <p> |
| 84 | * Note: created instance is not bound to NetworkConfigService, |
| 85 | * cannot use {@link #apply()}. Must be passed to the service |
| 86 | * using NetworkConfigService#applyConfig |
| 87 | * |
| 88 | * @param did DeviceId |
| 89 | */ |
| 90 | public FlowTableConfig(DeviceId did) { |
| 91 | ObjectMapper mapper = new ObjectMapper(); |
| 92 | init(did, CONFIG_KEY, mapper.createObjectNode(), mapper, null); |
| 93 | } |
| 94 | } |