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 org.eclipse.persistence.jaxb.JAXBContextFactory;
28 import org.eclipse.persistence.jaxb.UnmarshallerProperties;
29 import org.onap.aai.db.props.AAIProperties;
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.workarounds.NamingExceptions;
37 import javax.xml.bind.JAXBContext;
38 import javax.xml.bind.JAXBException;
39 import javax.xml.bind.Unmarshaller;
40 import javax.xml.transform.stream.StreamSource;
41 import java.io.StringReader;
44 public class PojoLoader extends Loader {
46 private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(PojoLoader.class);
47 private static final String POJO_BASE_PACKAGE_NAME = "org.onap.aai.domain.yang";
49 protected JAXBContext context;
50 private final String pojoPackageName;
52 protected PojoLoader(Version version) {
53 super(version, ModelType.POJO);
55 if (!version.equals(AAIProperties.LATEST)) {
56 pojoPackageName = POJO_BASE_PACKAGE_NAME + "." + version;
58 pojoPackageName = POJO_BASE_PACKAGE_NAME;
62 context = JAXBContextFactory.createContext(pojoPackageName, this.getClass().getClassLoader());
63 } catch (JAXBException e) {
64 LOGGER.error("JAXBException while instantiation contect for PojoLoader " + LogFormatTools.getStackTop(e));
69 public Introspector introspectorFromName(String name) throws AAIUnknownObjectException {
70 return IntrospectorFactory.newInstance(ModelType.POJO, objectFromName(name));
74 public Object objectFromName(String name) throws AAIUnknownObjectException {
77 throw new AAIUnknownObjectException("null name passed in");
79 final String sanitizedName = NamingExceptions.getInstance().getObjectName(name);
80 final String upperCamel;
82 //Contains any uppercase, then assume it's upper camel
83 if (sanitizedName.matches(".*[A-Z].*")) {
84 upperCamel = sanitizedName;
86 upperCamel = CaseFormat.LOWER_HYPHEN.to(CaseFormat.UPPER_CAMEL, sanitizedName);
89 final String objectClassName;
91 if (!upperCamel.contains(pojoPackageName)) {
92 objectClassName = pojoPackageName + "." + upperCamel;
94 objectClassName = upperCamel;
98 return Class.forName(objectClassName).newInstance();
99 } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
100 throw new AAIUnknownObjectException("Unrecognized AAI object " + name);
105 protected void process(Version version) {
106 LOGGER.warn("PojoLoader.process(Version) has not been implemented");
110 public Introspector unmarshal(String type, String json, MediaType mediaType) throws AAIUnmarshallingException {
113 final Unmarshaller unmarshaller = context.createUnmarshaller();
115 if (mediaType.equals(MediaType.APPLICATION_JSON_TYPE)) {
116 unmarshaller.setProperty(UnmarshallerProperties.MEDIA_TYPE, "application/json");
117 unmarshaller.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, false);
118 unmarshaller.setProperty(UnmarshallerProperties.JSON_WRAPPER_AS_ARRAY_NAME, true);
121 final Object clazz = objectFromName(type);
122 final Object obj = unmarshaller.unmarshal(new StreamSource(new StringReader(json)), clazz.getClass()).getValue();
124 return IntrospectorFactory.newInstance(ModelType.POJO, obj);
125 } catch (JAXBException e) {
126 ErrorLogHelper.logError("AAI_4007", "Could not unmarshall: " + e.getMessage());
127 throw new AAIUnmarshallingException("Could not unmarshall: " + e.getMessage());
128 } catch (AAIUnknownObjectException e) {
129 throw new AAIUnmarshallingException("Could not unmarshall: " + e.getMessage(), e);
134 public Map<String, Introspector> getAllObjects() {