blob: 1f2ae5556a92e79dd04bc602a8fe67f549e7fe40 [file] [log] [blame]
Jian Lie87c2712019-09-11 11:15:16 +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.openstacknetworking.impl;
17
18import org.onosproject.core.ApplicationId;
19import org.onosproject.core.CoreService;
20import org.onosproject.net.DeviceId;
21import org.onosproject.net.group.DefaultGroupDescription;
Jian Licf187132020-07-14 00:05:24 +090022import org.onosproject.net.group.Group;
Jian Lie87c2712019-09-11 11:15:16 +090023import org.onosproject.net.group.GroupBucket;
24import org.onosproject.net.group.GroupBuckets;
25import org.onosproject.net.group.GroupDescription;
26import org.onosproject.net.group.GroupService;
27import org.onosproject.openstacknetworking.api.OpenstackGroupRuleService;
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.openstacknetworking.util.OpenstackNetworkingUtil.getGroupKey;
38import static org.slf4j.LoggerFactory.getLogger;
39
40/**
41 * Sets group table rules directly using GroupService.
42 */
43@Component(immediate = true, service = OpenstackGroupRuleService.class)
44public class OpenstackGroupRuleManager implements OpenstackGroupRuleService {
45
46 private final Logger log = getLogger(getClass());
47
48 @Reference(cardinality = ReferenceCardinality.MANDATORY)
49 protected GroupService groupService;
50
51 @Reference(cardinality = ReferenceCardinality.MANDATORY)
52 protected CoreService coreService;
53
54 @Activate
55 protected void activate() {
56 log.info("Started");
57 }
58
59 @Deactivate
60 protected void deactivate() {
61 log.info("Stopped");
62 }
63
64 @Override
65 public void setRule(ApplicationId appId, DeviceId deviceId, int groupId,
66 GroupDescription.Type type, List<GroupBucket> buckets,
67 boolean install) {
Jian Licf187132020-07-14 00:05:24 +090068 Group group = groupService.getGroup(deviceId, getGroupKey(groupId));
Jian Lie87c2712019-09-11 11:15:16 +090069 if (install) {
Jian Licf187132020-07-14 00:05:24 +090070 if (group == null) {
71 GroupDescription groupDesc = new DefaultGroupDescription(deviceId,
72 type, new GroupBuckets(buckets), getGroupKey(groupId), groupId, appId);
73 groupService.addGroup(groupDesc);
74 log.debug("Adding group table rule {}", groupId);
75 }
Jian Lie87c2712019-09-11 11:15:16 +090076 } else {
Jian Licf187132020-07-14 00:05:24 +090077 if (group != null) {
78 groupService.removeGroup(deviceId, getGroupKey(groupId), appId);
79 log.debug("Removing group table rule {}", groupId);
80 }
Jian Lie87c2712019-09-11 11:15:16 +090081 }
82 }
83
84 @Override
85 public boolean hasGroup(DeviceId deviceId, int groupId) {
86 return groupService.getGroup(deviceId, getGroupKey(groupId)) != null;
87 }
88
89 @Override
90 public void setBuckets(ApplicationId appId, DeviceId deviceId,
91 int groupId, List<GroupBucket> buckets, boolean install) {
Jian Licf187132020-07-14 00:05:24 +090092 if (!hasGroup(deviceId, groupId)) {
93 return;
94 }
Jian Lie87c2712019-09-11 11:15:16 +090095 if (install) {
Jian Licf187132020-07-14 00:05:24 +090096 // we add the buckets into the group, only if the buckets do not exist
97 // in the given group
98 Group group = groupService.getGroup(deviceId, getGroupKey(groupId));
99 if (group.buckets() != null && !group.buckets().buckets().containsAll(buckets)) {
100 groupService.addBucketsToGroup(deviceId, getGroupKey(groupId),
101 new GroupBuckets(buckets), getGroupKey(groupId), appId);
102 log.debug("Adding buckets for group rule {}", groupId);
103 }
Jian Lie87c2712019-09-11 11:15:16 +0900104 } else {
105 groupService.removeBucketsFromGroup(deviceId, getGroupKey(groupId),
106 new GroupBuckets(buckets), getGroupKey(groupId), appId);
Jian Licf187132020-07-14 00:05:24 +0900107 log.debug("Removing buckets for group rule {}", groupId);
Jian Lie87c2712019-09-11 11:15:16 +0900108 }
109 }
110
111 @Override
112 public void setBuckets(ApplicationId appId, DeviceId deviceId,
113 int groupId, List<GroupBucket> buckets) {
114 groupService.setBucketsForGroup(deviceId, getGroupKey(groupId),
115 new GroupBuckets(buckets), getGroupKey(groupId), appId);
116 }
117}