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.util.genxsd;
22 import org.apache.commons.text.similarity.LevenshteinDistance;
23 import org.onap.aai.introspection.Version;
24 import org.onap.aai.util.GenerateXsd;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
29 import java.io.FileOutputStream;
30 import java.util.ArrayList;
31 import java.util.Collection;
32 import java.util.HashMap;
35 public class PutRelationPathSet {
36 private static final Logger logger = LoggerFactory.getLogger("PutRelationPathSet.class");
37 protected static HashMap<String, String> putRelationPaths = new HashMap<String, String>();
38 private static EdgeRuleSet edgeRuleSet = null;
39 public static void add(String useOpId, String path) {
40 putRelationPaths.put(useOpId, path);
46 protected ArrayList<String> relations = new ArrayList<String>();
47 String objectName = "";
49 public PutRelationPathSet(Version v) {
53 public PutRelationPathSet(String opId, String path, Version v) {
54 this.apiPath = path.replace("/relationship-list/relationship", "");
57 objectName = DeleteOperation.deletePaths.get(apiPath);
58 logger.debug("II-apiPath: "+apiPath+"\nPath: "+path+"\nopId="+opId+"\nobjectName="+objectName);
60 private void process() {
63 this.writeRelationsFile();
66 private void toRelations() {
67 logger.debug("{“comment”: “Valid TO Relations that can be added”},");
68 logger.debug("apiPath: "+apiPath+"\nopId="+opId+"\nobjectName="+objectName);
69 Collection<EdgeDescription> toEdges = edgeRuleSet.getEdgeRulesTO(objectName);
71 if(toEdges.size() > 0) {
72 relations.add("{\"comment\": \"Valid TO Relations that can be added\"}\n");
74 for (EdgeDescription ed : toEdges) {
75 logger.debug(ed.getRuleKey()+"Type="+ed.getType());
76 String obj = ed.getRuleKey().replace(objectName,"").replace("|","");
77 String selectedRelation = "";
78 if(ed.getType() == EdgeDescription.LineageType.UNRELATED) {
79 String selectObj = getUnrelatedObjectPaths(obj, apiPath);
80 logger.debug("SelectedObj:"+selectObj);
81 selectedRelation = formatObjectRelationSet(obj,selectObj);
82 logger.debug("ObjectRelationSet"+selectedRelation);
84 String selectObj = getKinObjectPath(obj, apiPath);
85 logger.debug("SelectedObj:"+selectObj);
86 selectedRelation = formatObjectRelation(obj,selectObj);
87 logger.debug("ObjectRelationSet"+selectedRelation);
89 relations.add(selectedRelation);
90 logger.debug("Relation added: "+selectedRelation);
94 private void fromRelations() {
95 logger.debug("“comment”: “Valid FROM Relations that can be added”");
96 Collection<EdgeDescription> fromEdges = edgeRuleSet.getEdgeRulesFROM(objectName);
97 if(fromEdges.size() > 0) {
98 relations.add("{\"comment\": \"Valid FROM Relations that can be added\"}\n");
100 for (EdgeDescription ed : fromEdges) {
101 logger.debug(ed.getRuleKey()+"Type="+ed.getType());
102 String obj = ed.getRuleKey().replace(objectName,"").replace("|","");
103 String selectedRelation = "";
104 if(ed.getType() == EdgeDescription.LineageType.UNRELATED) {
105 String selectObj = getUnrelatedObjectPaths(obj, apiPath);
106 logger.debug("SelectedObj"+selectObj);
107 selectedRelation = formatObjectRelationSet(obj,selectObj);
108 logger.trace("ObjectRelationSet"+selectedRelation);
110 String selectObj = getKinObjectPath(obj, apiPath);
111 logger.debug("SelectedObj"+selectObj);
112 selectedRelation = formatObjectRelation(obj,selectObj);
113 logger.trace("ObjectRelationSet"+selectedRelation);
115 relations.add(selectedRelation);
116 logger.trace(selectedRelation);
119 private void writeRelationsFile() {
120 File examplefilePath = new File(GenerateXsd.getYamlDir() + "/relations/" + version.name()+"/"+opId.replace("RelationshipListRelationship", "") + ".json");
122 logger.debug(String.join("exampleFilePath: ", examplefilePath.toString()));
123 FileOutputStream fop = null;
125 if (!examplefilePath.exists()) {
126 examplefilePath.getParentFile().mkdirs();
127 examplefilePath.createNewFile();
129 fop = new FileOutputStream(examplefilePath);
130 } catch(Exception e) {
135 if(relations.size() > 0) {fop.write("[\n".getBytes());}
136 fop.write(String.join(",\n", relations).getBytes());
137 if(relations.size() > 0) {fop.write("\n]\n".getBytes());}
140 } catch (Exception e) {
144 logger.debug(String.join(",\n", relations));
148 private static String formatObjectRelationSet(String obj, String selectObj) {
149 StringBuffer pathSb = new StringBuffer();
150 String[] paths = selectObj.split("[|]");
151 for (String s: paths) {
152 logger.trace("SelectOBJ"+s);
153 pathSb.append(formatObjectRelation(obj, s)+",\n");
155 pathSb.deleteCharAt(pathSb.length()-2);
156 return pathSb.toString();
159 private static String formatObjectRelation(String obj, String selectObj) {
160 StringBuffer pathSb = new StringBuffer();
161 pathSb.append("{\n");
162 pathSb.append("\"related-to\" : \""+obj+"\",\n");
163 pathSb.append("\"related-link\" : \""+selectObj+"\"\n");
165 return pathSb.toString();
168 private static String getKinObjectPath(String obj, String apiPath) {
169 LevenshteinDistance proximity = new LevenshteinDistance();
170 String targetPath = "";
171 int targetScore = Integer.MAX_VALUE;
172 int targetMaxScore = 0;
173 for (Map.Entry<String, String> p : DeleteOperation.deletePaths.entrySet()) {
174 if(p.getValue().equals(obj)) {
175 targetScore = (targetScore >= proximity.apply(apiPath, p.getKey())) ? proximity.apply(apiPath, p.getKey()) : targetScore;
176 targetPath = (targetScore >= proximity.apply(apiPath, p.getKey())) ? p.getKey() : targetPath;
177 targetMaxScore = (targetMaxScore <= proximity.apply(apiPath, p.getKey())) ? proximity.apply(apiPath, p.getKey()) : targetScore;
178 logger.trace(proximity.apply(apiPath, p.getKey())+":"+p.getKey());
179 logger.trace(proximity.apply(apiPath, p.getKey())+":"+apiPath);
185 private static String getUnrelatedObjectPaths(String obj, String apiPath) {
186 String targetPath = "";
187 logger.trace("Obj:"+obj +"\n" + apiPath);
188 for (Map.Entry<String, String> p : DeleteOperation.deletePaths.entrySet()) {
189 if(p.getValue().equals(obj)) {
190 logger.trace("p.getvalue:"+p.getValue()+"p.getkey:"+p.getKey());
191 targetPath += ((targetPath.length() == 0 ? "" : "|") + p.getKey());
192 logger.trace("Match:"+apiPath +"\n" + targetPath);
198 public void generateRelations(EdgeRuleSet edgeRuleSet) {
200 if(putRelationPaths == null)
202 if(edgeRuleSet == null)
205 PutRelationPathSet.edgeRuleSet = edgeRuleSet;
206 putRelationPaths.forEach((k,v)->{
207 logger.trace("k="+k+"\n"+"v="+v+v.equals("/business/customers/customer/{global-customer-id}/service-subscriptions/service-subscription/{service-type}/service-instances/service-instance/{service-instance-id}/allotted-resources/allotted-resource/{id}/relationship-list/relationship"));
208 logger.debug("apiPath(Operation): "+v);
209 logger.debug("Target object: "+v.replace("/relationship-list/relationship", ""));
210 logger.debug("Relations: ");
211 PutRelationPathSet prp = new PutRelationPathSet(k, v, this.version);