blob: cb2b864c029269b1b5df12830c093ee77ab0cb50 [file] [log] [blame]
Jian Li2cc2b632019-02-18 00:56:40 +09001/*
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 */
16package org.onosproject.k8snetworking.impl;
17
18import org.onosproject.core.ApplicationId;
19import org.onosproject.core.CoreService;
20import org.onosproject.k8snetworking.api.K8sGroupRuleService;
21import org.onosproject.net.DeviceId;
22import org.onosproject.net.group.DefaultGroupDescription;
23import org.onosproject.net.group.GroupBucket;
24import org.onosproject.net.group.GroupBuckets;
25import org.onosproject.net.group.GroupDescription;
26import org.onosproject.net.group.GroupDescription.Type;
27import org.onosproject.net.group.GroupService;
28import org.osgi.service.component.annotations.Activate;
29import org.osgi.service.component.annotations.Component;
30import org.osgi.service.component.annotations.Deactivate;
31import org.osgi.service.component.annotations.Reference;
32import org.osgi.service.component.annotations.ReferenceCardinality;
33import org.slf4j.Logger;
34
35import java.util.List;
36
37import static org.onosproject.k8snetworking.api.Constants.K8S_NETWORKING_APP_ID;
38import static org.onosproject.k8snetworking.util.K8sNetworkingUtil.getGroupKey;
39import static org.slf4j.LoggerFactory.getLogger;
40
41/**
42 * Sets group table rules directly using GroupService.
43 */
44@Component(immediate = true, service = K8sGroupRuleService.class)
45public 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 Li2cc2b632019-02-18 00:56:40 +090073
74 if (install) {
Jian Li4a7ce672019-04-09 15:20:25 +090075 GroupDescription groupDesc = new DefaultGroupDescription(deviceId,
76 type, new GroupBuckets(buckets), getGroupKey(groupId), groupId, appId);
Jian Li2cc2b632019-02-18 00:56:40 +090077 groupService.addGroup(groupDesc);
78 } else {
79 groupService.removeGroup(deviceId, getGroupKey(groupId), appId);
80 }
81 }
Jian Li4a7ce672019-04-09 15:20:25 +090082
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 Libf562c22019-04-15 18:07:14 +0900100
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 Li2cc2b632019-02-18 00:56:40 +0900107}