blob: 068ae0080fef975b5cc56ed7d9ca3184312b7639 [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
17/**
18 * This class acts as a software patch panel application.
19 * The user specifies 2 connectpoint on the same device that he/she would like to patch.
20 * Using a flow rule, the 2 connectpoints are patched.
21 *
22 * @author Parvahti Meyyappan
23 * @version %I%, %G%
24 */
25
26package org.onosproject.patchpanel.impl;
27
28import org.apache.felix.scr.annotations.Component;
29import org.apache.felix.scr.annotations.Reference;
30import org.apache.felix.scr.annotations.Service;
31import org.apache.felix.scr.annotations.ReferenceCardinality;
32import org.apache.felix.scr.annotations.Activate;
33import org.apache.felix.scr.annotations.Deactivate;
34import org.onosproject.core.ApplicationId;
35import org.onosproject.core.CoreService;
36import org.onosproject.net.PortNumber;
37import org.onosproject.net.flow.DefaultFlowRule;
38import org.onosproject.net.flow.FlowRuleService;
39import org.onosproject.net.flow.FlowRule;
40import org.onosproject.net.flow.DefaultTrafficSelector;
41import org.onosproject.net.flow.DefaultTrafficTreatment;
42import org.onosproject.net.packet.PacketPriority;
43import org.onosproject.net.ConnectPoint;
44import org.slf4j.Logger;
45import org.slf4j.LoggerFactory;
46import java.util.ArrayList;
47import java.util.List;
48
49
50@Component(immediate = true)
51@Service
52public class PatchPanel implements PatchPanelService {
53
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 {
68 log.info("Started");
69 try {
70 appId = coreService.getAppId("org.onosproject.patchPanel");
71 } catch (NullPointerException e) {
72 throw new NullPointerException("ERROR:App Id is null");
73 }
74 }
75
76 @Deactivate
77 protected void deactivate() {
78 log.info("Stopped");
79 }
80
81 @Override
82 public boolean addPatch(ConnectPoint num, ConnectPoint num2) {
83 cp1 = num;
84 cp2 = num2;
85 if ((cp1.port().equals(cp2.port())) || (previous.contains(cp1) || previous.contains(cp2))) {
86 log.info("One or both of these ports are already in use, NO FLOW");
87 return false;
88 } else {
89 previous.add(cp1);
90 previous.add(cp2);
91 setFlowRuleService();
92 return true;
93 }
94 }
95
96 public void setFlowRuleService() {
97 PortNumber outPort = cp2.port();
98 PortNumber inPort = cp1.port();
99 FlowRule fr = DefaultFlowRule.builder()
100 .forDevice(cp1.deviceId())
101 .withSelector(DefaultTrafficSelector.builder().matchInPort(inPort).build())
102 .withTreatment(DefaultTrafficTreatment.builder().setOutput(outPort).build())
103 .withPriority(PacketPriority.REACTIVE.priorityValue())
104 .makeTemporary(5)
105 .fromApp(appId).build();
106
107 flowRuleService.applyFlowRules(fr);
108
109 }
110
111}