34b7398358d79be9c81c0df79271b87c85ec4cce
[dcaegen2/platform.git] / mod / genprocessor / src / main / java / org / onap / dcae / genprocessor / CompSpec.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  * 
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  * 
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  * ============LICENSE_END=========================================================
17  */
18 package org.onap.dcae.genprocessor;
19
20 import java.io.File;
21 import java.net.URI;
22 import java.util.ArrayList;
23 import java.util.List;
24 import java.util.Map;
25
26 import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
27 import com.fasterxml.jackson.annotation.JsonProperty;
28 import com.fasterxml.jackson.core.JsonFactory;
29 import com.fasterxml.jackson.databind.ObjectMapper;
30
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34
35 @JsonIgnoreProperties(ignoreUnknown = true)
36 public class CompSpec {
37
38     static final Logger LOG = LoggerFactory.getLogger(App.class);
39
40     public String name;
41     // Name of component to be transformed to be more Java style
42     public String nameJavaClass;
43     public String version;
44     public String description;
45
46     // https://stackoverflow.com/questions/37010891/how-to-map-a-nested-value-to-a-property-using-jackson-annotations
47     @JsonProperty("self")
48     public void unpackSelf(Map<String, String> self) {
49         this.name = self.get("name");
50         this.nameJavaClass = Utils.formatNameForJavaClass(self.get("name"));
51         this.version = self.get("version");
52         this.description = self.get("description");
53     }
54
55
56     @JsonIgnoreProperties(ignoreUnknown = true)
57     public static class Parameter {
58         @JsonProperty("name")
59         public String name;
60         @JsonProperty("value")
61         public String value;
62         @JsonProperty("description")
63         public String description;
64         @JsonProperty("sourced_at_deployment")
65         public boolean sourcedAtDeployment;
66         @JsonProperty("policy_editable")
67         public boolean policyEditable;
68         @JsonProperty("designer_editable")
69         public boolean designerEditable;
70
71         public String toString() {
72             String[] params = new String[] {
73                 String.format("name: \"%s\"", this.name)
74                 , String.format("value: \"%s\"", this.value)
75                 , String.format("description: \"%s\"", this.description)
76             };
77             return String.join(", ", params);
78         }
79     }
80
81     @JsonProperty("parameters")
82     public List<Parameter> parameters;
83
84     @JsonIgnoreProperties(ignoreUnknown = true)
85     public static class Connection {
86         @JsonProperty("format")
87         public String format;
88         @JsonProperty("version")
89         public String version;
90         @JsonProperty("type")
91         public String type;
92         @JsonProperty("config_key")
93         public String configKey;
94     }
95
96     @JsonProperty("streams")
97     public Map<String, List<Connection>> streams;
98
99     public List<Connection> getPublishes() {
100         return streams.containsKey("publishes") ? streams.get("publishes") : null;
101     }
102
103     public List<Connection> getSubscribes() {
104         return streams.containsKey("subscribes") ? streams.get("subscribes") : null;
105     }
106
107     public String toString(String delimiter) {
108         List<String> items = new ArrayList();
109         items.add(String.format("name: %s", name));
110         items.add(String.format("version: %s", version));
111         items.add(String.format("description: %s", description));
112         items.add(String.format("parameters: %d", parameters.size()));
113
114         if (!parameters.isEmpty()) {
115             // Cap at MAX
116             int MAX=parameters.size() > 3 ? 3 : parameters.size();
117             for (int i=0; i<MAX; i++) {
118                 items.add(String.format("\t%s", parameters.get(i).toString()));
119             }
120         }
121
122         items.add("\t..");
123
124         return String.join(delimiter, items.toArray(new String[items.size()]));
125     }
126
127     public static CompSpec loadComponentSpec(File compSpecFile) {
128         return loadComponentSpec(compSpecFile.toURI());
129     }
130
131     public static CompSpec loadComponentSpec(URI compSpecURI) {
132         JsonFactory jf = new JsonFactory();
133         ObjectMapper om = new ObjectMapper();
134
135         try {
136             return om.readValue(jf.createParser(compSpecURI.toURL()), CompSpec.class);
137         } catch (Exception e) {
138             LOG.error("Uhoh", e);
139         }
140
141         throw new RuntimeException("REPLACE ME");
142     }
143
144 }