blob: 61643afb0640de358105fbaa851b41acd680e2d7 [file] [log] [blame]
Hyunsun Moon7ad92202016-04-20 10:36:02 -07001/*
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 */
16package org.onosproject.xosclient.impl;
17
18import com.google.common.base.Strings;
19import org.apache.felix.scr.annotations.Activate;
20import org.apache.felix.scr.annotations.Component;
21import org.apache.felix.scr.annotations.Deactivate;
22import org.apache.felix.scr.annotations.Modified;
23import org.apache.felix.scr.annotations.Property;
24import org.apache.felix.scr.annotations.Reference;
25import org.apache.felix.scr.annotations.ReferenceCardinality;
26import org.apache.felix.scr.annotations.Service;
27import org.onlab.util.Tools;
28import org.onosproject.cfg.ComponentConfigService;
29import org.onosproject.core.ApplicationId;
30import org.onosproject.core.CoreService;
31import org.onosproject.net.config.ConfigFactory;
32import org.onosproject.net.config.NetworkConfigEvent;
33import org.onosproject.net.config.NetworkConfigListener;
34import org.onosproject.net.config.NetworkConfigRegistry;
35import org.onosproject.net.config.basics.SubjectFactories;
Hyunsun Moon4c396632016-05-13 04:17:53 -070036import org.onosproject.xosclient.api.VtnPortApi;
Hyunsun Moon7ad92202016-04-20 10:36:02 -070037import org.onosproject.xosclient.api.VtnServiceApi;
38import org.onosproject.xosclient.api.XosAccess;
39import org.onosproject.xosclient.api.XosAccessConfig;
40import org.onosproject.xosclient.api.XosClientService;
41import org.osgi.service.component.ComponentContext;
42import org.slf4j.Logger;
43
44import java.util.Dictionary;
Hyunsun Moon4c396632016-05-13 04:17:53 -070045import java.util.Objects;
Hyunsun Moon7ad92202016-04-20 10:36:02 -070046
47import static org.slf4j.LoggerFactory.getLogger;
48
49import static com.google.common.base.Preconditions.checkNotNull;
50
51/**
52 * Provides interactions with XOS.
53 */
54@Component(immediate = true)
55@Service
56public class XosClient implements XosClientService {
57
58 protected final Logger log = getLogger(getClass());
59
Hyunsun Moon4c396632016-05-13 04:17:53 -070060 private static final String VTN_SERVICE_URL = "vtnServiceBaseUrl";
61 private static final String DEFAULT_VTN_SERVICE_URL = "/api/service/vtn/services/";
62
63 private static final String VTN_PORT_URL = "vtnPortBaseUrl";
64 private static final String DEFAULT_VTN_PORT_URL = "/api/service/vtn/ports/";
Hyunsun Moon7ad92202016-04-20 10:36:02 -070065
66 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
67 protected CoreService coreService;
68
69 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
70 protected ComponentConfigService componentConfigService;
71
72 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
73 protected NetworkConfigRegistry configRegistry;
74
Hyunsun Moon4c396632016-05-13 04:17:53 -070075 @Property(name = VTN_SERVICE_URL, value = DEFAULT_VTN_SERVICE_URL,
Hyunsun Moon7ad92202016-04-20 10:36:02 -070076 label = "XOS VTN service API base url")
Hyunsun Moon4c396632016-05-13 04:17:53 -070077 private String vtnServiceUrl = DEFAULT_VTN_SERVICE_URL;
78
79 @Property(name = VTN_PORT_URL, value = DEFAULT_VTN_PORT_URL,
80 label = "XOS VTN port API base url")
81 private String vtnPortUrl = DEFAULT_VTN_PORT_URL;
Hyunsun Moon7ad92202016-04-20 10:36:02 -070082
83 private final ConfigFactory configFactory =
84 new ConfigFactory(SubjectFactories.APP_SUBJECT_FACTORY, XosAccessConfig.class, "xosclient") {
85 @Override
86 public XosAccessConfig createConfig() {
87 return new XosAccessConfig();
88 }
89 };
90
91 private final NetworkConfigListener configListener = new InternalConfigListener();
92
93 private ApplicationId appId;
94 private XosAccess access = null;
95
Hyunsun Moon4c396632016-05-13 04:17:53 -070096 private VtnServiceApi vtnServiceApi = null;
97 private VtnPortApi vtnPortApi = null;
98
99 /*
100 * adds more XOS service APIs below.
101 */
102
Hyunsun Moon7ad92202016-04-20 10:36:02 -0700103 @Activate
104 protected void activate(ComponentContext context) {
105 appId = coreService.registerApplication("org.onosproject.xosclient");
106
107 componentConfigService.registerProperties(getClass());
108 modified(context);
109
110 configRegistry.registerConfigFactory(configFactory);
111 configRegistry.addListener(configListener);
112
113 log.info("Started");
114 }
115
116 @Deactivate
117 protected void deactivate() {
Hyunsun Moon4c396632016-05-13 04:17:53 -0700118 configRegistry.unregisterConfigFactory(configFactory);
119 configRegistry.removeListener(configListener);
120
Hyunsun Moon7ad92202016-04-20 10:36:02 -0700121 log.info("Stopped");
122 }
123
124 @Modified
125 protected void modified(ComponentContext context) {
126 Dictionary<?, ?> properties = context.getProperties();
Hyunsun Moon4c396632016-05-13 04:17:53 -0700127 String updatedUrl;
Hyunsun Moon7ad92202016-04-20 10:36:02 -0700128
Hyunsun Moon4c396632016-05-13 04:17:53 -0700129 updatedUrl = Tools.get(properties, VTN_SERVICE_URL);
130 if (!Strings.isNullOrEmpty(updatedUrl) && !updatedUrl.equals(vtnServiceUrl)) {
131 vtnServiceUrl = updatedUrl;
132 vtnServiceApi = new DefaultVtnServiceApi(vtnServiceUrl, access);
133 }
134
135 updatedUrl = Tools.get(properties, VTN_PORT_URL);
136 if (!Strings.isNullOrEmpty(updatedUrl) && !updatedUrl.equals(vtnPortUrl)) {
137 vtnPortUrl = updatedUrl;
138 vtnPortApi = new DefaultVtnPortApi(vtnPortUrl, access);
Hyunsun Moon7ad92202016-04-20 10:36:02 -0700139 }
140
141 log.info("Modified");
142 }
143
144 @Override
145 public XosAccess access() {
146 return access;
147 }
148
149 @Override
Hyunsun Moon4c396632016-05-13 04:17:53 -0700150 public synchronized XosClient getClient(XosAccess access) {
151 checkNotNull(access);
Hyunsun Moon7ad92202016-04-20 10:36:02 -0700152
Hyunsun Moon4c396632016-05-13 04:17:53 -0700153 if (!Objects.equals(this.access, access)) {
154 // TODO do authentication before return
155 this.access = access;
156
157 vtnServiceApi = new DefaultVtnServiceApi(vtnServiceUrl, access);
158 vtnPortApi = new DefaultVtnPortApi(vtnPortUrl, access);
159 }
160 return this;
Hyunsun Moon7ad92202016-04-20 10:36:02 -0700161 }
162
163 @Override
Hyunsun Moon4c396632016-05-13 04:17:53 -0700164 public VtnServiceApi vtnService() {
165 checkNotNull(vtnServiceApi, "VtnServiceApi is null");
166 return vtnServiceApi;
167 }
168
169 @Override
170 public VtnPortApi vtnPort() {
171 checkNotNull(vtnPortApi, "VtnPortApi is null");
172 return vtnPortApi;
Hyunsun Moon7ad92202016-04-20 10:36:02 -0700173 }
174
175 /*
176 * adds more XOS service APIs below.
177 */
178
179 private void readConfiguration() {
180 XosAccessConfig config = configRegistry.getConfig(appId, XosAccessConfig.class);
181 if (config == null) {
182 log.debug("No configuration found");
183 return;
184 }
Hyunsun Moon4c396632016-05-13 04:17:53 -0700185 getClient(config.xosAccess());
Hyunsun Moon7ad92202016-04-20 10:36:02 -0700186 }
187
188 private class InternalConfigListener implements NetworkConfigListener {
189
190 @Override
191 public void event(NetworkConfigEvent event) {
192 if (!event.configClass().equals(XosAccessConfig.class)) {
193 return;
194 }
195
196 switch (event.type()) {
197 case CONFIG_ADDED:
198 case CONFIG_UPDATED:
199 log.info("Network configuration changed");
200 readConfiguration();
201 break;
202 default:
203 break;
204 }
205 }
206 }
207}