blob: f837546be484a7ec1432591afd1798d2a97e4815 [file] [log] [blame]
Kyuhwi Choic5b33ea2016-04-26 11:45:32 +09001/*
2 * Copyright 2016-present 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 */
16
Hyunsun Moon71701292016-05-09 12:00:54 -070017package org.onosproject.scalablegateway.impl;
Kyuhwi Choic5b33ea2016-04-26 11:45:32 +090018
Kyuhwi Choidc2973b2016-05-13 14:54:31 +090019import com.google.common.collect.Lists;
20import com.google.common.collect.Maps;
21import org.apache.felix.scr.annotations.Activate;
22import org.apache.felix.scr.annotations.Component;
23import org.apache.felix.scr.annotations.Deactivate;
24
25import org.apache.felix.scr.annotations.Reference;
26import org.apache.felix.scr.annotations.ReferenceCardinality;
27import org.apache.felix.scr.annotations.Service;
28import org.onosproject.core.ApplicationId;
29import org.onosproject.core.CoreService;
30
Kyuhwi Choic5b33ea2016-04-26 11:45:32 +090031import org.onosproject.core.GroupId;
32import org.onosproject.net.DeviceId;
Kyuhwi Choidc2973b2016-05-13 14:54:31 +090033import org.onosproject.net.Port;
Kyuhwi Choic5b33ea2016-04-26 11:45:32 +090034import org.onosproject.net.PortNumber;
Kyuhwi Choidc2973b2016-05-13 14:54:31 +090035import org.onosproject.net.config.ConfigFactory;
36import org.onosproject.net.config.NetworkConfigEvent;
37import org.onosproject.net.config.NetworkConfigListener;
38import org.onosproject.net.config.NetworkConfigRegistry;
39import org.onosproject.net.config.NetworkConfigService;
40import org.onosproject.net.config.basics.SubjectFactories;
41import org.onosproject.net.device.DeviceService;
Hyunsun Moon71701292016-05-09 12:00:54 -070042import org.onosproject.scalablegateway.api.GatewayNode;
Kyuhwi Choidc2973b2016-05-13 14:54:31 +090043import org.onosproject.scalablegateway.api.GatewayNodeConfig;
Hyunsun Moon71701292016-05-09 12:00:54 -070044import org.onosproject.scalablegateway.api.ScalableGatewayService;
Kyuhwi Choic5b33ea2016-04-26 11:45:32 +090045
46import java.util.List;
Kyuhwi Choidc2973b2016-05-13 14:54:31 +090047import java.util.Map;
48
49import org.slf4j.Logger;
50import org.slf4j.LoggerFactory;
51
52import static com.google.common.base.Preconditions.checkNotNull;
Kyuhwi Choic5b33ea2016-04-26 11:45:32 +090053
54/**
55 * Manages gateway node for gateway scalability.
56 */
Kyuhwi Choidc2973b2016-05-13 14:54:31 +090057
58@Service
59@Component(immediate = true)
Kyuhwi Choic5b33ea2016-04-26 11:45:32 +090060public class ScalableGatewayManager implements ScalableGatewayService {
Kyuhwi Choidc2973b2016-05-13 14:54:31 +090061 private final Logger log = LoggerFactory.getLogger(getClass());
62
63 private ApplicationId appId;
64 private static final String APP_ID = "org.onosproject.scalablegateway";
65 private static final String APP_NAME = "scalablegateway";
66 private static final String GATEWAYNODE_CAN_NOT_BE_NULL = "The gateway node can not be null";
67 private static final String PORT_CAN_NOT_BE_NULL = "The port can not be null";
68 private static final String FAIL_ADD_GATEWAY = "Adding process is failed as existing deivce id";
69 private static final String FAIL_REMOVE_GATEWAY = "Removing process is failed as unknown deivce id";
70 private static final String PORT_NAME = "portName";
71
72 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
73 protected CoreService coreService;
74
75 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
76 protected NetworkConfigService configService;
77
78 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
79 protected NetworkConfigRegistry configRegistry;
80
81 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
82 protected DeviceService deviceService;
83
84 private GatewayNodeConfig config;
85
86 private final NetworkConfigListener configListener = new InternalConfigListener();
87
88 private final ConfigFactory configFactory =
89 new ConfigFactory(SubjectFactories.APP_SUBJECT_FACTORY, GatewayNodeConfig.class, APP_NAME) {
90 @Override
91 public GatewayNodeConfig createConfig() {
92 return new GatewayNodeConfig();
93 }
94 };
95 private Map<DeviceId, GatewayNode> gatewayNodeMap = Maps.newHashMap(); // Map<GatewayNode`s Id, GatewayNode object>
96
97 @Activate
98 protected void activate() {
99 appId = coreService.registerApplication(APP_ID);
100 configRegistry.registerConfigFactory(configFactory);
101 configService.addListener(configListener);
102
103 readConfiguration();
104
105 log.info("started");
106 }
107
108 @Deactivate
109 protected void deactivate() {
110 gatewayNodeMap.clear();
111
112 configService.removeListener(configListener);
113
114 log.info("stopped");
115 }
Kyuhwi Choic5b33ea2016-04-26 11:45:32 +0900116
117 @Override
118 public GatewayNode getGatewayNode(DeviceId deviceId) {
Kyuhwi Choidc2973b2016-05-13 14:54:31 +0900119 return checkNotNull(gatewayNodeMap.get(deviceId), GATEWAYNODE_CAN_NOT_BE_NULL);
Kyuhwi Choic5b33ea2016-04-26 11:45:32 +0900120 }
121
122 @Override
123 public List<PortNumber> getGatewayExternalPorts(DeviceId deviceId) {
Kyuhwi Choidc2973b2016-05-13 14:54:31 +0900124 GatewayNode gatewayNode = checkNotNull(gatewayNodeMap.get(deviceId), GATEWAYNODE_CAN_NOT_BE_NULL);
125 List<PortNumber> portNumbers = Lists.newArrayList();
126 gatewayNode.getGatewayExternalInterfaceNames()
127 .stream()
128 .forEach(name -> portNumbers.add(findPortNumFromPortName(gatewayNode.getGatewayDeviceId(), name)));
129 return portNumbers;
130 }
131
132 private PortNumber findPortNumFromPortName(DeviceId gatewayDeviceId, String name) {
133 Port port = deviceService.getPorts(gatewayDeviceId)
134 .stream()
135 .filter(p -> p.annotations().value(PORT_NAME).equals(name))
136 .iterator()
137 .next();
138 return checkNotNull(port, PORT_CAN_NOT_BE_NULL).number();
139
Kyuhwi Choic5b33ea2016-04-26 11:45:32 +0900140 }
141
142 @Override
143 public GroupId getGroupIdForGatewayLoadBalance(DeviceId srcDeviceId) {
Kyuhwi Choidc2973b2016-05-13 14:54:31 +0900144 //TODO: Implement group generation method by handler
Kyuhwi Choic5b33ea2016-04-26 11:45:32 +0900145 return null;
146 }
147
148 @Override
149 public List<GatewayNode> getGatewayNodes() {
Kyuhwi Choidc2973b2016-05-13 14:54:31 +0900150 List<GatewayNode> gatewayNodeList = Lists.newArrayList();
151 gatewayNodeMap.values()
152 .stream()
153 .forEach(gatewayNode -> gatewayNodeList.add(gatewayNode));
154 return gatewayNodeList;
155
Kyuhwi Choic5b33ea2016-04-26 11:45:32 +0900156 }
157
158 @Override
159 public List<DeviceId> getGatewayDeviceIds() {
Kyuhwi Choidc2973b2016-05-13 14:54:31 +0900160 List<DeviceId> deviceIdList = Lists.newArrayList();
161 gatewayNodeMap.values()
162 .stream()
163 .forEach(gatewayNode -> deviceIdList.add(gatewayNode.getGatewayDeviceId()));
164 return deviceIdList;
165
Kyuhwi Choic5b33ea2016-04-26 11:45:32 +0900166 }
167
168 @Override
169 public boolean addGatewayNode(GatewayNode gatewayNode) {
Kyuhwi Choidc2973b2016-05-13 14:54:31 +0900170 gatewayNodeMap.putIfAbsent(gatewayNode.getGatewayDeviceId(), gatewayNode);
171 return true;
Kyuhwi Choic5b33ea2016-04-26 11:45:32 +0900172 }
173
174 @Override
175 public boolean deleteGatewayNode(GatewayNode gatewayNode) {
Kyuhwi Choidc2973b2016-05-13 14:54:31 +0900176 return gatewayNodeMap.remove(gatewayNode.getGatewayDeviceId(), gatewayNode);
177 }
178
179 private class InternalConfigListener implements NetworkConfigListener {
180 @Override
181 public void event(NetworkConfigEvent event) {
182 if (!event.configClass().equals(GatewayNodeConfig.class)) {
183 return;
184 }
185 switch (event.type()) {
186 case CONFIG_UPDATED:
187 gatewayNodeMap.clear();
188 readConfiguration();
189 break;
190 case CONFIG_ADDED:
191 readConfiguration();
192 break;
193 default:
194 log.debug("Unsupportable event type is occurred");
195 break;
196 }
197 }
198 }
199
200 private void readConfiguration() {
201 config = configService.getConfig(appId, GatewayNodeConfig.class);
202 if (config == null) {
203 log.error("No configuration found");
204 return;
205 }
206
207 config.gatewayNodes().forEach(gatewayNode -> addGatewayNode(gatewayNode));
208
209 log.info("ScalableGateway configured");
Kyuhwi Choic5b33ea2016-04-26 11:45:32 +0900210 }
211}