5680d54a487821262bab5a5599b2986470afbc3e
[cps.git] / cps-ncmp-service / src / main / java / org / onap / cps / ncmp / api / impl / yangmodels / YangModelCmHandle.java
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2021-2022 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
21
22 package org.onap.cps.ncmp.api.impl.yangmodels;
23
24 import com.fasterxml.jackson.annotation.JsonInclude;
25 import com.fasterxml.jackson.annotation.JsonInclude.Include;
26 import com.fasterxml.jackson.annotation.JsonProperty;
27 import com.google.common.base.Strings;
28 import java.util.ArrayList;
29 import java.util.List;
30 import java.util.Map;
31 import lombok.AllArgsConstructor;
32 import lombok.Data;
33 import lombok.Getter;
34 import lombok.NoArgsConstructor;
35 import lombok.Setter;
36 import org.onap.cps.ncmp.api.impl.operations.RequiredDmiService;
37 import org.onap.cps.ncmp.api.inventory.CmHandleState;
38 import org.onap.cps.ncmp.api.models.NcmpServiceCmHandle;
39 import org.onap.cps.utils.CpsValidator;
40
41 /**
42  * Cm Handle which follows the Yang resource dmi registry model when persisting data to DMI or the DB.
43  * Yang model CmHandle
44  */
45 @Getter
46 @Setter
47 @NoArgsConstructor
48 @JsonInclude(Include.NON_NULL)
49 public class YangModelCmHandle {
50
51     private String id;
52
53     @JsonProperty("dmi-service-name")
54     private String dmiServiceName;
55
56     @JsonProperty("dmi-data-service-name")
57     private String dmiDataServiceName;
58
59     @JsonProperty("state")
60     private CmHandleState cmHandleState;
61
62     @JsonProperty("dmi-model-service-name")
63     private String dmiModelServiceName;
64
65     @JsonProperty("additional-properties")
66     private List<Property> dmiProperties;
67
68     @JsonProperty("public-properties")
69     private List<Property> publicProperties;
70
71     /**
72      * Create a yangModelCmHandle.
73      * @param dmiServiceName dmi service name
74      * @param dmiDataServiceName dmi data service name
75      * @param dmiModelServiceName dmi model service name
76      * @param ncmpServiceCmHandle the cm handle
77      * @return instance of yangModelCmHandle
78      */
79     public static YangModelCmHandle toYangModelCmHandle(final String dmiServiceName,
80                                                         final String dmiDataServiceName,
81                                                         final String dmiModelServiceName,
82                                                         final NcmpServiceCmHandle ncmpServiceCmHandle) {
83         CpsValidator.validateNameCharacters(ncmpServiceCmHandle.getCmHandleId());
84         final YangModelCmHandle yangModelCmHandle = new YangModelCmHandle();
85         yangModelCmHandle.setId(ncmpServiceCmHandle.getCmHandleId());
86         yangModelCmHandle.setDmiServiceName(dmiServiceName);
87         yangModelCmHandle.setDmiDataServiceName(dmiDataServiceName);
88         yangModelCmHandle.setDmiModelServiceName(dmiModelServiceName);
89         yangModelCmHandle.setDmiProperties(asYangModelCmHandleProperties(ncmpServiceCmHandle.getDmiProperties()));
90         yangModelCmHandle.setPublicProperties(asYangModelCmHandleProperties(
91             ncmpServiceCmHandle.getPublicProperties()));
92         return yangModelCmHandle;
93     }
94
95     /**
96      * Resolve a dmi service name.
97      * @param requiredService indicates what typo of service is required
98      * @return dmi service name
99      */
100     public String resolveDmiServiceName(final RequiredDmiService requiredService) {
101         if (isNullEmptyOrBlank(dmiServiceName)) {
102             if (RequiredDmiService.DATA.equals(requiredService)) {
103                 return dmiDataServiceName;
104             }
105             return dmiModelServiceName;
106         }
107         return dmiServiceName;
108     }
109
110     private static List<Property> asYangModelCmHandleProperties(final Map<String, String> propertiesAsMap) {
111         final List<Property> yangModelCmHandleProperties = new ArrayList<>(propertiesAsMap.size());
112         for (final Map.Entry<String, String> entry : propertiesAsMap.entrySet()) {
113             yangModelCmHandleProperties.add(new YangModelCmHandle.Property(entry.getKey(), entry.getValue()));
114         }
115         return yangModelCmHandleProperties;
116     }
117
118     private static boolean isNullEmptyOrBlank(final String serviceName) {
119         return Strings.isNullOrEmpty(serviceName) || serviceName.isBlank();
120     }
121
122     @AllArgsConstructor
123     @Data
124     public static class Property {
125
126         @JsonProperty()
127         private final String name;
128
129         @JsonProperty()
130         private final String value;
131     }
132
133 }