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
9 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
18 package org.onap.dcae.genprocessor;
22 import java.util.ArrayList;
23 import java.util.List;
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;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
35 @JsonIgnoreProperties(ignoreUnknown = true)
36 public class CompSpec {
38 static final Logger LOG = LoggerFactory.getLogger(App.class);
41 // Name of component to be transformed to be more Java style
42 public String nameJavaClass;
43 public String version;
44 public String description;
46 // https://stackoverflow.com/questions/37010891/how-to-map-a-nested-value-to-a-property-using-jackson-annotations
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");
56 @JsonIgnoreProperties(ignoreUnknown = true)
57 public static class Parameter {
60 @JsonProperty("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;
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)
77 return String.join(", ", params);
81 @JsonProperty("parameters")
82 public List<Parameter> parameters;
84 @JsonIgnoreProperties(ignoreUnknown = true)
85 public static class Connection {
86 @JsonProperty("format")
88 @JsonProperty("version")
89 public String version;
92 @JsonProperty("config_key")
93 public String configKey;
96 @JsonProperty("streams")
97 public Map<String, List<Connection>> streams;
99 public List<Connection> getPublishes() {
100 return streams.containsKey("publishes") ? streams.get("publishes") : null;
103 public List<Connection> getSubscribes() {
104 return streams.containsKey("subscribes") ? streams.get("subscribes") : null;
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()));
114 if (!parameters.isEmpty()) {
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()));
124 return String.join(delimiter, items.toArray(new String[items.size()]));
127 public static CompSpec loadComponentSpec(File compSpecFile) {
128 return loadComponentSpec(compSpecFile.toURI());
131 public static CompSpec loadComponentSpec(URI compSpecURI) {
132 JsonFactory jf = new JsonFactory();
133 ObjectMapper om = new ObjectMapper();
136 return om.readValue(jf.createParser(compSpecURI.toURL()), CompSpec.class);
137 } catch (Exception e) {
138 LOG.error("Uhoh", e);
141 throw new RuntimeException("REPLACE ME");