blob: 7b65a758b9a90c58977770a607a1f30d6fa68ce3 [file] [log] [blame]
Sean Condonfae8e662016-12-15 10:25:13 +00001/*
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 */
16package org.onosproject.drivers.microsemi.yang.impl;
17
18import static org.onosproject.yms.ych.YangProtocolEncodingFormat.XML;
19import static org.onosproject.yms.ydt.YmsOperationType.EDIT_CONFIG_REQUEST;
20import static org.onosproject.yms.ydt.YmsOperationType.QUERY_REPLY;
21import static org.onosproject.yms.ydt.YmsOperationType.QUERY_REQUEST;
22
23import java.util.List;
24
25import org.apache.felix.scr.annotations.Activate;
26import org.apache.felix.scr.annotations.Component;
27import org.apache.felix.scr.annotations.Deactivate;
28import org.apache.felix.scr.annotations.Reference;
29import org.apache.felix.scr.annotations.ReferenceCardinality;
30import org.apache.felix.scr.annotations.Service;
31import org.onosproject.core.ApplicationId;
32import org.onosproject.core.CoreService;
33import org.onosproject.netconf.NetconfException;
34import org.onosproject.netconf.NetconfSession;
35import org.onosproject.netconf.TargetConfig;
36import org.onosproject.yms.ych.YangCodecHandler;
37import org.onosproject.yms.ych.YangCompositeEncoding;
38import org.onosproject.yms.ymsm.YmsService;
39import org.slf4j.Logger;
40import org.slf4j.LoggerFactory;
41
42/**
43 * Abstract class that implements some of the core functions of a YANG model service.
44 *
45 */
46@Component(immediate = true)
47@Service
48public abstract class AbstractYangServiceImpl {
49 protected final Logger log = LoggerFactory.getLogger(getClass());
50 protected boolean alreadyLoaded = false;
51
52 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
53 protected CoreService coreService;
54
55 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
56 protected YmsService ymsService;
57
58 protected ApplicationId appId;
59
60 // YCH is not a service and is a class variable. Can be lost on deactivate.
61 // Must be recreated on activate
62 protected YangCodecHandler ych;
63
64 @Activate
65 public abstract void activate();
66
67 @Deactivate
68 public void deactivate() {
69 alreadyLoaded = false;
70 }
71
72 /**
73 * Internal method to generically make a NETCONF get query from YANG objects.
74 * @param yangObjectOpParamFilter A YANG object model
75 * @param session A NETCONF session
76 * @return YangObjectModel
77 * @throws NetconfException if the session has any error
78 */
79 protected final Object getNetconfObject(
80 Object yangObjectOpParamFilter, NetconfSession session)
81 throws NetconfException {
82 if (session == null) {
83 throw new NetconfException("Session is null when calling getNetconfObject()");
84 }
85 if (yangObjectOpParamFilter == null) {
86 throw new NetconfException("Query object cannot be null");
87 }
88 //Convert the param to XML to use as a filter
89 YangCompositeEncoding xmlQuery =
90 ych.encodeCompositeOperation(null, null,
91 yangObjectOpParamFilter, XML, QUERY_REQUEST);
92
93 String xmlQueryStr = xmlQuery.getResourceInformation().replace("<>", "").replace("</>", "").trim();
94 log.debug("Sending <get> query on NETCONF session " + session.getSessionId() +
95 ":\n" + xmlQueryStr);
96
97 String xmlResult = session.get(xmlQueryStr, null);
98
99 List<Object> objectList = ych.decode(xmlResult, XML, QUERY_REPLY);
100 if (objectList != null && objectList.size() > 0) {
101 Object systemObject = objectList.get(0);
102 return systemObject;
103 } else {
104 return null;
105 }
106 }
107
108 /**
109 * Internal method to generically make a NETCONF get-config query from YANG objects.
110 *
111 * @param yangObjectOpParamFilter A YANG object model
112 * @param session A NETCONF session
113 * @param targetDs - running,candidate or startup
114 * @return YangObjectModel
115 * @throws NetconfException if the session has any error
116 */
117 protected final Object getConfigNetconfObject(
118 Object yangObjectOpParamFilter, NetconfSession session, TargetConfig targetDs)
119 throws NetconfException {
120 if (session == null) {
121 throw new NetconfException("Session is null when calling getConfigNetconfObject()");
122 }
123
124 if (yangObjectOpParamFilter == null) {
125 throw new NetconfException("Query object cannot be null");
126 }
127 //Convert the param to XML to use as a filter
128 YangCompositeEncoding xmlQuery =
129 ych.encodeCompositeOperation(null, null,
130 yangObjectOpParamFilter, XML, QUERY_REQUEST);
131
132 String xmlQueryStr = xmlQuery.getResourceInformation().replace("<>", "").replace("</>", "").trim();
133 log.debug("Sending <get-config> for " + targetDs +
134 " query on NETCONF session " + session.getSessionId() +
135 ":\n" + xmlQueryStr);
136
137 String xmlResult = session.getConfig(targetDs, xmlQueryStr);
138
139 List<Object> objectList = ych.decode(xmlResult, XML, QUERY_REPLY);
140 if (objectList != null && objectList.size() > 0) {
141 Object systemObject = objectList.get(0);
142 return systemObject;
143 } else {
144 return null;
145 }
146 }
147
148 /**
149 * Internal method to generically make a NETCONF edit-config call from a set of YANG objects.
150 *
151 * @param yangObjectOpParamFilter A YANG object model
152 * @param session A NETCONF session
153 * @param targetDs - running,candidate or startup
154 * @throws NetconfException if the session has any error
155 */
156 protected final void setNetconfObject(
157 Object yangObjectOpParamFilter, NetconfSession session, TargetConfig targetDs)
158 throws NetconfException {
159 if (yangObjectOpParamFilter == null) {
160 throw new NetconfException("Query object cannot be null");
161 } else if (session == null) {
162 throw new NetconfException("Session is null when calling getMseaSaFiltering()");
163 }
164 //Convert the param to XML to use as a filter
165 YangCompositeEncoding xmlContent =
166 ych.encodeCompositeOperation(null, null,
167 yangObjectOpParamFilter, XML, EDIT_CONFIG_REQUEST);
168
169 String xmlContentStr = xmlContent.getResourceInformation()
170 .replace("<>", "").replace("</>", "")
171 //FIXME: Necessary for MEP ccmInterval
172 .replaceAll("yangAutoPrefix", "")
173 .trim();
174
175 log.debug("Sending XML <edit-config> on NETCONF session " + session.getSessionId() +
176 ":\n" + xmlContentStr);
177
178 boolean succeeded = session.editConfig(targetDs, null, xmlContentStr);
179 if (succeeded) {
180 log.debug("<edit-config> succeeded through NETCONF");
181 } else {
182 throw new NetconfException("Failed to run edit-config through NETCONF");
183 }
184 }
185}