2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017 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 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 package org.onap.aai.introspection;
24 import com.att.eelf.configuration.EELFLogger;
25 import com.att.eelf.configuration.EELFManager;
26 import com.google.common.base.CaseFormat;
27 import com.google.common.collect.ImmutableMap;
28 import org.eclipse.persistence.dynamic.DynamicEntity;
29 import org.eclipse.persistence.jaxb.UnmarshallerProperties;
30 import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext;
31 import org.onap.aai.exceptions.AAIException;
32 import org.onap.aai.introspection.exceptions.AAIUnknownObjectException;
33 import org.onap.aai.introspection.exceptions.AAIUnmarshallingException;
34 import org.onap.aai.logging.ErrorLogHelper;
35 import org.onap.aai.logging.LogFormatTools;
36 import org.onap.aai.restcore.MediaType;
37 import org.onap.aai.util.AAIConstants;
38 import org.onap.aai.workarounds.NamingExceptions;
39 import org.w3c.dom.Document;
40 import org.w3c.dom.NodeList;
41 import org.xml.sax.SAXException;
43 import javax.xml.XMLConstants;
44 import javax.xml.bind.JAXBException;
45 import javax.xml.bind.Unmarshaller;
46 import javax.xml.parsers.DocumentBuilder;
47 import javax.xml.parsers.DocumentBuilderFactory;
48 import javax.xml.parsers.ParserConfigurationException;
49 import javax.xml.transform.stream.StreamSource;
51 import java.util.HashSet;
55 public class MoxyLoader extends Loader {
57 private DynamicJAXBContext jaxbContext = null;
58 private EELFLogger LOGGER = EELFManager.getInstance().getLogger(MoxyLoader.class);
59 private Map<String, Introspector> allObjs = null;
62 * Instantiates a new moxy loader.
64 * @param version the version
65 * @param llBuilder the ll builder
67 protected MoxyLoader(Version version) {
68 super(version, ModelType.MOXY);
74 * @throws AAIUnknownObjectException
77 public Introspector introspectorFromName(String name) throws AAIUnknownObjectException {
79 return IntrospectorFactory.newInstance(ModelType.MOXY, objectFromName(name));
86 public Object objectFromName(String name) throws AAIUnknownObjectException {
89 throw new AAIUnknownObjectException("null name passed in");
91 final String sanitizedName = NamingExceptions.getInstance().getObjectName(name);
92 final String upperCamel;
94 //Contains any uppercase, then assume it's upper camel
95 if (name.matches(".*[A-Z].*")) {
96 upperCamel = sanitizedName;
98 upperCamel = CaseFormat.LOWER_HYPHEN.to(CaseFormat.UPPER_CAMEL, sanitizedName);
102 final DynamicEntity result = jaxbContext.newDynamicEntity(upperCamel);
104 if (result == null) throw new AAIUnknownObjectException("Unrecognized AAI object " + name);
107 } catch (IllegalArgumentException e) {
108 //entity does not exist
109 throw new AAIUnknownObjectException("Unrecognized AAI object " + name, e);
117 protected void process(Version version) {
118 ModelInjestor injestor = ModelInjestor.getInstance();
119 jaxbContext = injestor.getContextForVersion(version);
127 public Introspector unmarshal(String type, String json, MediaType mediaType) throws AAIUnmarshallingException {
129 final Object clazz = objectFromName(type);
130 final Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
132 if (mediaType.equals(MediaType.APPLICATION_JSON_TYPE)) {
133 unmarshaller.setProperty(UnmarshallerProperties.MEDIA_TYPE, "application/json");
134 unmarshaller.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, false);
135 unmarshaller.setProperty(UnmarshallerProperties.JSON_WRAPPER_AS_ARRAY_NAME, true);
138 final DynamicEntity entity = (DynamicEntity) unmarshaller.unmarshal(new StreamSource(new StringReader(json)), clazz.getClass()).getValue();
139 return IntrospectorFactory.newInstance(ModelType.MOXY, entity);
140 } catch (JAXBException e) {
141 AAIException ex = new AAIException("AAI_4007", e);
142 ErrorLogHelper.logException(ex);
143 throw new AAIUnmarshallingException("Could not unmarshall: " + e.getMessage(), ex);
144 } catch (AAIUnknownObjectException e) {
145 throw new AAIUnmarshallingException("Could not unmarshall: " + e.getMessage(), e);
150 public Map<String, Introspector> getAllObjects() {
151 if (this.allObjs != null) {
154 ImmutableMap.Builder<String, Introspector> map = new ImmutableMap.Builder<String, Introspector>();
155 Set<String> objs = objectsInVersion();
156 for (String objName : objs) {
158 Introspector introspector = this.introspectorFromName(objName);
159 map.put(introspector.getDbName(), introspector);
160 } catch (AAIUnknownObjectException e) {
161 LOGGER.warn("Unexpected AAIUnknownObjectException while running getAllObjects() " + LogFormatTools.getStackTop(e));
164 allObjs = map.build();
169 private Set<String> objectsInVersion() {
170 final Set<String> result = new HashSet<>();
173 final DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
174 final String fileName = ModelInjestor.getInstance().getOXMFileName(getVersion());
176 docFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
178 final DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
180 InputStream inputStream;
181 File oxmFile = new File(AAIConstants.AAI_HOME_ETC + fileName);
183 if(oxmFile.exists()){
184 LOGGER.info("Oxm file exists on the system {}", oxmFile);
185 inputStream = new FileInputStream(oxmFile);
187 LOGGER.warn("Unable to find oxm file {} on the system so using classloader", oxmFile);
188 inputStream = getClass().getClassLoader().getResourceAsStream(fileName);
191 final Document doc = docBuilder.parse(inputStream);
192 final NodeList list = doc.getElementsByTagName("java-type");
194 for (int i = 0; i < list.getLength(); i++) {
195 result.add(list.item(i).getAttributes().getNamedItem("name").getNodeValue());
197 } catch (ParserConfigurationException | SAXException | IOException e) {
198 LOGGER.warn("Exception while enumerating objects for API version " + getVersion() + " (returning partial results) " + LogFormatTools.getStackTop(e));
201 //result.remove("EdgePropNames");
205 public DynamicJAXBContext getJAXBContext() {
206 return this.jaxbContext;