Composite State to handle dmi-reg YANG updates
[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.CompositeState;
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 CompositeState compositeState;
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      *
74      * @param dmiServiceName      dmi service name
75      * @param dmiDataServiceName  dmi data service name
76      * @param dmiModelServiceName dmi model service name
77      * @param ncmpServiceCmHandle the cm handle
78      * @return instance of yangModelCmHandle
79      */
80     public static YangModelCmHandle toYangModelCmHandle(final String dmiServiceName,
81                                                         final String dmiDataServiceName,
82                                                         final String dmiModelServiceName,
83                                                         final NcmpServiceCmHandle ncmpServiceCmHandle) {
84         CpsValidator.validateNameCharacters(ncmpServiceCmHandle.getCmHandleId());
85         final YangModelCmHandle yangModelCmHandle = new YangModelCmHandle();
86         yangModelCmHandle.setId(ncmpServiceCmHandle.getCmHandleId());
87         yangModelCmHandle.setDmiServiceName(dmiServiceName);
88         yangModelCmHandle.setDmiDataServiceName(dmiDataServiceName);
89         yangModelCmHandle.setDmiModelServiceName(dmiModelServiceName);
90         yangModelCmHandle.setDmiProperties(asYangModelCmHandleProperties(ncmpServiceCmHandle.getDmiProperties()));
91         yangModelCmHandle.setPublicProperties(asYangModelCmHandleProperties(
92                 ncmpServiceCmHandle.getPublicProperties()));
93         return yangModelCmHandle;
94     }
95
96     /**
97      * Resolve a dmi service name.
98      *
99      * @param requiredService indicates what typo of service is required
100      * @return dmi service name
101      */
102     public String resolveDmiServiceName(final RequiredDmiService requiredService) {
103         if (isNullEmptyOrBlank(dmiServiceName)) {
104             if (RequiredDmiService.DATA.equals(requiredService)) {
105                 return dmiDataServiceName;
106             }
107             return dmiModelServiceName;
108         }
109         return dmiServiceName;
110     }
111
112     private static List<Property> asYangModelCmHandleProperties(final Map<String, String> propertiesAsMap) {
113         final List<Property> yangModelCmHandleProperties = new ArrayList<>(propertiesAsMap.size());
114         for (final Map.Entry<String, String> entry : propertiesAsMap.entrySet()) {
115             yangModelCmHandleProperties.add(new YangModelCmHandle.Property(entry.getKey(), entry.getValue()));
116         }
117         return yangModelCmHandleProperties;
118     }
119
120     private static boolean isNullEmptyOrBlank(final String serviceName) {
121         return Strings.isNullOrEmpty(serviceName) || serviceName.isBlank();
122     }
123
124     @AllArgsConstructor
125     @Data
126     public static class Property {
127
128         @JsonProperty()
129         private final String name;
130
131         @JsonProperty()
132         private final String value;
133     }
134
135 }