aboutsummaryrefslogtreecommitdiffstats
path: root/jtosca/src/main/java/org/openecomp/sdc/toscaparser/api/Policy.java
blob: 1474d0b1c46e95daabab9c3adb2c9cd78922b99e (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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package org.openecomp.sdc.toscaparser.api;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.Map;

import org.openecomp.sdc.toscaparser.api.common.ExceptionCollector;
import org.openecomp.sdc.toscaparser.api.utils.ValidateUtils;

public class Policy extends EntityTemplate {
	

	private static final String TYPE = "type";
	private static final String METADATA = "metadata";
	private static final String DESCRIPTION = "description";
	private static final String PROPERTIES = "properties";
	private static final String TARGETS = "targets";
	private static final String TRIGGERS = "triggers";
	private static final String SECTIONS[] = {
			TYPE, METADATA, DESCRIPTION, PROPERTIES, TARGETS, TRIGGERS};
	
	LinkedHashMap<String,Object> metaData;
	ArrayList<Object> targetsList; // *** a list of NodeTemplate OR a list of Group ***
	String targetsType;
	ArrayList<Object> triggers;
	LinkedHashMap<String,Object> properties;
	
	public Policy(String _name,
				  LinkedHashMap<String,Object> _policy,
//				  ArrayList<NodeTemplate> targetObjects,
				  ArrayList<Object> targetObjects,
				  String _targetsType,
				  LinkedHashMap<String,Object> _customDef) {
		super(_name,_policy,"policy_type",_customDef);

        metaData = null;
        if(_policy.get(METADATA) != null) {
        	metaData = (LinkedHashMap<String,Object>)_policy.get(METADATA);
        	ValidateUtils.validateMap(metaData);
        }

        targetsList = targetObjects;
        targetsType = _targetsType;
        triggers = _triggers((LinkedHashMap<String,Object>)_policy.get(TRIGGERS));
        properties = null;
        if(_policy.get("properties") != null) {
        	properties = (LinkedHashMap<String,Object>)_policy.get("properties");
        }
        _validateKeys();
	}

	public ArrayList<String> getTargets() {
		return (ArrayList<String>)entityTpl.get("targets");
	}

	public ArrayList<String> getDescription() {
		return (ArrayList<String>)entityTpl.get("description");
	}

	public ArrayList<String> getmetadata() {
		return (ArrayList<String>)entityTpl.get("metadata");
	}

	public String getTargetsType() {
		return targetsType;
	}
 
//	public ArrayList<NodeTemplate> getTargetsList() {
	public ArrayList<Object> getTargetsList() {
		return targetsList;
	}
	
	// entityTemplate already has a different getProperties...
	// this is to access the local properties variable
	public LinkedHashMap<String,Object> getPolicyProperties() {
		return properties;
	}
	
	private ArrayList<Object> _triggers(LinkedHashMap<String,Object> triggers) {
		ArrayList<Object> triggerObjs = new ArrayList<>();
		if(triggers != null) {
			for(Map.Entry<String,Object> me: triggers.entrySet()) {
				String tname = me.getKey();
				LinkedHashMap<String,Object> ttriggerTpl = 
						(LinkedHashMap<String,Object>)me.getValue();
				Triggers triggersObj = new Triggers(tname,ttriggerTpl);
                triggerObjs.add(triggersObj);
			}
		}
		return triggerObjs;
	}

	private void _validateKeys() {	
		for(String key: entityTpl.keySet()) {
			boolean bFound = false;
			for(int i=0; i<SECTIONS.length; i++) {
				if(key.equals(SECTIONS[i])) {
					bFound = true;
					break;
				}
			}
			if(!bFound) {
	            ExceptionCollector.appendException(String.format(
	                    "UnknownFieldError: Policy \"%s\" contains unknown field \"%s\"",
	                    name,key));
			}
		}
	}

	@Override
	public String toString() {
		return "Policy{" +
				"metaData=" + metaData +
				", targetsList=" + targetsList +
				", targetsType='" + targetsType + '\'' +
				", triggers=" + triggers +
				", properties=" + properties +
				'}';
	}
}

/*python

from toscaparser.common.exception import ExceptionCollector
from toscaparser.common.exception import UnknownFieldError
from toscaparser.entity_template import EntityTemplate
from toscaparser.triggers import Triggers
from toscaparser.utils import validateutils


SECTIONS = (TYPE, METADATA, DESCRIPTION, PROPERTIES, TARGETS, TRIGGERS) = \
           ('type', 'metadata', 'description',
            'properties', 'targets', 'triggers')

log = logging.getLogger('tosca')


class Policy(EntityTemplate):
    '''Policies defined in Topology template.'''
    def __init__(self, name, policy, targets, targets_type, custom_def=None):
        super(Policy, self).__init__(name,
                                     policy,
                                     'policy_type',
                                     custom_def)
        self.meta_data = None
        if self.METADATA in policy:
            self.meta_data = policy.get(self.METADATA)
            validateutils.validate_map(self.meta_data)
        self.targets_list = targets
        self.targets_type = targets_type
        self.triggers = self._triggers(policy.get(TRIGGERS))
        self._validate_keys()

    @property
    def targets(self):
        return self.entity_tpl.get('targets')

    @property
    def description(self):
        return self.entity_tpl.get('description')

    @property
    def metadata(self):
        return self.entity_tpl.get('metadata')

    def get_targets_type(self):
        return self.targets_type

    def get_targets_list(self):
        return self.targets_list

    def _triggers(self, triggers):
        triggerObjs = []
        if triggers:
            for name, trigger_tpl in triggers.items():
                triggersObj = Triggers(name, trigger_tpl)
                triggerObjs.append(triggersObj)
        return triggerObjs

    def _validate_keys(self):
        for key in self.entity_tpl.keys():
            if key not in SECTIONS:
                ExceptionCollector.appendException(
                    UnknownFieldError(what='Policy "%s"' % self.name,
                                      field=key))
*/