2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Copyright (C) 2017 Amdocs
8 * ================================================================================
9 * Modifications (C) 2019 Ericsson
10 * =============================================================================
11 * Licensed under the Apache License, Version 2.0 (the "License");
12 * you may not use this file except in compliance with the License.
13 * You may obtain a copy of the License at
15 * http://www.apache.org/licenses/LICENSE-2.0
17 * Unless required by applicable law or agreed to in writing, software
18 * distributed under the License is distributed on an "AS IS" BASIS,
19 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 * See the License for the specific language governing permissions and
21 * limitations under the License.
23 * ============LICENSE_END=========================================================
26 package org.onap.appc.mdsal.impl;
28 import com.fasterxml.jackson.annotation.JsonIgnore;
29 import com.fasterxml.jackson.annotation.JsonProperty;
30 import com.fasterxml.jackson.annotation.JsonValue;
31 import com.fasterxml.jackson.core.JsonProcessingException;
32 import com.fasterxml.jackson.databind.JsonNode;
33 import com.fasterxml.jackson.databind.ObjectMapper;
34 import org.apache.commons.io.IOUtils;
35 import org.apache.http.HttpResponse;
36 import org.opendaylight.yang.gen.v1.org.onap.appc.mdsal.store.rev170925.StoreYangInput;
37 import org.opendaylight.yang.gen.v1.org.onap.appc.mdsal.store.rev170925.StoreYangInputBuilder;
38 import org.opendaylight.yangtools.yang.binding.DataContainer;
39 import org.onap.appc.configuration.Configuration;
40 import org.onap.appc.configuration.ConfigurationFactory;
41 import org.onap.appc.exceptions.APPCException;
42 import org.onap.appc.mdsal.MDSALStore;
43 import org.onap.appc.mdsal.exception.MDSALStoreException;
44 import org.onap.appc.mdsal.objects.BundleInfo;
45 import com.att.eelf.configuration.EELFLogger;
46 import com.att.eelf.configuration.EELFManager;
47 import org.onap.appc.mdsal.operation.ConfigOperationRequestFormatter;
48 import org.onap.appc.rest.client.RestClientInvoker;
49 import org.osgi.framework.Bundle;
50 import org.osgi.framework.BundleContext;
51 import org.osgi.framework.FrameworkUtil;
53 import java.io.ByteArrayInputStream;
54 import java.io.ByteArrayOutputStream;
55 import java.io.IOException;
56 import java.net.MalformedURLException;
59 import java.util.jar.Attributes;
60 import java.util.jar.JarEntry;
61 import java.util.jar.JarOutputStream;
62 import java.util.jar.Manifest;
65 * Implementation of MDSALStore
67 public class MDSALStoreImpl implements MDSALStore {
69 private final EELFLogger logger = EELFManager.getInstance().getLogger(MDSALStoreImpl.class);
70 private RestClientInvoker client;
71 private ConfigOperationRequestFormatter requestFormatter = new ConfigOperationRequestFormatter();
72 private ObjectMapper mapper = new ObjectMapper();
73 private Map<String, RestClientInvoker> remoteClientMap = new HashMap<>();
76 String configUrl = null;
78 String password = null;
79 Configuration configuration = ConfigurationFactory.getConfiguration();
80 Properties properties = configuration.getProperties();
81 if (properties != null) {
82 configUrl = properties.getProperty(Constants.CONFIG_URL_PROPERTY, Constants.CONFIG_URL_DEFAULT);
83 user = properties.getProperty(Constants.CONFIG_USER_PROPERTY);
84 password = properties.getProperty(Constants.CONFIG_PASS_PROPERTY);
86 if (configUrl != null) {
88 client = getRestClientInvoker(new URL(configUrl));
89 client.setAuthentication(user, password);
90 } catch (MalformedURLException e) {
91 logger.error("Error initializing RestConf client: " + e.getMessage(), e);
98 public boolean isModulePresent(String moduleName, Date revision) {
100 if (logger.isDebugEnabled()) {
101 logger.debug("isModulePresent invoked with moduleName = " + moduleName + " , revision = " + revision);
104 BundleContext bundleContext = FrameworkUtil.getBundle(this.getClass()).getBundleContext();
106 * SchemaContext interface of ODL provides APIs for querying details of yang modules
107 * loaded into MD-SAL store, but its limitation is, it only returns information about
108 * static yang modules loaded on server start up, it does not return information about
109 * the yang modules loaded dynamically. Due to this limitation, we are checking the
110 * presence of OSGI bundle instead of yang module. (Note: Assuming OSGI bundle is named
111 * with the yang module name).
114 Bundle bundle = bundleContext.getBundle(moduleName);
115 if (logger.isDebugEnabled()) {
116 logger.debug("isModulePresent returned = " + (bundle != null));
118 return bundle != null;
122 public void storeYangModule(String yang, BundleInfo bundleInfo) throws MDSALStoreException {
124 BundleContext bundleContext = FrameworkUtil.getBundle(this.getClass()).getBundleContext();
125 byte[] byteArray = createBundleJar(yang, Constants.BLUEPRINT, bundleInfo);
127 try (ByteArrayInputStream inputStream = new ByteArrayInputStream(byteArray)) {
128 Bundle bundle = bundleContext.installBundle(bundleInfo.getLocation(), inputStream);
130 } catch (Exception e) {
131 logger.error(String.format("Error storing yang module: %s. Error message: %s.", yang, e.getMessage()));
132 throw new MDSALStoreException("Error storing yang module: " + yang + " " + e.getMessage(), e);
137 public void storeYangModuleOnLeader(String yang, String moduleName) throws MDSALStoreException {
139 String leader = getLeaderNode();
140 if (Constants.SELF.equals(leader)){
141 logger.debug("Current node is a leader.");
143 logger.debug("Attempting to load yang module on Leader: " + leader );
144 String inputJson = createInputJson(yang, moduleName);
145 RestClientInvoker remoteClient = getRemoteClient(leader);
146 HttpResponse response = remoteClient.doPost(Constants.YANG_LOADER_PATH, inputJson);
147 int httpCode = response.getStatusLine().getStatusCode();
148 String respBody = IOUtils.toString(response.getEntity().getContent());
149 if (httpCode < 200 || httpCode >= 300) {
150 logger.debug("Error while loading yang module on leader. Response code: " + httpCode);
151 processRestconfResponse(respBody);
153 logger.debug("Yang module successfully loaded on leader. Response code: " + httpCode);
156 } catch (APPCException e) {
157 logger.error("Error loading Yang on Leader. Error message: " + e.getMessage());
158 throw new MDSALStoreException("Error loading Yang on Leader. Error message: " + e.getMessage(), e);
159 } catch (IOException e) {
160 logger.error("Error reading response from remote client. Error message: " + e.getMessage());
161 throw new MDSALStoreException("Error reading response from remote client. Error message: " + e.getMessage(), e);
165 private String createInputJson(String yang, String moduleName) throws MDSALStoreException {
166 StoreYangInputBuilder builder = new StoreYangInputBuilder();
167 builder.setYang(yang).setModuleName(moduleName);
168 StoreYangInput input = builder.build();
170 ObjectMapper objectMapper = new ObjectMapper();
171 objectMapper.addMixIn(StoreYangInput.class, MixIn.class);
172 String inputJson = objectMapper.writer().withRootName("input").writeValueAsString(input);
173 logger.debug("Input JSON :" + inputJson);
175 } catch (JsonProcessingException e) {
176 logger.error(String.format("Error creating JSON input using yang: %s. Error message: %s",yang ,e.getMessage()));
177 throw new MDSALStoreException(String.format("Error creating JSON input using yang: %s. Error message: %s",yang ,e.getMessage()), e);
181 private RestClientInvoker getRemoteClient(String leader) throws MDSALStoreException {
182 if (remoteClientMap.containsKey(leader)) {
183 return remoteClientMap.get(leader);
185 Configuration configuration = ConfigurationFactory.getConfiguration();
186 Properties properties = configuration.getProperties();
187 if (properties != null) {
189 URL configUrl = new URL(properties.getProperty(Constants.CONFIG_URL_PROPERTY, Constants.CONFIG_URL_DEFAULT));
190 String user = properties.getProperty(Constants.CONFIG_USER_PROPERTY);
191 String password = properties.getProperty(Constants.CONFIG_PASS_PROPERTY);
192 RestClientInvoker remoteClient = getRestClientInvoker(new URL(configUrl.getProtocol(), leader, configUrl.getPort(), ""));
193 remoteClient.setAuthentication(user, password);
194 remoteClientMap.put(leader, remoteClient);
196 } catch (MalformedURLException e) {
197 logger.error("Error initializing remote RestConf client: " + e.getMessage(), e);
198 throw new MDSALStoreException("Error initializing Remote RestConf client: " + e.getMessage(), e);
201 logger.error("Error initializing Remote RestConf client. Could not read appc properties");
202 throw new MDSALStoreException("Error initializing Remote RestConf client. Could not read appc properties");
207 abstract class MixIn {
209 abstract Class<? extends DataContainer> getImplementedInterface(); // to be removed during serialization
212 abstract java.lang.String getValue();
214 @JsonProperty("module-name")
215 abstract java.lang.String getModuleName();
219 public void storeJson(String module, String requestId, String configJson) throws MDSALStoreException {
220 if (configJson == null) {
221 throw new MDSALStoreException("Configuration JSON is empty or null");
223 logger.debug("Configuration JSON: " + configJson + "\n" + "module" + module);
225 String path = requestFormatter.buildPath(module, org.onap.appc.Constants.YANG_BASE_CONTAINER,
226 org.onap.appc.Constants.YANG_VNF_CONFIG_LIST, requestId, org.onap.appc.Constants.YANG_VNF_CONFIG);
227 logger.debug("Configuration Path : " + path);
228 HttpResponse response = client.doPut(path, configJson);
229 int httpCode = response.getStatusLine().getStatusCode();
230 String respBody = IOUtils.toString(response.getEntity().getContent());
231 if (httpCode < 200 || httpCode >= 300) {
232 logger.debug("Error while storing configuration JSON to MD-SAL store. Response code: " + httpCode);
233 processRestconfResponse(respBody);
235 logger.debug("Configuration JSON stored to MD-SAL store successfully. Response code: " + httpCode);
237 } catch (IOException | APPCException e) {
238 logger.error("Error while storing configuration json. Error Message" + e.getMessage(), e);
239 throw new MDSALStoreException(e);
243 private void processRestconfResponse(String response) throws MDSALStoreException {
245 JsonNode responseJson = mapper.readTree(response);
246 ArrayList<String> errorMessage = new ArrayList<>();
247 if (responseJson != null && responseJson.get("errors") != null) {
248 JsonNode errors = responseJson.get("errors").get("error");
249 for (Iterator<JsonNode> i = errors.elements(); i.hasNext(); ) {
250 JsonNode error = i.next();
251 errorMessage.add(error.get("error-message").textValue());
254 logger.error("Failed to load config JSON to MD SAL store. " + errorMessage.toString());
255 throw new MDSALStoreException("Failed to load config JSON to MD SAL store. Error Message: " + errorMessage.toString());
256 } catch (IOException e) {
257 logger.error("Failed to process error response from RestConf: " + e.getMessage());
258 throw new MDSALStoreException("Failed to process RestConf response. Error Message: " + e.toString(), e);
262 private byte[] createBundleJar(String yang, String blueprint, BundleInfo bundleInfo) throws MDSALStoreException {
264 Manifest manifest = new Manifest();
265 manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, Constants.MANIFEST_VALUE_VERSION);
266 manifest.getMainAttributes().put(new Attributes.Name(Constants.MANIFEST_ATTR_BUNDLE_NAME), bundleInfo.getName());
267 manifest.getMainAttributes().put(new Attributes.Name(Constants.MANIFEST_ATTR_BUNDLE_SYMBOLIC_NAME), bundleInfo.getName());
268 manifest.getMainAttributes().put(new Attributes.Name(Constants.MANIFEST_ATTR_BUNDLE_DESCRIPTION), bundleInfo.getDescription());
269 manifest.getMainAttributes().put(new Attributes.Name(Constants.MANIFEST_ATTR_BUNDLE_MANIFEST_VERSION), Constants.MANIFEST_VALUE_BUNDLE_MAN_VERSION);
270 manifest.getMainAttributes().put(new Attributes.Name(Constants.MANIFEST_ATTR_BUNDLE_VERSION), String.valueOf(bundleInfo.getVersion()));
271 manifest.getMainAttributes().put(new Attributes.Name(Constants.MANIFEST_ATTR_BUNDLE_BLUEPRINT), Constants.MANIFEST_VALUE_BUNDLE_BLUEPRINT);
275 try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
276 JarOutputStream jarOutputStream = new JarOutputStream(outputStream, manifest)) {
277 jarOutputStream.putNextEntry(new JarEntry("META-INF/yang/"));
278 jarOutputStream.putNextEntry(new JarEntry("META-INF/yang/" + bundleInfo.getName() + ".yang"));
279 jarOutputStream.write(yang.getBytes());
280 jarOutputStream.closeEntry();
282 jarOutputStream.putNextEntry(new JarEntry("OSGI-INF/blueprint/"));
283 jarOutputStream.putNextEntry(new JarEntry(Constants.MANIFEST_VALUE_BUNDLE_BLUEPRINT));
284 jarOutputStream.write(blueprint.getBytes());
285 jarOutputStream.closeEntry();
286 jarOutputStream.close();
287 retunValue = outputStream.toByteArray();
288 } catch (Exception e) {
289 logger.error("Error creating bundle jar: " + bundleInfo.getName() + ". Error message: " + e.getMessage());
290 throw new MDSALStoreException("Error creating bundle jar: " + bundleInfo.getName() + " " + e.getMessage(), e);
295 private String getLeaderNode() throws MDSALStoreException {
297 String shardName = String.format(Constants.SHARD_NAME_FORMAT, getNodeName());
298 HttpResponse response = client.doGet(String.format(Constants.GET_NODE_STATUS_PATH_FORMAT, shardName));
299 int httpCode = response.getStatusLine().getStatusCode();
300 String respBody = IOUtils.toString(response.getEntity().getContent());
301 logger.debug(String.format("Get node status returned Code: %s. Response: %s ", httpCode, respBody));
302 if (httpCode == 200 && mapper.readTree(respBody).get(Constants.JSON_RESPONSE_VALUE) !=null ) {
303 JsonNode responseValue = mapper.readTree(respBody).get(Constants.JSON_RESPONSE_VALUE);
304 String leaderShard = responseValue.get("Leader").asText();
305 if (shardName.equals(leaderShard)) {
306 logger.debug("Current node is leader.");
307 return Constants.SELF;
309 String[] peers = responseValue.get("PeerAddresses").asText().split(",");
310 for (String peer : peers) {
311 if (peer.trim().startsWith(leaderShard)) {
312 String leader = peer.substring(peer.indexOf('@') + 1, peer.indexOf(':', peer.indexOf('@')));
313 logger.debug(String.format("Node %s is a leader", leader));
317 logger.error("No Leader found for a cluster");
318 throw new MDSALStoreException("No Leader found for a cluster");
321 logger.error("Error while retrieving leader node.");
322 throw new MDSALStoreException("Error while retrieving leader node.");
324 } catch (IOException | APPCException e) {
325 logger.error(String.format("Error while retrieving leader Node. Error message : %s ", e.getMessage()), e);
326 throw new MDSALStoreException(e);
330 private String getNodeName() throws MDSALStoreException {
332 HttpResponse response = client.doGet(Constants.GET_SHARD_LIST_PATH);
333 int httpCode = response.getStatusLine().getStatusCode();
334 String respBody = IOUtils.toString(response.getEntity().getContent());
335 logger.debug(String.format("Get shard list returned Code: %s. Response: %s ", httpCode, respBody));
336 if (httpCode == 200) {
337 JsonNode responseValue = mapper.readTree(respBody).get(Constants.JSON_RESPONSE_VALUE);
338 if (responseValue != null && responseValue.get(Constants.JSON_RESPONSE_MEMBER_NAME) != null) {
339 String name = responseValue.get(Constants.JSON_RESPONSE_MEMBER_NAME).asText();
340 logger.debug("Node name : " + name);
343 logger.error(String.format("Error while retrieving node name from response. Response body: %s.", respBody));
344 throw new MDSALStoreException(String.format("Error while retrieving node name from response. Response body: %s.", respBody));
347 logger.error(String.format("Error while retrieving node name. Error code: %s. Error response: %s.", httpCode, respBody));
348 throw new MDSALStoreException(String.format("Error while retrieving node name. Error code: %s. Error response: %s.", httpCode, respBody));
350 } catch (IOException | APPCException e) {
351 logger.error("Error while getting node name " + e.getMessage(), e);
352 throw new MDSALStoreException(e);
356 protected RestClientInvoker getRestClientInvoker(URL configUrl) throws MalformedURLException {
357 return new RestClientInvoker(configUrl);