summaryrefslogtreecommitdiffstats
path: root/openecomp-be/lib/openecomp-sdc-vendor-license-lib/openecomp-sdc-vendor-license-core/src/main/java/org/openecomp/sdc/vendorlicense/dao/impl/zusammen/LimitZusammenDaoImpl.java
blob: 7b90a270bf40c6d5ff3ad7c97feeeddcb78915d8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*-
 * ============LICENSE_START=======================================================
 * SDC
 * ================================================================================
 * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */
package org.openecomp.sdc.vendorlicense.dao.impl.zusammen;

import static org.openecomp.core.zusammen.api.ZusammenUtil.buildElement;
import static org.openecomp.core.zusammen.api.ZusammenUtil.buildStructuralElement;
import static org.openecomp.core.zusammen.api.ZusammenUtil.createSessionContext;

import com.amdocs.zusammen.adaptor.inbound.api.types.item.Element;
import com.amdocs.zusammen.adaptor.inbound.api.types.item.ElementInfo;
import com.amdocs.zusammen.adaptor.inbound.api.types.item.ZusammenElement;
import com.amdocs.zusammen.datatypes.Id;
import com.amdocs.zusammen.datatypes.SessionContext;
import com.amdocs.zusammen.datatypes.item.Action;
import com.amdocs.zusammen.datatypes.item.ElementContext;
import com.amdocs.zusammen.datatypes.item.Info;
import java.util.Collection;
import java.util.Collections;
import java.util.stream.Collectors;
import org.openecomp.core.zusammen.api.ZusammenAdaptor;
import org.openecomp.sdc.datatypes.model.ElementType;
import org.openecomp.sdc.vendorlicense.dao.LimitDao;
import org.openecomp.sdc.vendorlicense.dao.types.AggregationFunction;
import org.openecomp.sdc.vendorlicense.dao.types.LimitEntity;
import org.openecomp.sdc.vendorlicense.dao.types.LimitType;
import org.openecomp.sdc.versioning.dao.types.Version;
import org.openecomp.types.ElementPropertyName;

public class LimitZusammenDaoImpl implements LimitDao {

    private static final String LIMT_TYPE = "type";
    private static final String METRIC = "metric";
    private static final String AGGREGATIONFUNCTION = "aggregationfunction";
    private static final String TIME = "time";
    private static final String UNIT = "unit";
    private static final String VALUE = "value";
    private ZusammenAdaptor zusammenAdaptor;

    public LimitZusammenDaoImpl(ZusammenAdaptor zusammenAdaptor) {
        this.zusammenAdaptor = zusammenAdaptor;
    }

    @Override
    public void create(LimitEntity limitEntity) {
        ZusammenElement limitElement = limitToZusammen(limitEntity, Action.CREATE);
        ZusammenElement limitsElement = buildStructuralElement(ElementType.Limits, null);
        limitsElement.setSubElements(Collections.singletonList(limitElement));
        ZusammenElement epLkgElement = buildElement(new Id(limitEntity.getEpLkgId()), Action.IGNORE);
        epLkgElement.setSubElements(Collections.singletonList(limitsElement));
        SessionContext context = createSessionContext();
        ElementContext elementContext = new ElementContext(limitEntity.getVendorLicenseModelId(), limitEntity.getVersion().getId());
        Element savedElement = zusammenAdaptor.saveElement(context, elementContext, epLkgElement, "Create limit");
        limitEntity.setId(savedElement.getSubElements().iterator().next().getSubElements().iterator().next().getElementId().getValue());
    }

    @Override
    public boolean isLimitPresent(LimitEntity limitEntity) {
        SessionContext context = createSessionContext();
        ElementContext elementContext = new ElementContext(limitEntity.getVendorLicenseModelId(), limitEntity.getVersion().getId());
        Collection<ElementInfo> elementInfos = zusammenAdaptor
            .listElementsByName(context, elementContext, new Id(limitEntity.getEpLkgId()), ElementType.Limits.name());
        for (ElementInfo elementInfo : elementInfos) {
            if (elementInfo.getId().getValue().equals(limitEntity.getId())) {
                return true;
            }
        }
        return false;
    }

    @Override
    public Collection<LimitEntity> list(LimitEntity limitEntity) {
        SessionContext context = createSessionContext();
        ElementContext elementContext = new ElementContext(limitEntity.getVendorLicenseModelId(), limitEntity.getVersion().getId());
        return listLimits(context, elementContext, limitEntity);
    }

