Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [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 | |
| 17 | package org.onosproject.yms.app.ymsm; |
| 18 | |
| 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.onosproject.core.ApplicationId; |
| 26 | import org.onosproject.core.CoreService; |
| 27 | import org.onosproject.core.IdGenerator; |
Vidyashree Rama | 76faccc | 2016-10-17 22:06:52 +0530 | [diff] [blame^] | 28 | import org.onosproject.yms.app.yab.YangApplicationBroker; |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 29 | import org.onosproject.yms.app.ydt.DefaultYdtWalker; |
| 30 | import org.onosproject.yms.app.ydt.YangRequestWorkBench; |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 31 | import org.onosproject.yms.app.ynh.YangNotificationExtendedService; |
| 32 | import org.onosproject.yms.app.ysr.DefaultYangSchemaRegistry; |
| 33 | import org.onosproject.yms.app.ysr.YangSchemaRegistry; |
| 34 | import org.onosproject.yms.ych.YangCodecHandler; |
| 35 | import org.onosproject.yms.ych.YangDataTreeCodec; |
| 36 | import org.onosproject.yms.ych.YangProtocolEncodingFormat; |
| 37 | import org.onosproject.yms.ydt.YdtBuilder; |
| 38 | import org.onosproject.yms.ydt.YdtResponse; |
| 39 | import org.onosproject.yms.ydt.YdtWalker; |
| 40 | import org.onosproject.yms.ydt.YmsOperationType; |
| 41 | import org.onosproject.yms.ymsm.YmsService; |
| 42 | import org.onosproject.yms.ynh.YangNotificationService; |
| 43 | import org.onosproject.yms.ysr.YangModuleIdentifier; |
| 44 | import org.onosproject.yms.ysr.YangModuleLibrary; |
| 45 | import org.slf4j.Logger; |
| 46 | import org.slf4j.LoggerFactory; |
| 47 | |
| 48 | import java.util.List; |
| 49 | import java.util.concurrent.ExecutorService; |
| 50 | import java.util.concurrent.Executors; |
| 51 | |
| 52 | import static org.onlab.util.Tools.groupedThreads; |
| 53 | |
| 54 | /** |
| 55 | * Represents implementation of YANG management system manager. |
| 56 | */ |
| 57 | @Service |
| 58 | @Component(immediate = true) |
| 59 | public class YmsManager |
| 60 | implements YmsService { |
| 61 | |
| 62 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 63 | |
| 64 | private static final String APP_ID = "org.onosproject.app.yms"; |
| 65 | private static final String MODULE_ID = "module-id"; |
| 66 | private ApplicationId appId; |
| 67 | private YangSchemaRegistry schemaRegistry; |
| 68 | //module id generator should be used to generate a new module id for |
| 69 | //each YSR instance. So YCH also should generate it. |
| 70 | private IdGenerator moduleIdGenerator; |
| 71 | private ExecutorService schemaRegistryExecutor; |
| 72 | private YangNotificationExtendedService ynhExtendedService; |
| 73 | |
| 74 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 75 | protected CoreService coreService; |
| 76 | |
| 77 | @Activate |
| 78 | public void activate() { |
| 79 | appId = coreService.registerApplication(APP_ID); |
| 80 | moduleIdGenerator = coreService.getIdGenerator(MODULE_ID); |
| 81 | schemaRegistry = new DefaultYangSchemaRegistry(String.valueOf( |
| 82 | moduleIdGenerator.getNewId())); |
| 83 | schemaRegistryExecutor = |
| 84 | Executors.newSingleThreadExecutor(groupedThreads( |
| 85 | "onos/apps/yang-management-system/schema-registry", |
| 86 | "schema-registry-handler", log)); |
| 87 | log.info("Started"); |
| 88 | } |
| 89 | |
| 90 | @Deactivate |
| 91 | public void deactivate() { |
| 92 | ((DefaultYangSchemaRegistry) schemaRegistry).flushYsrData(); |
| 93 | schemaRegistryExecutor.shutdown(); |
| 94 | |
| 95 | // TODO implementation for other components. |
| 96 | log.info("Stopped"); |
| 97 | } |
| 98 | |
| 99 | @Override |
| 100 | public YdtBuilder getYdtBuilder(String logicalRootName, |
| 101 | String rootNamespace, |
| 102 | YmsOperationType operationType) { |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 103 | return new YangRequestWorkBench(logicalRootName, rootNamespace, |
| 104 | operationType, schemaRegistry, true); |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | @Override |
| 108 | public YdtBuilder getYdtBuilder(String logicalRootName, |
| 109 | String rootNamespace, |
| 110 | YmsOperationType operationType, |
| 111 | Object schemaRegistryForYdt) { |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 112 | if (schemaRegistryForYdt != null) { |
| 113 | return new YangRequestWorkBench(logicalRootName, rootNamespace, |
| 114 | operationType, |
| 115 | (YangSchemaRegistry) |
| 116 | schemaRegistryForYdt, |
| 117 | false); |
| 118 | } |
| 119 | return new YangRequestWorkBench(logicalRootName, rootNamespace, |
| 120 | operationType, schemaRegistry, true); |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | @Override |
| 124 | public YdtWalker getYdtWalker() { |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 125 | return new DefaultYdtWalker(); |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | @Override |
| 129 | public YdtResponse executeOperation(YdtBuilder operationRequest) { |
Vidyashree Rama | 76faccc | 2016-10-17 22:06:52 +0530 | [diff] [blame^] | 130 | YangApplicationBroker requestBroker = |
| 131 | new YangApplicationBroker(schemaRegistry); |
| 132 | switch (operationRequest.getYmsOperationType()) { |
| 133 | case EDIT_CONFIG_REQUEST: |
| 134 | try { |
| 135 | return requestBroker.processEdit(operationRequest); |
| 136 | } catch (CloneNotSupportedException e) { |
| 137 | log.error("YAB: failed to process edit request."); |
| 138 | } |
| 139 | case QUERY_CONFIG_REQUEST: |
| 140 | // TODO : to be implemented |
| 141 | case QUERY_REQUEST: |
| 142 | return requestBroker.processQuery(operationRequest); |
| 143 | case RPC_REQUEST: |
| 144 | return requestBroker.processOperation(operationRequest); |
| 145 | default: |
| 146 | // TODO : throw exception |
| 147 | } |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 148 | return null; |
| 149 | } |
| 150 | |
| 151 | @Override |
| 152 | public YangNotificationService getYangNotificationService() { |
| 153 | return ynhExtendedService; |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * Returns YANG notification extended service. |
| 158 | * |
| 159 | * @return YANG notification extended service |
| 160 | */ |
| 161 | private YangNotificationExtendedService getYnhExtendedService() { |
| 162 | return ynhExtendedService; |
| 163 | } |
| 164 | |
| 165 | @Override |
| 166 | public YangModuleLibrary getYangModuleLibrary() { |
| 167 | return ((DefaultYangSchemaRegistry) schemaRegistry).getLibrary(); |
| 168 | } |
| 169 | |
| 170 | @Override |
| 171 | public String getYangFile(YangModuleIdentifier moduleIdentifier) { |
| 172 | return ((DefaultYangSchemaRegistry) schemaRegistry) |
| 173 | .getYangFile(moduleIdentifier); |
| 174 | } |
| 175 | |
| 176 | @Override |
| 177 | public void registerDefaultCodec(YangDataTreeCodec defaultCodec, |
| 178 | YangProtocolEncodingFormat dataFormat) { |
| 179 | |
| 180 | } |
| 181 | |
| 182 | @Override |
| 183 | public void registerService(Object yangManager, Class<?> yangService, |
| 184 | List<String> supportedFeatureList) { |
| 185 | |
| 186 | //perform registration of service |
| 187 | schemaRegistryExecutor.execute(() -> schemaRegistry |
| 188 | .registerApplication(yangManager, yangService, |
| 189 | getYnhExtendedService())); |
| 190 | } |
| 191 | |
| 192 | @Override |
| 193 | public void unRegisterService(Object appManager, Class<?> yangService) { |
| 194 | schemaRegistry.unRegisterApplication(appManager, yangService); |
| 195 | } |
| 196 | |
| 197 | @Override |
| 198 | public YangCodecHandler getYangCodecHandler() { |
| 199 | return null; |
| 200 | } |
| 201 | |
| 202 | /** |
| 203 | * Returns schema registry. |
| 204 | * |
| 205 | * @return schema registry |
| 206 | */ |
| 207 | public YangSchemaRegistry getSchemaRegistry() { |
| 208 | return schemaRegistry; |
| 209 | } |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 210 | } |