Additional null checks and comments
[aai/babel.git] / src / main / java / org / onap / aai / babel / xml / generator / model / Resource.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright (c) 2017-2019 AT&T Intellectual Property. All rights reserved.
6  * Copyright (c) 2017-2019 European Software Marketing Ltd.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *       http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.aai.babel.xml.generator.model;
23
24 import com.google.common.collect.ImmutableMap;
25 import java.util.Collections;
26 import java.util.List;
27 import java.util.Map;
28 import java.util.Optional;
29 import java.util.stream.Collectors;
30 import org.onap.aai.babel.xml.generator.XmlArtifactGenerationException;
31 import org.onap.aai.babel.xml.generator.types.ModelType;
32
33 public class Resource extends Model {
34
35     private WidgetType type;
36     private boolean deleteFlag;
37     private ModelType modelType = ModelType.RESOURCE;
38     private Map<String, Object> properties = Collections.emptyMap();
39
40     Widget vserver = null;
41     boolean addlintf = false;
42     boolean addvolume = false;
43     List<String> members;
44
45     public Resource(WidgetType type, boolean deleteFlag) {
46         this.type = type;
47         this.deleteFlag = deleteFlag;
48     }
49
50     /**
51      * Copy Constructor.
52      *
53      * @param baseResource
54      */
55     public Resource(Resource baseResource) {
56         this(baseResource.getWidgetType(), baseResource.getDeleteFlag());
57         setModelType(baseResource.getModelType());
58     }
59
60     @Override
61     public int hashCode() {
62         final String uuid = getModelNameVersionId();
63         return uuid == null ? 0 : uuid.hashCode();
64     }
65
66     @Override
67     public boolean equals(Object obj) {
68         if (obj instanceof Resource) {
69             return getModelNameVersionId().equals(((Resource) obj).getModelNameVersionId());
70         }
71         return false;
72     }
73
74     @Override
75     public boolean getDeleteFlag() {
76         return deleteFlag;
77     }
78
79     public void setProperties(Map<String, Object> properties) {
80         this.properties = properties;
81     }
82
83     public Map<String, Object> getProperties() {
84         return properties;
85     }
86
87     public void setModelType(ModelType type) {
88         this.modelType = type;
89     }
90
91     public ModelType getModelType() {
92         return modelType;
93     }
94
95     public void setMembers(List<String> members) {
96         this.members = members;
97     }
98
99     /**
100      * Adds a Widget.
101      *
102      * @param widget
103      *            the widget
104      * @return the boolean
105      * @throws XmlArtifactGenerationException
106      */
107     @Override
108     public boolean addWidget(Widget widget) throws XmlArtifactGenerationException {
109         if (type == WidgetType.valueOf("VFMODULE")) {
110             if (widget.memberOf(members)) {
111                 if (vserver == null && widget.hasWidgetType("VSERVER")) {
112                     addVserverWidget(widget);
113                 } else if (widget.hasWidgetType("LINT")) {
114                     return addLIntfWidget(widget);
115                 } else if (widget.hasWidgetType("VOLUME")) {
116                     addVolumeWidget(widget);
117                     return true;
118                 }
119                 if (!widget.hasWidgetType("OAM_NETWORK")) {
120                     return widgets.add(widget);
121                 }
122             }
123             return false;
124         } else {
125             return widgets.add(widget);
126         }
127     }
128
129     @Override
130     public WidgetType getWidgetType() {
131         return type;
132     }
133
134     @Override
135     public String getModelTypeName() {
136         return "resource";
137     }
138
139     @Override
140     public String toString() {
141         return ImmutableMap.<String, String>builder() //
142                 .put("Resource", Optional.ofNullable(getModelId()).orElse("null")) //
143                 .put("widget type", getWidgetType().toString()) //
144                 .put("deleteFlag", Boolean.toString(deleteFlag)) //
145                 .put("modelType", modelType.toString()) //
146                 .put("properties", properties.toString()) //
147                 .put("vserver", Optional.ofNullable(vserver).map(Widget::toString).orElse("null")) //
148                 .put("addlintf", Boolean.toString(addlintf)) //
149                 .put("addvolume", Boolean.toString(addvolume)) //
150                 .put("members", Optional.ofNullable(members).map(List::toString).orElse("null")) //
151                 .build().entrySet().stream() //
152                 .map(e -> e.getKey() + "=" + e.getValue()) //
153                 .collect(Collectors.joining(", "));
154     }
155
156     private void addVolumeWidget(Widget widget) {
157         if (vserver != null) {
158             vserver.addWidget(widget);
159         } else {
160             addvolume = true;
161         }
162     }
163
164     /**
165      * @param widget
166      * @return
167      */
168     private boolean addLIntfWidget(Widget widget) {
169         if (vserver != null) {
170             vserver.addWidget(widget);
171             return true;
172         } else {
173             addlintf = true;
174             return false;
175         }
176     }
177
178     private void addVserverWidget(Widget widget) throws XmlArtifactGenerationException {
179         vserver = widget;
180         if (addlintf) {
181             vserver.addWidget(Widget.createWidget("LINT"));
182         }
183         if (addvolume) {
184             vserver.addWidget(Widget.createWidget("VOLUME"));
185         }
186     }
187
188 }