Update license files, sonar plugin and fix tests
[aai/aai-common.git] / aai-core / src / main / java / org / openecomp / aai / introspection / PojoLoader.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * org.openecomp.aai
4  * ================================================================================
5  * Copyright (C) 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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.openecomp.aai.introspection;
22
23 import java.io.StringReader;
24 import java.util.Map;
25
26 import javax.xml.bind.JAXBContext;
27 import javax.xml.bind.JAXBException;
28 import javax.xml.bind.Unmarshaller;
29 import javax.xml.transform.stream.StreamSource;
30
31 import org.eclipse.persistence.jaxb.UnmarshallerProperties;
32
33 import org.openecomp.aai.db.props.AAIProperties;
34 import org.openecomp.aai.introspection.exceptions.AAIUnknownObjectException;
35 import org.openecomp.aai.introspection.exceptions.AAIUnmarshallingException;
36 import org.openecomp.aai.logging.ErrorLogHelper;
37 import org.openecomp.aai.restcore.MediaType;
38 import org.openecomp.aai.workarounds.NamingExceptions;
39 import com.att.eelf.configuration.EELFLogger;
40 import com.att.eelf.configuration.EELFManager;
41 import com.google.common.base.CaseFormat;
42 import org.eclipse.persistence.jaxb.JAXBContextFactory;
43
44 public class PojoLoader extends Loader {
45
46         private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(PojoLoader.class);
47         private static final String POJO_BASE_PACKAGE_NAME = "org.openecomp.aai.domain.yang";
48
49         protected JAXBContext context;
50         private final String pojoPackageName;
51
52         protected PojoLoader(Version version) {
53                 super(version, ModelType.POJO);
54
55                 if (!version.equals(AAIProperties.LATEST)) {
56                         pojoPackageName = POJO_BASE_PACKAGE_NAME + "." + version;
57                 } else {
58                         pojoPackageName = POJO_BASE_PACKAGE_NAME;
59                 }
60
61                 try {
62                         context = JAXBContextFactory.createContext(pojoPackageName, this.getClass().getClassLoader());
63                 } catch (JAXBException e) {
64                         LOGGER.error("JAXBException while instantiation contect for PojoLoader", e);
65                 }
66         }
67
68         @Override
69         public Introspector introspectorFromName(String name) throws AAIUnknownObjectException {
70                 return IntrospectorFactory.newInstance(ModelType.POJO, objectFromName(name));
71         }
72         
73         @Override
74         public Object objectFromName(String name) throws AAIUnknownObjectException {
75
76                 final String sanitizedName = NamingExceptions.getInstance().getObjectName(name);
77                 final String upperCamel;
78
79                 //Contains any uppercase, then assume it's upper camel
80                 if (sanitizedName.matches(".*[A-Z].*")) {
81                         upperCamel = sanitizedName;
82                 } else {
83                         upperCamel = CaseFormat.LOWER_HYPHEN.to(CaseFormat.UPPER_CAMEL, sanitizedName);
84                 }
85
86                 final String objectClassName;
87                 
88                 if (!upperCamel.contains(pojoPackageName)) {
89                         objectClassName = pojoPackageName + "." + upperCamel;
90                 } else {
91                         objectClassName = upperCamel;
92                 }
93
94                 try {
95                         return Class.forName(objectClassName).newInstance();
96                 } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
97                         throw new AAIUnknownObjectException("Unrecognized AAI object " + name);
98                 }
99         }
100
101         @Override
102         protected void process(Version version) {
103                 LOGGER.warn("PojoLoader.process(Version) has not been implemented");
104         }
105
106         @Override
107         public Introspector unmarshal(String type, String json, MediaType mediaType) throws AAIUnmarshallingException {
108
109                  try {
110                         final Unmarshaller unmarshaller = context.createUnmarshaller();
111
112                         if (mediaType.equals(MediaType.APPLICATION_JSON_TYPE)) {
113                         unmarshaller.setProperty(UnmarshallerProperties.MEDIA_TYPE, "application/json");
114                         unmarshaller.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, false);
115                                 unmarshaller.setProperty(UnmarshallerProperties.JSON_WRAPPER_AS_ARRAY_NAME, true);
116                         }
117
118                         final Object clazz = objectFromName(type);
119                         final Object obj = unmarshaller.unmarshal(new StreamSource(new StringReader(json)), clazz.getClass()).getValue();
120
121                         return IntrospectorFactory.newInstance(ModelType.POJO, obj);
122                  } catch (JAXBException e) {
123                         ErrorLogHelper.logError("AAI_4007", "Could not unmarshall: " + e.getMessage());
124                         throw new AAIUnmarshallingException("Could not unmarshall: " + e.getMessage());
125                 } catch (AAIUnknownObjectException e) {
126                         throw new AAIUnmarshallingException("Could not unmarshall: " + e.getMessage(), e);
127                 }
128         }
129
130         @Override
131         public Map<String, Introspector> getAllObjects() {
132                 //TODO
133                 return null;
134         }
135
136 }