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