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