2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6 * Copyright © 2017 Amdocs
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
23 package org.onap.aai.sparky.viewandinspect.entity;
25 import java.io.IOException;
26 import java.util.ArrayList;
27 import java.util.Collection;
28 import java.util.HashMap;
29 import java.util.Iterator;
30 import java.util.List;
32 import java.util.Map.Entry;
34 import java.util.concurrent.ConcurrentLinkedDeque;
35 import java.util.concurrent.atomic.AtomicBoolean;
36 import java.util.regex.Matcher;
37 import java.util.regex.Pattern;
39 import org.onap.aai.cl.api.Logger;
40 import org.onap.aai.cl.eelf.LoggerFactory;
41 import org.onap.aai.restclient.client.OperationResult;
42 import org.onap.aai.sparky.config.oxm.OxmEntityLookup;
43 import org.onap.aai.sparky.config.oxm.OxmModelLoader;
44 import org.onap.aai.sparky.logging.AaiUiMsgs;
45 import org.onap.aai.sparky.viewandinspect.config.VisualizationConfigs;
46 import org.onap.aai.sparky.viewandinspect.enumeration.NodeProcessingAction;
47 import org.onap.aai.sparky.viewandinspect.enumeration.NodeProcessingState;
49 import com.fasterxml.jackson.databind.JsonNode;
50 import com.fasterxml.jackson.databind.ObjectMapper;
53 * The Class ActiveInventoryNode.
55 public class ActiveInventoryNode {
57 private static final Logger LOG = LoggerFactory.getInstance().getLogger(
58 ActiveInventoryNode.class);
59 private static final String URIRegexPattern = "aai/v[\\d]/";
61 public static final int DEFAULT_INIT_NODE_DEPTH = 1000;
63 private String nodeId;
64 private String selfLink;
66 private boolean isRootNode;
67 private ConcurrentLinkedDeque<String> inboundNeighbors;
68 private ConcurrentLinkedDeque<String> outboundNeighbors;
69 private List<JsonNode> complexGroups;
70 private List<RelationshipList> relationshipLists;
71 private int nodeDepth;
72 private OperationResult opResult;
74 private boolean processingErrorOccurred;
75 private List<String> errorCauses;
76 private boolean selflinkRetrievalFailure;
77 private NodeProcessingState state;
79 private boolean processedNeighbors;
81 private boolean selfLinkPendingResolve;
84 * I think we shouldn't be using this crutch flags. If these things are meant
85 * to represent the current state of the node, then they should be legitimate
89 private boolean selfLinkDeterminationPending;
91 private AtomicBoolean selfLinkProcessed;
92 private AtomicBoolean nodeIntegrityProcessed;
94 private OxmModelLoader oxmModelLoader;
95 private VisualizationConfigs visualizationConfigs;
97 private String entityType;
98 private String primaryKeyName;
99 private String primaryKeyValue;
101 private boolean nodeValidated;
102 private boolean nodeIssue;
103 private boolean ignoredByFilter;
105 private boolean resolvedSelfLink;
107 private Map<String, String> properties;
108 private ArrayList<String> queryParams;
110 private ObjectMapper mapper;
112 private OxmEntityLookup oxmEntityLookup;
115 * Instantiates a new active inventory node.
119 public ActiveInventoryNode(VisualizationConfigs visualizationConfigs, OxmEntityLookup oxmEntityLookup) {
120 this.oxmEntityLookup = oxmEntityLookup;
122 this.entityType = null;
123 this.selfLink = null;
124 this.properties = new HashMap<String, String>();
125 this.processingErrorOccurred = false;
126 this.errorCauses = new ArrayList<String>();
127 this.selflinkRetrievalFailure = false;
128 this.nodeIssue = false;
129 this.nodeValidated = false;
130 this.state = NodeProcessingState.INIT;
131 this.selfLinkPendingResolve = false;
132 this.selfLinkDeterminationPending = false;
134 selfLinkProcessed = new AtomicBoolean(Boolean.FALSE);
135 nodeIntegrityProcessed = new AtomicBoolean(Boolean.FALSE);
136 oxmModelLoader = null;
137 this.visualizationConfigs = visualizationConfigs ;
140 inboundNeighbors = new ConcurrentLinkedDeque<String>();
141 outboundNeighbors = new ConcurrentLinkedDeque<String>();
142 complexGroups = new ArrayList<JsonNode>();
143 relationshipLists = new ArrayList<RelationshipList>();
144 nodeDepth = DEFAULT_INIT_NODE_DEPTH;
145 queryParams = new ArrayList<String>();
147 mapper = new ObjectMapper();
149 processedNeighbors = false;
150 resolvedSelfLink = false;
155 public void clearQueryParams() {
159 public void addQueryParam(String queryParam) {
160 if ( queryParam!= null) {
161 if( !queryParams.contains(queryParam)) {
162 queryParams.add(queryParam);
167 public void addQueryParams(Collection<String> params) {
169 if (params != null & params.size() > 0) {
171 for (String param : params) {
172 addQueryParam(param);
178 public List<String> getQueryParams() {
182 public void setSelfLinkDeterminationPending(boolean selfLinkDeterminationPending) {
183 this.selfLinkDeterminationPending = selfLinkDeterminationPending;
186 public boolean isSelfLinkDeterminationPending() {
187 return selfLinkDeterminationPending;
190 public NodeProcessingState getState() {
194 public List<JsonNode> getComplexGroups() {
195 return complexGroups;
198 public List<RelationshipList> getRelationshipLists() {
199 return relationshipLists;
202 public OperationResult getOpResult() {
206 public void setOpResult(OperationResult opResult) {
207 this.opResult = opResult;
210 public String getPrimaryKeyName() {
211 return primaryKeyName;
215 * Gets the visualization config.
217 * @return the visualization config
219 public VisualizationConfigs getvisualizationConfigs() {
220 return visualizationConfigs;
223 public int getNodeDepth() {
227 public void setNodeDepth(int nodeDepth) {
228 this.nodeDepth = nodeDepth;
232 * Sets the visualization config.
234 * @param visualizationConfig the new visualization config
236 public void setvisualizationConfig(VisualizationConfigs visualizationConfigs) {
237 this.visualizationConfigs = visualizationConfigs;
240 public OxmModelLoader getOxmModelLoader() {
241 return oxmModelLoader;
244 public void setPrimaryKeyName(String primaryKeyName) {
245 this.primaryKeyName = primaryKeyName;
248 public String getPrimaryKeyValue() {
249 return primaryKeyValue;
252 public void setPrimaryKeyValue(String primaryKeyValue) {
253 this.primaryKeyValue = primaryKeyValue;
256 public boolean isNodeValidated() {
257 return nodeValidated;
260 public void setNodeValidated(boolean nodeValidated) {
261 this.nodeValidated = nodeValidated;
264 public boolean isNodeIssue() {
268 public boolean isIgnoredByFilter() {
269 return ignoredByFilter;
272 public void setIgnoredByFilter(boolean ignoredByFilter) {
273 this.ignoredByFilter = ignoredByFilter;
276 public void setNodeIssue(boolean nodeIssue) {
277 this.nodeIssue = nodeIssue;
281 * Checks for processed neighbors.
283 * @return true, if successful
285 public boolean hasProcessedNeighbors() {
286 return processedNeighbors;
289 public void setProcessedNeighbors(boolean processedNeighbors) {
290 this.processedNeighbors = processedNeighbors;
294 * Checks for resolved self link.
296 * @return true, if successful
298 public boolean hasResolvedSelfLink() {
299 return resolvedSelfLink;
302 public void setResolvedSelfLink(boolean resolvedSelfLink) {
303 this.resolvedSelfLink = resolvedSelfLink;
307 * Checks for neighbors.
309 * @return true, if successful
311 public boolean hasNeighbors() {
312 return (inboundNeighbors.size() > 0 || outboundNeighbors.size() > 0);
316 * Adds the inbound neighbor.
318 * @param nodeId the node id
320 public void addInboundNeighbor(String nodeId) {
322 if (nodeId == null) {
326 if (!inboundNeighbors.contains(nodeId)) {
327 inboundNeighbors.add(nodeId);
333 * Adds the outbound neighbor.
335 * @param nodeId the node id
337 public void addOutboundNeighbor(String nodeId) {
339 if (nodeId == null) {
343 if (!outboundNeighbors.contains(nodeId)) {
344 outboundNeighbors.add(nodeId);
349 public boolean isAtMaxDepth() {
350 return (nodeDepth >= this.visualizationConfigs.getMaxSelfLinkTraversalDepth());
353 public ConcurrentLinkedDeque<String> getInboundNeighbors() {
354 return inboundNeighbors;
357 public void setInboundNeighbors(ConcurrentLinkedDeque<String> inboundNeighbors) {
358 this.inboundNeighbors = inboundNeighbors;
361 public Collection<String> getOutboundNeighbors() {
362 List<String> result = new ArrayList<String>();
364 Iterator<String> neighborIterator = outboundNeighbors.iterator();
366 while (neighborIterator.hasNext()) {
367 result.add(neighborIterator.next());
376 * @param newDepth the new depth
377 * @return true, if successful
379 public boolean changeDepth(int newDepth) {
381 boolean nodeDepthWasChanged = false;
383 if (newDepth < nodeDepth) {
384 LOG.info(AaiUiMsgs.ACTIVE_INV_NODE_CHANGE_DEPTH, nodeId,
385 String.valueOf(this.nodeDepth), String.valueOf(newDepth));
386 this.nodeDepth = newDepth;
387 nodeDepthWasChanged = true;
390 return nodeDepthWasChanged;
394 public void setOutboundNeighbors(ConcurrentLinkedDeque<String> outboundNeighbors) {
395 this.outboundNeighbors = outboundNeighbors;
398 public boolean isRootNode() {
402 public void setRootNode(boolean isRootNode) {
403 this.isRootNode = isRootNode;
409 * @param newState the new state
410 * @param action the action
412 public void changeState(NodeProcessingState newState, NodeProcessingAction action) {
414 * NodeId may be null depending on the current node life-cycle state
417 if (getNodeId() != null) {
418 LOG.info(AaiUiMsgs.ACTIVE_INV_NODE_CHANGE_STATE, state.toString(), newState.toString(), action.toString());
420 LOG.info(AaiUiMsgs.ACTIVE_INV_NODE_CHANGE_STATE_NO_NODE_ID, state.toString(), newState.toString(), action.toString());
422 this.state = newState;
425 public boolean isSelfLinkPendingResolve() {
426 return selfLinkPendingResolve;
429 public void setSelfLinkPendingResolve(boolean selfLinkPendingResolve) {
430 this.selfLinkPendingResolve = selfLinkPendingResolve;
433 public boolean isSelflinkRetrievalFailure() {
434 return selflinkRetrievalFailure;
437 public void setSelflinkRetrievalFailure(boolean selflinkRetrievalFailure) {
438 this.selflinkRetrievalFailure = selflinkRetrievalFailure;
441 public void setOxmModelLoader(OxmModelLoader loader) {
442 this.oxmModelLoader = loader;
445 public boolean getSelfLinkProcessed() {
446 return selfLinkProcessed.get();
449 public void setSelfLinkProcessed(boolean selfLinkProcessed) {
450 this.selfLinkProcessed.set(selfLinkProcessed);
453 public boolean getNodeIntegrityProcessed() {
454 return nodeIntegrityProcessed.get();
457 public void setNodeIntegrityProcessed(boolean nodeIntegrityProcessed) {
458 this.nodeIntegrityProcessed.set(nodeIntegrityProcessed);
461 public boolean isDirectSelfLink() {
462 return isDirectSelfLink(this.selfLink);
466 * Checks if is direct self link.
468 * @param link the link
469 * @return true, if is direct self link
471 public static boolean isDirectSelfLink(String link) {
477 return link.contains("/resources/id/");
481 public Map<String, String> getProperties() {
486 * Adds the error cause.
488 * @param error the error
490 public void addErrorCause(String error) {
491 if (!errorCauses.contains(error)) {
492 errorCauses.add(error);
500 * @param value the value
502 public void addProperty(String key, String value) {
503 properties.put(key, value);
506 public boolean isProcessingErrorOccurred() {
507 return processingErrorOccurred;
510 public void setProcessingErrorOccurred(boolean processingErrorOccurred) {
511 this.processingErrorOccurred = processingErrorOccurred;
514 public String getNodeId() {
518 public void setNodeId(String nodeId) {
519 this.nodeId = nodeId;
522 public String getEntityType() {
526 public void setEntityType(String entityType) {
527 this.entityType = entityType;
530 public String getSelfLink() {
535 * Calculate edit attribute uri.
537 * @param link the link
540 public String calculateEditAttributeUri(String link) {
542 Pattern pattern = Pattern.compile(URIRegexPattern);
543 Matcher matcher = pattern.matcher(link);
544 if (matcher.find()) {
545 uri = link.substring(matcher.end());
551 * Analyze self link relationship list.
553 * @param jsonResult the json result
554 * @return the relationship list
556 private RelationshipList analyzeSelfLinkRelationshipList(String jsonResult) {
559 RelationshipList relationshipList = null;
562 relationshipList = mapper.readValue(jsonResult, RelationshipList.class);
563 } catch (Exception exc) {
564 LOG.error(AaiUiMsgs.SELF_LINK_RELATIONSHIP_LIST_ERROR, exc.toString());
567 return relationshipList;
571 * Adds the relationship list.
573 * @param relationshipList the relationship list
575 public void addRelationshipList(RelationshipList relationshipList) {
577 if (!relationshipLists.contains(relationshipList)) {
578 relationshipLists.add(relationshipList);
584 * Process pathed self link response.
586 * @param selfLinkJsonResponse the self link json response
587 * @param startNodeType the start node type
588 * @param startNodeResourceKey the start node resource key
590 public void processPathedSelfLinkResponse(String selfLinkJsonResponse, String startNodeType,
591 String startNodeResourceKey) {
593 if (selfLinkJsonResponse == null || selfLinkJsonResponse.length() == 0) {
594 LOG.error(AaiUiMsgs.SELF_LINK_NULL_EMPTY_RESPONSE);
599 JsonNode jsonNode = mapper.readValue(selfLinkJsonResponse, JsonNode.class);
601 Iterator<Entry<String, JsonNode>> fieldNames = jsonNode.fields();
602 Entry<String, JsonNode> field = null;
604 while (fieldNames.hasNext()) {
606 field = fieldNames.next();
609 * Is there a way to tell if the field is an aggregate or an atomic value? This is where our
610 * flattening code needs to live
613 String fieldName = field.getKey();
615 if ("relationship-list".equals(fieldName)) {
618 * Parse the relationship list like we were doing before, so we can determine whether or
619 * not to keep it or traverse it after we have performed the evaluative node depth logic.
621 RelationshipList relationshipList =
622 analyzeSelfLinkRelationshipList(field.getValue().toString());
624 if (relationshipList != null) {
625 this.relationshipLists.add(relationshipList);
627 LOG.info(AaiUiMsgs.NO_RELATIONSHIP_DISCOVERED, nodeId);
630 JsonNode nodeValue = field.getValue();
632 if (nodeValue != null && nodeValue.isValueNode()) {
635 * before we blindly add the fieldName and value to our property set, let's do one more
636 * check to see if the field name is an entity type. If it is, then our complex
637 * attribute processing code will pick it up and process it instead, but this is
638 * probably more likely just for array node types, but we'll see.
641 if (oxmEntityLookup.getEntityDescriptors().get(fieldName) == null) {
643 * this is no an entity type as far as we can tell, so we can add it to our property
647 addProperty(fieldName, nodeValue.asText());
653 if (nodeValue.isArray()) {
656 * make sure array entity-type collection is not an entityType before adding it to the
657 * property set. The expetation is that it will be added the visualization through a
658 * complex group or relationship.
661 if (oxmEntityLookup.getEntityDescriptors().get(field.getKey()) == null) {
663 * this is no an entity type as far as we can tell, so we can add it to our property
667 addProperty(field.getKey(), nodeValue.toString());
673 complexGroups.add(nodeValue);
683 } catch (IOException exc) {
684 LOG.error(AaiUiMsgs.JSON_CONVERSION_ERROR, "POJO", exc.getLocalizedMessage());
685 this.setProcessingErrorOccurred(true);
687 "An error occurred while converting JSON into POJO = " + exc.getLocalizedMessage());
692 public void setSelfLink(String selfLink) {
693 this.selfLink = selfLink;
697 * Adds the complex group.
699 * @param complexGroup the complex group
701 public void addComplexGroup(JsonNode complexGroup) {
703 if (!complexGroups.contains(complexGroup)) {
704 complexGroups.add(complexGroup);
712 * @param level the level
713 * @param paddingString the padding string
714 * @return the padding
716 private static String getPadding(int level, String paddingString) {
717 StringBuilder sb = new StringBuilder(32);
718 for (int x = 0; x < level; x++) {
719 sb.append(paddingString);
721 return sb.toString();
727 * @param showProperties the show properties
730 public String dumpNodeTree(boolean showProperties) {
731 return dumpNodeTree(0, showProperties);
737 * @param level the level
738 * @param showProperties the show properties
741 private String dumpNodeTree(int level, boolean showProperties) {
742 StringBuilder sb = new StringBuilder(128);
743 String padding = getPadding(level, " ");
745 sb.append(padding + " -> " + getNodeId() + "]").append("\n");
746 sb.append(padding + " -> primaryKeyName = " + primaryKeyName + "]").append("\n");
747 sb.append(padding + " -> primaryKeyValue = " + primaryKeyValue + "]").append("\n");
748 sb.append(padding + " -> entityType = " + entityType + "]").append("\n");
750 if (showProperties) {
751 Set<Entry<String, String>> entries = properties.entrySet();
752 for (Entry<String, String> entry : entries) {
754 padding + " ----> " + String.format("[ %s => %s ]", entry.getKey(), entry.getValue()))
759 sb.append(padding + " ----> " + String.format("[ selfLink => %s ]", getSelfLink()))
762 sb.append("\n").append(padding + " ----> Inbound Neighbors:").append("\n");
764 for (String inboundNeighbor : inboundNeighbors) {
765 sb.append("\n").append(inboundNeighbor.toString());
768 sb.append(padding + " ----> Outbound Neighbors:").append("\n");
769 sb.append("\n").append(padding + " ----> Outbound Neighbors:").append("\n");
771 for (String outboundNeighbor : outboundNeighbors) {
772 sb.append("\n").append(outboundNeighbor.toString());
775 return sb.toString();
779 public String getProcessingErrorCauses() {
781 StringBuilder sb = new StringBuilder(128);
783 for (String c : this.errorCauses) {
784 sb.append(c).append("\n");
787 return sb.toString();