aabc52c592e25d00cfdb922274edd39c44c40cab
[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.EqualsAndHashCode;
34 import lombok.Getter;
35 import lombok.NoArgsConstructor;
36 import lombok.Setter;
37 import org.onap.cps.ncmp.api.impl.operations.RequiredDmiService;
38 import org.onap.cps.ncmp.api.inventory.CompositeState;
39 import org.onap.cps.ncmp.api.models.NcmpServiceCmHandle;
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 @EqualsAndHashCode(onlyExplicitlyIncluded = true)
50 public class YangModelCmHandle {
51
52     @EqualsAndHashCode.Include
53     private String id;
54
55     @JsonProperty("dmi-service-name")
56     private String dmiServiceName;
57
58     @JsonProperty("dmi-data-service-name")
59     private String dmiDataServiceName;
60
61     @JsonProperty("state")
62     private CompositeState compositeState;
63
64     @JsonProperty("dmi-model-service-name")
65     private String dmiModelServiceName;
66
67     @JsonProperty("additional-properties")
68     private List<Property> dmiProperties;
69
70     @JsonProperty("public-properties")
71     private List<Property> publicProperties;
72
73     /**
74      * Creates a deep copy of Yang Model Cm Handle.
75      *
76      * @param original Yang Model Cm Handle
77      * @return instance of yangModelCmHandle
78      */
79     public static YangModelCmHandle deepCopyOf(final YangModelCmHandle original) {
80         final YangModelCmHandle copy = new YangModelCmHandle();
81         copy.id = original.getId();
82         copy.dmiServiceName = original.getDmiServiceName();
83         copy.dmiDataServiceName = original.getDmiDataServiceName();
84         copy.dmiModelServiceName = original.getDmiModelServiceName();
85         copy.compositeState =
86                 original.getCompositeState() == null ? null : new CompositeState(original.getCompositeState());
87         copy.dmiProperties = original.getDmiProperties() == null ? null : new ArrayList<>(original.getDmiProperties());
88         copy.publicProperties =
89                 original.getPublicProperties() == null ? null : new ArrayList<>(original.getPublicProperties());
90         return copy;
91     }
92
93     /**
94      * Create a yangModelCmHandle.
95      *
96      * @param dmiServiceName      dmi service name
97      * @param dmiDataServiceName  dmi data service name
98      * @param dmiModelServiceName dmi model service name
99      * @param ncmpServiceCmHandle the cm handle
100      * @return instance of yangModelCmHandle
101      */
102     public static YangModelCmHandle toYangModelCmHandle(final String dmiServiceName,
103                                                         final String dmiDataServiceName,
104                                                         final String dmiModelServiceName,
105                                                         final NcmpServiceCmHandle ncmpServiceCmHandle) {
106         final YangModelCmHandle yangModelCmHandle = new YangModelCmHandle();
107         yangModelCmHandle.setId(ncmpServiceCmHandle.getCmHandleId());
108         yangModelCmHandle.setDmiServiceName(dmiServiceName);
109         yangModelCmHandle.setDmiDataServiceName(dmiDataServiceName);
110         yangModelCmHandle.setDmiModelServiceName(dmiModelServiceName);
111         yangModelCmHandle.setDmiProperties(asYangModelCmHandleProperties(ncmpServiceCmHandle.getDmiProperties()));
112         yangModelCmHandle.setPublicProperties(asYangModelCmHandleProperties(
113                 ncmpServiceCmHandle.getPublicProperties()));
114         yangModelCmHandle.setCompositeState(ncmpServiceCmHandle.getCompositeState());
115         return yangModelCmHandle;
116     }
117
118     /**
119      * Resolve a dmi service name.
120      *
121      * @param requiredService indicates what typo of service is required
122      * @return dmi service name
123      */
124     public String resolveDmiServiceName(final RequiredDmiService requiredService) {
125         if (isNullEmptyOrBlank(dmiServiceName)) {
126             if (RequiredDmiService.DATA.equals(requiredService)) {
127                 return dmiDataServiceName;
128             }
129             return dmiModelServiceName;
130         }
131         return dmiServiceName;
132     }
133
134     private static List<Property> asYangModelCmHandleProperties(final Map<String, String> propertiesAsMap) {
135         final List<Property> yangModelCmHandleProperties = new ArrayList<>(propertiesAsMap.size());
136         for (final Map.Entry<String, String> entry : propertiesAsMap.entrySet()) {
137             yangModelCmHandleProperties.add(new YangModelCmHandle.Property(entry.getKey(), entry.getValue()));
138         }
139         return yangModelCmHandleProperties;
140     }
141
142     private static boolean isNullEmptyOrBlank(final String serviceName) {
143         return Strings.isNullOrEmpty(serviceName) || serviceName.isBlank();
144     }
145
146     @AllArgsConstructor
147     @Data
148     public static class Property {
149
150         @JsonProperty()
151         private final String name;
152
153         @JsonProperty()
154         private final String value;
155     }
156
157 }