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=========================================================
21 package org.onap.aai.introspection;
23 import com.att.eelf.configuration.EELFLogger;
24 import com.att.eelf.configuration.EELFManager;
25 import com.google.common.base.CaseFormat;
26 import com.google.common.collect.ImmutableMap;
29 import java.util.HashSet;
32 import java.util.stream.Collectors;
34 import javax.xml.bind.JAXBException;
35 import javax.xml.bind.Unmarshaller;
36 import javax.xml.transform.stream.StreamSource;
38 import org.eclipse.persistence.dynamic.DynamicEntity;
39 import org.eclipse.persistence.jaxb.UnmarshallerProperties;
40 import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext;
41 import org.onap.aai.exceptions.AAIException;
42 import org.onap.aai.introspection.exceptions.AAIUnknownObjectException;
43 import org.onap.aai.introspection.exceptions.AAIUnmarshallingException;
44 import org.onap.aai.logging.ErrorLogHelper;
45 import org.onap.aai.logging.LogFormatTools;
46 import org.onap.aai.nodes.CaseFormatStore;
47 import org.onap.aai.nodes.NodeIngestor;
48 import org.onap.aai.restcore.MediaType;
49 import org.onap.aai.schema.enums.ObjectMetadata;
50 import org.onap.aai.setup.SchemaVersion;
51 import org.onap.aai.workarounds.NamingExceptions;
53 public class MoxyLoader extends Loader {
55 private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(MoxyLoader.class);
57 private DynamicJAXBContext jaxbContext = null;
58 private Map<String, Introspector> allObjs = null;
60 private Map<SchemaVersion, MoxyLoader> moxyLoaderFactory;
62 private NodeIngestor nodeIngestor;
63 private CaseFormatStore caseFormatStore;
65 private Set<String> namedProps;
67 public MoxyLoader(SchemaVersion version, NodeIngestor nodeIngestor) {
68 super(version, ModelType.MOXY);
69 this.nodeIngestor = nodeIngestor;
70 this.caseFormatStore = nodeIngestor.getCaseFormatStore();
74 public MoxyLoader getMoxyLoader(SchemaVersion v) {
75 return moxyLoaderFactory.get(v);
82 * @throws AAIUnknownObjectException
85 public Introspector introspectorFromName(String name) throws AAIUnknownObjectException {
87 return IntrospectorFactory.newInstance(ModelType.MOXY, objectFromName(name));
90 private boolean containsUpperCase(String str) {
92 for (int i = 0; i < str.length(); i++) {
93 if (Character.isUpperCase(str.charAt(i))) {
105 public Object objectFromName(String name) throws AAIUnknownObjectException {
108 throw new AAIUnknownObjectException("null name passed in");
110 final String sanitizedName = NamingExceptions.getInstance().getObjectName(name);
111 final String upperCamel;
113 // Contains any uppercase, then assume it's upper camel
114 if (containsUpperCase(name)) {
115 upperCamel = sanitizedName;
117 upperCamel = caseFormatStore.fromLowerHyphenToUpperCamel(sanitizedName).orElseGet(() -> {
118 LOGGER.debug("Unable to find {} in the store for lower hyphen to upper camel", sanitizedName);
119 return CaseFormat.LOWER_HYPHEN.to(CaseFormat.UPPER_CAMEL, sanitizedName);
124 final DynamicEntity result = jaxbContext.newDynamicEntity(upperCamel);
127 throw new AAIUnknownObjectException("Unrecognized AAI object " + name);
130 } catch (IllegalArgumentException e) {
131 // entity does not exist
132 throw new AAIUnknownObjectException("Unrecognized AAI object " + name, e);
140 protected void process(SchemaVersion version) {
142 * We need to have just same JaxbContext for each version
144 jaxbContext = nodeIngestor.getContextForVersion(version);
152 public Introspector unmarshal(String type, String json, MediaType mediaType) throws AAIUnmarshallingException {
154 final Object clazz = objectFromName(type);
155 final Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
157 if (mediaType.equals(MediaType.APPLICATION_JSON_TYPE)) {
158 unmarshaller.setProperty(UnmarshallerProperties.MEDIA_TYPE, "application/json");
159 unmarshaller.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, false);
160 unmarshaller.setProperty(UnmarshallerProperties.JSON_WRAPPER_AS_ARRAY_NAME, true);
163 final DynamicEntity entity = (DynamicEntity) unmarshaller
164 .unmarshal(new StreamSource(new StringReader(json)), clazz.getClass()).getValue();
165 return IntrospectorFactory.newInstance(ModelType.MOXY, entity);
166 } catch (JAXBException e) {
167 AAIException ex = new AAIException("AAI_4007", e);
168 ErrorLogHelper.logException(ex);
169 throw new AAIUnmarshallingException("Could not unmarshall: " + e.getMessage(), ex);
170 } catch (AAIUnknownObjectException e) {
171 throw new AAIUnmarshallingException("Could not unmarshall: " + e.getMessage(), e);
176 public Map<String, Introspector> getAllObjects() {
177 if (this.allObjs != null) {
180 ImmutableMap.Builder<String, Introspector> map = new ImmutableMap.Builder<>();
181 Set<String> objs = objectsInVersion();
182 for (String objName : objs) {
184 Introspector introspector = this.introspectorFromName(objName);
185 map.put(introspector.getDbName(), introspector);
186 } catch (AAIUnknownObjectException e) {
187 LOGGER.warn("Unexpected AAIUnknownObjectException while running getAllObjects() "
188 + LogFormatTools.getStackTop(e));
191 allObjs = map.build();
196 private Set<String> objectsInVersion() {
197 Set<String> result = new HashSet<>();
200 result = nodeIngestor.getObjectsInVersion(getVersion());
202 } catch (Exception e) {
203 LOGGER.warn("Exception while enumerating objects for API version " + getVersion()
204 + " (returning partial results) " + LogFormatTools.getStackTop(e));
211 public Set<String> getNamedPropNodes() {
213 if (namedProps == null) {
214 namedProps = getAllObjects().entrySet().stream()
215 .filter((entry) -> entry.getValue().getMetadata(ObjectMetadata.NAME_PROPS) != null)
216 .map(entry -> entry.getKey()).collect(Collectors.toSet());
222 public DynamicJAXBContext getJAXBContext() {
223 return this.jaxbContext;