lishuai
Committed by Gerrit Code Review

[ONOS-2831] Refactor L2 code according to the L3 plan.

Change-Id: Ibc9dc47a18208b9b1602261064a33bc63b131fc3
Showing 20 changed files with 904 additions and 7 deletions
...@@ -13,7 +13,7 @@ ...@@ -13,7 +13,7 @@
13 * See the License for the specific language governing permissions and 13 * See the License for the specific language governing permissions and
14 * limitations under the License. 14 * limitations under the License.
15 */ 15 */
16 -package org.onosproject.vtn; 16 +package org.onosproject.vtn.manager;
17 17
18 import org.onosproject.net.Device; 18 import org.onosproject.net.Device;
19 import org.onosproject.net.Host; 19 import org.onosproject.net.Host;
...@@ -24,18 +24,20 @@ import org.onosproject.net.Host; ...@@ -24,18 +24,20 @@ import org.onosproject.net.Host;
24 public interface VTNService { 24 public interface VTNService {
25 25
26 /** 26 /**
27 - * Creates a vxlan tunnel and creates the ovs when a ovs controller node is detected. 27 + * Creates a vxlan tunnel and creates the ovs when a ovs controller node is
28 + * detected.
28 * 29 *
29 * @param device controller-type device 30 * @param device controller-type device
30 */ 31 */
31 - void onServerDetected(Device device); 32 + void onControllerDetected(Device device);
32 33
33 /** 34 /**
34 - * Drops a vxlan tunnel and drops the ovs when a ovs controller node is vanished. 35 + * Drops a vxlan tunnel and drops the ovs when a ovs controller node is
36 + * vanished.
35 * 37 *
36 * @param device controller-type device 38 * @param device controller-type device
37 */ 39 */
38 - void onServerVanished(Device device); 40 + void onControllerVanished(Device device);
39 41
40 /** 42 /**
41 * Applies default forwarding flows when a ovs is detected. 43 * Applies default forwarding flows when a ovs is detected.
......
...@@ -17,4 +17,4 @@ ...@@ -17,4 +17,4 @@
17 /** 17 /**
18 * VTN application that applies configuration and flows to the device. 18 * VTN application that applies configuration and flows to the device.
19 */ 19 */
20 -package org.onosproject.vtn.impl; 20 +package org.onosproject.vtn.manager.impl;
......
...@@ -17,4 +17,4 @@ ...@@ -17,4 +17,4 @@
17 /** 17 /**
18 * VTN application that applies configuration and flows to the device. 18 * VTN application that applies configuration and flows to the device.
19 */ 19 */
20 -package org.onosproject.vtn; 20 +package org.onosproject.vtn.manager;
......
1 +/*
2 + * Copyright 2015 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 +package org.onosproject.vtn.table;
17 +
18 +import org.onlab.packet.MacAddress;
19 +import org.onosproject.core.ApplicationId;
20 +import org.onosproject.net.DeviceId;
21 +import org.onosproject.net.PortNumber;
22 +import org.onosproject.net.flowobjective.Objective;
23 +import org.onosproject.vtnrsc.SegmentationId;
24 +
25 +/**
26 + * Applies classifier flows to the device.
27 + */
28 +public interface ClassifierService {
29 +
30 + /**
31 + * The port rule that message from host matches Table(0) Match: host mac and
32 + * ingress port Action: set vnid and go to table(50).
33 + *
34 + * @param deviceId Device Id
35 + * @param segmentationId the vnid of the host belong to
36 + * @param inPort the ingress port of the host
37 + * @param srcMac the mac of the host
38 + * @param appId the application ID of the vtn
39 + * @param type the operation of the flow
40 + */
41 + void programLocalIn(DeviceId deviceId, SegmentationId segmentationId,
42 + PortNumber inPort, MacAddress srcMac,
43 + ApplicationId appId, Objective.Operation type);
44 +
45 + /**
46 + * The port rule that message from tunnel Table(0) Match: tunnel port and
47 + * vnid Action: go to table(50).
48 + *
49 + * @param deviceId Device Id
50 + * @param segmentationId the vnid of the host belong to
51 + * @param localTunnelPorts the tunnel pors of the device
52 + * @param type the operation of the flow
53 + */
54 + void programTunnelIn(DeviceId deviceId, SegmentationId segmentationId,
55 + Iterable<PortNumber> localTunnelPorts,
56 + Objective.Operation type);
57 +
58 +}
1 +/*
2 + * Copyright 2015 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 +package org.onosproject.vtn.table;
17 +
18 +import org.onlab.packet.MacAddress;
19 +import org.onosproject.net.DeviceId;
20 +import org.onosproject.net.PortNumber;
21 +import org.onosproject.net.flowobjective.Objective;
22 +import org.onosproject.vtnrsc.SegmentationId;
23 +
24 +/**
25 + * Applies L2 flows to the device.
26 + */
27 +public interface L2ForwardService {
28 +
29 + /**
30 + * The local broadcast rule that message matches Table(50) Match: broadcast
31 + * mac and vnid Action: output port.
32 + *
33 + * @param deviceId Device Id
34 + * @param segmentationId the vnid of the host belong to
35 + * @param inPort the ingress port of the host
36 + * @param localVmPorts the local ports of the network which connect host
37 + * @param localTunnelPorts the tunnel pors of the device
38 + * @param type the operation of the flow
39 + */
40 + void programLocalBcastRules(DeviceId deviceId,
41 + SegmentationId segmentationId,
42 + PortNumber inPort,
43 + Iterable<PortNumber> localVmPorts,
44 + Iterable<PortNumber> localTunnelPorts,
45 + Objective.Operation type);
46 +
47 + /**
48 + * The tunnel broadcast rule that message matches Table(50) Match: broadcast
49 + * mac and vnid Action: output port.
50 + *
51 + * @param deviceId Device Id
52 + * @param segmentationId the vnid of the host belong to
53 + * @param localVmPorts the local ports of the network which connect host
54 + * @param localTunnelPorts the tunnel pors of the device
55 + * @param type the operation of the flow
56 + */
57 + void programTunnelBcastRules(DeviceId deviceId,
58 + SegmentationId segmentationId,
59 + Iterable<PortNumber> localVmPorts,
60 + Iterable<PortNumber> localTunnelPorts,
61 + Objective.Operation type);
62 +
63 + /**
64 + * The local out rule that message matches. Table(50) Match: local host mac
65 + * and vnid Action: output local host port.
66 + *
67 + * @param deviceId Device Id
68 + * @param segmentationId the vnid of the host belong to
69 + * @param outPort the ingress port of the host
70 + * @param sourceMac the mac of the host
71 + * @param type the operation of the flow
72 + */
73 + void programLocalOut(DeviceId deviceId, SegmentationId segmentationId,
74 + PortNumber outPort, MacAddress sourceMac,
75 + Objective.Operation type);
76 +
77 + /**
78 + * The tunnel out rule that message matches. Table(50) Match: host mac and
79 + * vnid Action: output tunnel port.
80 + *
81 + * @param deviceId Device Id
82 + * @param segmentationId the vnid of the host belong to
83 + * @param tunnelOutPort the port of the tunnel
84 + * @param dstMac the mac of the host
85 + * @param type the operation of the flow
86 + */
87 + void programTunnelOut(DeviceId deviceId, SegmentationId segmentationId,
88 + PortNumber tunnelOutPort, MacAddress dstMac,
89 + Objective.Operation type);
90 +
91 +}
1 +/*
2 + * Copyright 2015 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 +package org.onosproject.vtn.table.impl;
17 +
18 +import static com.google.common.base.Preconditions.checkNotNull;
19 +import static org.slf4j.LoggerFactory.getLogger;
20 +
21 +import org.onlab.osgi.DefaultServiceDirectory;
22 +import org.onlab.osgi.ServiceDirectory;
23 +import org.onlab.packet.MacAddress;
24 +import org.onosproject.core.ApplicationId;
25 +import org.onosproject.net.DeviceId;
26 +import org.onosproject.net.PortNumber;
27 +import org.onosproject.net.flow.DefaultTrafficSelector;
28 +import org.onosproject.net.flow.DefaultTrafficTreatment;
29 +import org.onosproject.net.flow.TrafficSelector;
30 +import org.onosproject.net.flow.TrafficTreatment;
31 +import org.onosproject.net.flow.criteria.Criteria;
32 +import org.onosproject.net.flow.instructions.Instructions;
33 +import org.onosproject.net.flowobjective.DefaultForwardingObjective;
34 +import org.onosproject.net.flowobjective.FlowObjectiveService;
35 +import org.onosproject.net.flowobjective.ForwardingObjective;
36 +import org.onosproject.net.flowobjective.ForwardingObjective.Flag;
37 +import org.onosproject.net.flowobjective.Objective;
38 +import org.onosproject.vtn.table.ClassifierService;
39 +import org.onosproject.vtnrsc.SegmentationId;
40 +import org.slf4j.Logger;
41 +
42 +import com.google.common.collect.Sets;
43 +
44 +/**
45 + * Provides implementation of ClassifierService.
46 + */
47 +public class ClassifierServiceImpl implements ClassifierService {
48 + private final Logger log = getLogger(getClass());
49 +
50 + private static final int L2_CLAFFIFIER_PRIORITY = 50000;
51 +
52 + private final FlowObjectiveService flowObjectiveService;
53 + private final ApplicationId appId;
54 +
55 + /**
56 + * Constructor.
57 + *
58 + * @param appId the application id of vtn
59 + */
60 + public ClassifierServiceImpl(ApplicationId appId) {
61 + this.appId = checkNotNull(appId, "ApplicationId can not be null");
62 + ServiceDirectory serviceDirectory = new DefaultServiceDirectory();
63 + this.flowObjectiveService = serviceDirectory.get(FlowObjectiveService.class);
64 + }
65 +
66 + @Override
67 + public void programLocalIn(DeviceId deviceId,
68 + SegmentationId segmentationId, PortNumber inPort,
69 + MacAddress srcMac, ApplicationId appid,
70 + Objective.Operation type) {
71 + TrafficSelector selector = DefaultTrafficSelector.builder()
72 + .matchInPort(inPort).matchEthSrc(srcMac).build();
73 + TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
74 + treatment.add(Instructions
75 + .modTunnelId(Long.parseLong(segmentationId.toString())));
76 + ForwardingObjective.Builder objective = DefaultForwardingObjective
77 + .builder().withTreatment(treatment.build())
78 + .withSelector(selector).fromApp(appId).makePermanent()
79 + .withFlag(Flag.SPECIFIC).withPriority(L2_CLAFFIFIER_PRIORITY);
80 + if (type.equals(Objective.Operation.ADD)) {
81 + log.debug("programLocalIn-->ADD");
82 + flowObjectiveService.forward(deviceId, objective.add());
83 + } else {
84 + log.debug("programLocalIn-->REMOVE");
85 + flowObjectiveService.forward(deviceId, objective.remove());
86 + }
87 + }
88 +
89 + @Override
90 + public void programTunnelIn(DeviceId deviceId,
91 + SegmentationId segmentationId,
92 + Iterable<PortNumber> localTunnelPorts,
93 + Objective.Operation type) {
94 + if (localTunnelPorts == null) {
95 + log.info("No tunnel port in device");
96 + return;
97 + }
98 + Sets.newHashSet(localTunnelPorts).stream().forEach(tp -> {
99 + TrafficSelector selector = DefaultTrafficSelector.builder()
100 + .matchInPort(tp).add(Criteria.matchTunnelId(Long
101 + .parseLong(segmentationId.toString())))
102 + .build();
103 +
104 + TrafficTreatment treatment = DefaultTrafficTreatment.builder()
105 + .build();
106 + ForwardingObjective.Builder objective = DefaultForwardingObjective
107 + .builder().withTreatment(treatment).withSelector(selector)
108 + .fromApp(appId).makePermanent().withFlag(Flag.SPECIFIC)
109 + .withPriority(L2_CLAFFIFIER_PRIORITY);
110 + if (type.equals(Objective.Operation.ADD)) {
111 + log.debug("programTunnelIn-->ADD");
112 + flowObjectiveService.forward(deviceId, objective.add());
113 + } else {
114 + log.debug("programTunnelIn-->REMOVE");
115 + flowObjectiveService.forward(deviceId, objective.remove());
116 + }
117 + });
118 + }
119 +
120 +}
1 +/*
2 + * Copyright 2015 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 +package org.onosproject.vtn.table.impl;
17 +
18 +import static com.google.common.base.Preconditions.checkNotNull;
19 +import static org.slf4j.LoggerFactory.getLogger;
20 +
21 +import org.onlab.osgi.DefaultServiceDirectory;
22 +import org.onlab.osgi.ServiceDirectory;
23 +import org.onlab.packet.MacAddress;
24 +import org.onosproject.core.ApplicationId;
25 +import org.onosproject.net.DeviceId;
26 +import org.onosproject.net.PortNumber;
27 +import org.onosproject.net.flow.DefaultTrafficSelector;
28 +import org.onosproject.net.flow.DefaultTrafficTreatment;
29 +import org.onosproject.net.flow.TrafficSelector;
30 +import org.onosproject.net.flow.TrafficTreatment;
31 +import org.onosproject.net.flow.criteria.Criteria;
32 +import org.onosproject.net.flowobjective.DefaultForwardingObjective;
33 +import org.onosproject.net.flowobjective.FlowObjectiveService;
34 +import org.onosproject.net.flowobjective.ForwardingObjective;
35 +import org.onosproject.net.flowobjective.ForwardingObjective.Flag;
36 +import org.onosproject.net.flowobjective.Objective;
37 +import org.onosproject.vtn.table.L2ForwardService;
38 +import org.onosproject.vtnrsc.SegmentationId;
39 +import org.slf4j.Logger;
40 +
41 +import com.google.common.collect.Sets;
42 +
43 +/**
44 + * Provides implementation of L2ForwardService.
45 + */
46 +public final class L2ForwardServiceImpl implements L2ForwardService {
47 + private final Logger log = getLogger(getClass());
48 +
49 + private static final int MAC_PRIORITY = 0xffff;
50 +
51 + private final FlowObjectiveService flowObjectiveService;
52 + private final ApplicationId appId;
53 +
54 + /**
55 + * Constructor.
56 + *
57 + * @param appId the application id of vtn
58 + */
59 + public L2ForwardServiceImpl(ApplicationId appId) {
60 + this.appId = checkNotNull(appId, "ApplicationId can not be null");
61 + ServiceDirectory serviceDirectory = new DefaultServiceDirectory();
62 + this.flowObjectiveService = serviceDirectory.get(FlowObjectiveService.class);
63 + }
64 +
65 + @Override
66 + public void programLocalBcastRules(DeviceId deviceId,
67 + SegmentationId segmentationId,
68 + PortNumber inPort,
69 + Iterable<PortNumber> localVmPorts,
70 + Iterable<PortNumber> localTunnelPorts,
71 + Objective.Operation type) {
72 + if (localVmPorts == null || localTunnelPorts == null) {
73 + log.info("No other host port and tunnel in the device");
74 + return;
75 + }
76 + Sets.newHashSet(localVmPorts).stream().forEach(lp -> {
77 + TrafficSelector selector = DefaultTrafficSelector.builder()
78 + .matchInPort(lp).matchEthDst(MacAddress.BROADCAST)
79 + .add(Criteria.matchTunnelId(Long
80 + .parseLong(segmentationId.toString())))
81 + .build();
82 + TrafficTreatment.Builder treatment = DefaultTrafficTreatment
83 + .builder();
84 + boolean flag = false;
85 + for (PortNumber outPort : localVmPorts) {
86 + flag = true;
87 + if (outPort != lp) {
88 + treatment.setOutput(outPort);
89 + }
90 + }
91 + if (type.equals(Objective.Operation.REMOVE) && inPort == lp) {
92 + flag = false;
93 + }
94 + for (PortNumber outport : localTunnelPorts) {
95 + treatment.setOutput(outport);
96 + }
97 + ForwardingObjective.Builder objective = DefaultForwardingObjective
98 + .builder().withTreatment(treatment.build())
99 + .withSelector(selector).fromApp(appId).makePermanent()
100 + .withFlag(Flag.SPECIFIC).withPriority(MAC_PRIORITY);
101 + if (flag) {
102 + flowObjectiveService.forward(deviceId, objective.add());
103 + } else {
104 + flowObjectiveService.forward(deviceId, objective.remove());
105 + }
106 + });
107 + }
108 +
109 + @Override
110 + public void programTunnelBcastRules(DeviceId deviceId,
111 + SegmentationId segmentationId,
112 + Iterable<PortNumber> localVmPorts,
113 + Iterable<PortNumber> localTunnelPorts,
114 + Objective.Operation type) {
115 + if (localVmPorts == null || localTunnelPorts == null) {
116 + log.info("No other host port or tunnel ports in the device");
117 + return;
118 + }
119 + Sets.newHashSet(localTunnelPorts).stream().forEach(tp -> {
120 + TrafficSelector selector = DefaultTrafficSelector.builder()
121 + .matchInPort(tp)
122 + .add(Criteria.matchTunnelId(Long
123 + .parseLong(segmentationId.toString())))
124 + .matchEthDst(MacAddress.BROADCAST).build();
125 + TrafficTreatment.Builder treatment = DefaultTrafficTreatment
126 + .builder();
127 +
128 + for (PortNumber outPort : localVmPorts) {
129 + treatment.setOutput(outPort);
130 + }
131 +
132 + ForwardingObjective.Builder objective = DefaultForwardingObjective
133 + .builder().withTreatment(treatment.build())
134 + .withSelector(selector).fromApp(appId).makePermanent()
135 + .withFlag(Flag.SPECIFIC).withPriority(MAC_PRIORITY);
136 + if (type.equals(Objective.Operation.ADD)) {
137 + if (Sets.newHashSet(localVmPorts).size() == 0) {
138 + flowObjectiveService.forward(deviceId, objective.remove());
139 + } else {
140 + flowObjectiveService.forward(deviceId, objective.add());
141 + }
142 + } else {
143 + flowObjectiveService.forward(deviceId, objective.remove());
144 + }
145 + });
146 + }
147 +
148 + @Override
149 + public void programLocalOut(DeviceId deviceId,
150 + SegmentationId segmentationId,
151 + PortNumber outPort, MacAddress sourceMac,
152 + Objective.Operation type) {
153 + TrafficSelector selector = DefaultTrafficSelector.builder()
154 + .matchTunnelId(Long.parseLong(segmentationId.toString()))
155 + .matchEthDst(sourceMac).build();
156 + TrafficTreatment treatment = DefaultTrafficTreatment.builder()
157 + .setOutput(outPort).build();
158 + ForwardingObjective.Builder objective = DefaultForwardingObjective
159 + .builder().withTreatment(treatment).withSelector(selector)
160 + .fromApp(appId).withFlag(Flag.SPECIFIC)
161 + .withPriority(MAC_PRIORITY);
162 + if (type.equals(Objective.Operation.ADD)) {
163 + flowObjectiveService.forward(deviceId, objective.add());
164 + } else {
165 + flowObjectiveService.forward(deviceId, objective.remove());
166 + }
167 +
168 + }
169 +
170 + @Override
171 + public void programTunnelOut(DeviceId deviceId,
172 + SegmentationId segmentationId,
173 + PortNumber tunnelOutPort, MacAddress dstMac,
174 + Objective.Operation type) {
175 + TrafficSelector selector = DefaultTrafficSelector.builder()
176 + .matchEthDst(dstMac).add(Criteria.matchTunnelId(Long
177 + .parseLong(segmentationId.toString())))
178 + .build();
179 + TrafficTreatment treatment = DefaultTrafficTreatment.builder()
180 + .setOutput(tunnelOutPort).build();
181 + ForwardingObjective.Builder objective = DefaultForwardingObjective
182 + .builder().withTreatment(treatment).withSelector(selector)
183 + .fromApp(appId).withFlag(Flag.SPECIFIC)
184 + .withPriority(MAC_PRIORITY);
185 + if (type.equals(Objective.Operation.ADD)) {
186 + flowObjectiveService.forward(deviceId, objective.add());
187 + } else {
188 + flowObjectiveService.forward(deviceId, objective.remove());
189 + }
190 +
191 + }
192 +
193 +}
1 +/*
2 + * Copyright 2015 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 + * VTN application that applies configuration and flows to the device.
19 + */
20 +package org.onosproject.vtn.table.impl;
1 +/*
2 + * Copyright 2015 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 + * VTN application that applies configuration and flows to the device.
19 + */
20 +package org.onosproject.vtn.table;
1 +package org.onosproject.vtn.util;
2 +
3 +import static org.onlab.util.Tools.toHex;
4 +
5 +import java.net.URI;
6 +import java.net.URISyntaxException;
7 +import java.util.Calendar;
8 +
9 +import org.onosproject.core.IdGenerator;
10 +import org.onosproject.net.DeviceId;
11 +
12 +public final class DataPathIdGenerator implements IdGenerator {
13 + private static final String SCHEME = "of";
14 + private String ipAddress;
15 + private String timeStamp;
16 +
17 + private DataPathIdGenerator(Builder builder) {
18 + this.ipAddress = builder.ipAddress;
19 + Calendar cal = Calendar.getInstance();
20 + this.timeStamp = String.valueOf(cal.get(Calendar.SECOND))
21 + + String.valueOf(cal.get(Calendar.MILLISECOND));
22 + }
23 +
24 + @Override
25 + public long getNewId() {
26 + String dpid = ipAddress.replace(".", "") + timeStamp;
27 + return Long.parseLong(dpid);
28 + }
29 +
30 + public String getDpId() {
31 + return toHex(getNewId());
32 + }
33 +
34 + public DeviceId getDeviceId() {
35 + try {
36 + URI uri = new URI(SCHEME, toHex(getNewId()), null);
37 + return DeviceId.deviceId(uri);
38 + } catch (URISyntaxException e) {
39 + return null;
40 + }
41 + }
42 +
43 + /**
44 + * Returns a new builder.
45 + *
46 + * @return new builder
47 + */
48 + public static Builder builder() {
49 + return new Builder();
50 + }
51 +
52 + public static final class Builder {
53 + private String ipAddress;
54 +
55 + public Builder addIpAddress(String ipAddress) {
56 + this.ipAddress = ipAddress;
57 + return this;
58 + }
59 +
60 + public DataPathIdGenerator build() {
61 + return new DataPathIdGenerator(this);
62 + }
63 + }
64 +}
1 +/*
2 + * Copyright 2015 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 +package org.onosproject.vtn.util;
17 +
18 +import java.util.Set;
19 +
20 +import org.onlab.packet.IpAddress;
21 +import org.onosproject.net.PortNumber;
22 +import org.onosproject.net.behaviour.BridgeConfig;
23 +import org.onosproject.net.behaviour.BridgeName;
24 +import org.onosproject.net.behaviour.DefaultTunnelDescription;
25 +import org.onosproject.net.behaviour.IpTunnelEndPoint;
26 +import org.onosproject.net.behaviour.TunnelConfig;
27 +import org.onosproject.net.behaviour.TunnelDescription;
28 +import org.onosproject.net.behaviour.TunnelEndPoint;
29 +import org.onosproject.net.driver.DriverHandler;
30 +
31 +/**
32 + * Applies configuration to the device.
33 + */
34 +public final class VtnConfig {
35 +
36 + private static final String DEFAULT_BRIDGE_NAME = "br-int";
37 +
38 + /**
39 + * Constructs a vtn config object. Utility classes should not have a
40 + * public or default constructor, otherwise IDE will compile unsuccessfully. This
41 + * class should not be instantiated.
42 + */
43 + private VtnConfig() {
44 + }
45 +
46 + /**
47 + * Creates or update bridge in the controller device.
48 + *
49 + * @param handler DriverHandler
50 + * @param dpid datapath id
51 + * @param exPortName external port name
52 + */
53 + public static void applyBridgeConfig(DriverHandler handler, String dpid, String exPortName) {
54 + BridgeConfig bridgeConfig = handler.behaviour(BridgeConfig.class);
55 + bridgeConfig.addBridge(BridgeName.bridgeName(DEFAULT_BRIDGE_NAME), dpid, exPortName);
56 + }
57 +
58 + /**
59 + * Creates or update tunnel in the controller device.
60 + *
61 + * @param handler DriverHandler
62 + * @param srcIp the ipAddress of the local controller device
63 + * @param dstIp the ipAddress of the remote controller device
64 + */
65 + public static void applyTunnelConfig(DriverHandler handler, IpAddress srcIp,
66 + IpAddress dstIp) {
67 + TunnelConfig tunnelConfig = handler.behaviour(TunnelConfig.class);
68 + TunnelEndPoint tunnelAsSrc = IpTunnelEndPoint.ipTunnelPoint(srcIp);
69 + TunnelEndPoint tunnelAsDst = IpTunnelEndPoint.ipTunnelPoint(dstIp);
70 + TunnelDescription tunnel = new DefaultTunnelDescription(
71 + tunnelAsSrc,
72 + tunnelAsDst,
73 + TunnelDescription.Type.VXLAN,
74 + null);
75 + tunnelConfig.createTunnel(tunnel);
76 + }
77 +
78 + /**
79 + * Creates or update tunnel in the controller device.
80 + *
81 + * @param handler DriverHandler
82 + * @param srcIp the ipAddress of the local controller device
83 + * @param dstIp the ipAddress of the remote controller device
84 + */
85 + public static void removeTunnelConfig(DriverHandler handler, IpAddress srcIp,
86 + IpAddress dstIp) {
87 + TunnelConfig tunnelConfig = handler.behaviour(TunnelConfig.class);
88 + TunnelEndPoint tunnelAsSrc = IpTunnelEndPoint.ipTunnelPoint(srcIp);
89 + TunnelEndPoint tunnelAsDst = IpTunnelEndPoint.ipTunnelPoint(dstIp);
90 + TunnelDescription tunnel = new DefaultTunnelDescription(
91 + tunnelAsSrc,
92 + tunnelAsDst,
93 + TunnelDescription.Type.VXLAN,
94 + null);
95 + tunnelConfig.removeTunnel(tunnel);
96 + }
97 +
98 + /**
99 + * Gets ports in the controller device.
100 + *
101 + * @param handler DriverHandler
102 + */
103 + public static Set<PortNumber> getPortNumbers(DriverHandler handler) {
104 + BridgeConfig bridgeConfig = handler.behaviour(BridgeConfig.class);
105 + return bridgeConfig.getPortNumbers();
106 + }
107 +
108 +}
1 +/*
2 + * Copyright 2015 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 +package org.onosproject.vtn.util;
17 +
18 +import java.util.ArrayList;
19 +import java.util.Collection;
20 +
21 +import org.onosproject.net.AnnotationKeys;
22 +import org.onosproject.net.Device;
23 +import org.onosproject.net.DeviceId;
24 +import org.onosproject.net.Port;
25 +import org.onosproject.net.PortNumber;
26 +import org.slf4j.Logger;
27 +import org.slf4j.LoggerFactory;
28 +
29 +import com.google.common.collect.Sets;
30 +
31 +/**
32 + * VtnData utility class.
33 + */
34 +public final class VtnData {
35 +
36 + private static final Logger log = LoggerFactory.getLogger(VtnData.class);
37 + private static final String SWITCH_CHANNEL_ID = "channelId";
38 + private static final String PORT_HEAD = "vxlan";
39 +
40 + /**
41 + * Constructs a VtnData object. Utility classes should not have a public or
42 + * default constructor, otherwise IDE will compile unsuccessfully. This
43 + * class should not be instantiated.
44 + */
45 + private VtnData() {
46 + }
47 +
48 + /**
49 + * Get the ControllerIp from the device .
50 + *
51 + * @param device Device
52 + * @return Controller Ip
53 + */
54 + public static String getControllerIpOfSwitch(Device device) {
55 + String url = device.annotations().value(SWITCH_CHANNEL_ID);
56 + return url.substring(0, url.lastIndexOf(":"));
57 + }
58 +
59 + /**
60 + * Get the ControllerId from the device .
61 + *
62 + * @param device Device
63 + * @param devices Devices
64 + * @return Controller Id
65 + */
66 + public static DeviceId getControllerId(Device device,
67 + Iterable<Device> devices) {
68 + for (Device d : devices) {
69 + if (d.type() == Device.Type.CONTROLLER && d.id().toString()
70 + .contains(getControllerIpOfSwitch(device))) {
71 + return d.id();
72 + }
73 + }
74 + log.info("Can not find controller for device : {}", device.id());
75 + return null;
76 + }
77 +
78 + /**
79 + * Get local tunnel ports.
80 + *
81 + * @param ports Iterable of Port
82 + * @return Collection of PortNumber
83 + */
84 + public static Collection<PortNumber> getLocalTunnelPorts(Iterable<Port> ports) {
85 + Collection<PortNumber> localTunnelPorts = new ArrayList<>();
86 + Sets.newHashSet(ports).stream()
87 + .filter(p -> !p.number().equals(PortNumber.LOCAL))
88 + .forEach(p -> {
89 + if (p.annotations().value(AnnotationKeys.PORT_NAME)
90 + .startsWith(PORT_HEAD)) {
91 + localTunnelPorts.add(p.number());
92 + }
93 + });
94 + return localTunnelPorts;
95 + }
96 +
97 +}
1 +/*
2 + * Copyright 2015 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 + * VTN application that applies configuration and flows to the device.
19 + */
20 +package org.onosproject.vtn.util;
...@@ -36,6 +36,15 @@ public interface BridgeConfig extends HandlerBehaviour { ...@@ -36,6 +36,15 @@ public interface BridgeConfig extends HandlerBehaviour {
36 void addBridge(BridgeName bridgeName); 36 void addBridge(BridgeName bridgeName);
37 37
38 /** 38 /**
39 + * Adds a bridge with given bridge name, dpid and exPortName.
40 + *
41 + * @param bridgeName bridge name
42 + * @param dpid dpid
43 + * @param exPortName external port name
44 + */
45 + void addBridge(BridgeName bridgeName, String dpid, String exPortName);
46 +
47 + /**
39 * Adds a bridge with given bridge name and dpid, and sets the controller 48 * Adds a bridge with given bridge name and dpid, and sets the controller
40 * of the bridge with given controllers. 49 * of the bridge with given controllers.
41 * 50 *
......
...@@ -54,6 +54,13 @@ public class OvsdbBridgeConfig extends AbstractHandlerBehaviour ...@@ -54,6 +54,13 @@ public class OvsdbBridgeConfig extends AbstractHandlerBehaviour
54 } 54 }
55 55
56 @Override 56 @Override
57 + public void addBridge(BridgeName bridgeName, String dpid, String exPortName) {
58 + DriverHandler handler = handler();
59 + OvsdbClientService clientService = getOvsdbClientService(handler);
60 + clientService.createBridge(bridgeName.name(), dpid, exPortName);
61 + }
62 +
63 + @Override
57 public boolean addBridge(BridgeName bridgeName, String dpid, List<ControllerInfo> controllers) { 64 public boolean addBridge(BridgeName bridgeName, String dpid, List<ControllerInfo> controllers) {
58 DriverHandler handler = handler(); 65 DriverHandler handler = handler();
59 OvsdbClientService clientService = getOvsdbClientService(handler); 66 OvsdbClientService clientService = getOvsdbClientService(handler);
......
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
16 package org.onosproject.ovsdb.controller; 16 package org.onosproject.ovsdb.controller;
17 17
18 import com.google.common.util.concurrent.ListenableFuture; 18 import com.google.common.util.concurrent.ListenableFuture;
19 +
19 import org.onlab.packet.IpAddress; 20 import org.onlab.packet.IpAddress;
20 import org.onosproject.net.DeviceId; 21 import org.onosproject.net.DeviceId;
21 import org.onosproject.net.behaviour.ControllerInfo; 22 import org.onosproject.net.behaviour.ControllerInfo;
...@@ -84,6 +85,15 @@ public interface OvsdbClientService extends OvsdbRPC { ...@@ -84,6 +85,15 @@ public interface OvsdbClientService extends OvsdbRPC {
84 void createBridge(String bridgeName); 85 void createBridge(String bridgeName);
85 86
86 /** 87 /**
88 + * Creates a bridge.
89 + *
90 + * @param bridgeName bridge name
91 + * @param dpid data path id
92 + * @param exPortName external port name
93 + */
94 + void createBridge(String bridgeName, String dpid, String exPortName);
95 +
96 + /**
87 * Creates a bridge with given name and dpid. 97 * Creates a bridge with given name and dpid.
88 * Sets the bridge's controller with given controllers. 98 * Sets the bridge's controller with given controllers.
89 * 99 *
......
...@@ -24,7 +24,9 @@ import com.google.common.collect.Sets; ...@@ -24,7 +24,9 @@ import com.google.common.collect.Sets;
24 import com.google.common.util.concurrent.Futures; 24 import com.google.common.util.concurrent.Futures;
25 import com.google.common.util.concurrent.ListenableFuture; 25 import com.google.common.util.concurrent.ListenableFuture;
26 import com.google.common.util.concurrent.SettableFuture; 26 import com.google.common.util.concurrent.SettableFuture;
27 +
27 import io.netty.channel.Channel; 28 import io.netty.channel.Channel;
29 +
28 import org.onlab.packet.IpAddress; 30 import org.onlab.packet.IpAddress;
29 import org.onosproject.net.DeviceId; 31 import org.onosproject.net.DeviceId;
30 import org.onosproject.net.behaviour.ControllerInfo; 32 import org.onosproject.net.behaviour.ControllerInfo;
...@@ -489,6 +491,76 @@ public class DefaultOvsdbClient ...@@ -489,6 +491,76 @@ public class DefaultOvsdbClient
489 } 491 }
490 492
491 @Override 493 @Override
494 + public void createBridge(String bridgeName, String dpid, String exPortName) {
495 + log.debug("create bridge {}", bridgeName);
496 +
497 + DatabaseSchema dbSchema = schema.get(OvsdbConstant.DATABASENAME);
498 + if (dbSchema == null) {
499 + log.warn("The schema is null");
500 + return;
501 + }
502 +
503 + Bridge bridge = (Bridge) TableGenerator.createTable(dbSchema,
504 + OvsdbTable.BRIDGE);
505 + if (bridge == null) {
506 + log.debug("Can not create bridge");
507 + return;
508 + }
509 +
510 + Set<String> failModes = new HashSet<>();
511 + failModes.add("secure");
512 + bridge.setFailMode(failModes);
513 +
514 + Set<String> protocols = new HashSet<>();
515 + protocols.add(OvsdbConstant.OPENFLOW13);
516 + bridge.setProtocols(protocols);
517 +
518 + String ovsUuid = getOvsUuid(OvsdbConstant.DATABASENAME);
519 + if (ovsUuid == null) {
520 + log.warn("The Open_vSwitch is null");
521 + return;
522 + }
523 +
524 + String bridgeUuid = getBridgeUuid(bridgeName);
525 + if (bridgeUuid == null) {
526 + log.debug("Create a new bridge");
527 +
528 + bridge.setName(bridgeName);
529 + if (dpid != null) {
530 + Map<String, String> options = new HashMap<>();
531 + options.put("datapath-id", dpid);
532 + bridge.setOtherConfig(options);
533 + }
534 + bridgeUuid = insertConfig(OvsdbConstant.BRIDGE, "_uuid",
535 + OvsdbConstant.DATABASENAME, "bridges",
536 + ovsUuid, bridge.getRow());
537 +
538 + if (bridgeUuid != null) {
539 + Port port = (Port) TableGenerator.createTable(dbSchema,
540 + OvsdbTable.PORT);
541 + if (port != null) {
542 + log.debug("the port is not null");
543 + port.setName(bridgeName);
544 +
545 + insertConfig(OvsdbConstant.PORT, "_uuid", "Bridge", "ports", bridgeUuid,
546 + port.getRow());
547 + }
548 + }
549 +
550 + } else {
551 + log.info("Update a bridge");
552 + updateConfig(OvsdbConstant.BRIDGE, "_uuid", bridgeUuid, bridge.getRow());
553 + }
554 + // Create external port
555 + if (exPortName != null) {
556 + createPort(bridgeName, exPortName);
557 + }
558 +
559 + setControllerAuto(bridgeUuid);
560 + log.info("Create bridge success");
561 + }
562 +
563 + @Override
492 public boolean createBridge(String bridgeName, String dpid, List<ControllerInfo> controllers) { 564 public boolean createBridge(String bridgeName, String dpid, List<ControllerInfo> controllers) {
493 565
494 DatabaseSchema dbSchema = schema.get(OvsdbConstant.DATABASENAME); 566 DatabaseSchema dbSchema = schema.get(OvsdbConstant.DATABASENAME);
......
...@@ -18,6 +18,7 @@ package org.onosproject.ovsdb.controller.driver; ...@@ -18,6 +18,7 @@ package org.onosproject.ovsdb.controller.driver;
18 18
19 import com.fasterxml.jackson.databind.JsonNode; 19 import com.fasterxml.jackson.databind.JsonNode;
20 import com.google.common.util.concurrent.ListenableFuture; 20 import com.google.common.util.concurrent.ListenableFuture;
21 +
21 import org.onlab.packet.IpAddress; 22 import org.onlab.packet.IpAddress;
22 import org.onosproject.net.DeviceId; 23 import org.onosproject.net.DeviceId;
23 import org.onosproject.net.behaviour.ControllerInfo; 24 import org.onosproject.net.behaviour.ControllerInfo;
...@@ -216,4 +217,9 @@ public class OvsdbClientServiceAdapter implements OvsdbClientService { ...@@ -216,4 +217,9 @@ public class OvsdbClientServiceAdapter implements OvsdbClientService {
216 public ListenableFuture<List<JsonNode>> transact(DatabaseSchema dbSchema, List<Operation> operations) { 217 public ListenableFuture<List<JsonNode>> transact(DatabaseSchema dbSchema, List<Operation> operations) {
217 return null; 218 return null;
218 } 219 }
220 +
221 + @Override
222 + public void createBridge(String bridgeName, String dpid, String exPortName) {
223 +
224 + }
219 } 225 }
......