blob: 9c737b5a1f0bdaca2da5c3ab503e577d9e1393e2 [file] [log] [blame]
Gaurav Agrawala4338f62017-03-07 17:08:26 +05301/*
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
17package org.onosproject.yang;
18
19import com.google.common.annotations.Beta;
20import org.apache.felix.scr.annotations.Activate;
21import org.apache.felix.scr.annotations.Component;
22import org.apache.felix.scr.annotations.Deactivate;
23import org.apache.felix.scr.annotations.Reference;
24import org.apache.felix.scr.annotations.ReferenceCardinality;
25import org.apache.felix.scr.annotations.Service;
26import org.onosproject.core.CoreService;
Gaurav Agrawal8ecb5a22017-03-15 19:56:00 +053027import org.onosproject.yang.model.ModelConverter;
28import org.onosproject.yang.model.ModelObjectData;
Gaurav Agrawala2951ed2017-03-24 11:20:20 +053029import org.onosproject.yang.model.NodeKey;
Gaurav Agrawal8ecb5a22017-03-15 19:56:00 +053030import org.onosproject.yang.model.ResourceData;
Gaurav Agrawala2951ed2017-03-24 11:20:20 +053031import org.onosproject.yang.model.ResourceId;
32import org.onosproject.yang.model.SchemaContext;
33import org.onosproject.yang.model.SchemaContextProvider;
Gaurav Agrawala4338f62017-03-07 17:08:26 +053034import org.onosproject.yang.model.YangModel;
35import org.onosproject.yang.runtime.CompositeData;
36import org.onosproject.yang.runtime.CompositeStream;
37import org.onosproject.yang.runtime.ModelRegistrationParam;
38import org.onosproject.yang.runtime.RuntimeContext;
39import org.onosproject.yang.runtime.YangModelRegistry;
40import org.onosproject.yang.runtime.YangRuntimeService;
41import org.onosproject.yang.runtime.YangSerializer;
42import org.onosproject.yang.runtime.YangSerializerRegistry;
Gaurav Agrawalfebde0f2017-03-28 11:48:04 +053043import org.onosproject.yang.runtime.impl.DefaultModelConverter;
Gaurav Agrawala4338f62017-03-07 17:08:26 +053044import org.onosproject.yang.runtime.impl.DefaultYangModelRegistry;
45import org.onosproject.yang.runtime.impl.DefaultYangRuntimeHandler;
46import org.onosproject.yang.runtime.impl.DefaultYangSerializerRegistry;
GauravAgrawal-Huawei77409ac2017-03-14 08:51:04 +000047import org.onosproject.yang.serializers.json.JsonSerializer;
48import org.onosproject.yang.serializers.xml.XmlSerializer;
Gaurav Agrawala4338f62017-03-07 17:08:26 +053049import org.slf4j.Logger;
50import org.slf4j.LoggerFactory;
51
52import java.util.Set;
53
Gaurav Agrawala2951ed2017-03-24 11:20:20 +053054import static com.google.common.base.Preconditions.checkNotNull;
55
Gaurav Agrawala4338f62017-03-07 17:08:26 +053056/**
57 * Represents implementation of YANG runtime manager.
58 */
59@Beta
60@Service
61@Component(immediate = true)
62public class YangRuntimeManager implements YangModelRegistry,
Gaurav Agrawala2951ed2017-03-24 11:20:20 +053063 YangSerializerRegistry, YangRuntimeService, ModelConverter,
64 SchemaContextProvider {
Gaurav Agrawala4338f62017-03-07 17:08:26 +053065
66 private static final String APP_ID = "org.onosproject.yang";
67 private final Logger log = LoggerFactory.getLogger(getClass());
68
69 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
70 protected CoreService coreService;
71
Thomas Vachuska68034012017-03-07 14:04:53 -080072 private DefaultYangModelRegistry modelRegistry;
73 private DefaultYangSerializerRegistry serializerRegistry;
74 private DefaultYangRuntimeHandler runtimeService;
Gaurav Agrawalfebde0f2017-03-28 11:48:04 +053075 private DefaultModelConverter modelConverter;
Gaurav Agrawala4338f62017-03-07 17:08:26 +053076
77 @Activate
78 public void activate() {
79 coreService.registerApplication(APP_ID);
80 serializerRegistry = new DefaultYangSerializerRegistry();
81 modelRegistry = new DefaultYangModelRegistry();
Gaurav Agrawala2951ed2017-03-24 11:20:20 +053082 runtimeService =
83 new DefaultYangRuntimeHandler(serializerRegistry, modelRegistry);
GauravAgrawal-Huawei77409ac2017-03-14 08:51:04 +000084 serializerRegistry.registerSerializer(new JsonSerializer());
85 serializerRegistry.registerSerializer(new XmlSerializer());
Gaurav Agrawalfebde0f2017-03-28 11:48:04 +053086 modelConverter = new DefaultModelConverter(modelRegistry);
Thomas Vachuska68034012017-03-07 14:04:53 -080087 log.info("Started");
Gaurav Agrawala4338f62017-03-07 17:08:26 +053088 }
89
90 @Deactivate
91 public void deactivate() {
Thomas Vachuska68034012017-03-07 14:04:53 -080092 log.info("Stopped");
Gaurav Agrawala4338f62017-03-07 17:08:26 +053093 }
94
95
96 @Override
97 public void registerModel(ModelRegistrationParam p) {
98 modelRegistry.registerModel(p);
99 }
100
101 @Override
102 public void unregisterModel(ModelRegistrationParam p) {
103 modelRegistry.unregisterModel(p);
104 }
105
106 @Override
107 public Set<YangModel> getModels() {
108 return modelRegistry.getModels();
109 }
110
111 @Override
112 public void registerSerializer(YangSerializer ys) {
Gaurav Agrawal45462172017-05-11 21:20:19 +0530113 //FIXME: Allow re-registration of model with additional service end
114 // points.
Gaurav Agrawala4338f62017-03-07 17:08:26 +0530115 serializerRegistry.registerSerializer(ys);
116 }
117
118 @Override
119 public void unregisterSerializer(YangSerializer ys) {
120 serializerRegistry.unregisterSerializer(ys);
121 }
122
123 @Override
124 public Set<YangSerializer> getSerializers() {
125 return serializerRegistry.getSerializers();
126 }
127
128 @Override
129 public CompositeData decode(CompositeStream cs, RuntimeContext rc) {
Thomas Vachuska68034012017-03-07 14:04:53 -0800130 return runtimeService.decode(cs, rc);
Gaurav Agrawala4338f62017-03-07 17:08:26 +0530131 }
132
133 @Override
134 public CompositeStream encode(CompositeData cd, RuntimeContext rc) {
Thomas Vachuska68034012017-03-07 14:04:53 -0800135 return runtimeService.encode(cd, rc);
Gaurav Agrawala4338f62017-03-07 17:08:26 +0530136 }
Gaurav Agrawal8ecb5a22017-03-15 19:56:00 +0530137
138 @Override
139 public ModelObjectData createModel(ResourceData resourceData) {
Gaurav Agrawalfebde0f2017-03-28 11:48:04 +0530140 return modelConverter.createModel(resourceData);
Gaurav Agrawal8ecb5a22017-03-15 19:56:00 +0530141 }
142
143 @Override
144 public ResourceData createDataNode(ModelObjectData modelObjectData) {
Gaurav Agrawalfebde0f2017-03-28 11:48:04 +0530145 return modelConverter.createDataNode(modelObjectData);
Gaurav Agrawal8ecb5a22017-03-15 19:56:00 +0530146 }
Gaurav Agrawala2951ed2017-03-24 11:20:20 +0530147
148 @Override
149 public SchemaContext getSchemaContext(ResourceId resourceId) {
150 checkNotNull(resourceId, " resource id can't be null.");
151 NodeKey key = resourceId.nodeKeys().get(0);
152 if (resourceId.nodeKeys().size() == 1 &&
Jon Halla3fcf672017-03-28 16:53:22 -0700153 "/".equals(key.schemaId().name())) {
Gaurav Agrawala2951ed2017-03-24 11:20:20 +0530154 return modelRegistry;
155 }
156 log.info("To be implemented.");
157 return null;
158 }
Gaurav Agrawala4338f62017-03-07 17:08:26 +0530159}