diff options
Diffstat (limited to 'appc-dg/appc-dg-shared/appc-dg-mdsal-store')
21 files changed, 1270 insertions, 786 deletions
diff --git a/appc-dg/appc-dg-shared/appc-dg-mdsal-store/appc-dg-mdsal-bundle/pom.xml b/appc-dg/appc-dg-shared/appc-dg-mdsal-store/appc-dg-mdsal-bundle/pom.xml new file mode 100644 index 000000000..9a16ca65a --- /dev/null +++ b/appc-dg/appc-dg-shared/appc-dg-mdsal-store/appc-dg-mdsal-bundle/pom.xml @@ -0,0 +1,218 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + ============LICENSE_START======================================================= + ONAP : APPC + ================================================================================ + Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + ================================================================================ + Copyright (C) 2017 Amdocs + ============================================================================= + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + + ECOMP is a trademark and service mark of AT&T Intellectual Property. + ============LICENSE_END========================================================= + --> +<project xmlns="http://maven.apache.org/POM/4.0.0" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> + <parent> + <groupId>org.onap.appc</groupId> + <artifactId>appc-dg-mdsal-store</artifactId> + <version>1.3.0-SNAPSHOT</version> + </parent> + + <artifactId>appc-dg-mdsal-bundle</artifactId> + <packaging>bundle</packaging> + + <properties> + <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> + <licenseDir>${project.parent.parent.parent.basedir}</licenseDir> + </properties> + + <build> + <plugins> + <plugin> + <groupId>org.apache.felix</groupId> + <artifactId>maven-bundle-plugin</artifactId> + <version>${bundle.plugin.version}</version> + <extensions>true</extensions> + <configuration> + <instructions> + <Export-Package>org.onap.appc.mdsal.*</Export-Package> + <Import-Package>*;resolution:=optional</Import-Package> + <DynamicImport-Package>*</DynamicImport-Package> + <Embed-Transitive>true</Embed-Transitive> + </instructions> + </configuration> + </plugin> + <plugin> + <groupId>org.opendaylight.yangtools</groupId> + <artifactId>yang-maven-plugin</artifactId> + <version>${odl.yangtools.version}</version> + <executions> + <execution> + <id>config</id> + <goals> + <goal>generate-sources</goal> + </goals> + <configuration> + <codeGenerators> + <generator> + <codeGeneratorClass> + org.opendaylight.controller.config.yangjmxgenerator.plugin.JMXGenerator + </codeGeneratorClass> + <outputBaseDir>${jmxGeneratorPath}</outputBaseDir> + <additionalConfiguration> + <namespaceToPackage1> + urn:opendaylight:params:xml:ns:yang:controller==org.opendaylight.controller.config.yang + </namespaceToPackage1> + </additionalConfiguration> + </generator> + <generator> + <codeGeneratorClass> + org.opendaylight.mdsal.binding.maven.api.gen.plugin.CodeGeneratorImpl + </codeGeneratorClass> + <outputBaseDir>${salGeneratorPath}</outputBaseDir> + </generator> + </codeGenerators> + <inspectDependencies>true</inspectDependencies> + </configuration> + </execution> + </executions> + <dependencies> + <dependency> + <groupId>org.opendaylight.mdsal</groupId> + <artifactId>maven-sal-api-gen-plugin</artifactId> + <version>${odl.sal.api.gen.plugin.version}</version> + <type>jar</type> + </dependency> + <dependency> + <groupId>org.opendaylight.controller</groupId> + <artifactId>yang-jmx-generator-plugin</artifactId> + <version>${odl.yang.jmx.generator.version}</version> + </dependency> + </dependencies> + </plugin> + <plugin> + <groupId>org.codehaus.mojo</groupId> + <artifactId>build-helper-maven-plugin</artifactId> + <executions> + <execution> + <id>attach-artifacts</id> + <goals> + <goal>attach-artifact</goal> + </goals> + <phase>package</phase> + <configuration> + <artifacts> + <artifact> + <file>${project.build.directory}/classes/initial/appc-mdsal-store.xml</file> + <type>xml</type> + <classifier>config</classifier> + </artifact> + </artifacts> + </configuration> + </execution> + </executions> + </plugin> + </plugins> + </build> + + <dependencies> + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.opendaylight.controller</groupId> + <artifactId>config-api</artifactId> + </dependency> + <dependency> + <groupId>org.opendaylight.controller</groupId> + <artifactId>sal-binding-config</artifactId> + </dependency> + <dependency> + <groupId>org.opendaylight.controller</groupId> + <artifactId>sal-binding-api</artifactId> + </dependency> + <dependency> + <groupId>org.opendaylight.controller</groupId> + <artifactId>sal-common-util</artifactId> + </dependency> + <dependency> + <artifactId>sal-test-model</artifactId> + <groupId>org.opendaylight.controller</groupId> + <scope>test</scope> + </dependency> + <dependency> + <artifactId>sal-rest-connector</artifactId> + <groupId>org.opendaylight.netconf</groupId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.opendaylight.controller</groupId> + <artifactId>sal-binding-broker-impl</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.opendaylight.controller</groupId> + <artifactId>sal-binding-broker-impl</artifactId> + <classifier>tests</classifier> + <version>${odl.mdsal.version}</version> + <type>test-jar</type> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.onap.ccsdk.sli.core</groupId> + <artifactId>sli-common</artifactId> + <scope>compile</scope> + <!-- Added exclusion to prevent missing dependency issue on dblib --> + <exclusions> + <exclusion> + <groupId>org.onap.ccsdk.sli.core</groupId> + <artifactId>dblib-provider</artifactId> + </exclusion> + </exclusions> + </dependency> + + <dependency> + <groupId>org.onap.ccsdk.sli.core</groupId> + <artifactId>sli-provider</artifactId> + <scope>compile</scope> + <!-- Added exclusion to prevent missing dependency issue on dblib --> + <exclusions> + <exclusion> + <groupId>org.onap.ccsdk.sli.core</groupId> + <artifactId>dblib-provider</artifactId> + </exclusion> + </exclusions> + </dependency> + + <dependency> + <groupId>org.onap.appc</groupId> + <artifactId>appc-common</artifactId> + <version>${project.version}</version> + </dependency> + <dependency> + <groupId>org.onap.appc</groupId> + <artifactId>appc-dg-mdsal-model</artifactId> + <version>${project.version}</version> + </dependency> + <dependency> + <groupId>commons-io</groupId> + <artifactId>commons-io</artifactId> + </dependency> + </dependencies> +</project> diff --git a/appc-dg/appc-dg-shared/appc-dg-mdsal-store/src/main/java/org/onap/appc/mdsal/MDSALStore.java b/appc-dg/appc-dg-shared/appc-dg-mdsal-store/appc-dg-mdsal-bundle/src/main/java/org/onap/appc/mdsal/MDSALStore.java index eba364a10..19fff2b5c 100644 --- a/appc-dg/appc-dg-shared/appc-dg-mdsal-store/src/main/java/org/onap/appc/mdsal/MDSALStore.java +++ b/appc-dg/appc-dg-shared/appc-dg-mdsal-store/appc-dg-mdsal-bundle/src/main/java/org/onap/appc/mdsal/MDSALStore.java @@ -62,4 +62,12 @@ public interface MDSALStore { */ void storeJson(String moduleName , String requestId , String configJSON ) throws MDSALStoreException; + /** + * This method will be used to store yang module to MD-SAL store + * @param yang - yang module that need to be stored. In String format + * @param moduleName - yang module name + * @throws MDSALStoreException + */ + void storeYangModuleOnLeader(String yang, String moduleName) throws MDSALStoreException; + } diff --git a/appc-dg/appc-dg-shared/appc-dg-mdsal-store/src/main/java/org/onap/appc/mdsal/exception/MDSALStoreException.java b/appc-dg/appc-dg-shared/appc-dg-mdsal-store/appc-dg-mdsal-bundle/src/main/java/org/onap/appc/mdsal/exception/MDSALStoreException.java index 2bb30b1c2..2bb30b1c2 100644 --- a/appc-dg/appc-dg-shared/appc-dg-mdsal-store/src/main/java/org/onap/appc/mdsal/exception/MDSALStoreException.java +++ b/appc-dg/appc-dg-shared/appc-dg-mdsal-store/appc-dg-mdsal-bundle/src/main/java/org/onap/appc/mdsal/exception/MDSALStoreException.java diff --git a/appc-dg/appc-dg-shared/appc-dg-mdsal-store/src/main/java/org/onap/appc/mdsal/impl/Constants.java b/appc-dg/appc-dg-shared/appc-dg-mdsal-store/appc-dg-mdsal-bundle/src/main/java/org/onap/appc/mdsal/impl/Constants.java index bb847477f..e414b4146 100644 --- a/appc-dg/appc-dg-shared/appc-dg-mdsal-store/src/main/java/org/onap/appc/mdsal/impl/Constants.java +++ b/appc-dg/appc-dg-shared/appc-dg-mdsal-store/appc-dg-mdsal-bundle/src/main/java/org/onap/appc/mdsal/impl/Constants.java @@ -26,7 +26,7 @@ package org.onap.appc.mdsal.impl; /** * This class contains the definitions of all constant values used in the appc-dg-mdsal-store * These properties are used for creating osgi bundle zip file. It also defines contents for Blueprint.xml file of bundle - */ +*/ public class Constants { private Constants(){} @@ -122,4 +122,18 @@ public class Constants { * Constant for backslash to be used while formatting URL */ public static final String URL_BACKSLASH ="/"; + + public static final String GET_NODE_STATUS_PATH_FORMAT = "/jolokia/read/org.opendaylight.controller:Category=Shards,name=%s,type=DistributedConfigDatastore"; + + public static final String SHARD_NAME_FORMAT = "%s-shard-default-config"; + + public static final String GET_SHARD_LIST_PATH = "/jolokia/read/org.opendaylight.controller:type=DistributedConfigDatastore,Category=ShardManager,name=shard-manager-config"; + + public static final String YANG_LOADER_PATH = "/restconf/operations/mdsal-store:store-yang"; + + public static final String JSON_RESPONSE_VALUE = "value"; + + public static final String JSON_RESPONSE_MEMBER_NAME = "MemberName"; + + public static final String SELF = "self"; } diff --git a/appc-dg/appc-dg-shared/appc-dg-mdsal-store/src/main/java/org/onap/appc/mdsal/impl/MDSALStoreFactory.java b/appc-dg/appc-dg-shared/appc-dg-mdsal-store/appc-dg-mdsal-bundle/src/main/java/org/onap/appc/mdsal/impl/MDSALStoreFactory.java index bc951d926..bc951d926 100644 --- a/appc-dg/appc-dg-shared/appc-dg-mdsal-store/src/main/java/org/onap/appc/mdsal/impl/MDSALStoreFactory.java +++ b/appc-dg/appc-dg-shared/appc-dg-mdsal-store/appc-dg-mdsal-bundle/src/main/java/org/onap/appc/mdsal/impl/MDSALStoreFactory.java diff --git a/appc-dg/appc-dg-shared/appc-dg-mdsal-store/appc-dg-mdsal-bundle/src/main/java/org/onap/appc/mdsal/impl/MDSALStoreImpl.java b/appc-dg/appc-dg-shared/appc-dg-mdsal-store/appc-dg-mdsal-bundle/src/main/java/org/onap/appc/mdsal/impl/MDSALStoreImpl.java new file mode 100644 index 000000000..fcd315bf6 --- /dev/null +++ b/appc-dg/appc-dg-shared/appc-dg-mdsal-store/appc-dg-mdsal-bundle/src/main/java/org/onap/appc/mdsal/impl/MDSALStoreImpl.java @@ -0,0 +1,354 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP : APPC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Copyright (C) 2017 Amdocs + * ============================================================================= + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + * ============LICENSE_END========================================================= + */ + +package org.onap.appc.mdsal.impl; + +import com.fasterxml.jackson.annotation.JsonIgnore; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.annotation.JsonValue; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.apache.commons.io.IOUtils; +import org.apache.http.HttpResponse; +import org.opendaylight.yang.gen.v1.org.onap.appc.mdsal.store.rev170925.StoreYangInput; +import org.opendaylight.yang.gen.v1.org.onap.appc.mdsal.store.rev170925.StoreYangInputBuilder; +import org.opendaylight.yangtools.yang.binding.DataContainer; +import org.onap.appc.configuration.Configuration; +import org.onap.appc.configuration.ConfigurationFactory; +import org.onap.appc.exceptions.APPCException; +import org.onap.appc.mdsal.MDSALStore; +import org.onap.appc.mdsal.exception.MDSALStoreException; +import org.onap.appc.mdsal.objects.BundleInfo; +import com.att.eelf.configuration.EELFLogger; +import com.att.eelf.configuration.EELFManager; +import org.onap.appc.mdsal.operation.ConfigOperationRequestFormatter; +import org.onap.appc.rest.client.RestClientInvoker; +import org.osgi.framework.Bundle; +import org.osgi.framework.BundleContext; +import org.osgi.framework.FrameworkUtil; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.net.MalformedURLException; +import java.net.URL; +import java.util.*; +import java.util.jar.Attributes; +import java.util.jar.JarEntry; +import java.util.jar.JarOutputStream; +import java.util.jar.Manifest; + +/** + * Implementation of MDSALStore + */ +public class MDSALStoreImpl implements MDSALStore { + + private final EELFLogger logger = EELFManager.getInstance().getLogger(MDSALStoreImpl.class); + private RestClientInvoker client; + private ConfigOperationRequestFormatter requestFormatter = new ConfigOperationRequestFormatter(); + private ObjectMapper mapper = new ObjectMapper(); + private Map<String, RestClientInvoker> remoteClientMap = new HashMap<>(); + + MDSALStoreImpl() { + String configUrl = null; + String user = null; + String password = null; + Configuration configuration = ConfigurationFactory.getConfiguration(); + Properties properties = configuration.getProperties(); + if (properties != null) { + configUrl = properties.getProperty(Constants.CONFIG_URL_PROPERTY, Constants.CONFIG_URL_DEFAULT); + user = properties.getProperty(Constants.CONFIG_USER_PROPERTY); + password = properties.getProperty(Constants.CONFIG_PASS_PROPERTY); + } + if (configUrl != null) { + try { + client = new RestClientInvoker(new URL(configUrl)); + client.setAuthentication(user, password); + } catch (MalformedURLException e) { + logger.error("Error initializing RestConf client: " + e.getMessage(), e); + } + } + } + + + @Override + public boolean isModulePresent(String moduleName, Date revision) { + + if (logger.isDebugEnabled()) { + logger.debug("isModulePresent invoked with moduleName = " + moduleName + " , revision = " + revision); + } + + BundleContext bundleContext = FrameworkUtil.getBundle(this.getClass()).getBundleContext(); + /* + * SchemaContext interface of ODL provides APIs for querying details of yang modules + * loaded into MD-SAL store, but its limitation is, it only returns information about + * static yang modules loaded on server start up, it does not return information about + * the yang modules loaded dynamically. Due to this limitation, we are checking the + * presence of OSGI bundle instead of yang module. (Note: Assuming OSGI bundle is named + * with the yang module name). + */ + + Bundle bundle = bundleContext.getBundle(moduleName); + if (logger.isDebugEnabled()) { + logger.debug("isModulePresent returned = " + (bundle != null)); + } + return bundle != null; + } + + @Override + public void storeYangModule(String yang, BundleInfo bundleInfo) throws MDSALStoreException { + + BundleContext bundleContext = FrameworkUtil.getBundle(this.getClass()).getBundleContext(); + byte[] byteArray = createBundleJar(yang, Constants.BLUEPRINT, bundleInfo); + + try (ByteArrayInputStream inputStream = new ByteArrayInputStream(byteArray)) { + Bundle bundle = bundleContext.installBundle(bundleInfo.getLocation(), inputStream); + bundle.start(); + } catch (Exception e) { + logger.error(String.format("Error storing yang module: %s. Error message: %s.", yang, e.getMessage())); + throw new MDSALStoreException("Error storing yang module: " + yang + " " + e.getMessage(), e); + } + } + + @Override + public void storeYangModuleOnLeader(String yang, String moduleName) throws MDSALStoreException { + try { + String leader = getLeaderNode(); + if (Constants.SELF.equals(leader)){ + logger.debug("Current node is a leader."); + }else{ + logger.debug("Attempting to load yang module on Leader: " + leader ); + String inputJson = createInputJson(yang, moduleName); + RestClientInvoker remoteClient = getRemoteClient(leader); + HttpResponse response = remoteClient.doPost(Constants.YANG_LOADER_PATH, inputJson); + int httpCode = response.getStatusLine().getStatusCode(); + String respBody = IOUtils.toString(response.getEntity().getContent()); + if (httpCode < 200 || httpCode >= 300) { + logger.debug("Error while loading yang module on leader. Response code: " + httpCode); + processRestconfResponse(respBody); + } else { + logger.debug("Yang module successfully loaded on leader. Response code: " + httpCode); + } + } + } catch (APPCException e) { + logger.error("Error loading Yang on Leader. Error message: " + e.getMessage()); + throw new MDSALStoreException("Error loading Yang on Leader. Error message: " + e.getMessage(), e); + } catch (IOException e) { + logger.error("Error reading response from remote client. Error message: " + e.getMessage()); + throw new MDSALStoreException("Error reading response from remote client. Error message: " + e.getMessage(), e); + } + } + + private String createInputJson(String yang, String moduleName) throws MDSALStoreException { + StoreYangInputBuilder builder = new StoreYangInputBuilder(); + builder.setYang(yang).setModuleName(moduleName); + StoreYangInput input = builder.build(); + try { + ObjectMapper objectMapper = new ObjectMapper(); + objectMapper.addMixInAnnotations(StoreYangInput.class, MixIn.class); + String inputJson = objectMapper.writer().withRootName("input").writeValueAsString(input); + logger.debug("Input JSON :" + inputJson); + return inputJson; + } catch (JsonProcessingException e) { + logger.error(String.format("Error creating JSON input using yang: %s. Error message: %s",yang ,e.getMessage())); + throw new MDSALStoreException(String.format("Error creating JSON input using yang: %s. Error message: %s",yang ,e.getMessage()), e); + } + } + + private RestClientInvoker getRemoteClient(String leader) throws MDSALStoreException { + if (remoteClientMap.containsKey(leader)) { + return remoteClientMap.get(leader); + } else { + Configuration configuration = ConfigurationFactory.getConfiguration(); + Properties properties = configuration.getProperties(); + if (properties != null) { + try { + URL configUrl = new URL(properties.getProperty(Constants.CONFIG_URL_PROPERTY, Constants.CONFIG_URL_DEFAULT)); + String user = properties.getProperty(Constants.CONFIG_USER_PROPERTY); + String password = properties.getProperty(Constants.CONFIG_PASS_PROPERTY); + RestClientInvoker remoteClient = new RestClientInvoker(new URL(configUrl.getProtocol(), leader, configUrl.getPort(), "")); + remoteClient.setAuthentication(user, password); + remoteClientMap.put(leader, remoteClient); + return remoteClient; + } catch (MalformedURLException e) { + logger.error("Error initializing remote RestConf client: " + e.getMessage(), e); + throw new MDSALStoreException("Error initializing Remote RestConf client: " + e.getMessage(), e); + } + } else { + logger.error("Error initializing Remote RestConf client. Could not read appc properties"); + throw new MDSALStoreException("Error initializing Remote RestConf client. Could not read appc properties"); + } + } + } + + abstract class MixIn { + @JsonIgnore + abstract Class<? extends DataContainer> getImplementedInterface(); // to be removed during serialization + + @JsonValue + abstract java.lang.String getValue(); + + @JsonProperty("module-name") + abstract java.lang.String getModuleName(); + } + + @Override + public void storeJson(String module, String requestId, String configJson) throws MDSALStoreException { + if (configJson == null) { + throw new MDSALStoreException("Configuration JSON is empty or null"); + } + logger.debug("Configuration JSON: " + configJson + "\n" + "module" + module); + try { + String path = requestFormatter.buildPath(module, org.onap.appc.Constants.YANG_BASE_CONTAINER, + org.onap.appc.Constants.YANG_VNF_CONFIG_LIST, requestId, org.onap.appc.Constants.YANG_VNF_CONFIG); + logger.debug("Configuration Path : " + path); + HttpResponse response = client.doPut(path, configJson); + int httpCode = response.getStatusLine().getStatusCode(); + String respBody = IOUtils.toString(response.getEntity().getContent()); + if (httpCode < 200 || httpCode >= 300) { + logger.debug("Error while storing configuration JSON to MD-SAL store. Response code: " + httpCode); + processRestconfResponse(respBody); + } else { + logger.debug("Configuration JSON stored to MD-SAL store successfully. Response code: " + httpCode); + } + } catch (IOException | APPCException e) { + logger.error("Error while storing configuration json. Error Message" + e.getMessage(), e); + throw new MDSALStoreException(e); + } + } + + private void processRestconfResponse(String response) throws MDSALStoreException { + try { + JsonNode responseJson = mapper.readTree(response); + ArrayList<String> errorMessage = new ArrayList<>(); + if (responseJson != null && responseJson.get("errors") != null) { + JsonNode errors = responseJson.get("errors").get("error"); + for (Iterator<JsonNode> i = errors.elements(); i.hasNext(); ) { + JsonNode error = i.next(); + errorMessage.add(error.get("error-message").textValue()); + } + } + logger.error("Failed to load config JSON to MD SAL store. " + errorMessage.toString()); + throw new MDSALStoreException("Failed to load config JSON to MD SAL store. Error Message: " + errorMessage.toString()); + } catch (IOException e) { + logger.error("Failed to process error response from RestConf: " + e.getMessage()); + throw new MDSALStoreException("Failed to process RestConf response. Error Message: " + e.toString(), e); + } + } + + private byte[] createBundleJar(String yang, String blueprint, BundleInfo bundleInfo) throws MDSALStoreException { + + Manifest manifest = new Manifest(); + manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, Constants.MANIFEST_VALUE_VERSION); + manifest.getMainAttributes().put(new Attributes.Name(Constants.MANIFEST_ATTR_BUNDLE_NAME), bundleInfo.getName()); + manifest.getMainAttributes().put(new Attributes.Name(Constants.MANIFEST_ATTR_BUNDLE_SYMBOLIC_NAME), bundleInfo.getName()); + manifest.getMainAttributes().put(new Attributes.Name(Constants.MANIFEST_ATTR_BUNDLE_DESCRIPTION), bundleInfo.getDescription()); + manifest.getMainAttributes().put(new Attributes.Name(Constants.MANIFEST_ATTR_BUNDLE_MANIFEST_VERSION), Constants.MANIFEST_VALUE_BUNDLE_MAN_VERSION); + manifest.getMainAttributes().put(new Attributes.Name(Constants.MANIFEST_ATTR_BUNDLE_VERSION), String.valueOf(bundleInfo.getVersion())); + manifest.getMainAttributes().put(new Attributes.Name(Constants.MANIFEST_ATTR_BUNDLE_BLUEPRINT), Constants.MANIFEST_VALUE_BUNDLE_BLUEPRINT); + + byte[] retunValue; + + try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + JarOutputStream jarOutputStream = new JarOutputStream(outputStream, manifest)) { + jarOutputStream.putNextEntry(new JarEntry("META-INF/yang/")); + jarOutputStream.putNextEntry(new JarEntry("META-INF/yang/" + bundleInfo.getName() + ".yang")); + jarOutputStream.write(yang.getBytes()); + jarOutputStream.closeEntry(); + + jarOutputStream.putNextEntry(new JarEntry("OSGI-INF/blueprint/")); + jarOutputStream.putNextEntry(new JarEntry(Constants.MANIFEST_VALUE_BUNDLE_BLUEPRINT)); + jarOutputStream.write(blueprint.getBytes()); + jarOutputStream.closeEntry(); + jarOutputStream.close(); + retunValue = outputStream.toByteArray(); + } catch (Exception e) { + logger.error("Error creating bundle jar: " + bundleInfo.getName() + ". Error message: " + e.getMessage()); + throw new MDSALStoreException("Error creating bundle jar: " + bundleInfo.getName() + " " + e.getMessage(), e); + } + return retunValue; + } + + private String getLeaderNode() throws MDSALStoreException { + try { + String shardName = String.format(Constants.SHARD_NAME_FORMAT, getNodeName()); + HttpResponse response = client.doGet(String.format(Constants.GET_NODE_STATUS_PATH_FORMAT, shardName)); + int httpCode = response.getStatusLine().getStatusCode(); + String respBody = IOUtils.toString(response.getEntity().getContent()); + logger.debug(String.format("Get node status returned Code: %s. Response: %s ", httpCode, respBody)); + if (httpCode == 200 && mapper.readTree(respBody).get(Constants.JSON_RESPONSE_VALUE) !=null ) { + JsonNode responseValue = mapper.readTree(respBody).get(Constants.JSON_RESPONSE_VALUE); + String leaderShard = responseValue.get("Leader").asText(); + if (shardName.equals(leaderShard)) { + logger.debug("Current node is leader."); + return Constants.SELF; + } else { + String[] peers = responseValue.get("PeerAddresses").asText().split(","); + for (String peer : peers) { + if (peer.trim().startsWith(leaderShard)) { + String leader = peer.substring(peer.indexOf('@') + 1, peer.indexOf(':', peer.indexOf('@'))); + logger.debug(String.format("Node %s is a leader", leader)); + return leader; + } + } + logger.error("No Leader found for a cluster"); + throw new MDSALStoreException("No Leader found for a cluster"); + } + } else { + logger.error("Error while retrieving leader node."); + throw new MDSALStoreException("Error while retrieving leader node."); + } + } catch (IOException | APPCException e) { + logger.error(String.format("Error while retrieving leader Node. Error message : %s ", e.getMessage()), e); + throw new MDSALStoreException(e); + } + } + + private String getNodeName() throws MDSALStoreException { + try { + HttpResponse response = client.doGet(Constants.GET_SHARD_LIST_PATH); + int httpCode = response.getStatusLine().getStatusCode(); + String respBody = IOUtils.toString(response.getEntity().getContent()); + logger.debug(String.format("Get shard list returned Code: %s. Response: %s ", httpCode, respBody)); + if (httpCode == 200) { + JsonNode responseValue = mapper.readTree(respBody).get(Constants.JSON_RESPONSE_VALUE); + if (responseValue != null && responseValue.get(Constants.JSON_RESPONSE_MEMBER_NAME) != null) { + String name = responseValue.get(Constants.JSON_RESPONSE_MEMBER_NAME).asText(); + logger.debug("Node name : " + name); + return name; + }else{ + logger.error(String.format("Error while retrieving node name from response. Response body: %s.", respBody)); + throw new MDSALStoreException(String.format("Error while retrieving node name from response. Response body: %s.", respBody)); + } + } else { + logger.error(String.format("Error while retrieving node name. Error code: %s. Error response: %s.", httpCode, respBody)); + throw new MDSALStoreException(String.format("Error while retrieving node name. Error code: %s. Error response: %s.", httpCode, respBody)); + } + } catch (IOException | APPCException e) { + logger.error("Error while getting node name " + e.getMessage(), e); + throw new MDSALStoreException(e); + } + } +} diff --git a/appc-dg/appc-dg-shared/appc-dg-mdsal-store/src/main/java/org/onap/appc/mdsal/objects/BundleInfo.java b/appc-dg/appc-dg-shared/appc-dg-mdsal-store/appc-dg-mdsal-bundle/src/main/java/org/onap/appc/mdsal/objects/BundleInfo.java index b6d65447d..b6d65447d 100644 --- a/appc-dg/appc-dg-shared/appc-dg-mdsal-store/src/main/java/org/onap/appc/mdsal/objects/BundleInfo.java +++ b/appc-dg/appc-dg-shared/appc-dg-mdsal-store/appc-dg-mdsal-bundle/src/main/java/org/onap/appc/mdsal/objects/BundleInfo.java diff --git a/appc-dg/appc-dg-shared/appc-dg-mdsal-store/src/main/java/org/onap/appc/mdsal/operation/ConfigOperationRequestFormatter.java b/appc-dg/appc-dg-shared/appc-dg-mdsal-store/appc-dg-mdsal-bundle/src/main/java/org/onap/appc/mdsal/operation/ConfigOperationRequestFormatter.java index 117349eea..117349eea 100644 --- a/appc-dg/appc-dg-shared/appc-dg-mdsal-store/src/main/java/org/onap/appc/mdsal/operation/ConfigOperationRequestFormatter.java +++ b/appc-dg/appc-dg-shared/appc-dg-mdsal-store/appc-dg-mdsal-bundle/src/main/java/org/onap/appc/mdsal/operation/ConfigOperationRequestFormatter.java diff --git a/appc-dg/appc-dg-shared/appc-dg-mdsal-store/appc-dg-mdsal-bundle/src/main/java/org/onap/appc/mdsal/provider/MdsalStoreProvider.java b/appc-dg/appc-dg-shared/appc-dg-mdsal-store/appc-dg-mdsal-bundle/src/main/java/org/onap/appc/mdsal/provider/MdsalStoreProvider.java new file mode 100644 index 000000000..f4d5ad159 --- /dev/null +++ b/appc-dg/appc-dg-shared/appc-dg-mdsal-store/appc-dg-mdsal-bundle/src/main/java/org/onap/appc/mdsal/provider/MdsalStoreProvider.java @@ -0,0 +1,127 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP : APPC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Copyright (C) 2017 Amdocs + * ============================================================================= + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + * ============LICENSE_END========================================================= + */ + +package org.onap.appc.mdsal.provider; + +import com.att.eelf.configuration.EELFLogger; +import com.att.eelf.configuration.EELFManager; +import com.google.common.util.concurrent.Futures; +import org.opendaylight.controller.md.sal.binding.api.DataBroker; +import org.opendaylight.controller.sal.binding.api.BindingAwareBroker; +import org.opendaylight.controller.sal.binding.api.NotificationProviderService; +import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry; +import org.opendaylight.yang.gen.v1.org.onap.appc.mdsal.store.rev170925.MdsalStoreService; +import org.opendaylight.yang.gen.v1.org.onap.appc.mdsal.store.rev170925.StoreYangInput; +import org.opendaylight.yang.gen.v1.org.onap.appc.mdsal.store.rev170925.StoreYangOutput; +import org.opendaylight.yang.gen.v1.org.onap.appc.mdsal.store.rev170925.StoreYangOutputBuilder; +import org.opendaylight.yang.gen.v1.org.onap.appc.mdsal.store.rev170925.response.Status; +import org.opendaylight.yang.gen.v1.org.onap.appc.mdsal.store.rev170925.response.StatusBuilder; +import org.opendaylight.yangtools.yang.common.RpcResult; +import org.opendaylight.yangtools.yang.common.RpcResultBuilder; +import org.onap.appc.Constants; +import org.onap.appc.mdsal.MDSALStore; +import org.onap.appc.mdsal.impl.MDSALStoreFactory; +import org.onap.appc.mdsal.objects.BundleInfo; + +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; + +public class MdsalStoreProvider implements MdsalStoreService ,AutoCloseable{ + + protected DataBroker dataBroker; + protected RpcProviderRegistry rpcRegistry; + protected NotificationProviderService notificationService; + + protected BindingAwareBroker.RpcRegistration<MdsalStoreService> rpcRegistration; + private final EELFLogger log = EELFManager.getInstance().getLogger(MdsalStoreProvider.class); + private final ExecutorService executor; + private final static String APP_NAME = "MdsalStoreProvider"; + + public MdsalStoreProvider(DataBroker dataBroker2, NotificationProviderService notificationProviderService + , RpcProviderRegistry rpcRegistry2){ + log.info("Creating provider for " + APP_NAME); + executor = Executors.newFixedThreadPool(1); + this.dataBroker = dataBroker2; + this.notificationService = notificationProviderService; + + this.rpcRegistry = rpcRegistry2; + + if (this.rpcRegistry != null) { + rpcRegistration = rpcRegistry.addRpcImplementation(MdsalStoreService.class, this); + } + log.info("Initialization complete for " + APP_NAME); + + } + + @Override + public void close() throws Exception { + log.info("Closing provider for " + APP_NAME); + if(this.executor != null){ + executor.shutdown(); + } + if(this.rpcRegistration != null){ + rpcRegistration.close(); + } + log.info("Successfully closed provider for " + APP_NAME); + } + + @Override + public Future<RpcResult<StoreYangOutput>> storeYang(StoreYangInput input) { + Status status =null;String message=null; + try{ + BundleInfo bundleInfo = new BundleInfo(); + bundleInfo.setName(input.getModuleName()); + bundleInfo.setDescription(input.getModuleName()); + bundleInfo.setLocation(input.getModuleName()); + + MDSALStore store = MDSALStoreFactory.createMDSALStore(); + + Date revision = new SimpleDateFormat(Constants.YANG_REVISION_FORMAT).parse(Constants.YANG_REVISION); + if(!store.isModulePresent(input.getModuleName(),revision)){ + message = "YANG module saved successfully"; + store.storeYangModule(input.getYang(),bundleInfo); + } + else{ + message = "YANG Module already available"; + } + store.storeYangModuleOnLeader(input.getYang(),input.getModuleName()); + status = new StatusBuilder().setCode(200).setMessage(message).build(); + } + catch (Exception e){ + + message = "Error in storeYang of MdsalStoreProvider"; + log.error(message,e); + status = new StatusBuilder().setCode(500).setMessage(message).build(); + } + StoreYangOutputBuilder builder = new StoreYangOutputBuilder().setStatus(status); + return Futures.immediateFuture( + RpcResultBuilder + .<StoreYangOutput>status(true) + .withResult(builder.build()) + .build()); + } +} diff --git a/appc-dg/appc-dg-shared/appc-dg-mdsal-store/appc-dg-mdsal-bundle/src/main/java/org/opendaylight/yang/gen/v1/org/onap/appc/mdsal/store/impl/rev170925/MdsalStoreProviderModule.java b/appc-dg/appc-dg-shared/appc-dg-mdsal-store/appc-dg-mdsal-bundle/src/main/java/org/opendaylight/yang/gen/v1/org/onap/appc/mdsal/store/impl/rev170925/MdsalStoreProviderModule.java new file mode 100644 index 000000000..50e9874dc --- /dev/null +++ b/appc-dg/appc-dg-shared/appc-dg-mdsal-store/appc-dg-mdsal-bundle/src/main/java/org/opendaylight/yang/gen/v1/org/onap/appc/mdsal/store/impl/rev170925/MdsalStoreProviderModule.java @@ -0,0 +1,58 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP : APPC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Copyright (C) 2017 Amdocs + * ============================================================================= + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + * ============LICENSE_END========================================================= + */ + +package org.opendaylight.yang.gen.v1.org.onap.appc.mdsal.store.impl.rev170925; + +import org.onap.appc.mdsal.provider.MdsalStoreProvider; + +public class MdsalStoreProviderModule extends org.opendaylight.yang.gen.v1.org.onap.appc.mdsal.store.impl.rev170925.AbstractMdsalStoreProviderModule { + public MdsalStoreProviderModule(org.opendaylight.controller.config.api.ModuleIdentifier identifier, org.opendaylight.controller.config.api.DependencyResolver dependencyResolver) { + super(identifier, dependencyResolver); + } + + public MdsalStoreProviderModule(org.opendaylight.controller.config.api.ModuleIdentifier identifier, org.opendaylight.controller.config.api.DependencyResolver dependencyResolver, org.opendaylight.yang.gen.v1.org.onap.appc.mdsal.store.impl.rev170925.MdsalStoreProviderModule oldModule, java.lang.AutoCloseable oldInstance) { + super(identifier, dependencyResolver, oldModule, oldInstance); + } + + @Override + public void customValidation() { + // add custom validation form module attributes here. + } + + @Override + public java.lang.AutoCloseable createInstance() { + MdsalStoreProvider provider = new MdsalStoreProvider(getDataBrokerDependency() + ,getNotificationServiceDependency() + ,getRpcRegistryDependency()); + + return new AutoCloseable() { + + @Override + public void close() throws Exception { + provider.close(); + } + }; + } + +} diff --git a/appc-dg/appc-dg-shared/appc-dg-mdsal-store/appc-dg-mdsal-bundle/src/main/java/org/opendaylight/yang/gen/v1/org/onap/appc/mdsal/store/impl/rev170925/MdsalStoreProviderModuleFactory.java b/appc-dg/appc-dg-shared/appc-dg-mdsal-store/appc-dg-mdsal-bundle/src/main/java/org/opendaylight/yang/gen/v1/org/onap/appc/mdsal/store/impl/rev170925/MdsalStoreProviderModuleFactory.java new file mode 100644 index 000000000..0c3254889 --- /dev/null +++ b/appc-dg/appc-dg-shared/appc-dg-mdsal-store/appc-dg-mdsal-bundle/src/main/java/org/opendaylight/yang/gen/v1/org/onap/appc/mdsal/store/impl/rev170925/MdsalStoreProviderModuleFactory.java @@ -0,0 +1,37 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP : APPC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Copyright (C) 2017 Amdocs + * ============================================================================= + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + * ============LICENSE_END========================================================= + */ + +/* +* Generated file +* +* Generated from: yang module name: mdsal-store-impl yang module local name: mdsal-store-impl +* Generated by: org.opendaylight.controller.config.yangjmxgenerator.plugin.JMXGenerator +* Generated at: Tue Sep 26 15:55:55 IST 2017 +* +* Do not modify this file unless it is present under src/main directory +*/ +package org.opendaylight.yang.gen.v1.org.onap.appc.mdsal.store.impl.rev170925; +public class MdsalStoreProviderModuleFactory extends org.opendaylight.yang.gen.v1.org.onap.appc.mdsal.store.impl.rev170925.AbstractMdsalStoreProviderModuleFactory { + +} diff --git a/appc-dg/appc-dg-shared/appc-dg-mdsal-store/appc-dg-mdsal-bundle/src/main/resources/initial/appc-mdsal-store.xml b/appc-dg/appc-dg-shared/appc-dg-mdsal-store/appc-dg-mdsal-bundle/src/main/resources/initial/appc-mdsal-store.xml new file mode 100644 index 000000000..c14838756 --- /dev/null +++ b/appc-dg/appc-dg-shared/appc-dg-mdsal-store/appc-dg-mdsal-bundle/src/main/resources/initial/appc-mdsal-store.xml @@ -0,0 +1,75 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + ============LICENSE_START======================================================= + ONAP : APPC + ================================================================================ + Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + ================================================================================ + Copyright (C) 2017 Amdocs + ============================================================================= + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + + ECOMP is a trademark and service mark of AT&T Intellectual Property. + ============LICENSE_END========================================================= + --> + +<!-- vi: set et smarttab sw=4 tabstop=4: --> +<snapshot> + <configuration> + <data xmlns="urn:ietf:params:xml:ns:netconf:base:1.0"> + <modules xmlns="urn:opendaylight:params:xml:ns:yang:controller:config"> + <module> + + <!-- This xmlns:prefix should match the namespace in the *-impl.yang file + The prefix: inside type should match the prefix of the yang file. --> + <type xmlns:prefix="org:onap:appc:mdsal:store:impl"> + prefix:mdsal-store-impl + </type> + <name>mdsal-store-impl</name> + + <!-- The following sections contain bindings to services defined in the + *-impl yang file. For example the rpc-registry is required + because we have a dependency (or augmentation) named "rpc-registry" + and which binds to the md-sa-binding-registry. If you remove those + dependencies from the yang file then you can remove them from here. --> + <rpc-registry> + <type xmlns:binding="urn:opendaylight:params:xml:ns:yang:controller:md:sal:binding">binding:binding-rpc-registry</type> + <name>binding-rpc-broker</name> + </rpc-registry> + + <data-broker> + <type xmlns:binding="urn:opendaylight:params:xml:ns:yang:controller:md:sal:binding">binding:binding-async-data-broker</type> + <name>binding-data-broker</name> + </data-broker> + + <notification-service> + <type xmlns:binding="urn:opendaylight:params:xml:ns:yang:controller:md:sal:binding"> + binding:binding-notification-service + </type> + <name>binding-notification-broker</name> + </notification-service> + </module> + + </modules> + </data> + + </configuration> + + <!-- Required capabilities are basically a listing of all modules that need to be imported before + our service can be resolved. Capabilities for dependencies defined above are implied which is + why we do not have define a required capability for the data broker, for example. --> + <required-capabilities> + <capability>org:onap:appc:mdsal:store:impl?module=mdsal-store-impl&revision=2017-09-25</capability> + </required-capabilities> + +</snapshot> diff --git a/appc-dg/appc-dg-shared/appc-dg-mdsal-store/appc-dg-mdsal-bundle/src/main/yang/mdsal-store.yang b/appc-dg/appc-dg-shared/appc-dg-mdsal-store/appc-dg-mdsal-bundle/src/main/yang/mdsal-store.yang new file mode 100644 index 000000000..67cd7212b --- /dev/null +++ b/appc-dg/appc-dg-shared/appc-dg-mdsal-store/appc-dg-mdsal-bundle/src/main/yang/mdsal-store.yang @@ -0,0 +1,86 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP : APPC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Copyright (C) 2017 Amdocs + * ============================================================================= + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + * ============LICENSE_END========================================================= + */ + +module mdsal-store-impl { + + yang-version 1; + namespace "org:onap:appc:mdsal:store:impl"; + prefix mdsal-store-impl; + + import config { prefix config; revision-date 2013-04-05; } + import opendaylight-md-sal-binding { prefix mdsal; revision-date 2013-10-28; } + + description + "This module contains the base YANG definitions for + mdsal-store-impl implementation."; + + revision "2017-09-25" { + description + "Initial revision."; + } + + // This is the definition of the service implementation as a module identity. + identity mdsal-store-impl { + base config:module-type; + + // Specifies the prefix for generated java classes. + config:java-name-prefix MdsalStoreProvider; + } + + // Augments the 'configuration' choice node under modules/module. + // We consume the two main services, RPCs and DataStore + // Notification service is not used + augment "/config:modules/config:module/config:configuration" { + case mdsal-store-impl { + when "/config:modules/config:module/config:type = 'mdsal-store-impl'"; + + container rpc-registry { + uses config:service-ref { + refine type { + mandatory true; + config:required-identity mdsal:binding-rpc-registry; + } + } + } + + container notification-service { + uses config:service-ref { + refine type { + mandatory true; + config:required-identity mdsal:binding-notification-service; + } + } + } + + container data-broker { + uses config:service-ref { + refine type { + mandatory false; + config:required-identity mdsal:binding-async-data-broker; + } + } + } + } + } +} diff --git a/appc-dg/appc-dg-shared/appc-dg-mdsal-store/appc-dg-mdsal-bundle/src/test/java/org/onap/appc/mdsal/MDSALStoreTest.java b/appc-dg/appc-dg-shared/appc-dg-mdsal-store/appc-dg-mdsal-bundle/src/test/java/org/onap/appc/mdsal/MDSALStoreTest.java new file mode 100644 index 000000000..aa659a1b2 --- /dev/null +++ b/appc-dg/appc-dg-shared/appc-dg-mdsal-store/appc-dg-mdsal-bundle/src/test/java/org/onap/appc/mdsal/MDSALStoreTest.java @@ -0,0 +1,75 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP : APPC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Copyright (C) 2017 Amdocs + * ============================================================================= + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + * ============LICENSE_END========================================================= + */ + +package org.onap.appc.mdsal; + +import com.fasterxml.jackson.databind.ObjectMapper; +import org.apache.http.HttpResponse; +import org.junit.Before; +import org.junit.Ignore; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mockito; +import org.onap.appc.mdsal.exception.MDSALStoreException; +import org.onap.appc.mdsal.impl.MDSALStoreFactory; +import org.onap.appc.mdsal.impl.MDSALStoreImpl; +import com.att.eelf.configuration.EELFLogger; +import com.att.eelf.configuration.EELFManager; +import org.onap.appc.mdsal.operation.ConfigOperationRequestFormatter; +import org.onap.appc.rest.client.RestClientInvoker; +import org.osgi.framework.*; +import org.powermock.api.mockito.PowerMockito; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; + +import java.net.URL; + +/** + * MDSALStore Tests + */ +@RunWith(PowerMockRunner.class) +@PrepareForTest({FrameworkUtil.class,BundleContext.class,ServiceReference.class, + BundleReference.class,Bundle.class,Filter.class,BundleListener.class,InvalidSyntaxException.class, + BundleException.class,FrameworkListener.class,ServiceRegistration.class,ServiceListener.class, + Version.class}) +public class MDSALStoreTest { + + private final EELFLogger logger = EELFManager.getInstance().getLogger(MDSALStoreTest.class); + private RestClientInvoker client = Mockito.mock(RestClientInvoker.class); + private ConfigOperationRequestFormatter requestFormatter = new ConfigOperationRequestFormatter(); + private ObjectMapper mapper = new ObjectMapper(); + MDSALStoreImpl store; + + @Before + public void init() throws Exception{ + PowerMockito.whenNew(RestClientInvoker.class).withArguments(Mockito.any(URL.class)).thenReturn(client); + store = (MDSALStoreImpl) MDSALStoreFactory.createMDSALStore(); + } + + @Ignore + public void testYangInput() throws MDSALStoreException { + store.storeYangModuleOnLeader("module test { }", "Name"); + } + +} diff --git a/appc-dg/appc-dg-shared/appc-dg-mdsal-store/appc-dg-mdsal-model/.gitignore b/appc-dg/appc-dg-shared/appc-dg-mdsal-store/appc-dg-mdsal-model/.gitignore new file mode 100644 index 000000000..b83d22266 --- /dev/null +++ b/appc-dg/appc-dg-shared/appc-dg-mdsal-store/appc-dg-mdsal-model/.gitignore @@ -0,0 +1 @@ +/target/ diff --git a/appc-dg/appc-dg-shared/appc-dg-mdsal-store/appc-dg-mdsal-model/pom.xml b/appc-dg/appc-dg-shared/appc-dg-mdsal-store/appc-dg-mdsal-model/pom.xml new file mode 100644 index 000000000..e0b1e81c8 --- /dev/null +++ b/appc-dg/appc-dg-shared/appc-dg-mdsal-store/appc-dg-mdsal-model/pom.xml @@ -0,0 +1,111 @@ +<?xml version="1.0"?> +<!-- + ============LICENSE_START======================================================= + ONAP : APPC + ================================================================================ + Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + ================================================================================ + Copyright (C) 2017 Amdocs + ============================================================================= + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + + ECOMP is a trademark and service mark of AT&T Intellectual Property. + ============LICENSE_END========================================================= + --> +<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> + <modelVersion>4.0.0</modelVersion> + <parent> + <groupId>org.onap.appc</groupId> + <artifactId>appc-dg-mdsal-store</artifactId> + <version>1.3.0-SNAPSHOT</version> + </parent> + + <artifactId>appc-dg-mdsal-model</artifactId> + <packaging>bundle</packaging> + + <properties> + <licenseDir>${project.parent.parent.parent.basedir}</licenseDir> + </properties> + + <build> + <plugins> + <plugin> + <groupId>org.apache.felix</groupId> + <artifactId>maven-bundle-plugin</artifactId> + <extensions>true</extensions> + <configuration> + <instructions> + <Import-Package>*</Import-Package> + </instructions> + </configuration> + </plugin> + <plugin> + <groupId>org.opendaylight.yangtools</groupId> + <artifactId>yang-maven-plugin</artifactId> + <version>${odl.yangtools.version}</version> + <dependencies> + <dependency> + <groupId>org.opendaylight.mdsal</groupId> + <artifactId>maven-sal-api-gen-plugin</artifactId> + <version>${odl.sal.api.gen.plugin.version}</version> + <type>jar</type> + </dependency> + </dependencies> + <executions> + <execution> + <goals> + <goal>generate-sources</goal> + </goals> + <configuration> + <yangFilesRootDir>${yang.file.directory}</yangFilesRootDir> + <codeGenerators> + <generator> + <codeGeneratorClass>org.opendaylight.mdsal.binding.maven.api.gen.plugin.CodeGeneratorImpl</codeGeneratorClass> + <outputBaseDir>${salGeneratorPath}</outputBaseDir> + </generator> + </codeGenerators> + <inspectDependencies>true</inspectDependencies> + </configuration> + </execution> + </executions> + </plugin> + </plugins> + </build> + + <dependencies> + <dependency> + <groupId>org.opendaylight.mdsal</groupId> + <artifactId>yang-binding</artifactId> + </dependency> + <dependency> + <groupId>org.opendaylight.yangtools</groupId> + <artifactId>yang-common</artifactId> + </dependency> + <dependency> + <groupId>org.opendaylight.mdsal.model</groupId> + <artifactId>ietf-inet-types</artifactId> + <version>${odl.ietf-inet-types.version}</version> + </dependency> + <dependency> + <groupId>org.opendaylight.mdsal.model</groupId> + <artifactId>ietf-yang-types</artifactId> + <version>${odl.ietf-yang-types.version}</version> + </dependency> + <dependency> + <groupId>org.opendaylight.controller</groupId> + <artifactId>yang-jmx-generator-plugin</artifactId> + <version>${odl.yang.jmx.generator.version}</version> + </dependency> + </dependencies> +</project> diff --git a/appc-dg/appc-dg-shared/appc-dg-mdsal-store/appc-dg-mdsal-model/src/main/yang/mdsal-store.yang b/appc-dg/appc-dg-shared/appc-dg-mdsal-store/appc-dg-mdsal-model/src/main/yang/mdsal-store.yang new file mode 100644 index 000000000..266ca9d7c --- /dev/null +++ b/appc-dg/appc-dg-shared/appc-dg-mdsal-store/appc-dg-mdsal-model/src/main/yang/mdsal-store.yang @@ -0,0 +1,63 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP : APPC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Copyright (C) 2017 Amdocs + * ============================================================================= + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + * ============LICENSE_END========================================================= + */ + +module mdsal-store { + yang-version 1; + + namespace "org:onap:appc:mdsal:store"; + prefix mdsal-store; + + description + "Defines api for accepting yang schema as input. Its implementation saves the schema on MD-SAL store"; + + revision "2017-09-25" { + description + "Initial draft"; + } + + grouping response { + container status { + leaf code { + type uint16; + } + leaf message { + type string; + } + } + } + + rpc store-yang { + input{ + leaf module-name{ + type string; + } + leaf yang{ + type string; + } + } + output{ + uses response; + } + } +} diff --git a/appc-dg/appc-dg-shared/appc-dg-mdsal-store/pom.xml b/appc-dg/appc-dg-shared/appc-dg-mdsal-store/pom.xml index a2b5487bb..74f7499f6 100644 --- a/appc-dg/appc-dg-shared/appc-dg-mdsal-store/pom.xml +++ b/appc-dg/appc-dg-shared/appc-dg-mdsal-store/pom.xml @@ -1,45 +1,46 @@ -<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" - xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> - <modelVersion>4.0.0</modelVersion> - <parent> - <groupId>org.onap.appc</groupId> - <artifactId>appc-dg-shared</artifactId> - <version>1.3.0-SNAPSHOT</version> - </parent> - - <artifactId>appc-dg-mdsal-store</artifactId> - <packaging>jar</packaging> +<?xml version="1.0" encoding="UTF-8"?> +<!-- + ============LICENSE_START======================================================= + ONAP : APPC + ================================================================================ + Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + ================================================================================ + Copyright (C) 2017 Amdocs + ============================================================================= + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at - <name>appc-dg-mdsal-store</name> - <url>http://maven.apache.org</url> + http://www.apache.org/licenses/LICENSE-2.0 - <properties> - <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> - </properties> + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. - <dependencies> - <dependency> - <groupId>org.onap.appc</groupId> - <artifactId>appc-common</artifactId> - <version>${project.version}</version> - </dependency> - <dependency> - <groupId>junit</groupId> - <artifactId>junit</artifactId> - <scope>test</scope> - </dependency> - <dependency> - <groupId>org.opendaylight.yangtools</groupId> - <artifactId>yang-model-api</artifactId> - <version>${odl.yangtools.version}</version> - </dependency> - <dependency> - <groupId>commons-io</groupId> - <artifactId>commons-io</artifactId> - </dependency> - <dependency> - <groupId>commons-codec</groupId> - <artifactId>commons-codec</artifactId> - </dependency> - </dependencies> + ECOMP is a trademark and service mark of AT&T Intellectual Property. + ============LICENSE_END========================================================= + --> +<project xmlns="http://maven.apache.org/POM/4.0.0" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> + <parent> + <groupId>org.onap.appc</groupId> + <artifactId>appc-dg-shared</artifactId> + <version>1.3.0-SNAPSHOT</version> + </parent> + + <artifactId>appc-dg-mdsal-store</artifactId> + <packaging>pom</packaging> + + <properties> + <licenseDir>${project.parent.parent.parent.basedir}</licenseDir> + </properties> + + <modules> + <module>appc-dg-mdsal-bundle</module> + <module>appc-dg-mdsal-model</module> + </modules> </project> diff --git a/appc-dg/appc-dg-shared/appc-dg-mdsal-store/src/main/java/org/onap/appc/mdsal/impl/MDSALStoreImpl.java b/appc-dg/appc-dg-shared/appc-dg-mdsal-store/src/main/java/org/onap/appc/mdsal/impl/MDSALStoreImpl.java deleted file mode 100644 index c9e083557..000000000 --- a/appc-dg/appc-dg-shared/appc-dg-mdsal-store/src/main/java/org/onap/appc/mdsal/impl/MDSALStoreImpl.java +++ /dev/null @@ -1,154 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * ONAP : APPC - * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * Copyright (C) 2017 Amdocs - * ============================================================================= - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * ECOMP is a trademark and service mark of AT&T Intellectual Property. - * ============LICENSE_END========================================================= - */ - -package org.onap.appc.mdsal.impl; - -import org.onap.appc.configuration.Configuration; -import org.onap.appc.configuration.ConfigurationFactory; -import org.onap.appc.exceptions.APPCException; -import org.onap.appc.mdsal.MDSALStore; -import org.onap.appc.mdsal.exception.MDSALStoreException; -import org.onap.appc.mdsal.objects.BundleInfo; -import org.onap.appc.mdsal.operation.ConfigOperation; -import com.att.eelf.configuration.EELFLogger; -import com.att.eelf.configuration.EELFManager; -import org.osgi.framework.Bundle; -import org.osgi.framework.BundleContext; -import org.osgi.framework.FrameworkUtil; - -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.util.Date; -import java.util.Properties; -import java.util.jar.Attributes; -import java.util.jar.JarEntry; -import java.util.jar.JarOutputStream; -import java.util.jar.Manifest; - -/** - * Implementation of MDSALStore - */ -public class MDSALStoreImpl implements MDSALStore{ - - private static final EELFLogger logger = EELFManager.getInstance().getLogger(MDSALStoreImpl.class); - - private static final Configuration configuration = ConfigurationFactory.getConfiguration(); - - MDSALStoreImpl(){ - String configUrl = null; - String user =null; - String password = null; - Properties properties = configuration.getProperties(); - if (properties != null) { - configUrl= properties.getProperty( Constants.CONFIG_URL_PROPERTY , Constants.CONFIG_URL_DEFAULT); - user = properties.getProperty(Constants.CONFIG_USER_PROPERTY); - password = properties.getProperty(Constants.CONFIG_PASS_PROPERTY); - } - ConfigOperation.setUrl(configUrl); - ConfigOperation.setAuthentication(user,password); - } - - - @Override - public boolean isModulePresent(String moduleName, Date revision) { - - if(logger.isDebugEnabled()){ - logger.debug("isModulePresent invoked with moduleName = " +moduleName + " , revision = " +revision); - } - - BundleContext bundleContext = FrameworkUtil.getBundle(this.getClass()).getBundleContext(); - /** - * SchemaContext interface of ODL provides APIs for querying details of yang modules - * loaded into MD-SAL store, but its limitation is, it only returns information about - * static yang modules loaded on server start up, it does not return information about - * the yang modules loaded dynamically. Due to this limitation, we are checking the - * presence of OSGI bundle instead of yang module. (Note: Assuming OSGI bundle is named - * with the yang module name). - */ - - Bundle bundle = bundleContext.getBundle(moduleName); - if(logger.isDebugEnabled()){ - logger.debug("isModulePresent returned = " + (bundle != null)); - } - return bundle != null; - } - - @Override - public void storeYangModule(String yang, BundleInfo bundleInfo) throws MDSALStoreException { - - BundleContext bundleContext = FrameworkUtil.getBundle(this.getClass()).getBundleContext(); - byte[] byteArray = createBundleJar(yang, Constants.BLUEPRINT, bundleInfo); - - try (ByteArrayInputStream inputStream = new ByteArrayInputStream(byteArray)){ - Bundle bundle = bundleContext.installBundle(bundleInfo.getLocation(), inputStream); - bundle.start(); - } catch (Exception e) { - logger.error("Error storing yang module: " + yang + ". Error message: " + e.getMessage()); - throw new MDSALStoreException("Error storing yang module: " + yang + " " + e.getMessage(), e); - } - } - - @Override - public void storeJson( String module , String requestId ,String configJSON) throws MDSALStoreException { - - try { - ConfigOperation.storeConfig(configJSON , module , org.onap.appc.Constants.YANG_BASE_CONTAINER, org.onap.appc.Constants.YANG_VNF_CONFIG_LIST,requestId,org.onap.appc.Constants.YANG_VNF_CONFIG); - } catch (APPCException e) { - throw new MDSALStoreException("Exception while storing config json to MDSAL store." +e.getMessage(), e); - } - } - - private byte[] createBundleJar(String yang, String blueprint, BundleInfo bundleInfo) throws MDSALStoreException { - - Manifest manifest = new Manifest(); - manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, Constants.MANIFEST_VALUE_VERSION); - manifest.getMainAttributes().put(new Attributes.Name(Constants.MANIFEST_ATTR_BUNDLE_NAME), bundleInfo.getName()); - manifest.getMainAttributes().put(new Attributes.Name(Constants.MANIFEST_ATTR_BUNDLE_SYMBOLIC_NAME), bundleInfo.getName()); - manifest.getMainAttributes().put(new Attributes.Name(Constants.MANIFEST_ATTR_BUNDLE_DESCRIPTION), bundleInfo.getDescription()); - manifest.getMainAttributes().put(new Attributes.Name(Constants.MANIFEST_ATTR_BUNDLE_MANIFEST_VERSION), Constants.MANIFEST_VALUE_BUNDLE_MAN_VERSION); - manifest.getMainAttributes().put(new Attributes.Name(Constants.MANIFEST_ATTR_BUNDLE_VERSION), String.valueOf(bundleInfo.getVersion())); - manifest.getMainAttributes().put(new Attributes.Name(Constants.MANIFEST_ATTR_BUNDLE_BLUEPRINT), Constants.MANIFEST_VALUE_BUNDLE_BLUEPRINT); - - byte[] retunValue; - - try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); - JarOutputStream jarOutputStream = new JarOutputStream(outputStream, manifest)) { - jarOutputStream.putNextEntry(new JarEntry("META-INF/yang/")); - jarOutputStream.putNextEntry(new JarEntry("META-INF/yang/"+bundleInfo.getName()+".yang")); - jarOutputStream.write(yang.getBytes()); - jarOutputStream.closeEntry(); - - jarOutputStream.putNextEntry(new JarEntry("OSGI-INF/blueprint/")); - jarOutputStream.putNextEntry(new JarEntry(Constants.MANIFEST_VALUE_BUNDLE_BLUEPRINT)); - jarOutputStream.write(blueprint.getBytes()); - jarOutputStream.closeEntry(); - jarOutputStream.close(); - retunValue = outputStream.toByteArray(); - } catch (Exception e) { - logger.error("Error creating bundle jar: " + bundleInfo.getName() + ". Error message: " + e.getMessage()); - throw new MDSALStoreException("Error creating bundle jar: " + bundleInfo.getName() + " " + e.getMessage(), e); - } - return retunValue; - } -} diff --git a/appc-dg/appc-dg-shared/appc-dg-mdsal-store/src/main/java/org/onap/appc/mdsal/operation/ConfigOperation.java b/appc-dg/appc-dg-shared/appc-dg-mdsal-store/src/main/java/org/onap/appc/mdsal/operation/ConfigOperation.java deleted file mode 100644 index 1dcf1c6b0..000000000 --- a/appc-dg/appc-dg-shared/appc-dg-mdsal-store/src/main/java/org/onap/appc/mdsal/operation/ConfigOperation.java +++ /dev/null @@ -1,295 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * ONAP : APPC - * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * Copyright (C) 2017 Amdocs - * ============================================================================= - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * ECOMP is a trademark and service mark of AT&T Intellectual Property. - * ============LICENSE_END========================================================= - */ - -package org.onap.appc.mdsal.operation; - -import org.onap.appc.exceptions.APPCException; -import org.onap.appc.mdsal.impl.Constants; -import com.att.eelf.configuration.EELFLogger; -import com.att.eelf.configuration.EELFManager; -import com.fasterxml.jackson.databind.JsonNode; -import com.fasterxml.jackson.databind.ObjectMapper; -import org.apache.commons.codec.binary.Base64; -import org.apache.http.HttpHeaders; -import org.apache.http.HttpResponse; -import org.apache.http.HttpVersion; -import org.apache.http.client.HttpClient; -import org.apache.http.client.methods.HttpPut; -import org.apache.http.conn.ClientConnectionManager; -import org.apache.http.conn.scheme.PlainSocketFactory; -import org.apache.http.conn.scheme.Scheme; -import org.apache.http.conn.scheme.SchemeRegistry; -import org.apache.http.conn.ssl.SSLSocketFactory; -import org.apache.http.entity.StringEntity; -import org.apache.http.impl.client.DefaultHttpClient; -import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager; -import org.apache.http.params.BasicHttpParams; -import org.apache.http.params.HttpParams; -import org.apache.http.params.HttpProtocolParams; -import org.apache.http.protocol.HTTP; - -import javax.net.ssl.SSLContext; -import javax.net.ssl.TrustManager; -import javax.net.ssl.X509TrustManager; -import java.io.IOException; -import java.io.UnsupportedEncodingException; -import java.net.MalformedURLException; -import java.net.Socket; -import java.net.URL; -import java.security.*; -import java.security.cert.CertificateException; -import java.security.cert.X509Certificate; -import java.util.ArrayList; -import java.util.Iterator; - -import org.apache.commons.io.IOUtils; - -/** - * Provides method to store configuration to MD-SAL store. It also exposes doPut operation which can be used to invoke REST Put operation. - */ -public class ConfigOperation { - private static final EELFLogger LOG = EELFManager.getInstance().getLogger(ConfigOperation.class); - - private static URL url; - private static String basicAuth; - - ConfigOperation(){} - - private static ConfigOperationRequestFormatter requestFormatter = new ConfigOperationRequestFormatter(); - - private static ObjectMapper mapper = new ObjectMapper(); - - /** - * This method stores configuration JSON to MD-SAL store. Following input parameters are expected as input - * @param configJson - configuration JSON as String. This value will be stored in MD-SAL store - * @param module - Module name that contains yang Schema - * @param containerName - yang container name which will be used as base container. - * @param subModules - Sub modules list if any. Order of sub module is top to bottom. - * @throws APPCException - */ - public static void storeConfig(String configJson , String module, String containerName, String... subModules ) throws APPCException { - if (configJson == null) { - throw new APPCException("Provided message was null"); - } - LOG.debug("Config JSON: " + configJson +"\n" - +"module" + module +"\n" - +"containerName" + containerName +"\n" - +"subModules length : " + subModules.length ); - - int httpCode; - String respBody ; - try { - String path = requestFormatter.buildPath(module, containerName, subModules); - LOG.debug("Configuration Path : " + path); - URL serviceUrl = new URL(url.getProtocol(), url.getHost(), url.getPort(), path); - HttpResponse response = doPut(serviceUrl , configJson); - httpCode = response.getStatusLine().getStatusCode(); - respBody = IOUtils.toString(response.getEntity().getContent()); - } catch (IOException e) { - LOG.error("Error while storing configuration json "+e.getMessage(), e); - throw new APPCException(e); - } - - if (httpCode < 200 || httpCode >= 300 ) { - try { - LOG.debug("Config operation Error response code: " + httpCode); - ArrayList<String> errorMessage = new ArrayList<>(); - JsonNode responseJson = toJsonNodeFromJsonString(respBody); - if(responseJson!=null && responseJson.get("errors")!=null) { - JsonNode errors = responseJson.get("errors").get("error"); - for (Iterator<JsonNode> i = errors.elements();i.hasNext();){ - JsonNode error = i.next(); - errorMessage.add(error.get("error-message").textValue()); - } - } - throw new APPCException("Failed to load config JSON to MD SAL store. Error code:" + httpCode +" Error Message:" + errorMessage.toString()); - } catch (Exception e) { - LOG.error("Error while loading config JSON to MD SAL store. Error code:" + httpCode +" Error Message:" + e.getMessage(), e); - throw new APPCException("Error while loading config JSON to MD SAL store. Error code:" + httpCode +" Error Message:" + e.getMessage(),e); - } - }else{ - LOG.debug("Config operation successful. Response code: " + httpCode); - } - } - - /** - * This is Generic method that can be used to perform REST Put operation - * @param url - Destination URL for put - * @param body - payload for put action which will be sent as request body. - * @return - HttpResponse object which is returned from put REST call. - * @throws APPCException - */ - public static HttpResponse doPut (URL url, String body) throws APPCException { - HttpPut put; - try { - put = new HttpPut(url.toExternalForm()); - put.setHeader(HttpHeaders.CONTENT_TYPE, Constants.OPERATION_APPLICATION_JSON); - put.setHeader(HttpHeaders.ACCEPT, Constants.OPERATION_APPLICATION_JSON); - - if (basicAuth != null) { - put.setHeader(HttpHeaders.AUTHORIZATION, "Basic " + basicAuth); - } - - StringEntity entity = new StringEntity(body); - entity.setContentType(Constants.OPERATION_APPLICATION_JSON); - put.setEntity(new StringEntity(body)); - } catch (UnsupportedEncodingException e) { - throw new APPCException(e); - } - - HttpClient client = getHttpClient(); - - try { - return client.execute(put); - } catch (IOException e) { - throw new APPCException(e); - } - - } - - /** - * Updates the static var URL and returns the value; - * - * @return The new value of URL - */ - public static String getUrl() { - return url.toExternalForm(); - } - - public static void setUrl(String newUrl) { - try { - url = new URL(newUrl); - } catch (MalformedURLException e) { - LOG.error("Malformed URL " +newUrl + e.getMessage(), e); - } - } - - /** - * Sets the basic authentication header for the given user and password. If either entry is null then set basic auth - * to null - * - * @param user The user with optional domain name (for AAF) - * @param password The password for the user - * @return The new value of the basic auth string that will be used in the request headers - */ - public static String setAuthentication(String user, String password) { - if (user != null && password != null) { - String authStr = user + ":" + password; - basicAuth = new String(Base64.encodeBase64(authStr.getBytes())); - } else { - basicAuth = null; - } - return basicAuth; - } - - @SuppressWarnings("deprecation") - private static HttpClient getHttpClient() throws APPCException { - HttpClient client; - if (url.getProtocol().equals(Constants.OPERATION_HTTPS)) { - try { - KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType()); - trustStore.load(null, null); - MySSLSocketFactory sf = new MySSLSocketFactory(trustStore); - sf.setHostnameVerifier(MySSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); - - HttpParams params = new BasicHttpParams(); - HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); - HttpProtocolParams.setContentCharset(params, HTTP.UTF_8); - - SchemeRegistry registry = new SchemeRegistry(); - registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); - registry.register(new Scheme(Constants.OPERATION_HTTPS, sf, 443)); - registry.register(new Scheme(Constants.OPERATION_HTTPS, sf, 8443)); - registry.register(new Scheme("http", sf, 8181)); - - ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, registry); - client = new DefaultHttpClient(ccm, params); - } catch (Exception e) { - LOG.error("Error creating HTTP Client. Creating default client." , e); - client = new DefaultHttpClient(); - } - } else if ("http".equals(url.getProtocol())) { - client = new DefaultHttpClient(); - } else { - throw new APPCException( - "The provider.topology.url property is invalid. The url did not start with http[s]"); - } - return client; - } - - @SuppressWarnings("deprecation") - private static class MySSLSocketFactory extends SSLSocketFactory { - private SSLContext sslContext = SSLContext.getInstance("TLS"); - - private MySSLSocketFactory(KeyStore truststore) throws NoSuchAlgorithmException, KeyManagementException, - KeyStoreException, UnrecoverableKeyException { - super(truststore); - - TrustManager tm = new X509TrustManager() { - @Override - public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException { - LOG.debug("Inside checkClientTrusted"); - } - - @Override - public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException { - LOG.debug("Inside checkServerTrusted"); - } - - @Override - public X509Certificate[] getAcceptedIssuers() { - return new X509Certificate[1]; - } - }; - - sslContext.init(null, new TrustManager[]{ - tm - }, null); - } - - @Override - public Socket createSocket(Socket socket, String host, int port, boolean autoClose) - throws IOException { - return sslContext.getSocketFactory().createSocket(socket, host, port, autoClose); - } - - @Override - public Socket createSocket() throws IOException { - return sslContext.getSocketFactory().createSocket(); - } - } - - private static JsonNode toJsonNodeFromJsonString(String jsonStr) { - JsonNode jsonNode = null; - if(jsonStr != null) { - try { - jsonNode = mapper.readTree(jsonStr); - } catch (IOException e) { - LOG.warn(String.format("Could not map %s to jsonNode.", jsonStr), e); - } - } - return jsonNode; - } - -} diff --git a/appc-dg/appc-dg-shared/appc-dg-mdsal-store/src/main/java/org/onap/appc/mdsal/operation/ConfigOperation.java~ccmerge b/appc-dg/appc-dg-shared/appc-dg-mdsal-store/src/main/java/org/onap/appc/mdsal/operation/ConfigOperation.java~ccmerge deleted file mode 100644 index 36f471853..000000000 --- a/appc-dg/appc-dg-shared/appc-dg-mdsal-store/src/main/java/org/onap/appc/mdsal/operation/ConfigOperation.java~ccmerge +++ /dev/null @@ -1,295 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * ONAP : APPC - * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * Copyright (C) 2017 Amdocs - * ============================================================================= - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * ECOMP is a trademark and service mark of AT&T Intellectual Property. - * ============LICENSE_END========================================================= - */ - -package org.onap.appc.mdsal.operation; - -import org.onap.appc.exceptions.APPCException; -import org.onap.appc.mdsal.impl.Constants; -import com.att.eelf.configuration.EELFLogger; -import com.att.eelf.configuration.EELFManager; -import com.fasterxml.jackson.databind.JsonNode; -import com.fasterxml.jackson.databind.ObjectMapper; -import org.apache.commons.codec.binary.Base64; -import org.apache.http.HttpHeaders; -import org.apache.http.HttpResponse; -import org.apache.http.HttpVersion; -import org.apache.http.client.HttpClient; -import org.apache.http.client.methods.HttpPut; -import org.apache.http.conn.ClientConnectionManager; -import org.apache.http.conn.scheme.PlainSocketFactory; -import org.apache.http.conn.scheme.Scheme; -import org.apache.http.conn.scheme.SchemeRegistry; -import org.apache.http.conn.ssl.SSLSocketFactory; -import org.apache.http.entity.StringEntity; -import org.apache.http.impl.client.DefaultHttpClient; -import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager; -import org.apache.http.params.BasicHttpParams; -import org.apache.http.params.HttpParams; -import org.apache.http.params.HttpProtocolParams; -import org.apache.http.protocol.HTTP; - -import javax.net.ssl.SSLContext; -import javax.net.ssl.TrustManager; -import javax.net.ssl.X509TrustManager; -import java.io.IOException; -import java.io.UnsupportedEncodingException; -import java.net.MalformedURLException; -import java.net.Socket; -import java.net.URL; -import java.security.*; -import java.security.cert.CertificateException; -import java.security.cert.X509Certificate; -import java.util.ArrayList; -import java.util.Iterator; - -import org.apache.commons.io.IOUtils; - -/** - * Provides method to store configuration to MD-SAL store. It also exposes doPut operation which can be used to invoke REST Put operation. -*/ -public class ConfigOperation { - private static final EELFLogger LOG = EELFManager.getInstance().getLogger(ConfigOperation.class); - - private static URL url; - private static String basicAuth; - - ConfigOperation(){} - - private static ConfigOperationRequestFormatter requestFormatter = new ConfigOperationRequestFormatter(); - - private static ObjectMapper mapper = new ObjectMapper(); - - /** - * This method stores configuration JSON to MD-SAL store. Following input parameters are expected as input - * @param configJson - configuration JSON as String. This value will be stored in MD-SAL store - * @param module - Module name that contains yang Schema - * @param containerName - yang container name which will be used as base container. - * @param subModules - Sub modules list if any. Order of sub module is top to bottom. - * @throws APPCException - */ - public static void storeConfig(String configJson , String module, String containerName, String... subModules ) throws APPCException { - if (configJson == null) { - throw new APPCException("Provided message was null"); - } - LOG.debug("Config JSON: " + configJson +"\n" - +"module" + module +"\n" - +"containerName" + containerName +"\n" - +"subModules length : " + subModules.length ); - - int httpCode; - String respBody ; - try { - String path = requestFormatter.buildPath(url, module, containerName, subModules); - LOG.debug("Configuration Path : " + path); - URL serviceUrl = new URL(url.getProtocol(), url.getHost(), url.getPort(), path); - HttpResponse response = doPut(serviceUrl , configJson); - httpCode = response.getStatusLine().getStatusCode(); - respBody = IOUtils.toString(response.getEntity().getContent()); - } catch (IOException e) { - LOG.error("Error while storing configuration json "+e.getMessage(), e); - throw new APPCException(e); - } - - if (httpCode < 200 || httpCode >= 300 ) { - try { - LOG.debug("Config operation Error response code: " + httpCode); - ArrayList<String> errorMessage = new ArrayList<>(); - JsonNode responseJson = toJsonNodeFromJsonString(respBody); - if(responseJson!=null && responseJson.get("errors")!=null) { - JsonNode errors = responseJson.get("errors").get("error"); - for (Iterator<JsonNode> i = errors.elements();i.hasNext();){ - JsonNode error = i.next(); - errorMessage.add(error.get("error-message").textValue()); - } - } - throw new APPCException("Failed to load config JSON to MD SAL store. Error code:" + httpCode +" Error Message:" + errorMessage.toString()); - } catch (Exception e) { - LOG.error("Error while loading config JSON to MD SAL store. Error code:" + httpCode +" Error Message:" + e.getMessage(), e); - throw new APPCException("Error while loading config JSON to MD SAL store. Error code:" + httpCode +" Error Message:" + e.getMessage(),e); - } - }else{ - LOG.debug("Config operation successful. Response code: " + httpCode); - } - } - - /** - * This is Generic method that can be used to perform REST Put operation - * @param url - Destination URL for put - * @param body - payload for put action which will be sent as request body. - * @return - HttpResponse object which is returned from put REST call. - * @throws APPCException - */ - public static HttpResponse doPut (URL url, String body) throws APPCException { - HttpPut put; - try { - put = new HttpPut(url.toExternalForm()); - put.setHeader(HttpHeaders.CONTENT_TYPE, Constants.OPERATION_APPLICATION_JSON); - put.setHeader(HttpHeaders.ACCEPT, Constants.OPERATION_APPLICATION_JSON); - - if (basicAuth != null) { - put.setHeader(HttpHeaders.AUTHORIZATION, "Basic " + basicAuth); - } - - StringEntity entity = new StringEntity(body); - entity.setContentType(Constants.OPERATION_APPLICATION_JSON); - put.setEntity(new StringEntity(body)); - } catch (UnsupportedEncodingException e) { - throw new APPCException(e); - } - - HttpClient client = getHttpClient(); - - try { - return client.execute(put); - } catch (IOException e) { - throw new APPCException(e); - } - - } - - /** - * Updates the static var URL and returns the value; - * - * @return The new value of URL - */ - public static String getUrl() { - return url.toExternalForm(); - } - - public static void setUrl(String newUrl) { - try { - url = new URL(newUrl); - } catch (MalformedURLException e) { - LOG.error("Malformed URL " +newUrl + e.getMessage(), e); - } - } - - /** - * Sets the basic authentication header for the given user and password. If either entry is null then set basic auth - * to null - * - * @param user The user with optional domain name (for AAF) - * @param password The password for the user - * @return The new value of the basic auth string that will be used in the request headers - */ - public static String setAuthentication(String user, String password) { - if (user != null && password != null) { - String authStr = user + ":" + password; - basicAuth = new String(Base64.encodeBase64(authStr.getBytes())); - } else { - basicAuth = null; - } - return basicAuth; - } - - @SuppressWarnings("deprecation") - private static HttpClient getHttpClient() throws APPCException { - HttpClient client; - if (url.getProtocol().equals(Constants.OPERATION_HTTPS)) { - try { - KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType()); - trustStore.load(null, null); - MySSLSocketFactory sf = new MySSLSocketFactory(trustStore); - sf.setHostnameVerifier(MySSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); - - HttpParams params = new BasicHttpParams(); - HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); - HttpProtocolParams.setContentCharset(params, HTTP.UTF_8); - - SchemeRegistry registry = new SchemeRegistry(); - registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); - registry.register(new Scheme(Constants.OPERATION_HTTPS, sf, 443)); - registry.register(new Scheme(Constants.OPERATION_HTTPS, sf, 8443)); - registry.register(new Scheme("http", sf, 8181)); - - ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, registry); - client = new DefaultHttpClient(ccm, params); - } catch (Exception e) { - LOG.error("Error creating HTTP Client. Creating default client." , e); - client = new DefaultHttpClient(); - } - } else if ("http".equals(url.getProtocol())) { - client = new DefaultHttpClient(); - } else { - throw new APPCException( - "The provider.topology.url property is invalid. The url did not start with http[s]"); - } - return client; - } - - @SuppressWarnings("deprecation") - private static class MySSLSocketFactory extends SSLSocketFactory { - private SSLContext sslContext = SSLContext.getInstance("TLS"); - - private MySSLSocketFactory(KeyStore truststore) throws NoSuchAlgorithmException, KeyManagementException, - KeyStoreException, UnrecoverableKeyException { - super(truststore); - - TrustManager tm = new X509TrustManager() { - @Override - public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException { - LOG.debug("Inside checkClientTrusted"); - } - - @Override - public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException { - LOG.debug("Inside checkServerTrusted"); - } - - @Override - public X509Certificate[] getAcceptedIssuers() { - return new X509Certificate[1]; - } - }; - - sslContext.init(null, new TrustManager[]{ - tm - }, null); - } - - @Override - public Socket createSocket(Socket socket, String host, int port, boolean autoClose) - throws IOException { - return sslContext.getSocketFactory().createSocket(socket, host, port, autoClose); - } - - @Override - public Socket createSocket() throws IOException { - return sslContext.getSocketFactory().createSocket(); - } - } - - private static JsonNode toJsonNodeFromJsonString(String jsonStr) { - JsonNode jsonNode = null; - if(jsonStr != null) { - try { - jsonNode = mapper.readTree(jsonStr); - } catch (IOException e) { - LOG.warn(String.format("Could not map %s to jsonNode.", jsonStr), e); - } - } - return jsonNode; - } - -} |