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 static org.slf4j.LoggerFactory.getLogger; |
| 19 | |
| 20 | import java.util.Collection; |
| 21 | import java.util.LinkedHashSet; |
| 22 | import java.util.Optional; |
| 23 | import java.util.Set; |
| 24 | import java.util.stream.Collectors; |
| 25 | |
| 26 | import org.onosproject.net.DeviceId; |
| 27 | import org.onosproject.net.config.NetworkConfigService; |
| 28 | import org.onosproject.net.driver.AbstractHandlerBehaviour; |
| 29 | import org.onosproject.net.flow.DefaultFlowEntry; |
| 30 | import org.onosproject.net.flow.FlowEntry; |
| 31 | import org.onosproject.net.flow.FlowRule; |
| 32 | import org.onosproject.net.flow.FlowRuleProgrammable; |
| 33 | |
| 34 | import com.google.common.annotations.Beta; |
| 35 | import com.google.common.collect.ImmutableSet; |
| 36 | |
| 37 | import org.onosproject.net.flow.FlowEntry.FlowEntryState; |
| 38 | import org.slf4j.Logger; |
| 39 | |
| 40 | // TODO consider relocating |
| 41 | /** |
| 42 | * {@link FlowRuleProgrammable} which pretends it accepted the requests. |
| 43 | * |
| 44 | * Can be useful, when you need to send totally different flow rules |
| 45 | * down to the Device. |
| 46 | */ |
| 47 | @Beta |
| 48 | public class ConfigFlowRuleProgrammable |
| 49 | extends AbstractHandlerBehaviour |
| 50 | implements FlowRuleProgrammable { |
| 51 | |
| 52 | private static final Logger log = getLogger(ConfigFlowRuleProgrammable.class); |
| 53 | |
| 54 | |
| 55 | @Override |
| 56 | public Collection<FlowEntry> getFlowEntries() { |
| 57 | Set<FlowRule> flowtable = getFlowTable(getFlowTableConfig()); |
| 58 | return flowtable.stream() |
| 59 | .map(fr -> new DefaultFlowEntry(fr, FlowEntryState.ADDED)) |
| 60 | .collect(Collectors.toList()); |
| 61 | } |
| 62 | |
| 63 | @Override |
| 64 | public Collection<FlowRule> applyFlowRules(Collection<FlowRule> rules) { |
| 65 | log.trace("applyFlowRules: {}", rules); |
| 66 | |
| 67 | Optional<FlowTableConfig> config = createFlowTableConfig(); |
| 68 | Set<FlowRule> table = new LinkedHashSet<>(getFlowTable(config)); |
| 69 | table.addAll(rules); |
| 70 | config.map(cfg -> cfg.flowtable(table)) |
| 71 | .ifPresent(cfg -> cfg.apply()); |
| 72 | log.trace("Updated flowtable: {}", table); |
| 73 | return table; |
| 74 | } |
| 75 | |
| 76 | @Override |
| 77 | public Collection<FlowRule> removeFlowRules(Collection<FlowRule> rules) { |
| 78 | log.trace("removeFlowRules: {}", rules); |
| 79 | Optional<FlowTableConfig> config = getFlowTableConfig(); |
| 80 | Set<FlowRule> table = new LinkedHashSet<>(getFlowTable(config)); |
| 81 | table.removeAll(rules); |
| 82 | config.map(cfg -> cfg.flowtable(table)) |
| 83 | .ifPresent(cfg -> cfg.apply()); |
| 84 | log.trace("Updated flowtable: {}", table); |
| 85 | return table; |
| 86 | } |
| 87 | |
| 88 | private Set<FlowRule> getFlowTable(Optional<FlowTableConfig> cfg) { |
| 89 | Set<FlowRule> flowtable = cfg |
| 90 | .map(FlowTableConfig::flowtable) |
| 91 | .orElse(ImmutableSet.of()); |
| 92 | return flowtable; |
| 93 | } |
| 94 | |
| 95 | private Optional<FlowTableConfig> createFlowTableConfig() { |
| 96 | NetworkConfigService netcfg = handler().get(NetworkConfigService.class); |
| 97 | DeviceId did = data().deviceId(); |
| 98 | return Optional.ofNullable(netcfg.addConfig(did, FlowTableConfig.class)); |
| 99 | } |
| 100 | |
| 101 | private Optional<FlowTableConfig> getFlowTableConfig() { |
| 102 | NetworkConfigService netcfg = handler().get(NetworkConfigService.class); |
| 103 | DeviceId did = data().deviceId(); |
| 104 | return Optional.ofNullable(netcfg.getConfig(did, FlowTableConfig.class)); |
| 105 | } |
| 106 | |
| 107 | } |