Claudine Chiu | b211b87 | 2017-09-05 17:27:00 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-present Open Networking Foundation |
| 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.ofagent.impl; |
| 17 | |
| 18 | import com.google.common.collect.Lists; |
| 19 | import org.onosproject.core.GroupId; |
| 20 | import org.onosproject.net.DeviceId; |
| 21 | import org.onosproject.net.PortNumber; |
| 22 | import org.onosproject.net.driver.DefaultDriverData; |
| 23 | import org.onosproject.net.driver.DefaultDriverHandler; |
| 24 | import org.onosproject.net.driver.Driver; |
| 25 | import org.onosproject.net.driver.DriverHandler; |
| 26 | import org.onosproject.net.driver.DriverService; |
| 27 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 28 | import org.onosproject.net.flow.TrafficTreatment; |
| 29 | import org.onosproject.net.group.DefaultGroupBucket; |
| 30 | import org.onosproject.net.group.GroupBucket; |
| 31 | import org.onosproject.net.group.GroupBuckets; |
| 32 | import org.onosproject.openflow.controller.Dpid; |
| 33 | import org.onosproject.provider.of.flow.util.FlowEntryBuilder; |
| 34 | import org.projectfloodlight.openflow.protocol.OFBucket; |
| 35 | import org.projectfloodlight.openflow.protocol.OFGroupType; |
| 36 | import org.projectfloodlight.openflow.protocol.action.OFAction; |
| 37 | import org.slf4j.Logger; |
| 38 | |
| 39 | import java.util.List; |
| 40 | |
| 41 | import static org.slf4j.LoggerFactory.getLogger; |
| 42 | |
| 43 | /** |
| 44 | * Group Bucket builder customized for OFAgent. |
| 45 | * <p> |
| 46 | * This builder is used to build GroupBucketEntry objects for virtual devices encountered by |
| 47 | * OFAgent. Its code is based on org.onosproject.provider.of.group.impl.GroupBucketEntryBuilder, |
| 48 | * except that the driver has been hardcoded to "ovs". |
| 49 | */ |
| 50 | public class OFAgentVirtualGroupBucketEntryBuilder { |
| 51 | |
| 52 | private static final String DRIVER_NAME = "ovs"; |
| 53 | |
| 54 | private Dpid dpid; |
| 55 | private List<OFBucket> ofBuckets; |
| 56 | private OFGroupType type; |
| 57 | private DriverService driverService; |
| 58 | |
| 59 | private final Logger log = getLogger(getClass()); |
| 60 | |
| 61 | |
| 62 | /** |
| 63 | * Creates a builder. |
| 64 | * |
| 65 | * @param dpid dpid |
| 66 | * @param ofBuckets list of OFBucket |
| 67 | * @param type Group type |
| 68 | * @param driverService driver service |
| 69 | */ |
| 70 | public OFAgentVirtualGroupBucketEntryBuilder(Dpid dpid, List<OFBucket> ofBuckets, OFGroupType type, |
| 71 | DriverService driverService) { |
| 72 | this.dpid = dpid; |
| 73 | this.ofBuckets = ofBuckets; |
| 74 | this.type = type; |
| 75 | this.driverService = driverService; |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Builds a GroupBuckets. |
| 80 | * |
| 81 | * @return GroupBuckets object, a list of GroupBuckets |
| 82 | */ |
| 83 | public GroupBuckets build() { |
| 84 | List<GroupBucket> bucketList = Lists.newArrayList(); |
| 85 | |
| 86 | for (OFBucket bucket: ofBuckets) { |
| 87 | TrafficTreatment treatment = buildTreatment(bucket.getActions()); |
| 88 | // TODO: Use GroupBucketEntry |
| 89 | GroupBucket groupBucket = null; |
| 90 | switch (type) { |
| 91 | case INDIRECT: |
| 92 | groupBucket = |
| 93 | DefaultGroupBucket.createIndirectGroupBucket(treatment); |
| 94 | break; |
| 95 | case SELECT: |
| 96 | groupBucket = |
| 97 | DefaultGroupBucket.createSelectGroupBucket(treatment, (short) bucket.getWeight()); |
| 98 | break; |
| 99 | case FF: |
| 100 | PortNumber port = |
| 101 | PortNumber.portNumber(bucket.getWatchPort().getPortNumber()); |
| 102 | GroupId groupId = |
| 103 | new GroupId(bucket.getWatchGroup().getGroupNumber()); |
| 104 | groupBucket = |
| 105 | DefaultGroupBucket.createFailoverGroupBucket(treatment, |
| 106 | port, groupId); |
| 107 | break; |
| 108 | case ALL: |
| 109 | groupBucket = |
| 110 | DefaultGroupBucket.createAllGroupBucket(treatment); |
| 111 | break; |
| 112 | default: |
| 113 | log.error("Unsupported Group type : {}", type); |
| 114 | } |
| 115 | if (groupBucket != null) { |
| 116 | bucketList.add(groupBucket); |
| 117 | } |
| 118 | } |
| 119 | return new GroupBuckets(bucketList); |
| 120 | } |
| 121 | |
| 122 | private TrafficTreatment buildTreatment(List<OFAction> actions) { |
| 123 | DriverHandler driverHandler = getDriver(dpid); |
| 124 | TrafficTreatment.Builder builder = DefaultTrafficTreatment.builder(); |
| 125 | |
| 126 | // If this is a drop rule |
| 127 | if (actions.isEmpty()) { |
| 128 | builder.drop(); |
| 129 | return builder.build(); |
| 130 | } |
| 131 | |
| 132 | return FlowEntryBuilder.configureTreatmentBuilder(actions, builder, |
| 133 | driverHandler, DeviceId.deviceId(Dpid.uri(dpid))).build(); |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Retrieves the driver handler for the specified device. |
| 138 | * |
| 139 | * @param dpid datapath identifier |
| 140 | * @return driver handler |
| 141 | */ |
| 142 | protected DriverHandler getDriver(Dpid dpid) { |
| 143 | DeviceId devId = DeviceId.deviceId(Dpid.uri(dpid)); |
| 144 | log.debug("running getDriver for {}", devId); |
| 145 | Driver driver = driverService.getDriver(DRIVER_NAME); |
| 146 | DriverHandler handler = new DefaultDriverHandler(new DefaultDriverData(driver, devId)); |
| 147 | return handler; |
| 148 | } |
| 149 | |
| 150 | } |