blob: c7448c73e8f63dc809050d66e1a1232dfa9e2520 [file] [log] [blame]
HIGUCHI Yuta1d7c9cb2016-01-20 18:22:36 -08001/*
2 * Copyright 2016 Open Networking Laboratory
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.net.newresource.impl;
17
18import static com.google.common.base.Preconditions.checkArgument;
19import static com.google.common.base.Preconditions.checkNotNull;
HIGUCHI Yuta1d7c9cb2016-01-20 18:22:36 -080020import static org.slf4j.LoggerFactory.getLogger;
21
22import java.util.Set;
23import java.util.concurrent.ExecutorService;
24
25import org.onlab.util.Bandwidth;
Sho SHIMIZU82b9d172016-02-24 16:53:59 -080026import org.onosproject.mastership.MastershipService;
HIGUCHI Yuta1d7c9cb2016-01-20 18:22:36 -080027import org.onosproject.net.ConnectPoint;
28import org.onosproject.net.config.NetworkConfigEvent;
29import org.onosproject.net.config.NetworkConfigListener;
30import org.onosproject.net.config.NetworkConfigService;
31import org.onosproject.net.newresource.BandwidthCapacity;
32import org.onosproject.net.newresource.ResourceAdminService;
Sho SHIMIZU460b9722016-01-28 10:48:26 -080033import org.onosproject.net.newresource.Resources;
HIGUCHI Yuta1d7c9cb2016-01-20 18:22:36 -080034import org.slf4j.Logger;
35
36import com.google.common.annotations.Beta;
37import com.google.common.collect.ImmutableSet;
38
39// TODO Consider merging this with ResourceDeviceListener.
40/**
41 * Handler for NetworkConfiguration changes.
42 */
43@Beta
44final class ResourceNetworkConfigListener implements NetworkConfigListener {
45
46 /**
47 * Config classes relevant to this listener.
48 */
49 private static final Set<Class<?>> CONFIG_CLASSES = ImmutableSet.of(BandwidthCapacity.class);
50
51 private final Logger log = getLogger(getClass());
52
53 private final ResourceAdminService adminService;
54 private final NetworkConfigService cfgService;
Sho SHIMIZU82b9d172016-02-24 16:53:59 -080055 private final MastershipService mastershipService;
HIGUCHI Yuta1d7c9cb2016-01-20 18:22:36 -080056 private final ExecutorService executor;
57
58 /**
59 * Creates an instance of listener.
60 *
61 * @param adminService {@link ResourceAdminService}
62 * @param cfgService {@link NetworkConfigService}
63 * @param executor Executor to use.
64 */
65 ResourceNetworkConfigListener(ResourceAdminService adminService, NetworkConfigService cfgService,
Sho SHIMIZU82b9d172016-02-24 16:53:59 -080066 MastershipService mastershipService, ExecutorService executor) {
HIGUCHI Yuta1d7c9cb2016-01-20 18:22:36 -080067 this.adminService = checkNotNull(adminService);
68 this.cfgService = checkNotNull(cfgService);
Sho SHIMIZU82b9d172016-02-24 16:53:59 -080069 this.mastershipService = checkNotNull(mastershipService);
HIGUCHI Yuta1d7c9cb2016-01-20 18:22:36 -080070 this.executor = checkNotNull(executor);
71 }
72
73 @Override
74 public boolean isRelevant(NetworkConfigEvent event) {
HIGUCHI Yuta86523892016-02-18 16:33:11 -080075 switch (event.type()) {
76 case CONFIG_ADDED:
77 case CONFIG_REMOVED:
78 case CONFIG_UPDATED:
79 return CONFIG_CLASSES.contains(event.configClass());
80
81 case CONFIG_REGISTERED:
82 case CONFIG_UNREGISTERED:
83 default:
84 return false;
85 }
HIGUCHI Yuta1d7c9cb2016-01-20 18:22:36 -080086 }
87
88 @Override
89 public void event(NetworkConfigEvent event) {
90 if (event.configClass() == BandwidthCapacity.class) {
91 executor.submit(() -> {
92 try {
93 handleBandwidthCapacity(event);
94 } catch (Exception e) {
95 log.error("Exception handling BandwidthCapacity", e);
96 }
97 });
98 }
99 }
100
101 private void handleBandwidthCapacity(NetworkConfigEvent event) {
102 checkArgument(event.configClass() == BandwidthCapacity.class);
103
104 ConnectPoint cp = (ConnectPoint) event.subject();
Sho SHIMIZU82b9d172016-02-24 16:53:59 -0800105 if (!mastershipService.isLocalMaster(cp.deviceId())) {
106 return;
107 }
108
HIGUCHI Yuta1d7c9cb2016-01-20 18:22:36 -0800109 BandwidthCapacity bwCapacity = cfgService.getConfig(cp, BandwidthCapacity.class);
110
111 switch (event.type()) {
112 case CONFIG_ADDED:
Sho SHIMIZU7b326972016-02-09 15:53:39 -0800113 if (!adminService.register(Resources.continuous(cp.deviceId(),
Sho SHIMIZU460b9722016-01-28 10:48:26 -0800114 cp.port(), Bandwidth.class)
Sho SHIMIZUf95b96e2016-01-25 19:35:15 -0800115 .resource(bwCapacity.capacity().bps()))) {
HIGUCHI Yuta1d7c9cb2016-01-20 18:22:36 -0800116 log.info("Failed to register Bandwidth for {}, attempting update", cp);
117
118 // Bandwidth based on port speed, was probably already registered.
119 // need to update to the valued based on configuration
120
121 if (!updateRegistration(cp, bwCapacity)) {
122 log.warn("Failed to update Bandwidth for {}", cp);
123 }
124 }
125 break;
126
127 case CONFIG_UPDATED:
128 if (!updateRegistration(cp, bwCapacity)) {
129 log.warn("Failed to update Bandwidth for {}", cp);
130 }
131 break;
132
133 case CONFIG_REMOVED:
134 // FIXME Following should be an update to the value based on port speed
Sho SHIMIZU7b326972016-02-09 15:53:39 -0800135 if (!adminService.unregister(Resources.continuous(cp.deviceId(),
Sho SHIMIZU460b9722016-01-28 10:48:26 -0800136 cp.port(),
Sho SHIMIZU72f81b12016-02-09 09:26:17 -0800137 Bandwidth.class).id())) {
HIGUCHI Yuta1d7c9cb2016-01-20 18:22:36 -0800138 log.warn("Failed to unregister Bandwidth for {}", cp);
139 }
140 break;
141
142 case CONFIG_REGISTERED:
143 case CONFIG_UNREGISTERED:
144 // no-op
145 break;
146
147 default:
148 break;
149 }
150 }
151
152 private boolean updateRegistration(ConnectPoint cp, BandwidthCapacity bwCapacity) {
153 // FIXME workaround until replace/update semantics become available
154 // this potentially blows up existing registration
155 // or end up as no-op
156 //
157 // Current code end up in situation like below:
158 // PortNumber: 2
159 // MplsLabel: [[16‥240)]
160 // VlanId: [[0‥4095)]
161 // Bandwidth: 2000000.000000
162 // Bandwidth: 20000000.000000
163 //
164 // but both unregisterResources(..) and registerResources(..)
165 // returns true (success)
166
Sho SHIMIZU7b326972016-02-09 15:53:39 -0800167 if (!adminService.unregister(
Sho SHIMIZU72f81b12016-02-09 09:26:17 -0800168 Resources.continuous(cp.deviceId(), cp.port(), Bandwidth.class).id())) {
HIGUCHI Yuta1d7c9cb2016-01-20 18:22:36 -0800169 log.warn("unregisterResources for {} failed", cp);
170 }
Sho SHIMIZU7b326972016-02-09 15:53:39 -0800171 return adminService.register(Resources.continuous(cp.deviceId(),
Sho SHIMIZU460b9722016-01-28 10:48:26 -0800172 cp.port(),
173 Bandwidth.class).resource(bwCapacity.capacity().bps()));
HIGUCHI Yuta1d7c9cb2016-01-20 18:22:36 -0800174 }
175
176}