Fix for ETSI_CATALOG onboarding NS package
[integration/csit.git] / plans / so / integration-etsi-testing / so-simulators / sdc-simulator / src / main / java / org / onap / so / sdcsimulator / models / AssetType.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2021 Nordix Foundation.
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  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20 package org.onap.so.sdcsimulator.models;
21
22 import static org.onap.so.sdcsimulator.utils.Constants.CATALOG_URL;
23 import static org.onap.so.sdcsimulator.utils.Constants.FORWARD_SLASH;
24 import java.io.File;
25 import java.io.IOException;
26 import org.springframework.core.io.Resource;
27 import com.fasterxml.jackson.databind.DeserializationFeature;
28 import com.fasterxml.jackson.databind.ObjectMapper;
29
30 /**
31  *
32  * @author Waqas Ikram (waqas.ikram@est.tech)
33  *
34  */
35 public enum AssetType {
36
37     RESOURCES {
38         @Override
39         public AssetInfo getAssetInfo(final Resource resource) throws IOException {
40             return OBJ_MAPPER.readValue(resource.getInputStream(), ResourceAssetInfo.class);
41         }
42
43         @Override
44         public AssetInfo getAssetInfo(final File file) throws IOException {
45             return OBJ_MAPPER.readValue(file, ResourceAssetInfo.class);
46         }
47
48         @Override
49         public Metadata getMetadata(final Resource resource) throws IOException {
50             return OBJ_MAPPER.readValue(resource.getInputStream(), ResourceMetadata.class);
51         }
52
53         @Override
54         public Metadata getMetadata(final File file) throws IOException {
55             return OBJ_MAPPER.readValue(file, ResourceMetadata.class);
56         }
57
58     },
59     SERVICES {
60         @Override
61         public AssetInfo getAssetInfo(final Resource resource) throws IOException {
62             return OBJ_MAPPER.readValue(resource.getInputStream(), ServiceAssetInfo.class);
63         }
64
65         @Override
66         public AssetInfo getAssetInfo(final File file) throws IOException {
67             return OBJ_MAPPER.readValue(file, ServiceAssetInfo.class);
68         }
69
70         @Override
71         public Metadata getMetadata(final Resource resource) throws IOException {
72             return OBJ_MAPPER.readValue(resource.getInputStream(), ServiceMetadata.class);
73         }
74
75         @Override
76         public Metadata getMetadata(final File file) throws IOException {
77             return OBJ_MAPPER.readValue(file, ServiceMetadata.class);
78         }
79
80     };
81
82     private static final ObjectMapper OBJ_MAPPER =
83             new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
84
85
86     public abstract AssetInfo getAssetInfo(final Resource resource) throws IOException;
87
88     public abstract AssetInfo getAssetInfo(final File file) throws IOException;
89
90     public abstract Metadata getMetadata(final Resource resource) throws IOException;
91
92     public abstract Metadata getMetadata(final File file) throws IOException;
93
94     public String getToscaModelUrl(final String filename) {
95         return CATALOG_URL + FORWARD_SLASH + this.toString().toLowerCase() + FORWARD_SLASH + filename + "/toscaModel";
96     }
97
98     public AssetInfo getDefaultAssetInfo(final String filename) {
99         AssetInfo defaultValue = null;
100
101         if (this.equals(RESOURCES)) {
102             defaultValue = new ResourceAssetInfo().subCategory("Network Service");
103         } else if (this.equals(SERVICES)) {
104             defaultValue = new ServiceAssetInfo().distributionStatus("DISTRIBUTED");
105         } else {
106             defaultValue = new AssetInfo();
107         }
108
109         return defaultValue.uuid(filename).invariantUuid(filename).name(filename).version("1.0")
110                 .toscaModelUrl(getToscaModelUrl(filename)).category("Generic").lifecycleState("CERTIFIED")
111                 .lastUpdaterUserId("SDC_SIMULATOR");
112     }
113
114 }