blob: aa2b90fce1063c87ba90178cbc635d807a842990 [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) {
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}