From 31540f8ba964a7d66ca04f1468db061abda5c7a1 Mon Sep 17 00:00:00 2001 From: Ramya Balaji Date: Mon, 8 Jan 2018 12:40:57 -0500 Subject: Platform hardening for appc-dg-md-sal-store Changes made to synch up code in ONAP for bundle appc-dg-md-sal-store Issue-ID: APPC-377 Change-Id: I603918a3d6b250e5edea59818549868d4a6964fd Signed-off-by: Ramya Balaji --- .../appc-dg-mdsal-bundle/pom.xml | 218 +++++++++++++ .../main/java/org/onap/appc/mdsal/MDSALStore.java | 73 +++++ .../appc/mdsal/exception/MDSALStoreException.java | 72 +++++ .../java/org/onap/appc/mdsal/impl/Constants.java | 139 ++++++++ .../onap/appc/mdsal/impl/MDSALStoreFactory.java | 50 +++ .../org/onap/appc/mdsal/impl/MDSALStoreImpl.java | 354 +++++++++++++++++++++ .../org/onap/appc/mdsal/objects/BundleInfo.java | 74 +++++ .../operation/ConfigOperationRequestFormatter.java | 52 +++ .../appc/mdsal/provider/MdsalStoreProvider.java | 127 ++++++++ .../impl/rev170925/MdsalStoreProviderModule.java | 58 ++++ .../rev170925/MdsalStoreProviderModuleFactory.java | 37 +++ .../main/resources/initial/appc-mdsal-store.xml | 75 +++++ .../src/main/yang/mdsal-store.yang | 86 +++++ .../java/org/onap/appc/mdsal/MDSALStoreTest.java | 75 +++++ .../appc-dg-mdsal-model/.gitignore | 1 + .../appc-dg-mdsal-model/pom.xml | 111 +++++++ .../src/main/yang/mdsal-store.yang | 63 ++++ appc-dg/appc-dg-shared/appc-dg-mdsal-store/pom.xml | 83 ++--- .../main/java/org/onap/appc/mdsal/MDSALStore.java | 65 ---- .../appc/mdsal/exception/MDSALStoreException.java | 72 ----- .../java/org/onap/appc/mdsal/impl/Constants.java | 125 -------- .../onap/appc/mdsal/impl/MDSALStoreFactory.java | 50 --- .../org/onap/appc/mdsal/impl/MDSALStoreImpl.java | 154 --------- .../org/onap/appc/mdsal/objects/BundleInfo.java | 74 ----- .../onap/appc/mdsal/operation/ConfigOperation.java | 295 ----------------- .../mdsal/operation/ConfigOperation.java~ccmerge | 295 ----------------- .../operation/ConfigOperationRequestFormatter.java | 52 --- 27 files changed, 1707 insertions(+), 1223 deletions(-) create mode 100644 appc-dg/appc-dg-shared/appc-dg-mdsal-store/appc-dg-mdsal-bundle/pom.xml create mode 100644 appc-dg/appc-dg-shared/appc-dg-mdsal-store/appc-dg-mdsal-bundle/src/main/java/org/onap/appc/mdsal/MDSALStore.java create mode 100644 appc-dg/appc-dg-shared/appc-dg-mdsal-store/appc-dg-mdsal-bundle/src/main/java/org/onap/appc/mdsal/exception/MDSALStoreException.java create mode 100644 appc-dg/appc-dg-shared/appc-dg-mdsal-store/appc-dg-mdsal-bundle/src/main/java/org/onap/appc/mdsal/impl/Constants.java create mode 100644 appc-dg/appc-dg-shared/appc-dg-mdsal-store/appc-dg-mdsal-bundle/src/main/java/org/onap/appc/mdsal/impl/MDSALStoreFactory.java create mode 100644 appc-dg/appc-dg-shared/appc-dg-mdsal-store/appc-dg-mdsal-bundle/src/main/java/org/onap/appc/mdsal/impl/MDSALStoreImpl.java create mode 100644 appc-dg/appc-dg-shared/appc-dg-mdsal-store/appc-dg-mdsal-bundle/src/main/java/org/onap/appc/mdsal/objects/BundleInfo.java create mode 100644 appc-dg/appc-dg-shared/appc-dg-mdsal-store/appc-dg-mdsal-bundle/src/main/java/org/onap/appc/mdsal/operation/ConfigOperationRequestFormatter.java create mode 100644 appc-dg/appc-dg-shared/appc-dg-mdsal-store/appc-dg-mdsal-bundle/src/main/java/org/onap/appc/mdsal/provider/MdsalStoreProvider.java create mode 100644 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 create mode 100644 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 create mode 100644 appc-dg/appc-dg-shared/appc-dg-mdsal-store/appc-dg-mdsal-bundle/src/main/resources/initial/appc-mdsal-store.xml create mode 100644 appc-dg/appc-dg-shared/appc-dg-mdsal-store/appc-dg-mdsal-bundle/src/main/yang/mdsal-store.yang create mode 100644 appc-dg/appc-dg-shared/appc-dg-mdsal-store/appc-dg-mdsal-bundle/src/test/java/org/onap/appc/mdsal/MDSALStoreTest.java create mode 100644 appc-dg/appc-dg-shared/appc-dg-mdsal-store/appc-dg-mdsal-model/.gitignore create mode 100644 appc-dg/appc-dg-shared/appc-dg-mdsal-store/appc-dg-mdsal-model/pom.xml create mode 100644 appc-dg/appc-dg-shared/appc-dg-mdsal-store/appc-dg-mdsal-model/src/main/yang/mdsal-store.yang delete mode 100644 appc-dg/appc-dg-shared/appc-dg-mdsal-store/src/main/java/org/onap/appc/mdsal/MDSALStore.java delete mode 100644 appc-dg/appc-dg-shared/appc-dg-mdsal-store/src/main/java/org/onap/appc/mdsal/exception/MDSALStoreException.java delete mode 100644 appc-dg/appc-dg-shared/appc-dg-mdsal-store/src/main/java/org/onap/appc/mdsal/impl/Constants.java delete mode 100644 appc-dg/appc-dg-shared/appc-dg-mdsal-store/src/main/java/org/onap/appc/mdsal/impl/MDSALStoreFactory.java delete mode 100644 appc-dg/appc-dg-shared/appc-dg-mdsal-store/src/main/java/org/onap/appc/mdsal/impl/MDSALStoreImpl.java delete mode 100644 appc-dg/appc-dg-shared/appc-dg-mdsal-store/src/main/java/org/onap/appc/mdsal/objects/BundleInfo.java delete mode 100644 appc-dg/appc-dg-shared/appc-dg-mdsal-store/src/main/java/org/onap/appc/mdsal/operation/ConfigOperation.java delete mode 100644 appc-dg/appc-dg-shared/appc-dg-mdsal-store/src/main/java/org/onap/appc/mdsal/operation/ConfigOperation.java~ccmerge delete mode 100644 appc-dg/appc-dg-shared/appc-dg-mdsal-store/src/main/java/org/onap/appc/mdsal/operation/ConfigOperationRequestFormatter.java (limited to 'appc-dg') 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 @@ + + + + 4.0.0 + + org.onap.appc + appc-dg-mdsal-store + 1.3.0-SNAPSHOT + + + appc-dg-mdsal-bundle + bundle + + + UTF-8 + ${project.parent.parent.parent.basedir} + + + + + + org.apache.felix + maven-bundle-plugin + ${bundle.plugin.version} + true + + + org.onap.appc.mdsal.* + *;resolution:=optional + * + true + + + + + org.opendaylight.yangtools + yang-maven-plugin + ${odl.yangtools.version} + + + config + + generate-sources + + + + + + org.opendaylight.controller.config.yangjmxgenerator.plugin.JMXGenerator + + ${jmxGeneratorPath} + + + urn:opendaylight:params:xml:ns:yang:controller==org.opendaylight.controller.config.yang + + + + + + org.opendaylight.mdsal.binding.maven.api.gen.plugin.CodeGeneratorImpl + + ${salGeneratorPath} + + + true + + + + + + org.opendaylight.mdsal + maven-sal-api-gen-plugin + ${odl.sal.api.gen.plugin.version} + jar + + + org.opendaylight.controller + yang-jmx-generator-plugin + ${odl.yang.jmx.generator.version} + + + + + org.codehaus.mojo + build-helper-maven-plugin + + + attach-artifacts + + attach-artifact + + package + + + + ${project.build.directory}/classes/initial/appc-mdsal-store.xml + xml + config + + + + + + + + + + + + junit + junit + test + + + org.opendaylight.controller + config-api + + + org.opendaylight.controller + sal-binding-config + + + org.opendaylight.controller + sal-binding-api + + + org.opendaylight.controller + sal-common-util + + + sal-test-model + org.opendaylight.controller + test + + + sal-rest-connector + org.opendaylight.netconf + test + + + org.opendaylight.controller + sal-binding-broker-impl + test + + + org.opendaylight.controller + sal-binding-broker-impl + tests + ${odl.mdsal.version} + test-jar + test + + + org.onap.ccsdk.sli.core + sli-common + compile + + + + org.onap.ccsdk.sli.core + dblib-provider + + + + + + org.onap.ccsdk.sli.core + sli-provider + compile + + + + org.onap.ccsdk.sli.core + dblib-provider + + + + + + org.onap.appc + appc-common + ${project.version} + + + org.onap.appc + appc-dg-mdsal-model + ${project.version} + + + commons-io + commons-io + + + diff --git a/appc-dg/appc-dg-shared/appc-dg-mdsal-store/appc-dg-mdsal-bundle/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 new file mode 100644 index 000000000..19fff2b5c --- /dev/null +++ b/appc-dg/appc-dg-shared/appc-dg-mdsal-store/appc-dg-mdsal-bundle/src/main/java/org/onap/appc/mdsal/MDSALStore.java @@ -0,0 +1,73 @@ +/*- + * ============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 org.onap.appc.mdsal.exception.MDSALStoreException; +import org.onap.appc.mdsal.objects.BundleInfo; + +import java.util.Date; + +/** + * Provides APIs for interacting with MD-SAL store + */ +public interface MDSALStore { + + /** + * Checks the presence of any yang module in the MD-SAL store, + * Due to limitation of SchemaContext interface of ODL that it does not + * contain the information about dynamically loaded yang modules, it + * checks the presence of OSGI bundle + * @param moduleName Name of the Module + * @param revision revision of the Module + * @return returns true- module is present, false - module is absent + */ + boolean isModulePresent(String moduleName, Date revision); + + /** + * 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 bundleInfo - Bundle Information that contains name , description, version , location. These parameters used to create bundle which will push yang to MD-SAL store. + * @throws MDSALStoreException + */ + void storeYangModule(String yang, BundleInfo bundleInfo) throws MDSALStoreException; + + /** + * This method is used to store configuration JSON to MD-SAL store. It invokes store configuration Operation with required parameters + * @param moduleName - Yang module name where JSON need to be posted + * @param requestId - Request ID which is used as unique key for configuration JSON + * @param configJSON - String value of configuration JSON that needs to be stored in MD-SAl store + * @throws MDSALStoreException + */ + 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/appc-dg-mdsal-bundle/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 new file mode 100644 index 000000000..2bb30b1c2 --- /dev/null +++ b/appc-dg/appc-dg-shared/appc-dg-mdsal-store/appc-dg-mdsal-bundle/src/main/java/org/onap/appc/mdsal/exception/MDSALStoreException.java @@ -0,0 +1,72 @@ +/*- + * ============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.exception; + +/** + * This is custom exception type defined for MD-SAL store. All exceptions thrown by mdsal store module need to be wrapped in this class. +*/ + public class MDSALStoreException extends Exception { + + private static final long serialVersionUID = 1L; + + public MDSALStoreException(){ + } + + /** + * Create MDSALStoreException using only message. + * @param message -- message to the caller. + */ + public MDSALStoreException (String message){ + super(message); + } + + /** + * Create MDSALStoreException using orignal cause + * @param cause - cause that is being wrapped / suppressed. + */ + public MDSALStoreException (Throwable cause){ + super(cause); + } + + /** + * + * @param message - message to the caller. + * @param cause - cause that is being wrapped / suppressed . + */ + public MDSALStoreException(String message , Throwable cause){ + super(message , cause); + } + + /** + * + * @param message - message to the caller. + * @param cause - cause that is being wrapped / suppressed . + * @param enableSuppression - Indicates if suppression is enabled. + * @param writableStackTrace - Indicates if writable stacktrace is supported + */ + public MDSALStoreException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { + super(message, cause, enableSuppression, writableStackTrace); + } +} diff --git a/appc-dg/appc-dg-shared/appc-dg-mdsal-store/appc-dg-mdsal-bundle/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 new file mode 100644 index 000000000..e414b4146 --- /dev/null +++ b/appc-dg/appc-dg-shared/appc-dg-mdsal-store/appc-dg-mdsal-bundle/src/main/java/org/onap/appc/mdsal/impl/Constants.java @@ -0,0 +1,139 @@ +/*- + * ============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; +/** + * 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(){} + /** + * Manifest attribute for OSGI Bundle Name + */ + public static final String MANIFEST_ATTR_BUNDLE_NAME= "Bundle-Name"; + + /** + * Manifest attribute for OSGI Bundle Symbolic Name + */ + public static final String MANIFEST_ATTR_BUNDLE_SYMBOLIC_NAME= "Bundle-SymbolicName"; + + /** + * Manifest attribute for OSGI Bundle Description + */ + public static final String MANIFEST_ATTR_BUNDLE_DESCRIPTION= "Bundle-Description"; + + /** + * Manifest attribute for OSGI Bundle Manifest version + */ + public static final String MANIFEST_ATTR_BUNDLE_MANIFEST_VERSION= "Bundle-ManifestVersion"; + + /** + * Manifest attribute for OSGI Bundle Version + */ + public static final String MANIFEST_ATTR_BUNDLE_VERSION= "Bundle-Version"; + + /** + * Manifest attribute for OSGI Bundle Blueprint + */ + public static final String MANIFEST_ATTR_BUNDLE_BLUEPRINT= "Bundle-Blueprint"; + + /** + * Manifest value for Mainfest Version + */ + public static final String MANIFEST_VALUE_VERSION= "1.0"; + + /** + * Manifest value for OSGI Bundle Vesion + */ + public static final String MANIFEST_VALUE_BUNDLE_MAN_VERSION= "2"; + + /** + * Manifest value for OSGI Bundle Blueprint location + */ + public static final String MANIFEST_VALUE_BUNDLE_BLUEPRINT= "OSGI-INF/blueprint/blueprint.xml"; + + /** + * Base URL for config actions exposed by RESTCONF API + */ + + public static final String CONFIG_URL_DEFAULT = "https://localhost:8443/restconf/config"; + + public static final String CONFIG_URL_PROPERTY = "appc.LCM.provider.url"; + + public static final String CONFIG_PATH = "/restconf/config"; + + /** + * Restconf authentication user property name + */ + public static final String CONFIG_USER_PROPERTY = "appc.LCM.provider.user"; + + /** + * Restconf authentication password property name + */ + public static final String CONFIG_PASS_PROPERTY = "appc.LCM.provider.pass"; + + /** + * Content for blueprint.xml used while creation of OSGI bundle. + */ + public static final String BLUEPRINT = "\n" + + "\n" + + "\n" + + "\n" + + ""; + + /** + * HTTP Header attribute for Content type - JSON + */ + public static final String OPERATION_APPLICATION_JSON= " application/json"; + + /** + * HTTP protocol used for config operations + */ + public static final String OPERATION_HTTPS= "https"; + + /** + * 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/appc-dg-mdsal-bundle/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 new file mode 100644 index 000000000..bc951d926 --- /dev/null +++ b/appc-dg/appc-dg-shared/appc-dg-mdsal-store/appc-dg-mdsal-bundle/src/main/java/org/onap/appc/mdsal/impl/MDSALStoreFactory.java @@ -0,0 +1,50 @@ +/*- + * ============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.mdsal.MDSALStore; + +/* + * Factory class to create/get instance of MDSALStore + */ +public class MDSALStoreFactory { + private static class ReferenceHolder{ + private static MDSALStore store = new MDSALStoreImpl(); + private ReferenceHolder(){} + } + private MDSALStoreFactory(){ + + } + + /** + * Method for creating MDSALStore instance, It creates an instance of + * MDSALStoreImpl once and returns the same instance everytime it is invoked. + * @return + */ + public static MDSALStore createMDSALStore (){ + return ReferenceHolder.store; + } +} + 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 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 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 errorMessage = new ArrayList<>(); + if (responseJson != null && responseJson.get("errors") != null) { + JsonNode errors = responseJson.get("errors").get("error"); + for (Iterator 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/appc-dg-mdsal-bundle/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 new file mode 100644 index 000000000..b6d65447d --- /dev/null +++ b/appc-dg/appc-dg-shared/appc-dg-mdsal-store/appc-dg-mdsal-bundle/src/main/java/org/onap/appc/mdsal/objects/BundleInfo.java @@ -0,0 +1,74 @@ +/*- + * ============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.objects; +/** + * Holds bundle information which includes name , description , version and location. This information will be used to create osgi bundle. + */ + public class BundleInfo { + + private String name; + private String description; + private Integer version; + private String location; + + /** + * Creates an object of BundleInfo with version initialized to 1 + */ + public BundleInfo(){ + version =1; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public Integer getVersion() { + return version; + } + + public void setVersion(Integer version) { + this.version = version; + } + + public String getLocation() { + return location; + } + + public void setLocation(String location) { + this.location = location; + } +} diff --git a/appc-dg/appc-dg-shared/appc-dg-mdsal-store/appc-dg-mdsal-bundle/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 new file mode 100644 index 000000000..117349eea --- /dev/null +++ b/appc-dg/appc-dg-shared/appc-dg-mdsal-store/appc-dg-mdsal-bundle/src/main/java/org/onap/appc/mdsal/operation/ConfigOperationRequestFormatter.java @@ -0,0 +1,52 @@ +/*- + * ============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.mdsal.impl.Constants; + +/** + * Creates request url path for config actions based on parameter like module name , container-name and sub modules if any. + */ + +public class ConfigOperationRequestFormatter { + /** + * Build a request url path for config actions + * @param module - yang module name + * @param containerName - yang container name + * @param subModules - sub module /container names as string in varargs ( String ) format + * @return - resultant path in String format + */ + public String buildPath(String module, String containerName , String... subModules ) { + + StringBuilder path = new StringBuilder( Constants.CONFIG_PATH + Constants.URL_BACKSLASH + module + ":"+containerName + Constants.URL_BACKSLASH); + if(subModules.length >0){ + for(String subModule : subModules){ + path.append(subModule); + path.append("/"); + } + } + return path.toString(); + } +} 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 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> 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 + .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 @@ + + + + + + + + + + + + + prefix:mdsal-store-impl + + mdsal-store-impl + + + + binding:binding-rpc-registry + binding-rpc-broker + + + + binding:binding-async-data-broker + binding-data-broker + + + + + binding:binding-notification-service + + binding-notification-broker + + + + + + + + + + + org:onap:appc:mdsal:store:impl?module=mdsal-store-impl&revision=2017-09-25 + + + 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 @@ + + + + 4.0.0 + + org.onap.appc + appc-dg-mdsal-store + 1.3.0-SNAPSHOT + + + appc-dg-mdsal-model + bundle + + + ${project.parent.parent.parent.basedir} + + + + + + org.apache.felix + maven-bundle-plugin + true + + + * + + + + + org.opendaylight.yangtools + yang-maven-plugin + ${odl.yangtools.version} + + + org.opendaylight.mdsal + maven-sal-api-gen-plugin + ${odl.sal.api.gen.plugin.version} + jar + + + + + + generate-sources + + + ${yang.file.directory} + + + org.opendaylight.mdsal.binding.maven.api.gen.plugin.CodeGeneratorImpl + ${salGeneratorPath} + + + true + + + + + + + + + + org.opendaylight.mdsal + yang-binding + + + org.opendaylight.yangtools + yang-common + + + org.opendaylight.mdsal.model + ietf-inet-types + ${odl.ietf-inet-types.version} + + + org.opendaylight.mdsal.model + ietf-yang-types + ${odl.ietf-yang-types.version} + + + org.opendaylight.controller + yang-jmx-generator-plugin + ${odl.yang.jmx.generator.version} + + + 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 @@ - - 4.0.0 - - org.onap.appc - appc-dg-shared - 1.3.0-SNAPSHOT - - - appc-dg-mdsal-store - jar + + + + 4.0.0 + + org.onap.appc + appc-dg-shared + 1.3.0-SNAPSHOT + + + appc-dg-mdsal-store + pom + + + ${project.parent.parent.parent.basedir} + + + + appc-dg-mdsal-bundle + appc-dg-mdsal-model + 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/src/main/java/org/onap/appc/mdsal/MDSALStore.java deleted file mode 100644 index eba364a10..000000000 --- a/appc-dg/appc-dg-shared/appc-dg-mdsal-store/src/main/java/org/onap/appc/mdsal/MDSALStore.java +++ /dev/null @@ -1,65 +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; - -import org.onap.appc.mdsal.exception.MDSALStoreException; -import org.onap.appc.mdsal.objects.BundleInfo; - -import java.util.Date; - -/** - * Provides APIs for interacting with MD-SAL store - */ -public interface MDSALStore { - - /** - * Checks the presence of any yang module in the MD-SAL store, - * Due to limitation of SchemaContext interface of ODL that it does not - * contain the information about dynamically loaded yang modules, it - * checks the presence of OSGI bundle - * @param moduleName Name of the Module - * @param revision revision of the Module - * @return returns true- module is present, false - module is absent - */ - boolean isModulePresent(String moduleName, Date revision); - - /** - * 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 bundleInfo - Bundle Information that contains name , description, version , location. These parameters used to create bundle which will push yang to MD-SAL store. - * @throws MDSALStoreException - */ - void storeYangModule(String yang, BundleInfo bundleInfo) throws MDSALStoreException; - - /** - * This method is used to store configuration JSON to MD-SAL store. It invokes store configuration Operation with required parameters - * @param moduleName - Yang module name where JSON need to be posted - * @param requestId - Request ID which is used as unique key for configuration JSON - * @param configJSON - String value of configuration JSON that needs to be stored in MD-SAl store - * @throws MDSALStoreException - */ - void storeJson(String moduleName , String requestId , String configJSON ) 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/src/main/java/org/onap/appc/mdsal/exception/MDSALStoreException.java deleted file mode 100644 index 2bb30b1c2..000000000 --- a/appc-dg/appc-dg-shared/appc-dg-mdsal-store/src/main/java/org/onap/appc/mdsal/exception/MDSALStoreException.java +++ /dev/null @@ -1,72 +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.exception; - -/** - * This is custom exception type defined for MD-SAL store. All exceptions thrown by mdsal store module need to be wrapped in this class. -*/ - public class MDSALStoreException extends Exception { - - private static final long serialVersionUID = 1L; - - public MDSALStoreException(){ - } - - /** - * Create MDSALStoreException using only message. - * @param message -- message to the caller. - */ - public MDSALStoreException (String message){ - super(message); - } - - /** - * Create MDSALStoreException using orignal cause - * @param cause - cause that is being wrapped / suppressed. - */ - public MDSALStoreException (Throwable cause){ - super(cause); - } - - /** - * - * @param message - message to the caller. - * @param cause - cause that is being wrapped / suppressed . - */ - public MDSALStoreException(String message , Throwable cause){ - super(message , cause); - } - - /** - * - * @param message - message to the caller. - * @param cause - cause that is being wrapped / suppressed . - * @param enableSuppression - Indicates if suppression is enabled. - * @param writableStackTrace - Indicates if writable stacktrace is supported - */ - public MDSALStoreException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { - super(message, cause, enableSuppression, writableStackTrace); - } -} 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/src/main/java/org/onap/appc/mdsal/impl/Constants.java deleted file mode 100644 index bb847477f..000000000 --- a/appc-dg/appc-dg-shared/appc-dg-mdsal-store/src/main/java/org/onap/appc/mdsal/impl/Constants.java +++ /dev/null @@ -1,125 +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; -/** - * 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(){} - /** - * Manifest attribute for OSGI Bundle Name - */ - public static final String MANIFEST_ATTR_BUNDLE_NAME= "Bundle-Name"; - - /** - * Manifest attribute for OSGI Bundle Symbolic Name - */ - public static final String MANIFEST_ATTR_BUNDLE_SYMBOLIC_NAME= "Bundle-SymbolicName"; - - /** - * Manifest attribute for OSGI Bundle Description - */ - public static final String MANIFEST_ATTR_BUNDLE_DESCRIPTION= "Bundle-Description"; - - /** - * Manifest attribute for OSGI Bundle Manifest version - */ - public static final String MANIFEST_ATTR_BUNDLE_MANIFEST_VERSION= "Bundle-ManifestVersion"; - - /** - * Manifest attribute for OSGI Bundle Version - */ - public static final String MANIFEST_ATTR_BUNDLE_VERSION= "Bundle-Version"; - - /** - * Manifest attribute for OSGI Bundle Blueprint - */ - public static final String MANIFEST_ATTR_BUNDLE_BLUEPRINT= "Bundle-Blueprint"; - - /** - * Manifest value for Mainfest Version - */ - public static final String MANIFEST_VALUE_VERSION= "1.0"; - - /** - * Manifest value for OSGI Bundle Vesion - */ - public static final String MANIFEST_VALUE_BUNDLE_MAN_VERSION= "2"; - - /** - * Manifest value for OSGI Bundle Blueprint location - */ - public static final String MANIFEST_VALUE_BUNDLE_BLUEPRINT= "OSGI-INF/blueprint/blueprint.xml"; - - /** - * Base URL for config actions exposed by RESTCONF API - */ - - public static final String CONFIG_URL_DEFAULT = "https://localhost:8443/restconf/config"; - - public static final String CONFIG_URL_PROPERTY = "appc.LCM.provider.url"; - - public static final String CONFIG_PATH = "/restconf/config"; - - /** - * Restconf authentication user property name - */ - public static final String CONFIG_USER_PROPERTY = "appc.LCM.provider.user"; - - /** - * Restconf authentication password property name - */ - public static final String CONFIG_PASS_PROPERTY = "appc.LCM.provider.pass"; - - /** - * Content for blueprint.xml used while creation of OSGI bundle. - */ - public static final String BLUEPRINT = "\n" + - "\n" + - "\n" + - "\n" + - ""; - - /** - * HTTP Header attribute for Content type - JSON - */ - public static final String OPERATION_APPLICATION_JSON= " application/json"; - - /** - * HTTP protocol used for config operations - */ - public static final String OPERATION_HTTPS= "https"; - - /** - * Constant for backslash to be used while formatting URL - */ - public static final String URL_BACKSLASH ="/"; -} 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/src/main/java/org/onap/appc/mdsal/impl/MDSALStoreFactory.java deleted file mode 100644 index bc951d926..000000000 --- a/appc-dg/appc-dg-shared/appc-dg-mdsal-store/src/main/java/org/onap/appc/mdsal/impl/MDSALStoreFactory.java +++ /dev/null @@ -1,50 +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.mdsal.MDSALStore; - -/* - * Factory class to create/get instance of MDSALStore - */ -public class MDSALStoreFactory { - private static class ReferenceHolder{ - private static MDSALStore store = new MDSALStoreImpl(); - private ReferenceHolder(){} - } - private MDSALStoreFactory(){ - - } - - /** - * Method for creating MDSALStore instance, It creates an instance of - * MDSALStoreImpl once and returns the same instance everytime it is invoked. - * @return - */ - public static MDSALStore createMDSALStore (){ - return ReferenceHolder.store; - } -} - 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/objects/BundleInfo.java b/appc-dg/appc-dg-shared/appc-dg-mdsal-store/src/main/java/org/onap/appc/mdsal/objects/BundleInfo.java deleted file mode 100644 index b6d65447d..000000000 --- a/appc-dg/appc-dg-shared/appc-dg-mdsal-store/src/main/java/org/onap/appc/mdsal/objects/BundleInfo.java +++ /dev/null @@ -1,74 +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.objects; -/** - * Holds bundle information which includes name , description , version and location. This information will be used to create osgi bundle. - */ - public class BundleInfo { - - private String name; - private String description; - private Integer version; - private String location; - - /** - * Creates an object of BundleInfo with version initialized to 1 - */ - public BundleInfo(){ - version =1; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public String getDescription() { - return description; - } - - public void setDescription(String description) { - this.description = description; - } - - public Integer getVersion() { - return version; - } - - public void setVersion(Integer version) { - this.version = version; - } - - public String getLocation() { - return location; - } - - public void setLocation(String location) { - this.location = location; - } -} 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 errorMessage = new ArrayList<>(); - JsonNode responseJson = toJsonNodeFromJsonString(respBody); - if(responseJson!=null && responseJson.get("errors")!=null) { - JsonNode errors = responseJson.get("errors").get("error"); - for (Iterator 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 errorMessage = new ArrayList<>(); - JsonNode responseJson = toJsonNodeFromJsonString(respBody); - if(responseJson!=null && responseJson.get("errors")!=null) { - JsonNode errors = responseJson.get("errors").get("error"); - for (Iterator 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/ConfigOperationRequestFormatter.java b/appc-dg/appc-dg-shared/appc-dg-mdsal-store/src/main/java/org/onap/appc/mdsal/operation/ConfigOperationRequestFormatter.java deleted file mode 100644 index 117349eea..000000000 --- a/appc-dg/appc-dg-shared/appc-dg-mdsal-store/src/main/java/org/onap/appc/mdsal/operation/ConfigOperationRequestFormatter.java +++ /dev/null @@ -1,52 +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.mdsal.impl.Constants; - -/** - * Creates request url path for config actions based on parameter like module name , container-name and sub modules if any. - */ - -public class ConfigOperationRequestFormatter { - /** - * Build a request url path for config actions - * @param module - yang module name - * @param containerName - yang container name - * @param subModules - sub module /container names as string in varargs ( String ) format - * @return - resultant path in String format - */ - public String buildPath(String module, String containerName , String... subModules ) { - - StringBuilder path = new StringBuilder( Constants.CONFIG_PATH + Constants.URL_BACKSLASH + module + ":"+containerName + Constants.URL_BACKSLASH); - if(subModules.length >0){ - for(String subModule : subModules){ - path.append(subModule); - path.append("/"); - } - } - return path.toString(); - } -} -- cgit 1.2.3-korg