001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *      http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018
019package org.apache.oozie.command.wf;
020
021import org.apache.hadoop.conf.Configuration;
022import org.apache.oozie.service.WorkflowAppService;
023import org.jdom.Element;
024import org.jdom.Namespace;
025import org.apache.oozie.client.XOozieClient;
026import org.apache.oozie.command.CommandException;
027
028import java.util.HashMap;
029import java.util.HashSet;
030import java.util.Iterator;
031import java.util.Map;
032import java.util.Set;
033
034public class SubmitMRXCommand extends SubmitHttpXCommand {
035    private static final Set<String> SKIPPED_CONFS = new HashSet<String>();
036    private static final Map<String, String> DEPRECATE_MAP = new HashMap<String, String>();
037
038    public SubmitMRXCommand(Configuration conf) {
039        super("submitMR", "submitMR", conf);
040    }
041
042    static {
043        SKIPPED_CONFS.add(WorkflowAppService.HADOOP_USER);
044        SKIPPED_CONFS.add(XOozieClient.RM);
045        SKIPPED_CONFS.add(XOozieClient.NN);
046
047        DEPRECATE_MAP.put(XOozieClient.NN, XOozieClient.NN_2);
048        DEPRECATE_MAP.put(WorkflowAppService.HADOOP_USER, "mapreduce.job.user.name");
049    }
050
051    @Override
052    protected Namespace getSectionNamespace(){
053        return Namespace.getNamespace("uri:oozie:workflow:0.2");
054    }
055
056    @Override
057    protected String getWorkflowName(){
058        return "mapreduce";
059    }
060
061    private Element generateConfigurationSection(Configuration conf, Namespace ns) {
062        Element configuration = null;
063        Iterator<Map.Entry<String, String>> iter = conf.iterator();
064        while (iter.hasNext()) {
065            Map.Entry<String, String> entry = iter.next();
066            String name = entry.getKey();
067            if (MANDATORY_OOZIE_CONFS.contains(name) || OPTIONAL_OOZIE_CONFS.contains(name)
068                    || SKIPPED_CONFS.contains(name)
069                    || DEPRECATE_MAP.containsValue(name)) {
070                continue;
071            }
072
073            if (configuration == null) {
074                configuration = new Element("configuration", ns);
075            }
076
077            String value = entry.getValue();
078            Element property = new Element("property", ns);
079            Element nameElement = new Element("name", ns);
080            nameElement.addContent(name != null ? name : "");
081            property.addContent(nameElement);
082            Element valueElement = new Element("value", ns);
083            valueElement.addContent(value != null ? value : "");
084            property.addContent(valueElement);
085            configuration.addContent(property);
086        }
087
088        return configuration;
089    }
090
091    @Override
092    protected Element generateSection(Configuration conf, Namespace ns) {
093        Element mapreduce = new Element("map-reduce", ns);
094        Element jt = new Element("job-tracker", ns);
095        jt.addContent(conf.get(XOozieClient.RM));
096        mapreduce.addContent(jt);
097        Element nn = new Element("name-node", ns);
098        String newNNVal = conf.get(DEPRECATE_MAP.get(XOozieClient.NN));
099        nn.addContent(newNNVal != null ? newNNVal : (conf.get(XOozieClient.NN)));
100        mapreduce.addContent(nn);
101
102        if (conf.size() > MANDATORY_OOZIE_CONFS.size()) { // excluding JT, NN,
103                                                          // LIBPATH
104            // configuration section
105            Element configuration = generateConfigurationSection(conf, ns);
106            if (configuration != null) {
107                mapreduce.addContent(configuration);
108            }
109
110            // file section
111            addFileSection(mapreduce, conf, ns);
112
113            // archive section
114            addArchiveSection(mapreduce, conf, ns);
115        }
116
117        return mapreduce;
118    }
119
120    @Override
121    protected void checkMandatoryConf(Configuration conf) {
122        for (String key : MANDATORY_OOZIE_CONFS) {
123            String value = conf.get(key);
124            if(value == null) {
125                if(DEPRECATE_MAP.containsKey(key)) {
126                    if(conf.get(DEPRECATE_MAP.get(key)) == null) {
127                        throw new RuntimeException(key + " or " + DEPRECATE_MAP.get(key) + " is not specified");
128                    }
129                }
130                else {
131                    throw new RuntimeException(key + " is not specified");
132                }
133            }
134        }
135    }
136
137    @Override
138    public String getEntityKey() {
139        return null;
140    }
141
142    @Override
143    protected boolean isLockRequired() {
144        return false;
145    }
146
147    @Override
148    protected void loadState() {
149
150    }
151
152    @Override
153    protected void verifyPrecondition() throws CommandException {
154
155    }
156}