Jian Li | e87c271 | 2019-09-11 11:15:16 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019-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.openstacknetworking.impl; |
| 17 | |
| 18 | import org.onosproject.core.ApplicationId; |
| 19 | import org.onosproject.core.CoreService; |
| 20 | import org.onosproject.net.DeviceId; |
| 21 | import org.onosproject.net.group.DefaultGroupDescription; |
Jian Li | cf18713 | 2020-07-14 00:05:24 +0900 | [diff] [blame] | 22 | import org.onosproject.net.group.Group; |
Jian Li | e87c271 | 2019-09-11 11:15:16 +0900 | [diff] [blame] | 23 | import org.onosproject.net.group.GroupBucket; |
| 24 | import org.onosproject.net.group.GroupBuckets; |
| 25 | import org.onosproject.net.group.GroupDescription; |
| 26 | import org.onosproject.net.group.GroupService; |
| 27 | import org.onosproject.openstacknetworking.api.OpenstackGroupRuleService; |
| 28 | import org.osgi.service.component.annotations.Activate; |
| 29 | import org.osgi.service.component.annotations.Component; |
| 30 | import org.osgi.service.component.annotations.Deactivate; |
| 31 | import org.osgi.service.component.annotations.Reference; |
| 32 | import org.osgi.service.component.annotations.ReferenceCardinality; |
| 33 | import org.slf4j.Logger; |
| 34 | |
| 35 | import java.util.List; |
| 36 | |
| 37 | import static org.onosproject.openstacknetworking.util.OpenstackNetworkingUtil.getGroupKey; |
| 38 | import static org.slf4j.LoggerFactory.getLogger; |
| 39 | |
| 40 | /** |
| 41 | * Sets group table rules directly using GroupService. |
| 42 | */ |
| 43 | @Component(immediate = true, service = OpenstackGroupRuleService.class) |
| 44 | public class OpenstackGroupRuleManager implements OpenstackGroupRuleService { |
| 45 | |
| 46 | private final Logger log = getLogger(getClass()); |
| 47 | |
| 48 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 49 | protected GroupService groupService; |
| 50 | |
| 51 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 52 | protected CoreService coreService; |
| 53 | |
| 54 | @Activate |
| 55 | protected void activate() { |
| 56 | log.info("Started"); |
| 57 | } |
| 58 | |
| 59 | @Deactivate |
| 60 | protected void deactivate() { |
| 61 | log.info("Stopped"); |
| 62 | } |
| 63 | |
| 64 | @Override |
| 65 | public void setRule(ApplicationId appId, DeviceId deviceId, int groupId, |
| 66 | GroupDescription.Type type, List<GroupBucket> buckets, |
| 67 | boolean install) { |
Jian Li | cf18713 | 2020-07-14 00:05:24 +0900 | [diff] [blame] | 68 | Group group = groupService.getGroup(deviceId, getGroupKey(groupId)); |
Jian Li | e87c271 | 2019-09-11 11:15:16 +0900 | [diff] [blame] | 69 | if (install) { |
Jian Li | cf18713 | 2020-07-14 00:05:24 +0900 | [diff] [blame] | 70 | if (group == null) { |
| 71 | GroupDescription groupDesc = new DefaultGroupDescription(deviceId, |
| 72 | type, new GroupBuckets(buckets), getGroupKey(groupId), groupId, appId); |
| 73 | groupService.addGroup(groupDesc); |
| 74 | log.debug("Adding group table rule {}", groupId); |
| 75 | } |
Jian Li | e87c271 | 2019-09-11 11:15:16 +0900 | [diff] [blame] | 76 | } else { |
Jian Li | cf18713 | 2020-07-14 00:05:24 +0900 | [diff] [blame] | 77 | if (group != null) { |
| 78 | groupService.removeGroup(deviceId, getGroupKey(groupId), appId); |
| 79 | log.debug("Removing group table rule {}", groupId); |
| 80 | } |
Jian Li | e87c271 | 2019-09-11 11:15:16 +0900 | [diff] [blame] | 81 | } |
| 82 | } |
| 83 | |
| 84 | @Override |
| 85 | public boolean hasGroup(DeviceId deviceId, int groupId) { |
| 86 | return groupService.getGroup(deviceId, getGroupKey(groupId)) != null; |
| 87 | } |
| 88 | |
| 89 | @Override |
| 90 | public void setBuckets(ApplicationId appId, DeviceId deviceId, |
| 91 | int groupId, List<GroupBucket> buckets, boolean install) { |
Jian Li | cf18713 | 2020-07-14 00:05:24 +0900 | [diff] [blame] | 92 | if (!hasGroup(deviceId, groupId)) { |
| 93 | return; |
| 94 | } |
Jian Li | e87c271 | 2019-09-11 11:15:16 +0900 | [diff] [blame] | 95 | if (install) { |
Jian Li | cf18713 | 2020-07-14 00:05:24 +0900 | [diff] [blame] | 96 | // we add the buckets into the group, only if the buckets do not exist |
| 97 | // in the given group |
| 98 | Group group = groupService.getGroup(deviceId, getGroupKey(groupId)); |
| 99 | if (group.buckets() != null && !group.buckets().buckets().containsAll(buckets)) { |
| 100 | groupService.addBucketsToGroup(deviceId, getGroupKey(groupId), |
| 101 | new GroupBuckets(buckets), getGroupKey(groupId), appId); |
| 102 | log.debug("Adding buckets for group rule {}", groupId); |
| 103 | } |
Jian Li | e87c271 | 2019-09-11 11:15:16 +0900 | [diff] [blame] | 104 | } else { |
| 105 | groupService.removeBucketsFromGroup(deviceId, getGroupKey(groupId), |
| 106 | new GroupBuckets(buckets), getGroupKey(groupId), appId); |
Jian Li | cf18713 | 2020-07-14 00:05:24 +0900 | [diff] [blame] | 107 | log.debug("Removing buckets for group rule {}", groupId); |
Jian Li | e87c271 | 2019-09-11 11:15:16 +0900 | [diff] [blame] | 108 | } |
| 109 | } |
| 110 | |
| 111 | @Override |
| 112 | public void setBuckets(ApplicationId appId, DeviceId deviceId, |
| 113 | int groupId, List<GroupBucket> buckets) { |
| 114 | groupService.setBucketsForGroup(deviceId, getGroupKey(groupId), |
| 115 | new GroupBuckets(buckets), getGroupKey(groupId), appId); |
| 116 | } |
| 117 | } |