Jian Li | 2cc2b63 | 2019-02-18 00:56:40 +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.k8snetworking.impl; |
| 17 | |
| 18 | import org.onosproject.core.ApplicationId; |
| 19 | import org.onosproject.core.CoreService; |
| 20 | import org.onosproject.k8snetworking.api.K8sGroupRuleService; |
| 21 | import org.onosproject.net.DeviceId; |
| 22 | import org.onosproject.net.group.DefaultGroupDescription; |
| 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.GroupDescription.Type; |
| 27 | import org.onosproject.net.group.GroupService; |
| 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.k8snetworking.api.Constants.K8S_NETWORKING_APP_ID; |
| 38 | import static org.onosproject.k8snetworking.util.K8sNetworkingUtil.getGroupKey; |
| 39 | import static org.slf4j.LoggerFactory.getLogger; |
| 40 | |
| 41 | /** |
| 42 | * Sets group table rules directly using GroupService. |
| 43 | */ |
| 44 | @Component(immediate = true, service = K8sGroupRuleService.class) |
| 45 | public class K8sGroupRuleManager implements K8sGroupRuleService { |
| 46 | |
| 47 | private final Logger log = getLogger(getClass()); |
| 48 | |
| 49 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 50 | protected GroupService groupService; |
| 51 | |
| 52 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 53 | protected CoreService coreService; |
| 54 | |
| 55 | private ApplicationId appId; |
| 56 | |
| 57 | @Activate |
| 58 | protected void activate() { |
| 59 | appId = coreService.registerApplication(K8S_NETWORKING_APP_ID); |
| 60 | coreService.registerApplication(K8S_NETWORKING_APP_ID); |
| 61 | |
| 62 | log.info("Started"); |
| 63 | } |
| 64 | |
| 65 | @Deactivate |
| 66 | protected void deactivate() { |
| 67 | log.info("Stopped"); |
| 68 | } |
| 69 | |
| 70 | @Override |
| 71 | public void setRule(ApplicationId appId, DeviceId deviceId, int groupId, |
| 72 | Type type, List<GroupBucket> buckets, boolean install) { |
Jian Li | 2cc2b63 | 2019-02-18 00:56:40 +0900 | [diff] [blame] | 73 | |
| 74 | if (install) { |
Jian Li | 4a7ce67 | 2019-04-09 15:20:25 +0900 | [diff] [blame] | 75 | GroupDescription groupDesc = new DefaultGroupDescription(deviceId, |
| 76 | type, new GroupBuckets(buckets), getGroupKey(groupId), groupId, appId); |
Jian Li | 2cc2b63 | 2019-02-18 00:56:40 +0900 | [diff] [blame] | 77 | groupService.addGroup(groupDesc); |
| 78 | } else { |
| 79 | groupService.removeGroup(deviceId, getGroupKey(groupId), appId); |
| 80 | } |
| 81 | } |
Jian Li | 4a7ce67 | 2019-04-09 15:20:25 +0900 | [diff] [blame] | 82 | |
| 83 | @Override |
| 84 | public boolean hasGroup(DeviceId deviceId, int groupId) { |
| 85 | return groupService.getGroup(deviceId, getGroupKey(groupId)) != null; |
| 86 | } |
| 87 | |
| 88 | @Override |
| 89 | public void setBuckets(ApplicationId appId, DeviceId deviceId, int groupId, |
| 90 | List<GroupBucket> buckets, boolean install) { |
| 91 | |
| 92 | if (install) { |
| 93 | groupService.addBucketsToGroup(deviceId, getGroupKey(groupId), |
| 94 | new GroupBuckets(buckets), getGroupKey(groupId), appId); |
| 95 | } else { |
| 96 | groupService.removeBucketsFromGroup(deviceId, getGroupKey(groupId), |
| 97 | new GroupBuckets(buckets), getGroupKey(groupId), appId); |
| 98 | } |
| 99 | } |
Jian Li | bf562c2 | 2019-04-15 18:07:14 +0900 | [diff] [blame] | 100 | |
| 101 | @Override |
| 102 | public void setBuckets(ApplicationId appId, DeviceId deviceId, int groupId, |
| 103 | List<GroupBucket> buckets) { |
| 104 | groupService.setBucketsForGroup(deviceId, getGroupKey(groupId), |
| 105 | new GroupBuckets(buckets), getGroupKey(groupId), appId); |
| 106 | } |
Jian Li | 2cc2b63 | 2019-02-18 00:56:40 +0900 | [diff] [blame] | 107 | } |