blob: b08be7e3cedb8401fc006d6b3f29cd8415358f2b [file] [log] [blame]
Parvathi Mef749632016-07-07 13:30:43 -07001/*
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 Mef749632016-07-07 13:30:43 -070017package org.onosproject.patchpanel.impl;
18
19import org.apache.felix.scr.annotations.Component;
20import org.apache.felix.scr.annotations.Reference;
21import org.apache.felix.scr.annotations.Service;
22import org.apache.felix.scr.annotations.ReferenceCardinality;
23import org.apache.felix.scr.annotations.Activate;
24import org.apache.felix.scr.annotations.Deactivate;
Jonathan Hart382119e2016-09-02 13:14:49 -070025import org.onosproject.cli.net.ConnectPointCompleter;
Parvathi Mef749632016-07-07 13:30:43 -070026import org.onosproject.core.ApplicationId;
27import org.onosproject.core.CoreService;
28import org.onosproject.net.PortNumber;
29import org.onosproject.net.flow.DefaultFlowRule;
30import org.onosproject.net.flow.FlowRuleService;
31import org.onosproject.net.flow.FlowRule;
32import org.onosproject.net.flow.DefaultTrafficSelector;
33import org.onosproject.net.flow.DefaultTrafficTreatment;
34import org.onosproject.net.packet.PacketPriority;
35import org.onosproject.net.ConnectPoint;
36import org.slf4j.Logger;
37import org.slf4j.LoggerFactory;
38import java.util.ArrayList;
39import java.util.List;
40
Jonathan Hart382119e2016-09-02 13:14:49 -070041/**
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 Mef749632016-07-07 13:30:43 -070046@Component(immediate = true)
47@Service
48public class PatchPanel implements PatchPanelService {
49
Jonathan Hart382119e2016-09-02 13:14:49 -070050 // OSGI: help bundle plugin discover runtime package dependency.
51 @SuppressWarnings("unused")
52 private ConnectPointCompleter connectPointCompleter;
53
Parvathi Mef749632016-07-07 13:30:43 -070054 @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 Hart382119e2016-09-02 13:14:49 -070068 appId = coreService.registerApplication("org.onosproject.patchpanel");
Parvathi Mef749632016-07-07 13:30:43 -070069 log.info("Started");
Parvathi Mef749632016-07-07 13:30:43 -070070 }
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 Hart382119e2016-09-02 13:14:49 -0700100 .makePermanent()
Parvathi Mef749632016-07-07 13:30:43 -0700101 .fromApp(appId).build();
102
Jonathan Hart382119e2016-09-02 13:14:49 -0700103 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 Mef749632016-07-07 13:30:43 -0700112
113 }
114
115}