2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
20 package org.onap.aai.introspection;
22 import com.att.eelf.configuration.EELFLogger;
23 import com.att.eelf.configuration.EELFManager;
24 import com.google.common.base.CaseFormat;
25 import com.google.common.collect.ImmutableMap;
26 import org.eclipse.persistence.dynamic.DynamicEntity;
27 import org.eclipse.persistence.jaxb.UnmarshallerProperties;
28 import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext;
29 import org.onap.aai.exceptions.AAIException;
30 import org.onap.aai.introspection.exceptions.AAIUnknownObjectException;
31 import org.onap.aai.introspection.exceptions.AAIUnmarshallingException;
32 import org.onap.aai.logging.ErrorLogHelper;
33 import org.onap.aai.logging.LogFormatTools;
34 import org.onap.aai.restcore.MediaType;
35 import org.onap.aai.util.AAIConstants;
36 import org.onap.aai.workarounds.NamingExceptions;
37 import org.w3c.dom.Document;
38 import org.w3c.dom.NodeList;
39 import org.xml.sax.SAXException;
41 import javax.xml.XMLConstants;
42 import javax.xml.bind.JAXBException;
43 import javax.xml.bind.Unmarshaller;
44 import javax.xml.parsers.DocumentBuilder;
45 import javax.xml.parsers.DocumentBuilderFactory;
46 import javax.xml.parsers.ParserConfigurationException;
47 import javax.xml.transform.stream.StreamSource;
49 import java.util.HashSet;
53 public class MoxyLoader extends Loader {
55 private DynamicJAXBContext jaxbContext = null;
56 private EELFLogger LOGGER = EELFManager.getInstance().getLogger(MoxyLoader.class);
57 private Map<String, Introspector> allObjs = null;
60 * Instantiates a new moxy loader.
62 * @param version the version
63 * @param llBuilder the ll builder
65 protected MoxyLoader(Version version) {
66 super(version, ModelType.MOXY);
72 * @throws AAIUnknownObjectException
75 public Introspector introspectorFromName(String name) throws AAIUnknownObjectException {
77 return IntrospectorFactory.newInstance(ModelType.MOXY, objectFromName(name));
84 public Object objectFromName(String name) throws AAIUnknownObjectException {
87 throw new AAIUnknownObjectException("null name passed in");
89 final String sanitizedName = NamingExceptions.getInstance().getObjectName(name);
90 final String upperCamel;
92 //Contains any uppercase, then assume it's upper camel
93 if (name.matches(".*[A-Z].*")) {
94 upperCamel = sanitizedName;
96 upperCamel = CaseFormat.LOWER_HYPHEN.to(CaseFormat.UPPER_CAMEL, sanitizedName);
100 final DynamicEntity result = jaxbContext.newDynamicEntity(upperCamel);
102 if (result == null) throw new AAIUnknownObjectException("Unrecognized AAI object " + name);
105 } catch (IllegalArgumentException e) {
106 //entity does not exist
107 throw new AAIUnknownObjectException("Unrecognized AAI object " + name, e);
115 protected void process(Version version) {
116 ModelInjestor injestor = ModelInjestor.getInstance();
117 jaxbContext = injestor.getContextForVersion(version);
125 public Introspector unmarshal(String type, String json, MediaType mediaType) throws AAIUnmarshallingException {
127 final Object clazz = objectFromName(type);
128 final Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
130 if (mediaType.equals(MediaType.APPLICATION_JSON_TYPE)) {
131 unmarshaller.setProperty(UnmarshallerProperties.MEDIA_TYPE, "application/json");
132 unmarshaller.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, false);
133 unmarshaller.setProperty(UnmarshallerProperties.JSON_WRAPPER_AS_ARRAY_NAME, true);
136 final DynamicEntity entity = (DynamicEntity) unmarshaller.unmarshal(new StreamSource(new StringReader(json)), clazz.getClass()).getValue();
137 return IntrospectorFactory.newInstance(ModelType.MOXY, entity);
138 } catch (JAXBException e) {
139 AAIException ex = new AAIException("AAI_4007", e);
140 ErrorLogHelper.logException(ex);
141 throw new AAIUnmarshallingException("Could not unmarshall: " + e.getMessage(), ex);
142 } catch (AAIUnknownObjectException e) {
143 throw new AAIUnmarshallingException("Could not unmarshall: " + e.getMessage(), e);
148 public Map<String, Introspector> getAllObjects() {
149 if (this.allObjs != null) {
152 ImmutableMap.Builder<String, Introspector> map = new ImmutableMap.Builder<String, Introspector>();
153 Set<String> objs = objectsInVersion();
154 for (String objName : objs) {
156 Introspector introspector = this.introspectorFromName(objName);
157 map.put(introspector.getDbName(), introspector);
158 } catch (AAIUnknownObjectException e) {
159 LOGGER.warn("Unexpected AAIUnknownObjectException while running getAllObjects() " + LogFormatTools.getStackTop(e));
162 allObjs = map.build();
167 private Set<String> objectsInVersion() {
168 final Set<String> result = new HashSet<>();
171 final DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
172 final String fileName = ModelInjestor.getInstance().getOXMFileName(getVersion());
174 docFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
176 final DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
178 InputStream inputStream;
179 File oxmFile = new File(AAIConstants.AAI_HOME_ETC + fileName);
181 if(oxmFile.exists()){
182 LOGGER.info("Oxm file exists on the system {}", oxmFile);
183 inputStream = new FileInputStream(oxmFile);
185 LOGGER.warn("Unable to find oxm file {} on the system so using classloader", oxmFile);
186 inputStream = getClass().getClassLoader().getResourceAsStream(fileName);
189 final Document doc = docBuilder.parse(inputStream);
190 final NodeList list = doc.getElementsByTagName("java-type");
192 for (int i = 0; i < list.getLength(); i++) {
193 result.add(list.item(i).getAttributes().getNamedItem("name").getNodeValue());
195 } catch (ParserConfigurationException | SAXException | IOException e) {
196 LOGGER.warn("Exception while enumerating objects for API version " + getVersion() + " (returning partial results) " + LogFormatTools.getStackTop(e));
199 //result.remove("EdgePropNames");
203 public DynamicJAXBContext getJAXBContext() {
204 return this.jaxbContext;