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 | |
| 19 | import org.apache.felix.scr.annotations.Component; |
| 20 | import org.apache.felix.scr.annotations.Reference; |
| 21 | import org.apache.felix.scr.annotations.Service; |
| 22 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 23 | import org.apache.felix.scr.annotations.Activate; |
| 24 | import org.apache.felix.scr.annotations.Deactivate; |
Jonathan Hart | 382119e | 2016-09-02 13:14:49 -0700 | [diff] [blame] | 25 | import org.onosproject.cli.net.ConnectPointCompleter; |
Parvathi M | ef74963 | 2016-07-07 13:30:43 -0700 | [diff] [blame] | 26 | import org.onosproject.core.ApplicationId; |
| 27 | import org.onosproject.core.CoreService; |
| 28 | import org.onosproject.net.PortNumber; |
| 29 | import org.onosproject.net.flow.DefaultFlowRule; |
| 30 | import org.onosproject.net.flow.FlowRuleService; |
| 31 | import org.onosproject.net.flow.FlowRule; |
| 32 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 33 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 34 | import org.onosproject.net.packet.PacketPriority; |
| 35 | import org.onosproject.net.ConnectPoint; |
| 36 | import org.slf4j.Logger; |
| 37 | import org.slf4j.LoggerFactory; |
| 38 | import java.util.ArrayList; |
| 39 | import java.util.List; |
| 40 | |
Jonathan Hart | 382119e | 2016-09-02 13:14:49 -0700 | [diff] [blame] | 41 | /** |
| 42 | * This class acts as a software patch panel application. |
| 43 | * The user specifies 2 connectpoint on the same device that he/she would like to patch. |
| 44 | * Using a flow rule, the 2 connectpoints are patched. |
| 45 | */ |
Parvathi M | ef74963 | 2016-07-07 13:30:43 -0700 | [diff] [blame] | 46 | @Component(immediate = true) |
| 47 | @Service |
| 48 | public class PatchPanel implements PatchPanelService { |
| 49 | |
Jonathan Hart | 382119e | 2016-09-02 13:14:49 -0700 | [diff] [blame] | 50 | // OSGI: help bundle plugin discover runtime package dependency. |
| 51 | @SuppressWarnings("unused") |
| 52 | private ConnectPointCompleter connectPointCompleter; |
| 53 | |
Parvathi M | ef74963 | 2016-07-07 13:30:43 -0700 | [diff] [blame] | 54 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 55 | protected FlowRuleService flowRuleService; |
| 56 | |
| 57 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 58 | protected CoreService coreService; |
| 59 | |
| 60 | private List<ConnectPoint> previous = new ArrayList<>(); |
| 61 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 62 | private ApplicationId appId; |
| 63 | private ConnectPoint cp1, cp2; |
| 64 | |
| 65 | |
| 66 | @Activate |
| 67 | protected void activate() throws NullPointerException { |
Jonathan Hart | 382119e | 2016-09-02 13:14:49 -0700 | [diff] [blame] | 68 | appId = coreService.registerApplication("org.onosproject.patchpanel"); |
Parvathi M | ef74963 | 2016-07-07 13:30:43 -0700 | [diff] [blame] | 69 | log.info("Started"); |
Parvathi M | ef74963 | 2016-07-07 13:30:43 -0700 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | @Deactivate |
| 73 | protected void deactivate() { |
| 74 | log.info("Stopped"); |
| 75 | } |
| 76 | |
| 77 | @Override |
| 78 | public boolean addPatch(ConnectPoint num, ConnectPoint num2) { |
| 79 | cp1 = num; |
| 80 | cp2 = num2; |
| 81 | if ((cp1.port().equals(cp2.port())) || (previous.contains(cp1) || previous.contains(cp2))) { |
| 82 | log.info("One or both of these ports are already in use, NO FLOW"); |
| 83 | return false; |
| 84 | } else { |
| 85 | previous.add(cp1); |
| 86 | previous.add(cp2); |
| 87 | setFlowRuleService(); |
| 88 | return true; |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | public void setFlowRuleService() { |
| 93 | PortNumber outPort = cp2.port(); |
| 94 | PortNumber inPort = cp1.port(); |
| 95 | FlowRule fr = DefaultFlowRule.builder() |
| 96 | .forDevice(cp1.deviceId()) |
| 97 | .withSelector(DefaultTrafficSelector.builder().matchInPort(inPort).build()) |
| 98 | .withTreatment(DefaultTrafficTreatment.builder().setOutput(outPort).build()) |
| 99 | .withPriority(PacketPriority.REACTIVE.priorityValue()) |
Jonathan Hart | 382119e | 2016-09-02 13:14:49 -0700 | [diff] [blame] | 100 | .makePermanent() |
Parvathi M | ef74963 | 2016-07-07 13:30:43 -0700 | [diff] [blame] | 101 | .fromApp(appId).build(); |
| 102 | |
Jonathan Hart | 382119e | 2016-09-02 13:14:49 -0700 | [diff] [blame] | 103 | FlowRule fr2 = DefaultFlowRule.builder() |
| 104 | .forDevice(cp1.deviceId()) |
| 105 | .withSelector(DefaultTrafficSelector.builder().matchInPort(outPort).build()) |
| 106 | .withTreatment(DefaultTrafficTreatment.builder().setOutput(inPort).build()) |
| 107 | .withPriority(PacketPriority.REACTIVE.priorityValue()) |
| 108 | .makePermanent() |
| 109 | .fromApp(appId).build(); |
| 110 | |
| 111 | flowRuleService.applyFlowRules(fr, fr2); |
Parvathi M | ef74963 | 2016-07-07 13:30:43 -0700 | [diff] [blame] | 112 | |
| 113 | } |
| 114 | |
| 115 | } |