blob: a75da25745fc1d96ec9104cec079a4efdefcb8a7 [file] [log] [blame]
HIGUCHI Yuta1d7c9cb2016-01-20 18:22:36 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
HIGUCHI Yuta1d7c9cb2016-01-20 18:22:36 -08003 *
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 */
Sho SHIMIZUe18cb122016-02-22 21:04:56 -080016package org.onosproject.net.resource.impl;
HIGUCHI Yuta1d7c9cb2016-01-20 18:22:36 -080017
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;
Sho SHIMIZUe18cb122016-02-22 21:04:56 -080031import org.onosproject.net.resource.BandwidthCapacity;
32import org.onosproject.net.resource.ResourceAdminService;
33import org.onosproject.net.resource.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}
Ray Milkeyd7909ca2016-03-04 08:46:13 -080063 * @param mastershipService {@link MastershipService}
HIGUCHI Yuta1d7c9cb2016-01-20 18:22:36 -080064 * @param executor Executor to use.
65 */
66 ResourceNetworkConfigListener(ResourceAdminService adminService, NetworkConfigService cfgService,
Sho SHIMIZU82b9d172016-02-24 16:53:59 -080067 MastershipService mastershipService, ExecutorService executor) {
HIGUCHI Yuta1d7c9cb2016-01-20 18:22:36 -080068 this.adminService = checkNotNull(adminService);
69 this.cfgService = checkNotNull(cfgService);
Sho SHIMIZU82b9d172016-02-24 16:53:59 -080070 this.mastershipService = checkNotNull(mastershipService);
HIGUCHI Yuta1d7c9cb2016-01-20 18:22:36 -080071 this.executor = checkNotNull(executor);
72 }
73
74 @Override
75 public boolean isRelevant(NetworkConfigEvent event) {
HIGUCHI Yuta86523892016-02-18 16:33:11 -080076 switch (event.type()) {
77 case CONFIG_ADDED:
78 case CONFIG_REMOVED:
79 case CONFIG_UPDATED:
80 return CONFIG_CLASSES.contains(event.configClass());
81
82 case CONFIG_REGISTERED:
83 case CONFIG_UNREGISTERED:
84 default:
85 return false;
86 }
HIGUCHI Yuta1d7c9cb2016-01-20 18:22:36 -080087 }
88
89 @Override
90 public void event(NetworkConfigEvent event) {
91 if (event.configClass() == BandwidthCapacity.class) {
HIGUCHI Yuta060da9a2016-03-11 19:16:35 -080092 executor.execute(() -> {
HIGUCHI Yuta1d7c9cb2016-01-20 18:22:36 -080093 try {
94 handleBandwidthCapacity(event);
95 } catch (Exception e) {
96 log.error("Exception handling BandwidthCapacity", e);
97 }
98 });
99 }
100 }
101
102 private void handleBandwidthCapacity(NetworkConfigEvent event) {
103 checkArgument(event.configClass() == BandwidthCapacity.class);
104
105 ConnectPoint cp = (ConnectPoint) event.subject();
Sho SHIMIZU82b9d172016-02-24 16:53:59 -0800106 if (!mastershipService.isLocalMaster(cp.deviceId())) {
107 return;
108 }
109
HIGUCHI Yuta1d7c9cb2016-01-20 18:22:36 -0800110 BandwidthCapacity bwCapacity = cfgService.getConfig(cp, BandwidthCapacity.class);
111
112 switch (event.type()) {
113 case CONFIG_ADDED:
Sho SHIMIZU7b326972016-02-09 15:53:39 -0800114 if (!adminService.register(Resources.continuous(cp.deviceId(),
Sho SHIMIZU460b9722016-01-28 10:48:26 -0800115 cp.port(), Bandwidth.class)
Sho SHIMIZUf95b96e2016-01-25 19:35:15 -0800116 .resource(bwCapacity.capacity().bps()))) {
HIGUCHI Yuta1d7c9cb2016-01-20 18:22:36 -0800117 log.info("Failed to register Bandwidth for {}, attempting update", cp);
118
119 // Bandwidth based on port speed, was probably already registered.
120 // need to update to the valued based on configuration
121
122 if (!updateRegistration(cp, bwCapacity)) {
123 log.warn("Failed to update Bandwidth for {}", cp);
124 }
125 }
126 break;
127
128 case CONFIG_UPDATED:
129 if (!updateRegistration(cp, bwCapacity)) {
130 log.warn("Failed to update Bandwidth for {}", cp);
131 }
132 break;
133
134 case CONFIG_REMOVED:
135 // FIXME Following should be an update to the value based on port speed
Sho SHIMIZU7b326972016-02-09 15:53:39 -0800136 if (!adminService.unregister(Resources.continuous(cp.deviceId(),
Sho SHIMIZU460b9722016-01-28 10:48:26 -0800137 cp.port(),
Sho SHIMIZU72f81b12016-02-09 09:26:17 -0800138 Bandwidth.class).id())) {
HIGUCHI Yuta1d7c9cb2016-01-20 18:22:36 -0800139 log.warn("Failed to unregister Bandwidth for {}", cp);
140 }
141 break;
142
143 case CONFIG_REGISTERED:
144 case CONFIG_UNREGISTERED:
145 // no-op
146 break;
147
148 default:
149 break;
150 }
151 }
152
153 private boolean updateRegistration(ConnectPoint cp, BandwidthCapacity bwCapacity) {
154 // FIXME workaround until replace/update semantics become available
155 // this potentially blows up existing registration
156 // or end up as no-op
157 //
158 // Current code end up in situation like below:
159 // PortNumber: 2
160 // MplsLabel: [[16‥240)]
161 // VlanId: [[0‥4095)]
162 // Bandwidth: 2000000.000000
163 // Bandwidth: 20000000.000000
164 //
165 // but both unregisterResources(..) and registerResources(..)
166 // returns true (success)
167
Sho SHIMIZU7b326972016-02-09 15:53:39 -0800168 if (!adminService.unregister(
Sho SHIMIZU72f81b12016-02-09 09:26:17 -0800169 Resources.continuous(cp.deviceId(), cp.port(), Bandwidth.class).id())) {
HIGUCHI Yuta1d7c9cb2016-01-20 18:22:36 -0800170 log.warn("unregisterResources for {} failed", cp);
171 }
Sho SHIMIZU7b326972016-02-09 15:53:39 -0800172 return adminService.register(Resources.continuous(cp.deviceId(),
Sho SHIMIZU460b9722016-01-28 10:48:26 -0800173 cp.port(),
174 Bandwidth.class).resource(bwCapacity.capacity().bps()));
HIGUCHI Yuta1d7c9cb2016-01-20 18:22:36 -0800175 }
176
177}