blob: 3cf175cf980bc259837049b5da93f014f60ece4a [file] [log] [blame]
Yi Tseng3dca0f82018-04-03 14:33:03 +08001/*
2 * Copyright 2017-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.provider.nil;
17
18import io.netty.util.Timeout;
19import io.netty.util.TimerTask;
20import org.onlab.util.Timer;
21import org.onosproject.core.GroupId;
22import org.onosproject.net.DeviceId;
23import org.onosproject.net.group.DefaultGroup;
24import org.onosproject.net.group.Group;
25import org.onosproject.net.group.GroupOperation;
26import org.onosproject.net.group.GroupOperations;
27import org.onosproject.net.group.GroupProvider;
28import org.onosproject.net.group.GroupProviderService;
29import org.slf4j.Logger;
30
31import java.util.Collection;
32import java.util.Map;
33import java.util.concurrent.ConcurrentHashMap;
34import java.util.concurrent.TimeUnit;
35
36import static org.slf4j.LoggerFactory.getLogger;
37
38/**
39 * Null provider to accept any group and report them.
40 */
41public class NullGroupProvider extends NullProviders.AbstractNullProvider
42 implements GroupProvider {
43
44 private static final long DEFAULT_POLL_DELAY = 1;
45
46 private final Logger log = getLogger(getClass());
47 private final Map<DeviceId, Map<GroupId, Group>> groupTables = new ConcurrentHashMap<>();
48 private GroupProviderService providerService;
49 private Timeout timeout;
50
51 /**
52 * Start group provider.
53 *
54 * @param providerService group provider service
55 */
56 void start(GroupProviderService providerService) {
57 this.providerService = providerService;
58 timeout = Timer.newTimeout(new StatisticTask(), DEFAULT_POLL_DELAY, TimeUnit.SECONDS);
59 }
60
61 /**
62 * Stops the group provider simulation.
63 */
64 void stop() {
65 timeout.cancel();
66 }
67
68 public void initDevicesGroupTable(Collection<DeviceId> deviceIds) {
69 // Initialize group table for the device
70 deviceIds.forEach(deviceId -> groupTables.put(deviceId, new ConcurrentHashMap<>()));
71 pushGroupMetrics();
72 }
73
74 @Override
75 public void performGroupOperation(DeviceId deviceId, GroupOperations groupOps) {
76 groupOps.operations().forEach(go -> {
77 switch (go.opType()) {
78 case ADD:
79 groupAdd(deviceId, go);
80 break;
81 case MODIFY:
82 groupModify(deviceId, go);
83 break;
84 case DELETE:
85 groupDelete(deviceId, go);
86 break;
87 default:
88 log.warn("Unsupported op type: {}", go.opType());
89 break;
90 }
91 });
92 }
93
94 private void groupAdd(DeviceId deviceId, GroupOperation go) {
95 GroupId gid = go.groupId();
96 DefaultGroup group = new DefaultGroup(gid, deviceId, go.groupType(), go.buckets());
97 group.setState(Group.GroupState.ADDED);
98 groupTables.get(deviceId).put(gid, group);
99 }
100
101 private void groupModify(DeviceId deviceId, GroupOperation go) {
102 groupTables.get(deviceId).computeIfPresent(go.groupId(), (gid, group) -> {
103 DefaultGroup grp = new DefaultGroup(gid, deviceId, go.groupType(), go.buckets());
104 grp.setState(Group.GroupState.ADDED);
105 return grp;
106 });
107 }
108
109 private void groupDelete(DeviceId deviceId, GroupOperation go) {
110 groupTables.get(deviceId).remove(go.groupId());
111 }
112
113 private void pushGroupMetrics() {
114 groupTables.forEach((deviceId, groups) -> {
115 providerService.pushGroupMetrics(deviceId, groups.values());
116 });
117 }
118
119 // Periodically reports flow rule statistics.
120 private class StatisticTask implements TimerTask {
121 @Override
122 public void run(Timeout to) {
123 pushGroupMetrics();
124 timeout = to.timer().newTimeout(to.task(), DEFAULT_POLL_DELAY, TimeUnit.SECONDS);
125 }
126 }
127}