blob: ed0b9b6a7a78944e66cbec9262b4db073d4d58d1 [file] [log] [blame]
Kyuhwi Choib0718212016-06-01 11:33:27 +09001/*
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
17package org.onosproject.scalablegateway.impl;
18
19import com.google.common.collect.Lists;
20import org.onlab.packet.Ip4Address;
21import org.onosproject.core.ApplicationId;
Hyunsun Moonf9a16ed2016-07-20 21:59:48 -070022import org.onosproject.core.DefaultGroupId;
23import org.onosproject.core.GroupId;
Kyuhwi Choib0718212016-06-01 11:33:27 +090024import org.onosproject.net.DeviceId;
25import org.onosproject.net.Port;
26import org.onosproject.net.PortNumber;
27import org.onosproject.net.behaviour.ExtensionTreatmentResolver;
28import org.onosproject.net.device.DeviceService;
29import org.onosproject.net.driver.DefaultDriverData;
30import org.onosproject.net.driver.DefaultDriverHandler;
31import org.onosproject.net.driver.Driver;
32import org.onosproject.net.driver.DriverHandler;
33import org.onosproject.net.driver.DriverService;
34import org.onosproject.net.flow.DefaultTrafficTreatment;
35import org.onosproject.net.flow.TrafficTreatment;
36import org.onosproject.net.flow.instructions.ExtensionPropertyException;
37import org.onosproject.net.flow.instructions.ExtensionTreatment;
38import org.onosproject.net.flow.instructions.ExtensionTreatmentType;
39import org.onosproject.net.group.DefaultGroupDescription;
40import org.onosproject.net.group.DefaultGroupKey;
Kyuhwi Choib0718212016-06-01 11:33:27 +090041import org.onosproject.net.group.GroupBucket;
42import org.onosproject.net.group.GroupBuckets;
43import org.onosproject.net.group.GroupDescription;
44import org.onosproject.net.group.GroupKey;
45import org.onosproject.net.group.GroupService;
46import org.onosproject.scalablegateway.api.GatewayNode;
47import org.slf4j.Logger;
48import org.slf4j.LoggerFactory;
49
50import java.util.List;
51
Hyunsun Moonf9a16ed2016-07-20 21:59:48 -070052import static org.onosproject.net.AnnotationKeys.PORT_NAME;
Kyuhwi Choib0718212016-06-01 11:33:27 +090053import static org.onosproject.net.group.DefaultGroupBucket.createSelectGroupBucket;
54
55/**
56 * Handles group generation request from ScalableGateway.
57 */
58public class SelectGroupHandler {
59
60 private final Logger log = LoggerFactory.getLogger(getClass());
61
62 private static final String TUNNEL_DESTINATION = "tunnelDst";
63 private static final String PORTNAME_PREFIX_TUNNEL = "vxlan";
Kyuhwi Choib0718212016-06-01 11:33:27 +090064
65 private final GroupService groupService;
66 private final DeviceService deviceService;
67 private final DriverService driverService;
68 private final ApplicationId appId;
69
70 /**
71 * Default constructor.
72 *
73 * @param targetGroupService group service
74 * @param targetDeviceService device service
75 * @param targetDriverService driver service
76 * @param appId application id for group service
77 */
78 public SelectGroupHandler(GroupService targetGroupService, DeviceService targetDeviceService,
79 DriverService targetDriverService, ApplicationId appId) {
80 groupService = targetGroupService;
81 deviceService = targetDeviceService;
82 driverService = targetDriverService;
83 this.appId = appId;
84 }
85
86 /**
87 * Creates select type group description according to given deviceId.
88 *
89 * @param srcDeviceId target device id for group description
90 * @param nodeList gateway node list for bucket action
91 * @return created select type group description
92 */
Hyunsun Moonf9a16ed2016-07-20 21:59:48 -070093 public GroupId createGatewayGroup(DeviceId srcDeviceId, List<GatewayNode> nodeList) {
Kyuhwi Choib0718212016-06-01 11:33:27 +090094 List<GroupBucket> bucketList = generateBucketsForSelectGroup(srcDeviceId, nodeList);
Hyunsun Moonf9a16ed2016-07-20 21:59:48 -070095 GroupId groupId = getGroupId(srcDeviceId);
96 GroupDescription groupDescription = new DefaultGroupDescription(
97 srcDeviceId,
98 GroupDescription.Type.SELECT,
99 new GroupBuckets(bucketList),
100 getGroupKey(srcDeviceId),
101 groupId.id(),
102 appId);
103
104 groupService.addGroup(groupDescription);
105 return groupId;
Kyuhwi Choib0718212016-06-01 11:33:27 +0900106 }
107
Hyunsun Moonf9a16ed2016-07-20 21:59:48 -0700108 /**
109 * Returns unique group key with supplied source device ID as a hash.
110 *
111 * @param srcDeviceId source device id
112 * @return group key
113 */
114 public GroupKey getGroupKey(DeviceId srcDeviceId) {
115 return new DefaultGroupKey(srcDeviceId.toString().getBytes());
116 }
Kyuhwi Choib0718212016-06-01 11:33:27 +0900117
Hyunsun Moonf9a16ed2016-07-20 21:59:48 -0700118 private GroupId getGroupId(DeviceId srcDeviceId) {
119 return new DefaultGroupId(srcDeviceId.toString().hashCode());
Kyuhwi Choib0718212016-06-01 11:33:27 +0900120 }
121
122 /**
123 * Updates groupBuckets in select type group.
124 *
Hyunsun Moonf9a16ed2016-07-20 21:59:48 -0700125 * @param deviceId target device id to update the group
Kyuhwi Choib0718212016-06-01 11:33:27 +0900126 * @param nodeList updated gateway node list for bucket action
Hyunsun Moonf9a16ed2016-07-20 21:59:48 -0700127 * @param isInsert update type(add or remove)
Kyuhwi Choib0718212016-06-01 11:33:27 +0900128 * @return result of process
129 */
Hyunsun Moonf9a16ed2016-07-20 21:59:48 -0700130 public void updateGatewayGroupBuckets(DeviceId deviceId,
131 List<GatewayNode> nodeList,
132 boolean isInsert) {
Kyuhwi Choib0718212016-06-01 11:33:27 +0900133 List<GroupBucket> bucketList = generateBucketsForSelectGroup(deviceId, nodeList);
Hyunsun Moonf9a16ed2016-07-20 21:59:48 -0700134 GroupKey groupKey = getGroupKey(deviceId);
135 if (isInsert) {
136 groupService.addBucketsToGroup(
137 deviceId,
138 groupKey,
139 new GroupBuckets(bucketList),
140 groupKey, appId);
Kyuhwi Choib0718212016-06-01 11:33:27 +0900141 } else {
Hyunsun Moonf9a16ed2016-07-20 21:59:48 -0700142 groupService.removeBucketsFromGroup(
143 deviceId,
144 groupKey,
145 new GroupBuckets(bucketList),
146 groupKey, appId);
Kyuhwi Choib0718212016-06-01 11:33:27 +0900147 }
Kyuhwi Choib0718212016-06-01 11:33:27 +0900148 }
149
150 private List<GroupBucket> generateBucketsForSelectGroup(DeviceId deviceId, List<GatewayNode> nodeList) {
151 List<GroupBucket> bucketList = Lists.newArrayList();
152 nodeList.forEach(node -> {
153 TrafficTreatment tBuilder = DefaultTrafficTreatment.builder()
154 .extension(buildNiciraExtenstion(deviceId, node.getDataIpAddress()), deviceId)
155 .setOutput(getTunnelPort(deviceId))
156 .build();
157 bucketList.add(createSelectGroupBucket(tBuilder));
158 });
159 return bucketList;
160 }
161
162 /**
163 * Builds Nicira extension for tagging remoteIp of vxlan.
164 *
165 * @param id device id of vxlan source device
166 * @param hostIp remote ip of vxlan destination device
167 * @return NiciraExtension Treatment
168 */
169 private ExtensionTreatment buildNiciraExtenstion(DeviceId id, Ip4Address hostIp) {
170 Driver driver = driverService.getDriver(id);
171 DriverHandler driverHandler = new DefaultDriverHandler(new DefaultDriverData(driver, id));
172 ExtensionTreatmentResolver resolver = driverHandler.behaviour(ExtensionTreatmentResolver.class);
173
174 ExtensionTreatment extensionInstruction =
175 resolver.getExtensionInstruction(
176 ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_SET_TUNNEL_DST.type());
177
178 try {
179 extensionInstruction.setPropertyValue(TUNNEL_DESTINATION, hostIp);
180 } catch (ExtensionPropertyException e) {
181 log.error("Error setting Nicira extension setting {}", e);
182 }
183
184 return extensionInstruction;
185 }
186
187 /**
188 * Returns port number of vxlan tunnel.
189 *
190 * @param deviceId target Device Id
191 * @return portNumber
192 */
193 private PortNumber getTunnelPort(DeviceId deviceId) {
194 Port port = deviceService.getPorts(deviceId).stream()
Hyunsun Moonf9a16ed2016-07-20 21:59:48 -0700195 .filter(p -> p.annotations().value(PORT_NAME).equals(PORTNAME_PREFIX_TUNNEL))
Kyuhwi Choib0718212016-06-01 11:33:27 +0900196 .findAny().orElse(null);
197
198 if (port == null) {
199 log.error("No TunnelPort was created.");
200 return null;
201 }
202 return port.number();
203
204 }
205}