Hyunsun Moon | 7ad9220 | 2016-04-20 10:36:02 -0700 | [diff] [blame] | 1 | /* |
| 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 | package org.onosproject.xosclient.impl; |
| 17 | |
| 18 | import com.google.common.base.Strings; |
| 19 | import org.apache.felix.scr.annotations.Activate; |
| 20 | import org.apache.felix.scr.annotations.Component; |
| 21 | import org.apache.felix.scr.annotations.Deactivate; |
| 22 | import org.apache.felix.scr.annotations.Modified; |
| 23 | import org.apache.felix.scr.annotations.Property; |
| 24 | import org.apache.felix.scr.annotations.Reference; |
| 25 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 26 | import org.apache.felix.scr.annotations.Service; |
| 27 | import org.onlab.util.Tools; |
| 28 | import org.onosproject.cfg.ComponentConfigService; |
| 29 | import org.onosproject.core.ApplicationId; |
| 30 | import org.onosproject.core.CoreService; |
| 31 | import org.onosproject.net.config.ConfigFactory; |
| 32 | import org.onosproject.net.config.NetworkConfigEvent; |
| 33 | import org.onosproject.net.config.NetworkConfigListener; |
| 34 | import org.onosproject.net.config.NetworkConfigRegistry; |
| 35 | import org.onosproject.net.config.basics.SubjectFactories; |
| 36 | import org.onosproject.xosclient.api.VtnServiceApi; |
| 37 | import org.onosproject.xosclient.api.XosAccess; |
| 38 | import org.onosproject.xosclient.api.XosAccessConfig; |
| 39 | import org.onosproject.xosclient.api.XosClientService; |
| 40 | import org.osgi.service.component.ComponentContext; |
| 41 | import org.slf4j.Logger; |
| 42 | |
| 43 | import java.util.Dictionary; |
| 44 | |
| 45 | import static org.slf4j.LoggerFactory.getLogger; |
| 46 | |
| 47 | import static com.google.common.base.Preconditions.checkNotNull; |
| 48 | |
| 49 | /** |
| 50 | * Provides interactions with XOS. |
| 51 | */ |
| 52 | @Component(immediate = true) |
| 53 | @Service |
| 54 | public class XosClient implements XosClientService { |
| 55 | |
| 56 | protected final Logger log = getLogger(getClass()); |
| 57 | |
| 58 | private static final String VTN_BASE_URL = "vtnBaseUrl"; |
Hyunsun Moon | 5c9591c | 2016-04-28 16:25:14 -0500 | [diff] [blame] | 59 | private static final String DEFAULT_VTN_BASE_URL = "/api/service/vtn/services/"; |
Hyunsun Moon | 7ad9220 | 2016-04-20 10:36:02 -0700 | [diff] [blame] | 60 | |
| 61 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 62 | protected CoreService coreService; |
| 63 | |
| 64 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 65 | protected ComponentConfigService componentConfigService; |
| 66 | |
| 67 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 68 | protected NetworkConfigRegistry configRegistry; |
| 69 | |
| 70 | @Property(name = VTN_BASE_URL, value = DEFAULT_VTN_BASE_URL, |
| 71 | label = "XOS VTN service API base url") |
| 72 | private String vtnBaseUrl = DEFAULT_VTN_BASE_URL; |
| 73 | |
| 74 | private final ConfigFactory configFactory = |
| 75 | new ConfigFactory(SubjectFactories.APP_SUBJECT_FACTORY, XosAccessConfig.class, "xosclient") { |
| 76 | @Override |
| 77 | public XosAccessConfig createConfig() { |
| 78 | return new XosAccessConfig(); |
| 79 | } |
| 80 | }; |
| 81 | |
| 82 | private final NetworkConfigListener configListener = new InternalConfigListener(); |
| 83 | |
| 84 | private ApplicationId appId; |
| 85 | private XosAccess access = null; |
| 86 | |
| 87 | @Activate |
| 88 | protected void activate(ComponentContext context) { |
| 89 | appId = coreService.registerApplication("org.onosproject.xosclient"); |
| 90 | |
| 91 | componentConfigService.registerProperties(getClass()); |
| 92 | modified(context); |
| 93 | |
| 94 | configRegistry.registerConfigFactory(configFactory); |
| 95 | configRegistry.addListener(configListener); |
| 96 | |
| 97 | log.info("Started"); |
| 98 | } |
| 99 | |
| 100 | @Deactivate |
| 101 | protected void deactivate() { |
| 102 | log.info("Stopped"); |
| 103 | } |
| 104 | |
| 105 | @Modified |
| 106 | protected void modified(ComponentContext context) { |
| 107 | Dictionary<?, ?> properties = context.getProperties(); |
| 108 | |
| 109 | String updatedUrl = Tools.get(properties, VTN_BASE_URL); |
| 110 | if (!Strings.isNullOrEmpty(updatedUrl)) { |
| 111 | vtnBaseUrl = updatedUrl; |
| 112 | } |
| 113 | |
| 114 | log.info("Modified"); |
| 115 | } |
| 116 | |
| 117 | @Override |
| 118 | public XosAccess access() { |
| 119 | return access; |
| 120 | } |
| 121 | |
| 122 | @Override |
| 123 | public synchronized boolean setAccess(XosAccess xosAccess) { |
| 124 | checkNotNull(xosAccess); |
| 125 | |
| 126 | // TODO authentication later before using the access |
| 127 | access = xosAccess; |
| 128 | return true; |
| 129 | } |
| 130 | |
| 131 | @Override |
| 132 | public VtnServiceApi vtnServiceApi() { |
| 133 | checkNotNull(access, "XOS API access is not set"); |
| 134 | return DefaultVtnServiceApi.getInstance(vtnBaseUrl, access); |
| 135 | } |
| 136 | |
| 137 | /* |
| 138 | * adds more XOS service APIs below. |
| 139 | */ |
| 140 | |
| 141 | private void readConfiguration() { |
| 142 | XosAccessConfig config = configRegistry.getConfig(appId, XosAccessConfig.class); |
| 143 | if (config == null) { |
| 144 | log.debug("No configuration found"); |
| 145 | return; |
| 146 | } |
| 147 | |
| 148 | setAccess(config.xosAccess()); |
| 149 | } |
| 150 | |
| 151 | private class InternalConfigListener implements NetworkConfigListener { |
| 152 | |
| 153 | @Override |
| 154 | public void event(NetworkConfigEvent event) { |
| 155 | if (!event.configClass().equals(XosAccessConfig.class)) { |
| 156 | return; |
| 157 | } |
| 158 | |
| 159 | switch (event.type()) { |
| 160 | case CONFIG_ADDED: |
| 161 | case CONFIG_UPDATED: |
| 162 | log.info("Network configuration changed"); |
| 163 | readConfiguration(); |
| 164 | break; |
| 165 | default: |
| 166 | break; |
| 167 | } |
| 168 | } |
| 169 | } |
| 170 | } |