Parvathi M | ef74963 | 2016-07-07 13:30:43 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-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 | |
Parvathi M | ef74963 | 2016-07-07 13:30:43 -0700 | [diff] [blame] | 17 | package org.onosproject.patchpanel.impl; |
| 18 | |
Jonathan Hart | 194e764 | 2016-12-15 15:33:34 -0800 | [diff] [blame^] | 19 | import com.google.common.collect.ImmutableList; |
| 20 | import com.google.common.collect.ImmutableSet; |
Parvathi M | ef74963 | 2016-07-07 13:30:43 -0700 | [diff] [blame] | 21 | import org.apache.felix.scr.annotations.Activate; |
Jonathan Hart | 194e764 | 2016-12-15 15:33:34 -0800 | [diff] [blame^] | 22 | import org.apache.felix.scr.annotations.Component; |
Parvathi M | ef74963 | 2016-07-07 13:30:43 -0700 | [diff] [blame] | 23 | import org.apache.felix.scr.annotations.Deactivate; |
Jonathan Hart | 194e764 | 2016-12-15 15:33:34 -0800 | [diff] [blame^] | 24 | import org.apache.felix.scr.annotations.Reference; |
| 25 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 26 | import org.apache.felix.scr.annotations.Service; |
Jonathan Hart | 382119e | 2016-09-02 13:14:49 -0700 | [diff] [blame] | 27 | import org.onosproject.cli.net.ConnectPointCompleter; |
Parvathi M | ef74963 | 2016-07-07 13:30:43 -0700 | [diff] [blame] | 28 | import org.onosproject.core.ApplicationId; |
| 29 | import org.onosproject.core.CoreService; |
Jonathan Hart | 194e764 | 2016-12-15 15:33:34 -0800 | [diff] [blame^] | 30 | import org.onosproject.net.ConnectPoint; |
| 31 | import org.onosproject.net.DeviceId; |
Parvathi M | ef74963 | 2016-07-07 13:30:43 -0700 | [diff] [blame] | 32 | import org.onosproject.net.PortNumber; |
| 33 | import org.onosproject.net.flow.DefaultFlowRule; |
Parvathi M | ef74963 | 2016-07-07 13:30:43 -0700 | [diff] [blame] | 34 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 35 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
Jonathan Hart | 194e764 | 2016-12-15 15:33:34 -0800 | [diff] [blame^] | 36 | import org.onosproject.net.flow.FlowRule; |
| 37 | import org.onosproject.net.flow.FlowRuleService; |
Parvathi M | ef74963 | 2016-07-07 13:30:43 -0700 | [diff] [blame] | 38 | import org.onosproject.net.packet.PacketPriority; |
Parvathi M | ef74963 | 2016-07-07 13:30:43 -0700 | [diff] [blame] | 39 | import org.slf4j.Logger; |
| 40 | import org.slf4j.LoggerFactory; |
Jonathan Hart | 194e764 | 2016-12-15 15:33:34 -0800 | [diff] [blame^] | 41 | |
| 42 | import java.util.Collection; |
| 43 | import java.util.HashMap; |
| 44 | import java.util.Map; |
| 45 | import java.util.Set; |
| 46 | import java.util.concurrent.atomic.AtomicInteger; |
| 47 | |
| 48 | import static com.google.common.base.Preconditions.checkArgument; |
| 49 | import static com.google.common.base.Preconditions.checkNotNull; |
Parvathi M | ef74963 | 2016-07-07 13:30:43 -0700 | [diff] [blame] | 50 | |
Jonathan Hart | 382119e | 2016-09-02 13:14:49 -0700 | [diff] [blame] | 51 | /** |
| 52 | * This class acts as a software patch panel application. |
| 53 | * The user specifies 2 connectpoint on the same device that he/she would like to patch. |
| 54 | * Using a flow rule, the 2 connectpoints are patched. |
| 55 | */ |
Parvathi M | ef74963 | 2016-07-07 13:30:43 -0700 | [diff] [blame] | 56 | @Component(immediate = true) |
| 57 | @Service |
| 58 | public class PatchPanel implements PatchPanelService { |
| 59 | |
Jonathan Hart | 194e764 | 2016-12-15 15:33:34 -0800 | [diff] [blame^] | 60 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 61 | |
Jonathan Hart | 382119e | 2016-09-02 13:14:49 -0700 | [diff] [blame] | 62 | // OSGI: help bundle plugin discover runtime package dependency. |
| 63 | @SuppressWarnings("unused") |
| 64 | private ConnectPointCompleter connectPointCompleter; |
| 65 | |
Parvathi M | ef74963 | 2016-07-07 13:30:43 -0700 | [diff] [blame] | 66 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 67 | protected FlowRuleService flowRuleService; |
| 68 | |
| 69 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 70 | protected CoreService coreService; |
| 71 | |
Jonathan Hart | 194e764 | 2016-12-15 15:33:34 -0800 | [diff] [blame^] | 72 | private Map<PatchId, Patch> patches; |
| 73 | |
Parvathi M | ef74963 | 2016-07-07 13:30:43 -0700 | [diff] [blame] | 74 | private ApplicationId appId; |
Jonathan Hart | 194e764 | 2016-12-15 15:33:34 -0800 | [diff] [blame^] | 75 | |
| 76 | private AtomicInteger ids = new AtomicInteger(); |
Parvathi M | ef74963 | 2016-07-07 13:30:43 -0700 | [diff] [blame] | 77 | |
| 78 | |
| 79 | @Activate |
| 80 | protected void activate() throws NullPointerException { |
Jonathan Hart | 194e764 | 2016-12-15 15:33:34 -0800 | [diff] [blame^] | 81 | patches = new HashMap<>(); |
| 82 | |
Jonathan Hart | 382119e | 2016-09-02 13:14:49 -0700 | [diff] [blame] | 83 | appId = coreService.registerApplication("org.onosproject.patchpanel"); |
Parvathi M | ef74963 | 2016-07-07 13:30:43 -0700 | [diff] [blame] | 84 | log.info("Started"); |
Parvathi M | ef74963 | 2016-07-07 13:30:43 -0700 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | @Deactivate |
| 88 | protected void deactivate() { |
| 89 | log.info("Stopped"); |
| 90 | } |
| 91 | |
| 92 | @Override |
Jonathan Hart | 194e764 | 2016-12-15 15:33:34 -0800 | [diff] [blame^] | 93 | public boolean addPatch(ConnectPoint cp1, ConnectPoint cp2) { |
| 94 | checkNotNull(cp1); |
| 95 | checkNotNull(cp2); |
| 96 | |
| 97 | checkArgument(cp1.deviceId().equals(cp2.deviceId()), "Ports must be on the same device"); |
| 98 | checkArgument(!cp1.equals(cp2), "Ports cannot be the same"); |
| 99 | |
| 100 | if (patches.values().stream() |
| 101 | .filter(patch -> patch.port1().equals(cp1) || patch.port1().equals(cp2) |
| 102 | || patch.port2().equals(cp1) || patch.port2().equals(cp2)) |
| 103 | .findAny().isPresent()) { |
Parvathi M | ef74963 | 2016-07-07 13:30:43 -0700 | [diff] [blame] | 104 | log.info("One or both of these ports are already in use, NO FLOW"); |
| 105 | return false; |
Parvathi M | ef74963 | 2016-07-07 13:30:43 -0700 | [diff] [blame] | 106 | } |
Jonathan Hart | 194e764 | 2016-12-15 15:33:34 -0800 | [diff] [blame^] | 107 | |
| 108 | Patch patch = new Patch(PatchId.patchId(ids.incrementAndGet()), cp1, cp2); |
| 109 | |
| 110 | patches.put(patch.id(), patch); |
| 111 | |
| 112 | setFlowRuleService(patch); |
| 113 | |
| 114 | return true; |
Parvathi M | ef74963 | 2016-07-07 13:30:43 -0700 | [diff] [blame] | 115 | } |
| 116 | |
Jonathan Hart | 194e764 | 2016-12-15 15:33:34 -0800 | [diff] [blame^] | 117 | @Override |
| 118 | public boolean removePatch(PatchId id) { |
| 119 | Patch patch = patches.remove(id); |
| 120 | if (patch == null) { |
| 121 | return false; |
| 122 | } |
| 123 | |
| 124 | removePatchFlows(patch); |
| 125 | return true; |
| 126 | } |
| 127 | |
| 128 | @Override |
| 129 | public Set<Patch> getPatches() { |
| 130 | return ImmutableSet.copyOf(patches.values()); |
| 131 | } |
| 132 | |
| 133 | public void setFlowRuleService(Patch patch) { |
| 134 | createFlowRules(patch).forEach(flowRuleService::applyFlowRules); |
| 135 | } |
| 136 | |
| 137 | private void removePatchFlows(Patch patch) { |
| 138 | createFlowRules(patch).forEach(flowRuleService::removeFlowRules); |
| 139 | } |
| 140 | |
| 141 | private Collection<FlowRule> createFlowRules(Patch patch) { |
| 142 | DeviceId deviceId = patch.port1().deviceId(); |
| 143 | PortNumber outPort = patch.port2().port(); |
| 144 | PortNumber inPort = patch.port1().port(); |
Parvathi M | ef74963 | 2016-07-07 13:30:43 -0700 | [diff] [blame] | 145 | FlowRule fr = DefaultFlowRule.builder() |
Jonathan Hart | 194e764 | 2016-12-15 15:33:34 -0800 | [diff] [blame^] | 146 | .forDevice(deviceId) |
Parvathi M | ef74963 | 2016-07-07 13:30:43 -0700 | [diff] [blame] | 147 | .withSelector(DefaultTrafficSelector.builder().matchInPort(inPort).build()) |
| 148 | .withTreatment(DefaultTrafficTreatment.builder().setOutput(outPort).build()) |
| 149 | .withPriority(PacketPriority.REACTIVE.priorityValue()) |
Jonathan Hart | 382119e | 2016-09-02 13:14:49 -0700 | [diff] [blame] | 150 | .makePermanent() |
Parvathi M | ef74963 | 2016-07-07 13:30:43 -0700 | [diff] [blame] | 151 | .fromApp(appId).build(); |
| 152 | |
Jonathan Hart | 382119e | 2016-09-02 13:14:49 -0700 | [diff] [blame] | 153 | FlowRule fr2 = DefaultFlowRule.builder() |
Jonathan Hart | 194e764 | 2016-12-15 15:33:34 -0800 | [diff] [blame^] | 154 | .forDevice(deviceId) |
Jonathan Hart | 382119e | 2016-09-02 13:14:49 -0700 | [diff] [blame] | 155 | .withSelector(DefaultTrafficSelector.builder().matchInPort(outPort).build()) |
| 156 | .withTreatment(DefaultTrafficTreatment.builder().setOutput(inPort).build()) |
| 157 | .withPriority(PacketPriority.REACTIVE.priorityValue()) |
| 158 | .makePermanent() |
| 159 | .fromApp(appId).build(); |
| 160 | |
Jonathan Hart | 194e764 | 2016-12-15 15:33:34 -0800 | [diff] [blame^] | 161 | return ImmutableList.of(fr, fr2); |
Parvathi M | ef74963 | 2016-07-07 13:30:43 -0700 | [diff] [blame] | 162 | } |
Parvathi M | ef74963 | 2016-07-07 13:30:43 -0700 | [diff] [blame] | 163 | } |