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) { |
| 73 | GroupDescription groupDesc = new DefaultGroupDescription(deviceId, |
| 74 | type, new GroupBuckets(buckets), getGroupKey(groupId), groupId, appId); |
| 75 | |
| 76 | if (install) { |
| 77 | groupService.addGroup(groupDesc); |
| 78 | } else { |
| 79 | groupService.removeGroup(deviceId, getGroupKey(groupId), appId); |
| 80 | } |
| 81 | } |
| 82 | } |