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