    private Collection<LimitEntity> listLimits(SessionContext context, ElementContext elementContext, LimitEntity limitEntity) {
        return zusammenAdaptor.listElementsByName(context, elementContext, new Id(limitEntity.getEpLkgId()), ElementType.Limits.name()).stream().map(
            elementInfo -> mapElementInfoToLimit(limitEntity.getVendorLicenseModelId(), limitEntity.getVersion(), limitEntity.getEpLkgId(),
                elementInfo)).collect(Collectors.toList());
    }

    private LimitEntity mapElementInfoToLimit(String vlmId, Version version, String epLkgId, ElementInfo elementInfo) {
        LimitEntity limitEntity = new LimitEntity(vlmId, version, epLkgId, elementInfo.getId().getValue());
        limitEntity.setName(elementInfo.getInfo().getName());
        limitEntity.setDescription(elementInfo.getInfo().getDescription());
        limitEntity.setType(elementInfo.getInfo().getProperties().get(LIMT_TYPE) != null ? LimitType
            .valueOf((String) elementInfo.getInfo().getProperties().get(LIMT_TYPE)) : null);
        limitEntity.setTime((String) elementInfo.getInfo().getProperties().get(TIME));
        limitEntity.setMetric((String) elementInfo.getInfo().getProperties().get(METRIC));
        limitEntity.setAggregationFunction(elementInfo.getInfo().getProperties().get(AGGREGATIONFUNCTION) != null ? AggregationFunction
            .valueOf((String) elementInfo.getInfo().getProperties().get(AGGREGATIONFUNCTION)) : null);
        Object unit = elementInfo.getInfo().getProperties().get(UNIT);
        limitEntity.setUnit((String) unit);
        Object value = elementInfo.getInfo().getProperties().get(VALUE);
        limitEntity.setValue((String) value);
        return limitEntity;
    }

    @Override
    public void update(LimitEntity limitEntity) {
        ZusammenElement limitElement = limitToZusammen(limitEntity, Action.UPDATE);
        SessionContext context = createSessionContext();
        ElementContext elementContext = new ElementContext(limitEntity.getVendorLicenseModelId(), limitEntity.getVersion().getId());
        zusammenAdaptor.saveElement(context, elementContext, limitElement, String.format("Update limit with id %s", limitEntity.getId()));
    }

    @Override
    public LimitEntity get(LimitEntity limitEntity) {
        SessionContext context = createSessionContext();
        ElementContext elementContext = new ElementContext(limitEntity.getVendorLicenseModelId(), limitEntity.getVersion().getId());
        return zusammenAdaptor.getElementInfo(context, elementContext, new Id(limitEntity.getId())).map(
            elementInfo -> mapElementInfoToLimit(limitEntity.getVendorLicenseModelId(), limitEntity.getVersion(), limitEntity.getEpLkgId(),
                elementInfo)).orElse(null);
    }

    @Override
    public void delete(LimitEntity limitEntity) {
        ZusammenElement zusammenElement = buildElement(new Id(limitEntity.getId()), Action.DELETE);
        SessionContext context = createSessionContext();
        ElementContext elementContext = new ElementContext(limitEntity.getVendorLicenseModelId(), limitEntity.getVersion().getId());
        zusammenAdaptor.saveElement(context, elementContext, zusammenElement, "delete limit Id:" + limitEntity.getId() + ".");
    }

    @Override
    public void registerVersioning(String versionableEntityType) {
        //registerVersioning not implemented for LimitZusammenDaoImpl
    }

    private ZusammenElement limitToZusammen(LimitEntity limit, Action action) {
        ZusammenElement limitElement = buildElement(limit.getId() == null ? null : new Id(limit.getId()), action);
        Info info = new Info();
        info.setName(limit.getName());
        info.setDescription(limit.getDescription());
        info.addProperty(ElementPropertyName.elementType.name(), ElementType.Limit);
        info.addProperty(LIMT_TYPE, limit.getType());
        info.addProperty(METRIC, limit.getMetric());
        info.addProperty(AGGREGATIONFUNCTION, limit.getAggregationFunction());
        info.addProperty(TIME, limit.getTime());
        info.addProperty(VALUE, limit.getValue());
        info.addProperty(UNIT, limit.getUnit());
        limitElement.setInfo(info);
        return limitElement;
    }
}