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;
27 import org.eclipse.persistence.dynamic.DynamicEntity;
28 import org.eclipse.persistence.jaxb.UnmarshallerProperties;
29 import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext;
30 import org.onap.aai.exceptions.AAIException;
31 import org.onap.aai.introspection.exceptions.AAIUnknownObjectException;
32 import org.onap.aai.introspection.exceptions.AAIUnmarshallingException;
33 import org.onap.aai.logging.ErrorLogHelper;
34 import org.onap.aai.logging.LogFormatTools;
35 import org.onap.aai.nodes.NodeIngestor;
36 import org.onap.aai.restcore.MediaType;
37 import org.onap.aai.setup.SchemaVersion;
38 import org.onap.aai.workarounds.NamingExceptions;
39 import org.springframework.stereotype.Component;
40 import javax.xml.bind.JAXBException;
41 import javax.xml.bind.Unmarshaller;
42 import javax.xml.transform.stream.StreamSource;
44 import java.util.HashSet;
48 public class MoxyLoader extends Loader {
50 private DynamicJAXBContext jaxbContext = null;
51 private EELFLogger LOGGER = EELFManager.getInstance().getLogger(MoxyLoader.class);
52 private Map<String, Introspector> allObjs = null;
54 private Map<SchemaVersion, MoxyLoader> moxyLoaderFactory;
56 private NodeIngestor nodeIngestor;
58 public MoxyLoader(SchemaVersion version, NodeIngestor nodeIngestor) {
59 super(version, ModelType.MOXY);
60 this.nodeIngestor = nodeIngestor;
64 public MoxyLoader getMoxyLoader(SchemaVersion v) {
65 return moxyLoaderFactory.get(v);
70 * @throws AAIUnknownObjectException
73 public Introspector introspectorFromName(String name) throws AAIUnknownObjectException {
75 return IntrospectorFactory.newInstance(ModelType.MOXY, objectFromName(name));
82 public Object objectFromName(String name) throws AAIUnknownObjectException {
85 throw new AAIUnknownObjectException("null name passed in");
87 final String sanitizedName = NamingExceptions.getInstance().getObjectName(name);
88 final String upperCamel;
90 //Contains any uppercase, then assume it's upper camel
91 if (name.matches(".*[A-Z].*")) {
92 upperCamel = sanitizedName;
94 upperCamel = CaseFormat.LOWER_HYPHEN.to(CaseFormat.UPPER_CAMEL, sanitizedName);
98 final DynamicEntity result = jaxbContext.newDynamicEntity(upperCamel);
100 if (result == null) throw new AAIUnknownObjectException("Unrecognized AAI object " + name);
103 } catch (IllegalArgumentException e) {
104 //entity does not exist
105 throw new AAIUnknownObjectException("Unrecognized AAI object " + name, e);
113 protected void process(SchemaVersion version) {
115 * We need to have just same JaxbContext for each version
117 jaxbContext = nodeIngestor.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 Set<String> result = new HashSet<>();
171 result = nodeIngestor.getObjectsInVersion(getVersion());
173 } catch (Exception e) {
174 LOGGER.warn("Exception while enumerating objects for API version " + getVersion() + " (returning partial results) " + LogFormatTools.getStackTop(e));
177 //result.remove("EdgePropNames");
181 public DynamicJAXBContext getJAXBContext() {
182 return this.jaxbContext;
186 * Im keeping this for now - Just in case
188 /*private static class Helper {
189 private static final Map<SchemaVersion, MoxyLoader> INSTANCEMAP = new ConcurrentHashMap<>();
193 private static MoxyLoader getLoaderBySchemaVersion(SchemaVersion v) {
194 if (!INSTANCEMAP.containsKey(v)) {
195 INSTANCEMAP.put(v, new MoxyLoader(v, nodeIngestor));
197 return INSTANCEMAP.get(v);