blob: fa3c921a0891fba1954b7290d5fb5fe64c3ad28a [file] [log] [blame]
Yuta HIGUCHI44f18e92017-03-02 22:05:41 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Yuta HIGUCHI44f18e92017-03-02 22:05:41 -08003 *
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 */
16package org.onosproject.driver.optical.config;
17
18import static org.slf4j.LoggerFactory.getLogger;
19
20import java.util.Collection;
21import java.util.LinkedHashSet;
22import java.util.Optional;
23import java.util.Set;
24import java.util.stream.Collectors;
25
26import org.onosproject.net.DeviceId;
27import org.onosproject.net.config.NetworkConfigService;
28import org.onosproject.net.driver.AbstractHandlerBehaviour;
29import org.onosproject.net.flow.DefaultFlowEntry;
30import org.onosproject.net.flow.FlowEntry;
31import org.onosproject.net.flow.FlowRule;
32import org.onosproject.net.flow.FlowRuleProgrammable;
33
34import com.google.common.annotations.Beta;
35import com.google.common.collect.ImmutableSet;
36
37import org.onosproject.net.flow.FlowEntry.FlowEntryState;
38import 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
48public 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}