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; |
| 22 | import org.onosproject.net.group.GroupBucket; |
| 23 | import org.onosproject.net.group.GroupBuckets; |
| 24 | import org.onosproject.net.group.GroupDescription; |
| 25 | import org.onosproject.net.group.GroupService; |
| 26 | import org.onosproject.openstacknetworking.api.OpenstackGroupRuleService; |
| 27 | import org.osgi.service.component.annotations.Activate; |
| 28 | import org.osgi.service.component.annotations.Component; |
| 29 | import org.osgi.service.component.annotations.Deactivate; |
| 30 | import org.osgi.service.component.annotations.Reference; |
| 31 | import org.osgi.service.component.annotations.ReferenceCardinality; |
| 32 | import org.slf4j.Logger; |
| 33 | |
| 34 | import java.util.List; |
| 35 | |
| 36 | import static org.onosproject.openstacknetworking.util.OpenstackNetworkingUtil.getGroupKey; |
| 37 | import static org.slf4j.LoggerFactory.getLogger; |
| 38 | |
| 39 | /** |
| 40 | * Sets group table rules directly using GroupService. |
| 41 | */ |
| 42 | @Component(immediate = true, service = OpenstackGroupRuleService.class) |
| 43 | public class OpenstackGroupRuleManager implements OpenstackGroupRuleService { |
| 44 | |
| 45 | private final Logger log = getLogger(getClass()); |
| 46 | |
| 47 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 48 | protected GroupService groupService; |
| 49 | |
| 50 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 51 | protected CoreService coreService; |
| 52 | |
| 53 | @Activate |
| 54 | protected void activate() { |
| 55 | log.info("Started"); |
| 56 | } |
| 57 | |
| 58 | @Deactivate |
| 59 | protected void deactivate() { |
| 60 | log.info("Stopped"); |
| 61 | } |
| 62 | |
| 63 | @Override |
| 64 | public void setRule(ApplicationId appId, DeviceId deviceId, int groupId, |
| 65 | GroupDescription.Type type, List<GroupBucket> buckets, |
| 66 | boolean install) { |
| 67 | if (install) { |
| 68 | GroupDescription groupDesc = new DefaultGroupDescription(deviceId, |
| 69 | type, new GroupBuckets(buckets), getGroupKey(groupId), groupId, appId); |
| 70 | groupService.addGroup(groupDesc); |
| 71 | log.info("Adding group rule {}", groupId); |
| 72 | } else { |
| 73 | groupService.removeGroup(deviceId, getGroupKey(groupId), appId); |
| 74 | log.info("Removing group rule {}", groupId); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | @Override |
| 79 | public boolean hasGroup(DeviceId deviceId, int groupId) { |
| 80 | return groupService.getGroup(deviceId, getGroupKey(groupId)) != null; |
| 81 | } |
| 82 | |
| 83 | @Override |
| 84 | public void setBuckets(ApplicationId appId, DeviceId deviceId, |
| 85 | int groupId, List<GroupBucket> buckets, boolean install) { |
| 86 | if (install) { |
| 87 | groupService.addBucketsToGroup(deviceId, getGroupKey(groupId), |
| 88 | new GroupBuckets(buckets), getGroupKey(groupId), appId); |
| 89 | log.info("Adding buckets for group rule {}", groupId); |
| 90 | } else { |
| 91 | groupService.removeBucketsFromGroup(deviceId, getGroupKey(groupId), |
| 92 | new GroupBuckets(buckets), getGroupKey(groupId), appId); |
| 93 | log.info("Removing buckets for group rule {}", groupId); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | @Override |
| 98 | public void setBuckets(ApplicationId appId, DeviceId deviceId, |
| 99 | int groupId, List<GroupBucket> buckets) { |
| 100 | groupService.setBucketsForGroup(deviceId, getGroupKey(groupId), |
| 101 | new GroupBuckets(buckets), getGroupKey(groupId), appId); |
| 102 | } |
| 103 | } |