Henry Yu | ab25a71 | 2017-02-18 13:15:25 -0500 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2017-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.protocol.restconf.server.restconfmanager; |
| 17 | |
| 18 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 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.Reference; |
| 23 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 24 | import org.apache.felix.scr.annotations.Service; |
| 25 | import org.glassfish.jersey.server.ChunkedOutput; |
| 26 | import org.onosproject.core.ApplicationId; |
| 27 | import org.onosproject.core.CoreService; |
| 28 | import org.onosproject.incubator.net.config.basics.ConfigException; |
| 29 | import org.onosproject.net.config.ConfigFactory; |
| 30 | import org.onosproject.net.config.NetworkConfigEvent; |
| 31 | import org.onosproject.net.config.NetworkConfigListener; |
| 32 | import org.onosproject.net.config.NetworkConfigRegistry; |
| 33 | import org.onosproject.protocol.restconf.server.api.RestconfException; |
| 34 | import org.onosproject.protocol.restconf.server.api.RestconfServiceBroker; |
| 35 | import org.onosproject.restconf.api.RestconfService; |
| 36 | import org.slf4j.Logger; |
| 37 | import org.slf4j.LoggerFactory; |
| 38 | |
| 39 | import static org.onosproject.net.config.NetworkConfigEvent.Type.CONFIG_ADDED; |
| 40 | import static org.onosproject.net.config.NetworkConfigEvent.Type.CONFIG_UPDATED; |
| 41 | import static org.onosproject.net.config.basics.SubjectFactories.APP_SUBJECT_FACTORY; |
| 42 | |
| 43 | /** |
| 44 | * Implementation of the RestconfServiceBroker interface. |
| 45 | */ |
| 46 | @Component(immediate = false) |
| 47 | @Service |
| 48 | public class RestconfBrokerImpl implements RestconfServiceBroker { |
| 49 | |
| 50 | private static final String APP_NAME = "org.onosproject.protocols.restconfserver"; |
| 51 | private static final String CONFIG_KEY = "restconfCfg"; |
| 52 | private static final String DYN_CONFIG_MODE = "true"; |
| 53 | private static final String RESTCONF_ROOT = "/onos/restconf"; |
| 54 | |
| 55 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 56 | |
| 57 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 58 | protected CoreService coreService; |
| 59 | |
| 60 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 61 | protected NetworkConfigRegistry cfgService; |
| 62 | |
| 63 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 64 | protected org.onosproject.protocol.restconf.server.api.RestconfService restconfYms; |
| 65 | |
| 66 | |
| 67 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 68 | protected RestconfService restconfDynConfig; |
| 69 | |
| 70 | private ApplicationId appId; |
| 71 | private boolean useDynamicConfig = false; |
| 72 | |
| 73 | private final NetworkConfigListener cfgLister = new InternalConfigListener(); |
| 74 | private final ConfigFactory<ApplicationId, RestconfConfig> factory = |
| 75 | new ConfigFactory<ApplicationId, RestconfConfig>(APP_SUBJECT_FACTORY, |
| 76 | RestconfConfig.class, |
| 77 | CONFIG_KEY, |
| 78 | false) { |
| 79 | @Override |
| 80 | public RestconfConfig createConfig() { |
| 81 | return new RestconfConfig(); |
| 82 | } |
| 83 | }; |
| 84 | |
| 85 | @Activate |
| 86 | protected void activate() { |
| 87 | appId = coreService.registerApplication(APP_NAME); |
| 88 | cfgService.registerConfigFactory(factory); |
| 89 | cfgService.addListener(cfgLister); |
| 90 | log.info("Started"); |
| 91 | } |
| 92 | |
| 93 | @Deactivate |
| 94 | protected void deactivate() { |
| 95 | cfgService.removeListener(cfgLister); |
| 96 | cfgService.unregisterConfigFactory(factory); |
| 97 | log.info("Stopped"); |
| 98 | } |
| 99 | |
| 100 | @Override |
| 101 | public ObjectNode runGetOperationOnDataResource(String uri) |
| 102 | throws RestconfException { |
| 103 | return useDynamicConfig ? |
| 104 | restconfDynConfig.runGetOperationOnDataResource(uri) : |
| 105 | restconfYms.runGetOperationOnDataResource(uri); |
| 106 | } |
| 107 | |
| 108 | |
| 109 | @Override |
| 110 | public void runPostOperationOnDataResource(String uri, ObjectNode rootNode) |
| 111 | throws RestconfException { |
| 112 | if (useDynamicConfig) { |
| 113 | restconfDynConfig.runPostOperationOnDataResource(uri, rootNode); |
| 114 | } else { |
| 115 | restconfYms.runPostOperationOnDataResource(uri, rootNode); |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | @Override |
| 120 | public void runPutOperationOnDataResource(String uri, ObjectNode rootNode) |
| 121 | throws RestconfException { |
| 122 | if (useDynamicConfig) { |
| 123 | restconfDynConfig.runPutOperationOnDataResource(uri, rootNode); |
| 124 | } else { |
| 125 | restconfYms.runPutOperationOnDataResource(uri, rootNode); |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | @Override |
| 130 | public void runDeleteOperationOnDataResource(String uri) |
| 131 | throws RestconfException { |
| 132 | if (useDynamicConfig) { |
| 133 | restconfDynConfig.runDeleteOperationOnDataResource(uri); |
| 134 | } else { |
| 135 | restconfYms.runDeleteOperationOnDataResource(uri); |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | @Override |
| 140 | public void runPatchOperationOnDataResource(String uri, ObjectNode rootNode) |
| 141 | throws RestconfException { |
| 142 | if (useDynamicConfig) { |
| 143 | restconfDynConfig.runPatchOperationOnDataResource(uri, rootNode); |
| 144 | } else { |
| 145 | restconfYms.runPatchOperationOnDataResource(uri, rootNode); |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | @Override |
| 150 | public String getRestconfRootPath() { |
| 151 | return RESTCONF_ROOT; |
| 152 | } |
| 153 | |
| 154 | @Override |
| 155 | public void subscribeEventStream(String streamId, |
| 156 | ChunkedOutput<String> output) |
| 157 | throws RestconfException { |
| 158 | if (useDynamicConfig) { |
| 159 | restconfDynConfig.subscribeEventStream(streamId, output); |
| 160 | } else { |
| 161 | restconfYms.subscribeEventStream(streamId, output); |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | private class InternalConfigListener implements NetworkConfigListener { |
| 166 | |
| 167 | @Override |
| 168 | public void event(NetworkConfigEvent event) { |
| 169 | try { |
| 170 | useDynamicConfig = cfgService.getConfig(appId, RestconfConfig.class) |
| 171 | .useDynamicConfig().equals(DYN_CONFIG_MODE); |
| 172 | } catch (ConfigException e) { |
| 173 | log.error("Configuration error {}", e); |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | @Override |
| 178 | public boolean isRelevant(NetworkConfigEvent event) { |
| 179 | return event.configClass().equals(RestconfConfig.class) && |
| 180 | (event.type() == CONFIG_ADDED || |
| 181 | event.type() == CONFIG_UPDATED); |
| 182 | } |
| 183 | } |
| 184 | } |