diff options
Diffstat (limited to 'sparkybe-onap-service/src/main/java')
4 files changed, 167 insertions, 107 deletions
diff --git a/sparkybe-onap-service/src/main/java/org/onap/aai/sparky/config/oxm/OxmConfigTranslator.java b/sparkybe-onap-service/src/main/java/org/onap/aai/sparky/config/oxm/OxmConfigTranslator.java new file mode 100644 index 0000000..65181c1 --- /dev/null +++ b/sparkybe-onap-service/src/main/java/org/onap/aai/sparky/config/oxm/OxmConfigTranslator.java @@ -0,0 +1,116 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved. + * Copyright © 2017-2018 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. + * ============LICENSE_END========================================================= + */ +package org.onap.aai.sparky.config.oxm; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.PathMatcher; +import java.nio.file.Paths; +import java.util.Arrays; +import java.util.List; +import java.util.Map; +import java.util.ServiceConfigurationError; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +import org.onap.aai.cl.api.Logger; +import org.onap.aai.cl.eelf.LoggerFactory; +import org.onap.aai.setup.ConfigTranslator; +import org.onap.aai.setup.SchemaLocationsBean; +import org.onap.aai.setup.Version; +import org.onap.aai.sparky.logging.AaiUiMsgs; +import org.onap.aai.sparky.util.NodeUtils; + +/** + * Determine which OXM and edge rules files to return based on the latest Version + * + */ +public class OxmConfigTranslator extends ConfigTranslator { + + private static final Logger LOG = LoggerFactory.getInstance().getLogger(OxmConfigTranslator.class); + + public OxmConfigTranslator(SchemaLocationsBean bean) { + super(bean); + } + + @Override + public Map<Version, List<String>> getNodeFiles() { + String nodeDirectory = bean.getNodeDirectory(); + if (nodeDirectory == null) { + throw new ServiceConfigurationError( + "Node(s) directory is empty in the schema location bean (" + bean.getSchemaConfigLocation() + ")"); + } + try { + return getVersionMap(Paths.get(nodeDirectory), "*_v*.xml"); + } catch (IOException e) { + throw new ServiceConfigurationError("Failed to read node(s) directory " + getPath(nodeDirectory), e); + } + } + + @Override + public Map<Version, List<String>> getEdgeFiles() { + String edgeDirectory = bean.getEdgeDirectory(); + if (edgeDirectory == null) { + throw new ServiceConfigurationError( + "Edge(s) directory is empty in the schema location bean (" + bean.getSchemaConfigLocation() + ")"); + } + try { + return getVersionMap(Paths.get(edgeDirectory), "*_v*.json"); + } catch (IOException e) { + throw new ServiceConfigurationError("Failed to read edge(s) directory " + getPath(edgeDirectory), e); + } + } + + private String getPath(String nodeDirectory) { + return Paths.get(nodeDirectory).toAbsolutePath().toString(); + } + + /** + * Creates a map containing each OXM Version and the matching OXM file path(s) + * + * @param folderPath the folder/directory containing the OXM files + * @param fileSuffix + * @return a new Map object (may be empty) + * @throws IOException if there is a problem reading the specified directory path + */ + private Map<Version, List<String>> getVersionMap(Path folderPath, String globPattern) throws IOException { + final PathMatcher filter = folderPath.getFileSystem().getPathMatcher("glob:**/" + globPattern); + try (final Stream<Path> stream = Files.list(folderPath)) { + return stream.filter(filter::matches).map(Path::toString).filter(p -> getVersionFromPath(p) != null) + .collect(Collectors.groupingBy(this::getVersionFromPath)); + } + } + + private Version getVersionFromPath(String pathName) { + + String version = "V" + NodeUtils.extractOxmVersionFromPath(pathName); + + try { + return Version.valueOf(version); + } catch (IllegalArgumentException e) { + LOG.error(AaiUiMsgs.ERROR_GENERIC, "Failed to find OXM version '" + version + + "' from Version enumeration value set = " + Arrays.asList(Version.values()) + "."); + } + + return null; + } +}
\ No newline at end of file diff --git a/sparkybe-onap-service/src/main/java/org/onap/aai/sparky/config/oxm/OxmModelLoader.java b/sparkybe-onap-service/src/main/java/org/onap/aai/sparky/config/oxm/OxmModelLoader.java index 02ce01a..fad6fdd 100644 --- a/sparkybe-onap-service/src/main/java/org/onap/aai/sparky/config/oxm/OxmModelLoader.java +++ b/sparkybe-onap-service/src/main/java/org/onap/aai/sparky/config/oxm/OxmModelLoader.java @@ -2,8 +2,8 @@ * ============LICENSE_START======================================================= * org.onap.aai * ================================================================================ - * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved. - * Copyright © 2017-2018 Amdocs + * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved. + * Copyright © 2017-2018 Amdocs * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,29 +20,19 @@ */ package org.onap.aai.sparky.config.oxm; -import java.io.IOException; -import java.io.InputStream; -import java.util.HashMap; -import java.util.HashSet; -import java.util.Map; import java.util.Set; -import java.util.regex.Matcher; -import java.util.regex.Pattern; -import org.eclipse.persistence.jaxb.JAXBContextProperties; import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext; -import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContextFactory; import org.onap.aai.cl.api.Logger; import org.onap.aai.cl.eelf.LoggerFactory; +import org.onap.aai.nodes.NodeIngestor; +import org.onap.aai.setup.Version; import org.onap.aai.sparky.logging.AaiUiMsgs; -import org.springframework.core.io.Resource; -import org.springframework.core.io.support.PathMatchingResourcePatternResolver; -import org.springframework.core.io.support.ResourcePatternResolver; public class OxmModelLoader { - + private static final Logger LOG = LoggerFactory.getInstance().getLogger(OxmModelLoader.class); - + /* * The intent of this parameter is to be able to programmatically over-ride the latest AAI schema * version discovered from the aai-schema jar file. This property is optional, but if set on the @@ -52,114 +42,47 @@ public class OxmModelLoader { * specified version, and that stream will be returned if available. */ - protected int oxmApiVersionOverride; + protected Version oxmApiVersion; protected Set<OxmModelProcessor> processors; - private int latestVersionNum = 0; - - private final static Pattern p = Pattern.compile("aai_oxm_(v)(.*).xml"); - - public OxmModelLoader() { - this(-1, new HashSet<OxmModelProcessor>()); + + private NodeIngestor nodeIngestor; + + public OxmModelLoader(Version apiVersionOverride, Set<OxmModelProcessor> oxmModelProcessors, + NodeIngestor nodeIngestor) { + this.oxmApiVersion = apiVersionOverride; + this.processors = oxmModelProcessors; + this.nodeIngestor = nodeIngestor; } - - public OxmModelLoader(int apiVersionOverride,Set<OxmModelProcessor> oxmModelProcessors) { - this.oxmApiVersionOverride = apiVersionOverride; + + public OxmModelLoader(Set<OxmModelProcessor> oxmModelProcessors, NodeIngestor nodeIngestor) { + this.oxmApiVersion = Version.getLatest(); this.processors = oxmModelProcessors; + this.nodeIngestor = nodeIngestor; + } + + public Version getLatestVersionNum() { + return oxmApiVersion; } - - protected synchronized Map<Integer, InputStream> getStreamHandlesForOxmFromResource() { - Map<Integer, InputStream> listOfOxmFiles = new HashMap<Integer, InputStream>(); - ClassLoader oxmClassLoader = OxmModelLoader.class.getClassLoader(); - ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(oxmClassLoader); - Resource[] resources = null; - try { - resources = resolver.getResources("classpath*:/oxm/aai_oxm*.xml"); - } catch (IOException ex) { - LOG.error(AaiUiMsgs.OXM_LOADING_ERROR, ex.getMessage()); - } - if (resources == null) { - LOG.error(AaiUiMsgs.OXM_LOADING_ERROR, "No OXM schema files found on classpath"); - } - for (Resource resource : resources) { - Matcher m = p.matcher(resource.getFilename()); - if (m.matches()) { - try { - listOfOxmFiles.put(new Integer(m.group(2)), resource.getInputStream()); - } catch (Exception e) { - LOG.error(AaiUiMsgs.OXM_LOADING_ERROR, - resource.getFilename(), e.getMessage()); - } - } - } - return listOfOxmFiles; - } - /** * Load an oxm model. + * * @param inputStream file handle for oxm */ - protected void loadModel(InputStream inputStream) { - Map<String, Object> properties = new HashMap<String, Object>(); - properties.put(JAXBContextProperties.OXM_METADATA_SOURCE, inputStream); + public void loadModel() { try { - final DynamicJAXBContext oxmContext = DynamicJAXBContextFactory - .createContextFromOXM(Thread.currentThread().getContextClassLoader(), properties); - + final DynamicJAXBContext oxmContext = nodeIngestor.getContextForVersion(oxmApiVersion); parseOxmContext(oxmContext); // populateSearchableOxmModel(); - LOG.info(AaiUiMsgs.OXM_LOAD_SUCCESS, String.valueOf(latestVersionNum)); + LOG.info(AaiUiMsgs.OXM_LOAD_SUCCESS, String.valueOf(oxmApiVersion)); } catch (Exception exc) { LOG.info(AaiUiMsgs.OXM_PARSE_ERROR_NONVERBOSE); - LOG.error(AaiUiMsgs.OXM_PARSE_ERROR_VERBOSE, "OXM v" + latestVersionNum, exc.getMessage()); - } - } - - /** - * Load the latest oxm model. - */ - public synchronized void loadLatestOxmModel() { - - LOG.info(AaiUiMsgs.INITIALIZE_OXM_MODEL_LOADER); - - // find handles for available oxm models - final Map<Integer, InputStream> listOfOxmStreams = getStreamHandlesForOxmFromResource(); - if (listOfOxmStreams.isEmpty()) { - LOG.error(AaiUiMsgs.OXM_FILE_NOT_FOUND); - return; - } - - InputStream stream = null; - - if (oxmApiVersionOverride > 0) { - latestVersionNum = oxmApiVersionOverride; - LOG.warn(AaiUiMsgs.WARN_GENERIC, "Overriding AAI Schema with version = " + latestVersionNum); - stream = listOfOxmStreams.get(latestVersionNum); - } else { - - for (Integer key : listOfOxmStreams.keySet()) { - if (key.intValue() > latestVersionNum) { - latestVersionNum = key.intValue(); - stream = listOfOxmStreams.get(key); - } - } + LOG.error(AaiUiMsgs.OXM_PARSE_ERROR_VERBOSE, "OXM v" + oxmApiVersion, exc.getMessage()); } - - // load the latest oxm file - loadModel(stream); - - } - - public int getLatestVersionNum() { - return latestVersionNum; } - public void setLatestVersionNum(int latestVersionNum) { - this.latestVersionNum = latestVersionNum; - } - /** * Parses the oxm context. * @@ -187,5 +110,5 @@ public class OxmModelLoader { } } - + } diff --git a/sparkybe-onap-service/src/main/java/org/onap/aai/sparky/dal/ActiveInventoryAdapter.java b/sparkybe-onap-service/src/main/java/org/onap/aai/sparky/dal/ActiveInventoryAdapter.java index 656ffba..214538f 100644 --- a/sparkybe-onap-service/src/main/java/org/onap/aai/sparky/dal/ActiveInventoryAdapter.java +++ b/sparkybe-onap-service/src/main/java/org/onap/aai/sparky/dal/ActiveInventoryAdapter.java @@ -148,7 +148,7 @@ public class ActiveInventoryAdapter { versionStr = String.valueOf(oxmModelLoader.getLatestVersionNum()); } - return "/aai/v" + versionStr; + return "/aai/" + versionStr.toLowerCase(); } diff --git a/sparkybe-onap-service/src/main/java/org/onap/aai/sparky/util/NodeUtils.java b/sparkybe-onap-service/src/main/java/org/onap/aai/sparky/util/NodeUtils.java index dbb9877..7a9d0e0 100644 --- a/sparkybe-onap-service/src/main/java/org/onap/aai/sparky/util/NodeUtils.java +++ b/sparkybe-onap-service/src/main/java/org/onap/aai/sparky/util/NodeUtils.java @@ -71,6 +71,7 @@ public class NodeUtils { private static SecureRandom sRandom = new SecureRandom(); private static final Pattern AAI_VERSION_PREFIX = Pattern.compile("/aai/v[0-9]+/(.*)"); + private static final Pattern OXM_VERSION_PREFIX = Pattern.compile(".*_v([0-9]+).*"); private static final Pattern GIZMO_VERSION_PREFIX = Pattern.compile("[/]*services/inventory/v[0-9]+/(.*)"); private static final Pattern GIZMO_RELATIONSHIP_VERSION_PREFIX = Pattern.compile("services/inventory/relationships/v[0-9]+/(.*)"); @@ -97,6 +98,26 @@ public class NodeUtils { return sb.toString(); } + public static String extractOxmVersionFromPath(String filePath) { + + try { + + Matcher m = OXM_VERSION_PREFIX.matcher(filePath); + + if (m.matches()) { + + if ( m.groupCount() >= 1) { + return m.group(1); + } + + } + } catch (Exception e) { + } + + return null; + + } + public static String extractRawPathWithoutVersion(String selfLinkUri) { |