blob: 862af0f3fd1c24399de91f122e65d3a15e28a43c [file] [log] [blame]
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Praseed Balakrishnan8c67d172014-11-10 10:15: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 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.optical.testapp;
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -080017
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -080018import org.apache.felix.scr.annotations.Activate;
19import org.apache.felix.scr.annotations.Deactivate;
20import org.apache.felix.scr.annotations.Reference;
21import org.apache.felix.scr.annotations.ReferenceCardinality;
Jonathan Hartd9df7bd2015-11-10 17:10:25 -080022import org.onlab.packet.Ethernet;
23import org.onlab.packet.MplsLabel;
Brian O'Connorabafb502014-12-02 22:26:20 -080024import org.onosproject.core.ApplicationId;
25import org.onosproject.core.CoreService;
26import org.onosproject.net.Device;
27import org.onosproject.net.DeviceId;
28import org.onosproject.net.PortNumber;
29import org.onosproject.net.device.DeviceEvent;
30import org.onosproject.net.device.DeviceListener;
31import org.onosproject.net.device.DeviceService;
32import org.onosproject.net.flow.DefaultFlowRule;
33import org.onosproject.net.flow.DefaultTrafficSelector;
34import org.onosproject.net.flow.DefaultTrafficTreatment;
35import org.onosproject.net.flow.FlowRule;
36import org.onosproject.net.flow.FlowRuleService;
37import org.onosproject.net.flow.TrafficSelector;
38import org.onosproject.net.flow.TrafficTreatment;
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -080039import org.slf4j.Logger;
40
Jonathan Hartd9df7bd2015-11-10 17:10:25 -080041import java.util.HashMap;
42import java.util.Map;
43
44import static org.slf4j.LoggerFactory.getLogger;
45
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -080046/**
47 * Sample reactive forwarding application.
48 */
49//@Component(immediate = true)
Jonathan Hartd9df7bd2015-11-10 17:10:25 -080050public class MplsForwarding {
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -080051
52 private final Logger log = getLogger(getClass());
53
54 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
55 protected FlowRuleService flowRuleService;
56
57 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
58 protected CoreService coreService;
59
60 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
61 protected DeviceService deviceService;
62
63 private ApplicationId appId;
64
65 private final InternalDeviceListener listener = new InternalDeviceListener();
66
67 private final Map<DeviceId, Integer> uglyMap = new HashMap<>();
68
69 @Activate
70 public void activate() {
Brian O'Connorf1781632015-12-11 15:34:03 -080071 appId = coreService.registerApplication("org.onosproject.mplsfwd");
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -080072
73 uglyMap.put(DeviceId.deviceId("of:0000000000000001"), 1);
74 uglyMap.put(DeviceId.deviceId("of:0000000000000002"), 2);
75 uglyMap.put(DeviceId.deviceId("of:0000000000000003"), 3);
76
77 deviceService.addListener(listener);
78
79 for (Device d : deviceService.getDevices()) {
80 pushRules(d);
81 }
82
83
84 log.info("Started with Application ID {}", appId.id());
85 }
86
87 @Deactivate
88 public void deactivate() {
89 flowRuleService.removeFlowRulesById(appId);
90
91 log.info("Stopped");
92 }
93
94
95 private void pushRules(Device device) {
96
97 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
98 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
99 int inport = 1;
100 int outport = 2;
Michele Santuari4b6019e2014-12-19 11:31:45 +0100101 MplsLabel mplsLabel = MplsLabel.mplsLabel(101);
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800102 Integer switchNumber = uglyMap.get(device.id());
103 if (switchNumber == null) {
104 return;
105 }
106
107 switch (switchNumber) {
108 case 1:
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800109 sbuilder.matchInPort(PortNumber.portNumber(inport));
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800110 tbuilder.setOutput(PortNumber.portNumber(outport))
111 .pushMpls()
112 .setMpls(mplsLabel);
113 break;
114 case 2:
115 sbuilder.matchMplsLabel(mplsLabel)
116 .matchEthType(Ethernet.MPLS_UNICAST)
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800117 .matchInPort(PortNumber.portNumber(inport));
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800118 tbuilder.setOutput(PortNumber.portNumber(outport));
119 break;
120 case 3:
121 sbuilder.matchMplsLabel(mplsLabel)
122 .matchEthType(Ethernet.MPLS_UNICAST)
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800123 .matchInPort(PortNumber.portNumber(inport));
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800124 tbuilder.popMpls().setOutput(PortNumber.portNumber(outport));
125 break;
126 default:
127 }
128
129 TrafficTreatment treatement = tbuilder.build();
130 TrafficSelector selector = sbuilder.build();
131
Ray Milkeyd13a37b2015-06-12 11:55:17 -0700132 FlowRule f = DefaultFlowRule.builder()
133 .forDevice(device.id())
134 .withSelector(selector)
135 .withTreatment(treatement)
136 .withPriority(100)
137 .fromApp(appId)
138 .makeTemporary(600)
139 .build();
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800140
141 flowRuleService.applyFlowRules(f);
142 }
143
144
145 public class InternalDeviceListener implements DeviceListener {
146
147 @Override
148 public void event(DeviceEvent event) {
149 switch (event.type()) {
150 case DEVICE_ADDED:
151 pushRules(event.subject());
152 break;
153 case DEVICE_AVAILABILITY_CHANGED:
154 break;
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800155 case DEVICE_REMOVED:
156 break;
157 case DEVICE_SUSPENDED:
158 break;
159 case DEVICE_UPDATED:
160 break;
161 case PORT_ADDED:
162 break;
163 case PORT_REMOVED:
164 break;
165 case PORT_UPDATED:
166 break;
167 default:
168 break;
169
170 }
171
172 }
173
174 }
175
176
177}
178
179